Here is the updated script. It introduces a session input set to standard New York stock exchange hours (09:30 to 16:00 EST). I also added a subtle grey background highlight to your chart so you can visually verify exactly when the algorithm is "awake" and looking for trades.
Code: Select all
//@version=5
strategy("The Complete Stranger Rule - NY Session", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// ==========================================
// 1. SETTINGS INPUTS (Adjustable)
// ==========================================
slPercent = input.float(1.0, title="Stop Loss (%)", step=0.1) / 100
tpPercent = input.float(2.0, title="Take Profit (%)", step=0.1) / 100
// Session Time Inputs
tradeSession = input.session("0930-1600", title="Trading Session")
timeZone = input.string("America/New_York", title="Time Zone")
// ==========================================
// 2. DEFINE THE TIME FILTER
// ==========================================
// The time() function returns 'na' (not applicable) if the current bar is outside the session.
// We use 'not na()' to create a simple true/false condition.
inSession = not na(time(timeframe.period, tradeSession, timeZone))
// ==========================================
// 3. DEFINE THE INDICATORS
// ==========================================
ema20 = ta.ema(close, 20)
rsiValue = ta.rsi(close, 14)
volSma20 = ta.sma(volume, 20)
// ==========================================
// 4. DEFINE THE EXACT ENTRY RULES
// ==========================================
// LONG: Price > EMA, RSI > 50, Volume spike, AND market is in the defined session
enterLong = (close > ema20) and (rsiValue > 50) and (volume > volSma20) and inSession
// SHORT: Price < EMA, RSI < 50, Volume spike, AND market is in the defined session
enterShort = (close < ema20) and (rsiValue < 50) and (volume > volSma20) and inSession
// ==========================================
// 5. EXECUTE TRADES & SET EXITS
// ==========================================
// Enter Long
if (enterLong and strategy.position_size == 0)
strategy.entry("Long", strategy.long)
// Enter Short
if (enterShort and strategy.position_size == 0)
strategy.entry("Short", strategy.short)
// Calculate dynamic SL/TP levels depending on trade direction
longStopPrice = strategy.position_avg_price * (1 - slPercent)
longTakeProfit = strategy.position_avg_price * (1 + tpPercent)
shortStopPrice = strategy.position_avg_price * (1 + slPercent)
shortTakeProfit = strategy.position_avg_price * (1 - tpPercent)
// Send the exit orders to the broker/backtester
if (strategy.position_size > 0) // If we are Long
strategy.exit("Exit Long", from_entry="Long", stop=longStopPrice, limit=longTakeProfit)
if (strategy.position_size < 0) // If we are Short
strategy.exit("Exit Short", from_entry="Short", stop=shortStopPrice, limit=shortTakeProfit)
// ==========================================
// 6. CHART VISUALS
// ==========================================
// Plot the 20 EMA
plot(ema20, color=color.blue, title="20 EMA", linewidth=2)
// Highlight the background green for Longs, red for Shorts
bgcolor(enterLong ? color.new(color.green, 85) : na, title="Long Entry Met")
bgcolor(enterShort ? color.new(color.red, 85) : na, title="Short Entry Met")
// Highlight the trading session background slightly to see when the bot is active
bgcolor(inSession ? color.new(color.gray, 95) : na, title="Session Active")
// Determine which level to plot based on position direction
activeTP = strategy.position_size > 0 ? longTakeProfit : (strategy.position_size < 0 ? shortTakeProfit : na)
activeSL = strategy.position_size > 0 ? longStopPrice : (strategy.position_size < 0 ? shortStopPrice : na)
// Draw visual lines for SL and TP while in a trade
plot(activeTP, color=color.green, style=plot.style_linebr, linewidth=2, title="Active TP Level")
plot(activeSL, color=color.red, style=plot.style_linebr, linewidth=2, title="Active SL Level")