//@version=2 strategy("Forex Master v4.0", overlay=true) Price = close Length = input(20) Mult = input(1.5) Basis = sma(Price, Length) StdDev = Mult * stdev(Price, Length) Upper = Basis + StdDev Lower = Basis - StdDev ADX_Length = input(50) TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1]))) DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0 DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0 SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/ADX_Length) + TrueRange SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/ADX_Length) + DirectionalMovementPlus SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/ADX_Length) + DirectionalMovementMinus DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100 DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100 DX = abs(DIPlus - DIMinus) / (DIPlus + DIMinus)*100 SmoothedADX1 = ema(DX, input(6)) SmoothedADX2 = ema(DX, input(12)) //*******ATR********* ATR = atr(input(defval=14,title="ATR")) bought = strategy.position_size[0] > strategy.position_size[1] EntryATR = valuewhen(bought, ATR, 0) Entryprice = valuewhen(bought, open, 0) LongLoss = (EntryATR * input(defval=2,title="ATRSL"))/close LongProfit = (EntryATR * input(defval=4,title="ATRTP"))/close //plot(Longstop,"Long Stop Loss") //plot(Shortstop,"Short Stop Loss") //*******ATR********* // Configure backtest start date with inputs startDate = input(title="Start Date", defval=1, minval=1, maxval=31) startMonth = input(title="Start Month", defval=1, minval=1, maxval=12) startYear = input(title="Start Year", defval=2020, minval=1800, maxval=2100) // See if this bar's time happened on/after start date afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) //************************************************SL TP************************************************ takeprofit = (close*(input(defval=4,title="0.TP")/1000)/syminfo.mintick) stoploss = (close*(input(defval=2,title="0.SL")/1000)/syminfo.mintick) //************************************************Condition************************************************ Condition1 = crossover(Price, Lower) and SmoothedADX1 < SmoothedADX2 Condition2 = crossunder(Price, Upper) and SmoothedADX1 < SmoothedADX2 if (afterStartDate and Condition1) strategy.entry("LongEntry", true, when = Condition1) strategy.exit("LongExit", "LongEntry", profit = takeprofit, loss = stoploss) if (afterStartDate and Condition2) strategy.entry("ShortEntry", false, when = Condition2) strategy.exit("ShortExit", "ShortEntry", profit = takeprofit, loss = stoploss)