//XAUUSD YB14APR23 //PRICE Crossing Moving Average STRATEGY POSITIONAL with MA filter, Donchian, RSI filter and Take Profit and SL //Trailing Stop //RSI max and min criteria for entries //Initial stop loss set //Trailing by Moving Average //@version=5 strategy('XAUUSD - Trend Trading YB V3', initial_capital = 100, default_qty_value = 1, commission_value = 0.001, slippage = 2, overlay=true) Periods = input(title='ATR Period', defval=10) STsrc = input(hl2, title='Source') Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=2.1) ta.supertrend(Multiplier,Periods) // FILTER BY MA - type, source, length ma_type = input.string(defval='SMA', title='MA Filter Type: ', options=['SMA', 'EMA'], group = "Moving Average Setting") ma_len = input.int(defval=175, title='MA Filter Length', minval=1, group = "Moving Average Setting") //ma_Slopelen = input.int(defval=3, title='MA Slope Filter Length', minval=1, group = "Moving Average Setting") ma_trail = input.int(defval=20, title='MA Trail Length', minval=1, group = "Moving Average Setting") ma_len4 = input.int(defval=50, title='MA Filter Length2', minval=1, group = "Moving Average Setting") //ma_len2 = input.int(defval=21, title='MA Filter Length - 1 hr', minval=1, group = "Moving Average Setting") //ma_len3 = input.int(defval=21, title='MA Filter Length - 4 hr', minval=1, group = "Moving Average Setting") ma_src = close ma_Hsrc = high ma_Lsrc = low // === MA FUNCTIONS variant(type, ma_src, ma_len) => type == 'SMA' ? ta.sma(ma_src, ma_len) : ta.ema(ma_src, ma_len) // === Moving Average ma_series1 = variant(ma_type, ma_src, ma_len) plot(ma_series1, title='MA Plot', linewidth=2, join=true, color=color.rgb(20, 150, 37)) ma_seriesT = variant(ma_type, ma_src, ma_trail) MAF1 = plot(ma_seriesT, title='MA Trail Plot', linewidth=2, join=true, color=color.rgb(35, 49, 179)) ma_seriesF4 = variant(ma_type, ma_src, ma_len4) MAF4 = plot(ma_seriesF4, title='MA 2 Plot', linewidth=2, join=true, color=color.rgb(233, 212, 21, 1)) //maRising = ta.rising(ma_series1, ma_Slopelen) //maFalling = ta.falling(ma_series1, ma_Slopelen) // FILTER BY RSI RSI_F = input.int(defval=20, title='RSI FILTER length', minval=1, group = "RSI SETTING") RSI_Upper = input.int(defval=56, title='RSI Upper %', minval=1, group = "RSI SETTING") RSI_Lower = input.int(defval=44, title='RSI Lower %', minval=1, group = "RSI SETTING") maTypeInput = input.string("WMA", title="RSI MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group= "RSI SETTING") maLengthInput = input.int(10, title="RSI MA Length", group = "RSI SETTING") RSI_UpperMax = input.int(defval=85, title='RSI Upper Max %', minval=1, group = "RSI SETTING") RSI_LowerMax = input.int(defval=18, title='RSI Lower Max %', minval=1, group = "RSI SETTING") // ma(source, length, type) => switch type "SMA" => ta.sma(source, length) "Bollinger Bands" => ta.sma(source, length) "EMA" => ta.ema(source, length) "SMMA (RMA)" => ta.rma(source, length) "WMA" => ta.wma(source, length) "VWMA" => ta.vwma(source, length) rsi_v = ta.rsi(close, RSI_F) rsiMA = ma(rsi_v, maLengthInput, maTypeInput) // Donchian DClength = input(35, title="Donchian Channel Length", group = 'Donchian Channel Setting') DClower = ta.lowest(DClength) DCupper = ta.highest(DClength) basis = math.avg(DCupper, DClower) //plot(basis, "Basis", color=#FF6D00) //indicator("Waddah Attar Explosion YB", shorttitle="WAE YB SIGNAL", overlay=false) sensitivity = input.int(175, title="Sensitivity") fastLength = input.int(21, title="FastEMA Length") slowLength = input.int(40, title="SlowEMA Length") channelLength = input.int(20, title="BB Channel Length") mult = input.float(2.0, title="BB Stdev Multiplier") DEAD_ZONE = ta.rma(ta.tr(true), 100) * 3.7 calc_macd(source, fastLength, slowLength) => fastMA = ta.ema(source, fastLength) slowMA = ta.ema(source, slowLength) fastMA - slowMA calc_BBUpper(source, length, mult) => BBbasis = ta.sma(source, length) dev = mult * ta.stdev(source, length) BBbasis + dev calc_BBLower(source, length, mult) => BBbasis = ta.sma(source, length) dev = mult * ta.stdev(source, length) BBbasis - dev t1 = (calc_macd(close, fastLength, slowLength) - calc_macd(close[1], fastLength, slowLength)) * sensitivity e1 = calc_BBUpper(close, channelLength, mult) - calc_BBLower(close, channelLength, mult) trendUp = t1 >= 0 ? t1 : 0 trendDown = t1 < 0 ? -1 * t1 : 0 upColor = trendUp < trendUp[1] ? color.rgb(77, 161, 121) : color.rgb(3, 53, 5) downColor = trendDown < trendDown[1] ? color.orange : color.rgb(109, 21, 21) // Buy Signal Plot buySignal2 = ta.cross(trendUp, DEAD_ZONE) and ta.cross(trendUp, e1) buySignal3 = trendUp > DEAD_ZONE plotshape(buySignal2, style=shape.triangleup, location=location.belowbar, size=size.small, color=color.green, title="Buy Signal") // Sell Signal Plot sellSignal2 = ta.cross(DEAD_ZONE, trendDown) and ta.cross(trendDown, e1) sellSignal3 = trendDown > DEAD_ZONE plotshape(sellSignal2, style=shape.triangledown, location=location.abovebar, size=size.small, color=color.red, title="Sell Signal") //Trailing Stop useTrailStop = input.bool(title='Trailing Stop', defval=false) IslPoints = input.float(title='Fixed Initial Stop Points', minval=0.1, maxval=5000, step=0.1, defval=4, confirm=false) IslPoints /= syminfo.mintick slPoints = input.float(title='Trailing Stop Points', minval=0.1, maxval=5000, step=0.1, defval=1.5, confirm=false) slPoints /= syminfo.mintick slOffset = input.float(title='Trailing Stop Offset Points', minval=0.1, maxval=5000, step=0.1, defval=1, confirm=false) slOffset /= syminfo.mintick //adding stoploss and target option ut = input(defval=false, title='USE TARGET') us = input(defval=false, title='USE STOPLOSS') tar = input(defval=8.0, title='TARGET') stop = input(defval=4.0, title='STOP LOSS') tar /= syminfo.mintick stop /= syminfo.mintick //RSImaFilter2 = request.security(syminfo.tickerid, tfSet, ma(ma2Type, close, ma2Length)) Hclose = request.security(syminfo.tickerid, '60', close) Hrsi_v = ta.rsi(Hclose, RSI_F) HrsiMA = ma(Hrsi_v, maLengthInput, maTypeInput) // === BACKTEST RANGE === Start = input.time(defval = timestamp("01 Jan 2023 09:00 +0530"), title = "Backtest Start Date", group = "backtest window") Finish = input.time(defval = timestamp("01 Jan 2100 11:45 +0530"), title = "Backtest End Date", group = "backtest window") window() =>time >= Start and time <= Finish ? true : false //Entry conditions BuyEntry = close > ma_series1 and close > basis and close > ma_seriesF4 and rsi_v > RSI_Upper and rsi_v > rsiMA and Hrsi_v > HrsiMA and rsi_v < RSI_UpperMax and ma_seriesT > ma_seriesF4 and close > close[1] SellEntry = close < ma_series1 and close < basis and close < ma_seriesF4 and rsi_v < RSI_Lower and rsi_v < rsiMA and Hrsi_v < HrsiMA and rsi_v > RSI_LowerMax and ma_seriesT < ma_seriesF4 and close < close[1] LongExit = close < ma_seriesT ShortExit = close > ma_seriesT //Long entry longCondition = BuyEntry and trendUp > DEAD_ZONE if longCondition strategy.entry('BUY', strategy.long, when=window()) if LongExit strategy.close(id='BUY', when=window(), comment='Long MA Trail Exit') //Short entry shortCondition = SellEntry and trendDown > DEAD_ZONE if shortCondition strategy.entry('SELL', strategy.short, when=window()) if ShortExit strategy.close(id='SELL', when=window(), comment='Short MA Trail Exit') //Exit with Trailing Stop if useTrailStop == true atr = ta.atr(Periods) BIslPoints = atr[1] * Multiplier * 1 BIslPoints /= syminfo.mintick SIslPoints = atr[1] * Multiplier * -1 SIslPoints /= syminfo.mintick strategy.exit('Trail Exit long', from_entry='BUY', loss=BIslPoints, trail_points=slPoints, trail_offset=slOffset) strategy.exit('Trail Exit short', from_entry='SELL', loss=SIslPoints, trail_points=slPoints, trail_offset=slOffset) //Exit with Target/Stop loss if ut == true and us == false strategy.exit(id="BUY", from_entry="BUY", profit=tar, comment="Target Hit") strategy.exit(id="SELL", from_entry="SELL", profit=tar, comment="Target Hit") if us == true and ut == false strategy.exit(id="BUY", from_entry='BUY', loss=stop, comment="SL Hit") strategy.exit(id="SELL", from_entry='SELL', loss=stop, comment="SL Hit") if ut == true and us == true strategy.exit(id="BUY", from_entry='BUY', profit=tar, loss=stop, comment='TP / SL') strategy.exit(id="SELL", from_entry='SELL', profit=tar, loss=stop, comment='TP / SL')