Trend Breakout high/low #1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zxcv55602
//@version=4
strategy("Trend #1 - ATR+Breakout high/low", overlay = true,slippage=0,commission_value=0,initial_capital=100000)
date1 = input(title="Start Date", type=input.time, defval=timestamp("2020-01-01T00:00:00"))
date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))
t1= input(title ="Trading period",type=input.session,defval="3D",options=["1M","3W","2W","1W","5D","4D","3D","2D","1D","720","480","360","240","180","120","60","30","15","10","5","3","1"] )
stop_loss=input(1000,"Stop loss", minval=0,step =1)
Trange=input(100,"Trading range", step =1)
range=input(100,"Stop range", step =1)
atrlen = input(1, "ATR Period")
mult_atr = input(1, "ATR mult", step=0.1)
smalong=input(title="SMA switch", type=input.bool, defval=false)
smainput=input(300,"SMA switch", minval=2,step =1)
longsignal=input(title="Long", type=input.bool, defval=true)
shortsignal=input(title="Short", type=input.bool, defval=true)
fixedmode=input(title="Fixed mode", type=input.bool, defval=false)
fixed=input(1,"Fixed mode",step =0.1)
//-----------
var float Y_Low = na
var float Y_High = na
if change(time(t1),length=1)!= 0
    Y_High:=high
    Y_Low:= low
else
    Y_High:=high>nz(Y_High)?high:Y_High
    Y_Low:=low<nz(Y_Low)?low:Y_Low
con1= change(time(t1),length = 1 )  != 0?1:0
//plot(Y_High[0],"LOW",color.yellow,linewidth = 2)
//plot(Y_Low[0],"LOW",color.white,linewidth = 2)
//-----------
var upper=0.0
var lower=0.0
var con_op=0.0
var central_line=0.0
if con1==1
    upper:=Y_High[1]-(((Y_High[1]-Y_Low[1])/100)*(100-Trange)/2)
    lower:=Y_Low[1]+(((Y_High[1]-Y_Low[1])/100)*(100-Trange)/2)
    con_op:=0
    central_line:=(Y_High[1]+Y_Low[1])/2
//----------- 
//plotarrow(con1==1?con1:na,colorup=color.white,colordown=color.white)
//-----------
var sma1=0.0
sma=sma(close[0],smainput)
sma1:=security(syminfo.tickerid,t1,sma)
if smalong==false
    sma1:=sma(close[0],smainput)
plot(sma1,color=color.new(color.blue,0),linewidth=1)
//------------------
upper_atr = ema(tr(true),atrlen) * mult_atr + close
lower_atr = close - ema(tr(true),atrlen) * mult_atr
plot(upper_atr, color=color.new(color.white,80),style=plot.style_stepline)
plot(lower_atr, color=color.new(color.white,80),style=plot.style_stepline)
//------------------
plot(upper,"HIGH",color.green,linewidth = 1,style=plot.style_stepline)
plot(lower,"LOW",color.red,linewidth = 1,style=plot.style_stepline)
plot(upper-(((upper-lower)/100)*range),color=color.new(color.green,50), linewidth=1)
plot(lower+(((upper-lower)/100)*range),color=color.new(color.red,50), linewidth=1)
//-----------
var OP_L_stop=0.0
var OP_S_stop=0.0
//-----------
if time >= date1 and time <= date2 and con_op==0
    if longsignal
        if strategy.position_size==0 and upper>sma1 and close>upper
            OP_L_stop:=upper-(((upper-lower)/100)*range)
            con_op:=1
            strategy.entry("OP_L",strategy.long,when=close>upper,qty=fixedmode==true?fixed:(stop_loss/(close-OP_L_stop)),limit=upper_atr)
            strategy.exit("OP_L",stop=OP_L_stop,comment="sell_L",limit=upper_atr)
        if strategy.position_size>0
            strategy.exit("OP_L",stop=OP_L_stop,comment="stop_L",limit=upper_atr)
    if shortsignal
        if strategy.position_size==0 and lower<sma1 and close<lower
            OP_S_stop:=lower+(((upper-lower)/100)*range)
            con_op:=1
            strategy.entry("OP_S",strategy.short,when=close<lower,qty=fixedmode==true?fixed:(stop_loss/(OP_S_stop-close)),limit=lower_atr)
            strategy.exit("OP_S",stop=OP_S_stop,comment="sell_S",limit=lower_atr)
        if strategy.position_size<0
            strategy.exit("OP_S",stop=OP_S_stop,comment="stop_S",limit=lower_atr)