// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
strategy("Forex Master v4.0", overlay=true, pyramiding=0, commission_type=strategy.commission.percent, commission_value=0.07, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
ribbon_period = input(33, "Period", step=1)
leadLine1 = ema(close, ribbon_period)
leadLine2 = sma(close, ribbon_period)
p1 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)
p2 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)
fill(p1, p2, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)
Length = input(17, step=1)
Mult = input(1.7, step=0.1)
Basis = sma(Price, Length)
StdDev = Mult * stdev(Price, Length)
adxlen = input(1, title="ADX Smoothing")
dilen = input(77, title="DI Length")
plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
[plus, minus] = dirmov(dilen)
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
[sig, up, down] = adx(dilen, adxlen)
// plot(sig, color=color.red, title="ADX")
// plot(up, color=color.blue, title="+DI")
// plot(down, color=color.orange, title="-DI")
SmoothedADX1 = ema(sig, input(7))
SmoothedADX2 = ema(sig, input(14))
long_tp_inp = input(6.4, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(4.2, title='Long Stop Loss %', step=0.1)/100
long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
entry_long = crossover(Price, Lower) and SmoothedADX1 < SmoothedADX2 and leadLine2<leadLine1
exit_long = crossunder(Price, Upper) and SmoothedADX1 < SmoothedADX2 and leadLine2>leadLine1
////// BACKTEST PERIOD ///////
testStartYear = input(2019, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testStopYear = input(9999, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(31, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
time >= testPeriodStart and time <= testPeriodStop ? true : false
strategy.entry("LongEntry", true, when = entry_long)
strategy.exit("LongExit", "LongEntry", limit = long_take_level, stop = long_stop_level)
strategy.close("LongEntry", when=exit_long)