Code: Select all
//@version=5
strategy("Pro 5-Min Sweet Spot Scalper", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.02, calc_on_every_tick=true)
// =========================================================================
// 1. INPUTS: Pro Trade Desk Settings
// =========================================================================
grpRisk = "1. Risk Management & Sizing"
riskPct = input.float(1.0, title="Risk Per Trade (%)", group=grpRisk, tooltip="Calculates position size to strictly risk this % of your equity.")
atrLen = input.int(14, title="ATR Length", group=grpRisk)
slMult = input.float(1.5, title="Stop Loss ATR Multiplier", step=0.1, group=grpRisk)
tp1Mult = input.float(1.5, title="TP1 (Scale Out 50%) ATR", step=0.1, group=grpRisk)
tp2Mult = input.float(3.0, title="TP2 (Runner) ATR", step=0.1, group=grpRisk)
grpTime = "2. Session Constraints"
useTime = input.bool(true, title="Enable Session Filter?", group=grpTime)
tradeTime = input.session("0930-1530", title="Trading Hours (Exchange Time)", group=grpTime)
grpTrend = "3. Trend & Momentum Filters"
fastLen = input.int(9, title="Fast EMA", group=grpTrend)
slowLen = input.int(21, title="Slow EMA", group=grpTrend)
trendLen = input.int(200, title="200 EMA (Macro Trend)", group=grpTrend)
adxThresh = input.int(20, title="ADX Minimum (Chop Filter)", group=grpTrend, tooltip="Requires ADX to be above this number. Filters out sideways, choppy markets.")
// =========================================================================
// 2. INDICATORS & FILTERS
// =========================================================================
fastEma = ta.ema(close, fastLen)
slowEma = ta.ema(close, slowLen)
macroTrend = ta.ema(close, trendLen)
atr = ta.atr(atrLen)
// ADX Calculation for Chop Filter
[diPlus, diMinus, adx] = ta.dmi(14, 14)
isTrending = adx > adxThresh
// Session Filter Logic
inSession = useTime ? not na(time(timeframe.period, tradeTime)) : true
// Plotting
plot(fastEma, color=color.new(color.blue, 0), title="9 EMA", linewidth=1)
plot(slowEma, color=color.new(color.orange, 0), title="21 EMA", linewidth=1)
plot(macroTrend, color=color.new(color.white, 0), title="200 EMA", linewidth=2)
// =========================================================================
// 3. EXECUTION LOGIC & POSITION SIZING
// =========================================================================
longSignal = ta.crossover(fastEma, slowEma) and close > macroTrend and isTrending and inSession
shortSignal = ta.crossunder(fastEma, slowEma) and close < macroTrend and isTrending and inSession
// Persistent variables for dynamic trade management
var float entryPrice = na
var float slPrice = na
var float tp1Price = na
var float tp2Price = na
var bool tp1Hit = false
// Position Sizing Math (Risk % / Stop Loss Distance)
riskAmount = (strategy.equity * riskPct) / 100
slDistance = atr * slMult
tradeQty = slDistance > 0 ? (riskAmount / slDistance) : 0
if (longSignal and strategy.position_size == 0)
entryPrice := close
slPrice := close - slDistance
tp1Price := close + (atr * tp1Mult)
tp2Price := close + (atr * tp2Mult)
tp1Hit := false
strategy.entry("Long", strategy.long, qty=tradeQty)
if (shortSignal and strategy.position_size == 0)
entryPrice := close
slPrice := close + slDistance
tp1Price := close - (atr * tp1Mult)
tp2Price := close - (atr * tp2Mult)
tp1Hit := false
strategy.entry("Short", strategy.short, qty=tradeQty)
// =========================================================================
// 4. ADVANCED TRADE MANAGEMENT (Scale-outs & Breakeven)
// =========================================================================
if (strategy.position_size > 0)
// Check if TP1 is hit to trigger breakeven
if high >= tp1Price
tp1Hit := true
// If TP1 is hit, SL moves to Entry. Otherwise, it stays at original SL.
activeSL = tp1Hit ? entryPrice : slPrice
// Manage Exits
strategy.exit("TP1", "Long", qty_percent=50, limit=tp1Price, stop=activeSL)
strategy.exit("TP2", "Long", limit=tp2Price, stop=activeSL)
if (strategy.position_size < 0)
if low <= tp1Price
tp1Hit := true
activeSL = tp1Hit ? entryPrice : slPrice
strategy.exit("TP1", "Short", qty_percent=50, limit=tp1Price, stop=activeSL)
strategy.exit("TP2", "Short", limit=tp2Price, stop=activeSL)
// =========================================================================
// 5. VISUAL SIGNALS & HUD DASHBOARD
// =========================================================================
plotshape(series=longSignal and strategy.position_size == 0, title="Long Entry", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", textcolor=color.white, size=size.small)
plotshape(series=shortSignal and strategy.position_size == 0, title="Short Entry", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", textcolor=color.white, size=size.small)
// HUD (Heads Up Display)
var table hud = table.new(position.bottom_right, 2, 4, border_width=1)
if barstate.islast
table.cell(hud, 0, 0, "Macro Trend (200 EMA)", text_color=color.white, bgcolor=color.gray)
table.cell(hud, 1, 0, close > macroTrend ? "BULLISH" : "BEARISH", text_color=color.white, bgcolor=close > macroTrend ? color.green : color.red)
table.cell(hud, 0, 1, "Market State (ADX)", text_color=color.white, bgcolor=color.gray)
table.cell(hud, 1, 1, isTrending ? "TRENDING" : "CHOPPING", text_color=color.white, bgcolor=isTrending ? color.green : color.orange)
table.cell(hud, 0, 2, "Session Active", text_color=color.white, bgcolor=color.gray)
table.cell(hud, 1, 2, inSession ? "YES" : "NO", text_color=color.white, bgcolor=inSession ? color.green : color.red)