//@version=5
strategy(“NASDAQ 100 Strategy”, overlay=true,
default_qty_type=strategy.percent_of_equity,
default_qty_value=1.5,
process_orders_on_close=true) // Added to prevent intrabar repainting
// User-configurable inputs
initialCapital = input.float(10000, “Initial Capital ($)”, minval=1000, step=1000)
equityPercentPerTrade = input.float(1.5, “Equity % per Trade”, minval=0.1, maxval=100, step=0.1)
// Time settings – using session.ismarket to prevent time-based repainting
isRegularSession = time(“1”, “0800-1600”)
isExtendedSession = time(“1”, “0800-1600”)
// Indicator settings
longTermEmaLength = input.int(21, “Long Term EMA Length”, minval=5)
shortTermEmaLength = input.int(9, “Short Term EMA Length”, minval=3)
rsiLength = input.int(14, “RSI Length”, minval=5)
atrLength = input.int(14, “ATR Length”, minval=5)
// Risk management inputs
trailStopATRMultiplier = input.float(1.5, “Trail Stop ATR Multiplier”, minval=0.5, step=0.1)
initialStopLossMultiplier = input.float(0.5, “Initial Stop Loss Multiplier”, minval=0.1, step=0.1)
breakEvenTriggerMultiplier = input.float(1.5, “Break-even Trigger Multiplier”, minval=0.5, step=0.1)
timeExitBars = input.int(20, “Time Exit Bars”, minval=5)
maxDrawdown = input.float(20, “Max Drawdown %”, minval=1, maxval=50)
// Calculate position size
tradeQty = (initialCapital * equityPercentPerTrade / 100) / close
// Non-repainting indicators
vwap = ta.vwap(hlc3)
longTermEma = ta.ema(close, longTermEmaLength)
shortTermEma = ta.ema(close, shortTermEmaLength)
rsi = ta.rsi(close, rsiLength)
atr = ta.atr(atrLength)
volatilityFilter = ta.valuewhen(barstate.isconfirmed, atr > ta.sma(atr, 20), 0)
// EMA directions calculated on confirmed bars only
longEmaDirection = ta.valuewhen(barstate.isconfirmed, longTermEma > longTermEma[1], 0)
shortEmaDirection = ta.valuewhen(barstate.isconfirmed, shortTermEma > shortTermEma[1], 0)
isTradeWindow = isExtendedSession and volatilityFilter
// Conditions – evaluated only on bar close
longCondition = barstate.isconfirmed and (close > shortTermEma) and
(shortTermEma > longTermEma) and longEmaDirection and
shortEmaDirection and (rsi > 50) and (close > vwap) and
isTradeWindow and isRegularSession
shortCondition = barstate.isconfirmed and (close < shortTermEma) and
(shortTermEma < longTermEma) and not longEmaDirection and
not shortEmaDirection and (rsi < 50) and (close < vwap) and
isTradeWindow and isRegularSession
// Entry/Exit tracking
var int longEntryBar = na
var int shortEntryBar = na
var float longEntryPrice = na
var float shortEntryPrice = na
// Execute trades on confirmed bars
if (longCondition)
strategy.entry("Long", strategy.long, qty=tradeQty)
longEntryBar := bar_index
longEntryPrice := close
if (shortCondition)
strategy.entry("Short", strategy.short, qty=tradeQty)
shortEntryBar := bar_index
shortEntryPrice := close
// Exit logic - non-repainting
strategy.exit("Exit Long", "Long",
stop=longEntryPrice - atr * initialStopLossMultiplier,
trail_points=atr * trailStopATRMultiplier,
trail_offset=atr * trailStopATRMultiplier / 2)
strategy.exit("Exit Short", "Short",
stop=shortEntryPrice + atr * initialStopLossMultiplier,
trail_points=atr * trailStopATRMultiplier,
trail_offset=atr * trailStopATRMultiplier / 2)
// Break-even logic - non-repainting
breakEvenTrigger = breakEvenTriggerMultiplier * atr
if (strategy.position_size > 0 and close > longEntryPrice + breakEvenTrigger)
strategy.exit(“Break-even Long”, “Long”, stop=longEntryPrice)
if (strategy.position_size < 0 and close < shortEntryPrice - breakEvenTrigger)
strategy.exit("Break-even Short", "Short", stop=shortEntryPrice)
// Time-based exit - non-repainting
if (not na(longEntryBar) and bar_index - longEntryBar >= timeExitBars)
strategy.close(“Long”)
longEntryBar := na
if (not na(shortEntryBar) and bar_index – shortEntryBar >= timeExitBars)
strategy.close(“Short”)
shortEntryBar := na
// Trend reversal exit – non-repainting
trendReversalExitLong = barstate.isconfirmed and (shortTermEma < longTermEma) and not longEmaDirection
trendReversalExitShort = barstate.isconfirmed and (shortTermEma > longTermEma) and not shortEmaDirection
if (strategy.position_size > 0 and trendReversalExitLong)
strategy.close(“Long”)
if (strategy.position_size < 0 and trendReversalExitShort)
strategy.close("Short")
// Max drawdown protection
if (strategy.equity / initialCapital < 1 - (maxDrawdown / 100.0))
strategy.cancel_all()
strategy.close_all()
// Plotting (non-repainting)
plot(vwap, "VWAP", color=color.purple, linewidth=2)
plot(longTermEma, "Long EMA", color=color.blue, linewidth=2)
plot(shortTermEma, "Short EMA", color=color.orange, linewidth=2)
// Labels only on bar close
if barstate.isconfirmed
label.new(bar_index, high, "RSI: " + str.tostring(rsi, "#.00"),
color=color.rgb(0, 0, 0, 80), textcolor=color.white,
style=label.style_label_center, yloc=yloc.price, size=size.small)
label.new(bar_index, low, "ATR: " + str.tostring(atr, "#.00"),
color=color.rgb(0, 0, 0, 80), textcolor=color.white,
style=label.style_label_center, yloc=yloc.price, size=size.small)