EMAs SAR Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Coinekid

//@version=5
indicator("EMAs SAR Indicator", overlay=true)

// REF
// Sonic R VuTien
///// Var
var tradeCount = 0

// -------- INPUT ----------
useSAR = input.bool(false, "Use SAR")
// TP1_Ratio = input(title="Sell Postion Size % @ TP1", type=input.float, defval=50, step=1, group="TP & SL", tooltip="Example: 50 closing 50% of the position once TP1 is reached")/100




// ------- Util functions ----------
// distance = 1 -> k and pivot is the same. abs(k-pivot) = 0
distanceRatio(k, pivot) =>
    math.abs(pivot - math.abs(pivot-k))/pivot*100.0
    
distance(k, pivot) =>
    math.abs(pivot-k)

getPipSize() =>
    if syminfo.ticker == "XAUUSD"
        syminfo.mintick * 100
    else
        syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)
    
getPip(x,y) => math.abs(x - y) / getPipSize()



curProfitPip() =>
    if strategy.position_size > 0
        (high - strategy.position_avg_price) / getPipSize()
    else if strategy.position_size < 0
        (strategy.position_avg_price - low) / getPipSize()
    else
        0
        

getCurrentStage(tp1, tp2) =>
    var stage = 0
    entry = strategy.position_avg_price
    if strategy.position_size == 0 
        stage := 0
    if stage == 0 and strategy.position_size != 0
        stage := 1
    if stage == 1 and curProfitPip() >= getPip(entry, tp1)
        stage := 2
    if stage == 2 and curProfitPip() >= getPip(entry, tp2)
        stage := 3
    stage
    
calcSLPrice(offsetPip) =>
    if strategy.position_size > 0
        strategy.position_avg_price - offsetPip * getPipSize()
    else if strategy.position_size < 0
        strategy.position_avg_price + offsetPip * getPipSize()
    else
        na
        
calcTPPrice(offsetPip) =>
    calcSLPrice(-offsetPip)

///////////// ----------------------------------


///////////// RSI
rsiLength = input.int(14, title="RSI Period Length") 
rsiOverSold = 30
rsiOverBought = 70
rsiNeutral = 50

rsi = ta.rsi(close, rsiLength)
// plot(rsi, style=plot.style_line, linewidth=1 , color=color.orange)

// 
ema200 = ta.ema(close, 200)

/// SAR
sar = ta.sar(0.02, 0.02, 0.2)

plot(useSAR ? sar:na , style=plot.style_cross, linewidth=1 , color=sar < low? color.green: color.red)



///// Volume
// volMALength = input(title="Volume MA Length", defval=20)
// highVol = volume >= ta.sma(volume, volMALength)


//Sonic R
HiLoLen     = input.int(34, minval=2,title="EMA channel")
EMA = input.int(defval=89, title="EMA Signal")
pacC        = ta.ema(close,HiLoLen)
pacL        = ta.ema(low,HiLoLen)
pacH        = ta.ema(high,HiLoLen)
DODGERBLUE = #1E90FFFF
// Plot the Price Action Channel (PAC) base on EMA high,low and close
L=plot(pacL, color=color.new(DODGERBLUE, transp=40), linewidth=1, title="High PAC EMA")
H=plot(pacH, color=color.new(DODGERBLUE, transp=40), linewidth=1, title="Low PAC EMA")
C=plot(pacC, color=color.blue, linewidth=2, title="Close PAC EMA")
fill(L,H, color=color.new(color.aqua, 95), title="Fill HiLo PAC")
//Moving Average
signalMA = ta.ema(close,EMA)
ema610= ta.ema(close, 610)

plot(ema610,title="EMA 610",color=color.white,linewidth=4,style=plot.style_line)
plot(ema200,title="EMA 200",color=color.orange,linewidth=3,style=plot.style_line)
plot(signalMA,title="EMA Signal",color=color.red, linewidth=3,style=plot.style_line)

