ATR_Trade_strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © arameshraju
//Thanks to Vamsikrishna Kona  who planned this Idea in my mind. helped ot inprove it

//@version=4
strategy("ATR_Trade_strategy-2",shorttitle="ATR_Trade", overlay=true) 

showMiddle = input(false, title="Show Middle Line", type=input.bool)
showEMA = input(true, title="Show EMA Line", type=input.bool)
showTypical = input(false, title="Show Typical Line", type=input.bool)

length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="EMA", options=["RMA", "SMA", "EMA", "WMA"])
maxLoss = input(0.5, title="Max Loss on one Trade", type=input.float)
emaCount = input(10, title="EMA Periods", type=input.integer)
tradeQty = input(10, title="Trade Quanity", type=input.integer)
SlasMiddle = input(false, title="Stop Loss as Middle", type=input.bool)
onTypical = input(false, title="Trade on Typical", type=input.bool)


getSeries(_e, _timeFrame) => security(syminfo.tickerid, _timeFrame, _e, lookahead=barmerge.lookahead_on) 

previousClose= getSeries(close[1], 'D')
previousHigh= getSeries(high[1], 'D')
previousLow= getSeries(low[1], 'D')

typicalPrice=(close+high+low)/3

mxloss=close*maxLoss
 

ma_function(source, length) =>
    if smoothing == "RMA"
        rma(source, length)
    else
        if smoothing == "SMA"
            sma(source, length)
        else
            if smoothing == "EMA"
                ema(source, length)
            else
                wma(source, length)

ATRH=ma_function(tr(true), length)+previousClose
ATRL=previousClose-ma_function(tr(true), length)
ATRM= (ATRH+ATRL)/2
plot(ATRH, title = "ATR-HIGH", color=#991515, transp=80)
plot(ATRL, title = "ATR-LOW", color=#991515, transp=80)
plot(showMiddle?ATRM:na, title = "ATR-MIDDLE", color=#80cbc4, transp=60)
plot(showTypical?typicalPrice:na, title = "ATR-MIDDLE", color=color.black)

plot(showEMA?ema(close, emaCount):na, color=color.blue)
lognCondition =  onTypical? typicalPrice> (ATRH):close> (ATRH*1.01) 
shortCondition = onTypical ? typicalPrice< (ATRL)  :  close< (ATRL) 

// if onTypical
//     lognCondition=typicalPrice> (ATRH)  
//     shortCondition = typicalPrice< (ATRL) 
// else
//     lognCondition=close> (ATRH*1.01) 
//     shortCondition = close< (ATRL) 



if(lognCondition  and year>2009)   
    strategy.entry("Buy", strategy.long, tradeQty,when=strategy.position_size <= 0)    


if(shortCondition and year>2009)   
    strategy.entry("sell", strategy.short, tradeQty,when=strategy.position_size > 0)


strategy.close("buy", when = close < ema(close, emaCount),  comment = "close if blow ema")
strategy.close("sell", when = close > ema(close, emaCount),  comment = "close if blow ema")

strategy.close("buy", when = (close < ATRM) and SlasMiddle,  comment = "close if blow ATRM")
strategy.close("sell", when = (close > ATRM) and SlasMiddle,  comment = "close if ABOVE ATRM")


strategy.exit("Buy", "long", loss = mxloss)
strategy.exit("sell", "long", loss = mxloss)


//plot(strategy.equity)