//@version=4 // Strategy posted by Trade Pro on YT: https://www.youtube.com/watch?v=7NM7bR2mL7U&t=299s // This strat uses a 3:1 risk reward ratio, but can be changed by setting the ATR multiplier in Version 1 // The script has a time range picker added to back test from a specific date. // // Version 1.1.2 // Replaced the 3:1 risk reward ratio from the video with absolute max stoploss of 1 ATR price (configurable) and a trailing stoploss // Added Messaging Default for Buy/Sell Trigger. Use {{strategy.order.alert_message}} string in message box to trigger Buy/Sell command for Webhook strategy(title="Stoch RSI Crossover Strat + EMA", shorttitle="Matjik SR Cross + EMA", overlay = true, pyramiding = 0, calc_on_order_fills = false, commission_type = strategy.commission.percent, commission_value = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital=10000, currency=currency.USD) //Message Inputs message_long_entry = input("Buy") message_long_exit = input("Close") message_short_entry = input("Sell") message_short_exit = input("Close") // STEP 1: Time Range // Configure backtest start date with inputs startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31) startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12) startYear = input(title="Start Year", type=input.integer, defval=2000, minval=1800, maxval=2100) // STEP 2: // See if this bar's time happened on/after start date afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) smoothK = input(3, minval=1) smoothD = input(3, minval=1) lengthRSI = input(14, minval=1) lengthStoch = input(14, minval=1) src = input(close, title="RSI Source") rsi1 = rsi(src, lengthRSI) k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) d = sma(k, smoothD) //ATR lengthATR = input(title="ATR Length", defval=14, minval=1) atr_loss = input(title="Absolute Stoploss (ATR price multiplier", defval=1, minval=1) atr = atr(lengthATR) //Multi EMA emasrc = close, len1 = input(8, minval=1, title="EMA 1") len2 = input(14, minval=1, title="EMA 2") len3 = input(50, minval=1, title="EMA 3") ema1 = ema(emasrc, len1) ema2 = ema(emasrc, len2) ema3 = ema(emasrc, len3) col1 = color.lime col2 = color.blue col3 = color.orange //EMA Plots //plot(ema1, title="EMA 1", linewidth=1, color=col1) //plot(ema2, title="EMA 2", linewidth=1, color=col2) //plot(ema3, title="EMA 3", linewidth=1, color=col3) crossup = k[1] >= d[1] and k[2] <= d[2] and k <= 60 and k >= 10 barbuy = crossup and ema1 > ema2 and ema2 > ema3 and close > ema1 crossdown = k[1] <= d[1] and k[2] >= d[2] and k >= 40 and k <= 95 barsell = crossdown and ema3 > ema2 and ema2 > ema1 and close < ema1 //plotshape(crossup, style=shape.triangleup, location=location.belowbar, color=color.white) plotshape(barbuy, style=shape.triangleup, location=location.belowbar, color=color.green) //plotshape(crossdown, style=shape.triangledown, location=location.abovebar, color=color.white) plotshape(barsell, style=shape.triangledown, location=location.abovebar, color=color.red) longloss = sma(open, 1) shortloss = sma(open, 1) longloss := barbuy ? close - (atr * atr_loss) : longloss[1] shortloss := barsell ? close + (atr * atr_loss) : shortloss[1] //plot(longloss, color=color.red) //plot(shortloss, color=color.red) if (afterStartDate) strategy.entry("Long", strategy.long, when = barbuy, alert_message = message_long_entry) strategy.entry("Short", strategy.short, when = barsell, alert_message = message_short_entry) trailPoints = input(100, "Trail Points (in ticks)") trailOffset = input(0, "Trail Offset (in ticks)") strategy.exit("exit long", from_entry="Long", alert_message = message_long_exit, trail_points= trailPoints, trail_offset= trailOffset, stop=longloss) strategy.exit("exit short", from_entry="Short", alert_message = message_short_exit, trail_points= trailPoints, trail_offset= trailOffset, stop=shortloss)