ETF 3-Day Reversion Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 5
// Author = TradeAutomation


strategy(title="ETF 3-Day Reversion Strategy", shorttitle="ETF 3-Day Reversion Strategy", process_orders_on_close=true, overlay=true, commission_type=strategy.commission.cash_per_order, commission_value=1, initial_capital = 10000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)


// Backtest Date Range Inputs // 
StartTime = input.time(defval=timestamp('01 Jan 2012 05:00 +0000'), title='Start Time')
EndTime = input.time(defval=timestamp('01 Jan 2099 00:00 +0000'), title='End Time')
InDateRange = time>=StartTime and time<=EndTime

// Strategy Rules //
DayEMA5 = ta.ema(close, 5)
EMAQualInput = input.bool(false, "Only enter trades when above qualifier EMA?", tooltip="When this is selected, a trade will only be entered when the price is above the EMA qualifier line.")
QEMA = ta.ema(close, input.int(200, "EMA Trade Qualifier Length"))
EMAQualifier = close>QEMA
Rule1 = EMAQualInput==true ? EMAQualifier : close>0
Rule2 = close<DayEMA5
Rule3 = high<high[1] and low<low[1] and high[1]<high[2] and low[1]<low[2] and high[2]<high[3] and low[2]<low[3]
ExitEMA = ta.ema(close, input.int(5, "EMA Length For Exit Strategy", tooltip = "The strategy will sell when the price crosses over this EMA"))
plot(DayEMA5, "Five Period EMA")
plot(QEMA, "Qualifier EMA")
plot(ExitEMA, "Exit EMA", color=color.green)

// Entry & Exit Functions //
if (InDateRange)
    strategy.entry("Long", strategy.long, when = Rule1 and Rule2 and Rule3)
    strategy.close("Long", when = ta.crossover(close, ExitEMA))
if (not InDateRange)
    strategy.close_all()