Strategy Template
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//@version=4
strategy("Strategy Template", overlay=true, initial_capital = 20000, default_qty_type = strategy.percent_of_equity,
             default_qty_value = 90, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01,
             margin_long=90, margin_short=90, max_lines_count=500, max_labels_count=500, max_boxes_count=500)

HigherTimeframe = input("W", type=input.resolution)
HTFMAType = input(title="HTF Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])
HTFMaLength= input(20, step=10)
HTFUseHighLowRange = input(true)

atrLength = input(16, step=5)
atrMult = input(3, step=0.5)

tradeDirection = input(title="Trade Direction", defval=strategy.direction.all, 
             options=[strategy.direction.all, strategy.direction.long, strategy.direction.short], group="Trade Filters")
strategy.risk.allow_entry_in(tradeDirection)
i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Start Time", type = input.time, group="Trade Filters")
i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "End Time", type = input.time, group="Trade Filters")
inDateRange = time >= i_startTime and time <= i_endTime
waitForCloseBeforeExit = input(false)

f_secureSecurity(_symbol, _res, _src, _offset) => security(_symbol, _res, _src[_offset], lookahead = barmerge.lookahead_on)

f_getMovingAverage(source, MAType, length)=>
    ma = sma(source, length)
    if(MAType == "ema")
        ma := ema(source,length)
    if(MAType == "hma")
        ma := hma(source,length)
    if(MAType == "rma")
        ma := rma(source,length)
    if(MAType == "vwma")
        ma := vwma(source,length)
    if(MAType == "wma")
        ma := wma(source,length)
    ma
    
maHTF = f_secureSecurity(syminfo.tickerid, HigherTimeframe, f_getMovingAverage(close, HTFMAType, HTFMaLength), 1)
maHTFHigh = f_secureSecurity(syminfo.tickerid, HigherTimeframe, f_getMovingAverage(high, HTFMAType, HTFMaLength), 1)
maHTFLow = f_secureSecurity(syminfo.tickerid, HigherTimeframe, f_getMovingAverage(low, HTFMAType, HTFMaLength), 1)

[supertrend, dir] = supertrend(atrMult, atrLength)

mh = plot(maHTFHigh, "MTF MA High", color=color.new(color.green, 90))
ml = plot(maHTFLow, "MTF MA Low", color=color.new(color.red, 90))
fill(mh, ml, title="No Trade Zone", color=color.new(color.yellow, 50))

plot(strategy.position_size != 0 ? supertrend : na, title="Trailing Stop", color=dir < 0? color.green : color.red, style=plot.style_stepline)

buyCondition = crossover(0, dir) and (close > (HTFUseHighLowRange? maHTFHigh : maHTF)) and inDateRange
closeBuyCondition = crossunder(0, dir)
sellCondition = crossunder(0, dir) and (close < (HTFUseHighLowRange? maHTFLow : maHTF)) and inDateRange
closeSellCondition = crossover(0, dir)

strategy.entry("Long", strategy.long, when=buyCondition, oca_name="oca_buy", oca_type=strategy.oca.cancel)
strategy.close("Long", when=closeBuyCondition and waitForCloseBeforeExit)
strategy.exit("ExitLong", "Long", stop=supertrend, when=not waitForCloseBeforeExit)
strategy.entry("Short", strategy.short, when=sellCondition, oca_name="oca_sell", oca_type=strategy.oca.cancel)
strategy.close("Short", when=closeSellCondition and waitForCloseBeforeExit)
strategy.exit("ExitShort", "Short", stop=supertrend, when=not waitForCloseBeforeExit)