Ma Pullback
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Shauryam_or

//@version=5
indicator("Ma Pullback",overlay = true)

//Ema 1
on_ma=input.bool(true,"Enable EMa 1 Plot On/Off",group="EMa Set")
ma_len= input.int(32, minval=1, title="Ema Length",group = "EMa Set")
ma_src = input.source(close, title="Ema Source",group = "EMa Set")
ma_out = ta.ema(ma_src, ma_len)

//Ema 2
on_ma2=input.bool(true,"Enable EMa 2 Plot On/Off",group="EMa2 Set")
ma_len2= input.int(32, minval=1, title="Ema2 Length",group = "EMa2 Set")
ma_src2 = input.source(low, title="Ema2 Source",group = "EMa2 Set")
ma_out2 = ta.ema(ma_src2, ma_len2)

// Ema 3
on_ma3=input.bool(true,"Enable EMa 3 Plot On/Off",group="EMa3 Set")
ma_len3= input.int(32, minval=1, title="Ema3 Length",group = "EMa3 Set")
ma_src3 = input.source(high, title="Ema3 Source",group = "EMa3 Set")
ma_out3 = ta.ema(ma_src3, ma_len3)


//ploting ema 
plot(on_ma?ma_out:na, color=color.white, title="MA")
plot(on_ma2?ma_out2:na, color=color.lime, title="MA")
plot(on_ma3?ma_out3:na, color=color.lime, title="MA")

//ema as cloud


cloud_top = math.max(ma_out2 , ma_out3)
cloud_bottom = math.min(ma_out2, ma_out3)

var buy_cond1=false

var sell_cond1=false

if ta.crossover(close, cloud_top) 
    buy_cond1:=true
    sell_cond1:=false
if ta.crossunder(close, cloud_bottom ) 
    sell_cond1:=true
    buy_cond1:=false


buy_cond =  buy_cond1 
sell_cond =sell_cond1 


// plotshape(buy_cond,title = "Buy1",style = shape.labelup,location = location.belowbar,color = color.green,text = "1st buyc t",textcolor =color.white)
// plotshape(sell_cond ,title = "Sell1",style = shape.labeldown,location = location.abovebar,color = color.red,text = "1st sellc t",textcolor =color.white)

var pullback_crossup=false
var pullback_crossdown=false

if (ta.crossover(high,ma_out2) or ta.crossover(high,ma_out) or ta.crossover(high,ma_out3)) and close<cloud_top and close>open and sell_cond
    pullback_crossup:=true
if (ta.crossunder(low,ma_out2) or ta.crossunder(low,ma_out) or ta.crossunder(low,ma_out3)) and close>cloud_bottom and close<open and buy_cond
    pullback_crossdown:=true


// plotshape(pullback_crossup,title = "Buy2",style = shape.labelup,location = location.belowbar,color = color.green,text = "2nd buyc t",textcolor =color.white)
// plotshape(pullback_crossdown,title = "Sell2",style = shape.labeldown,location = location.abovebar,color = color.red,text = "2nd sellc t",textcolor =color.white)

if  ta.crossunder(close, cloud_bottom ) 
    pullback_crossdown:=false
if ta.crossover(close, cloud_top)
    pullback_crossup:=false


final_buy= pullback_crossdown and ta.crossover(close, cloud_top) and close>open
final_sell=pullback_crossup and ta.crossunder(close, cloud_bottom ) and close<open 

if final_buy
    pullback_crossdown:=false
if final_sell
    pullback_crossup:=false

//input to check bar distance 
bar_limit=input.int(7,title ="Candle Limit",group ="Candle Setting",tooltip ="Barsince Candle 7 should be greater/lower then ema band")
perc_candle=input.float(0.1,step =0.1,title ="%Candle Size",group ="Candle Setting",tooltip ="Percentage candle size to check movementam candle")
//candle size
candle_size=math.abs(open - close) / math.abs(high - low) > perc_candle

since_x=(close[bar_limit]>cloud_top) 
since_y=(close[bar_limit]<cloud_bottom) 


plotshape(final_buy and since_x and candle_size,title = "Buy3",style = shape.labelup,location = location.belowbar,color = color.green,text = "Buy",textcolor =color.white,size=size.tiny)
plotshape(final_sell and since_y and candle_size,title = "Sell3",style = shape.labeldown,location = location.abovebar,color = color.red,text = "Sell",textcolor =color.white,size=size.tiny)

