Mean reverting strategy based on time and SMAs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LorenzoT95, thanks to ZenandTheArtofTrading for script tips

//@version=4
study(title="Prima.Strat.Lollo", shorttitle="Strategia 1 L", overlay = true)

//MAs definition
src = input(hl2, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
len1 = input(50, minval=1, title="Length1")
out1 = sma(src, len1)
plot(out1, color=color.yellow, transp=50)

len2 = input(100, minval=1, title="Length2")
out2 = sma(src, len2)
plot(out2, color=color.orange, transp=50)

len3 = input(200, minval=1, title="Length3")
out3 = sma(src, len3)
plot(out3, color=color.red, transp=50)


//Tiempo50 var def
var tiempo1 = 0
if low > out1
    tiempo1 := tiempo1 + 1
else 
    tiempo1 :=0

var tiempo2 = 0
if high < out1
    tiempo2 := tiempo2 - 1
else 
    tiempo2 := 0
  
 //Tiempo100 var def  
var tiempo3 = 0
if low > out2
    tiempo3 := tiempo3 + 1
else 
    tiempo3 :=0

var tiempo4 = 0
if high < out2
    tiempo4 := tiempo4 - 1
else 
    tiempo4 := 0
    
//Tiempo 200 var def
var tiempo5 = 0
if low > out3
    tiempo5 := tiempo5 + 1
else 
    tiempo5 :=0

var tiempo6 = 0
if high < out3
    tiempo6 := tiempo6 - 1
else 
    tiempo6 := 0


//Tiempo conditions
TiempoBear50 = tiempo1 >= len1 or tiempo1[1] >= len1
TiempoBull50 = tiempo2 <= -len1 or tiempo2[1] <= -len1

TiempoBear100 = tiempo3 >= len2 or tiempo3[1] >= len2
TiempoBull100 = tiempo4 <= -len2 or tiempo4[1] <= -len2

TiempoBear200 = tiempo5 >= len3 or tiempo5[1] >= len3
TiempoBull200 = tiempo6 <= -len3 or tiempo6[1] <= -len3

    
// Detect candlestick patterns
bearishEngulfing = close <= open[1] and close[1] >= open[1]
bullishEngulfing = close >= open[1] and close[1] <= open[1]

//Detect trading setups
bearishSetup50 = TiempoBear50 and bearishEngulfing
bullishSetup50 = TiempoBull50 and bullishEngulfing

bearishSetup100 = TiempoBear100 and bearishEngulfing
bullishSetup100 = TiempoBull100 and bullishEngulfing

bearishSetup200 = TiempoBear200 and bearishEngulfing
bullishSetup200 = TiempoBull200 and bullishEngulfing

//Draw data on chart
plotshape(bearishSetup50 ? 1 : na, style=shape.arrowdown, color=color.yellow, location=location.abovebar, title="Bear Signal", text="Sell")
plotshape(bullishSetup50 ? 1 : na, style=shape.arrowup, color=color.yellow, location=location.belowbar, title="Bull Signal", text="Buy")

plotshape(bearishSetup100 ? 1 : na, style=shape.arrowdown, color=color.orange, location=location.abovebar, title="Bear Signal", text="Sell")
plotshape(bullishSetup100 ? 1 : na, style=shape.arrowup, color=color.orange, location=location.belowbar, title="Bull Signal", text="Buy")

plotshape(bearishSetup200 ? 1 : na, style=shape.arrowdown, color=color.red, location=location.abovebar, title="Bear Signal", text="Sell")
plotshape(bullishSetup200 ? 1 : na, style=shape.arrowup, color=color.red, location=location.belowbar, title="Bull Signal", text="Buy")

alertcondition(condition= bearishSetup50 or bullishSetup50, title="Tiempo Alert!", message="Tiempo Setup detected for: {{ticker}}")









