Code: Select all
//@version=5
strategy("Institutional XAUUSD Scalper Pro [Forex-Scalping.com]", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=5, commission_type=strategy.commission.cash_per_order, commission_value=3, slippage=2)
// ==========================================
// 1. USER INPUTS
// ==========================================
grp1 = "Trend & Trigger Settings"
fastEmaLen = input.int(9, title="Fast EMA Length", group=grp1)
slowEmaLen = input.int(21, title="Slow EMA Length", group=grp1)
trendEmaLen = input.int(200, title="Baseline Trend EMA", group=grp1)
grp2 = "Momentum, Volatility & Chop Filter"
rsiLen = input.int(14, title="RSI Length", group=grp2)
atrLen = input.int(14, title="ATR Length", group=grp2)
useAdxFilter = input.bool(true, title="Use ADX Chop Filter?", group=grp2)
adxLen = input.int(14, title="ADX Length", group=grp2)
adxThreshold = input.int(20, title="ADX Minimum Strength", group=grp2)
grp3 = "Risk Management (SL, TP & Trailing)"
slMultiplier = input.float(1.5, title="Stop Loss ATR Multiplier", step=0.1, group=grp3)
rrRatio = input.float(2.0, title="Risk/Reward Ratio", step=0.1, group=grp3)
useBreakEven = input.bool(true, title="Move SL to Break-Even at 1R?", group=grp3)
grp4 = "Session & Time Filters"
useSession = input.bool(true, title="Only Trade During Specific Sessions?", group=grp4)
sessionTime = input.session("0800-1600", title="Trading Hours (EST)", group=grp4)
// ==========================================
// 2. CALCULATIONS
// ==========================================
fastEma = ta.ema(close, fastEmaLen)
slowEma = ta.ema(close, slowEmaLen)
trendEma = ta.ema(close, trendEmaLen)
rsiVal = ta.rsi(close, rsiLen)
atrVal = ta.atr(atrLen)
// ADX Calculation
[diPlus, diMinus, adx] = ta.dmi(14, adxLen)
// Time Session Logic
inSession = not useSession or not na(time(timeframe.period, sessionTime, "America/New_York"))
// ==========================================
// 3. LOGIC & CONDITIONS
// ==========================================
bullishTrend = close > trendEma
bearishTrend = close < trendEma
trendStrong = not useAdxFilter or adx > adxThreshold
buySignal = ta.crossover(fastEma, slowEma) and bullishTrend and rsiVal > 50 and trendStrong and inSession
sellSignal = ta.crossunder(fastEma, slowEma) and bearishTrend and rsiVal < 50 and trendStrong and inSession
// ==========================================
// 4. STRATEGY EXECUTION & TRADE MANAGEMENT
// ==========================================
var float entryPrice = na
var float slLevel = na
var float tpLevel = na
var bool beTriggered = false
var float riskAmount = na
// Entry Logic
if buySignal and strategy.position_size == 0
entryPrice := close
riskAmount := atrVal * slMultiplier
slLevel := entryPrice - riskAmount
tpLevel := entryPrice + (riskAmount * rrRatio)
beTriggered := false
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", from_entry="Long", stop=slLevel, limit=tpLevel)
if sellSignal and strategy.position_size == 0
entryPrice := close
riskAmount := atrVal * slMultiplier
slLevel := entryPrice + riskAmount
tpLevel := entryPrice - (riskAmount * rrRatio)
beTriggered := false
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", from_entry="Short", stop=slLevel, limit=tpLevel)
// Trade Management (Break-Even Logic)
if strategy.position_size > 0 and useBreakEven and not beTriggered
if high >= entryPrice + riskAmount // Price reached 1R
slLevel := entryPrice // Move SL to entry
beTriggered := true
strategy.exit("Exit Long", from_entry="Long", stop=slLevel, limit=tpLevel)
if strategy.position_size < 0 and useBreakEven and not beTriggered
if low <= entryPrice - riskAmount // Price reached 1R
slLevel := entryPrice // Move SL to entry
beTriggered := true
strategy.exit("Exit Short", from_entry="Short", stop=slLevel, limit=tpLevel)
// Reset levels when flat
if strategy.position_size == 0
slLevel := na
tpLevel := na
entryPrice := na
// ==========================================
// 5. PLOTTING
// ==========================================
plot(fastEma, color=color.new(color.blue, 0), title="Fast EMA", linewidth=1)
plot(slowEma, color=color.new(color.orange, 0), title="Slow EMA", linewidth=1)
plot(trendEma, color=color.new(color.white, 0), title="200 EMA Baseline", linewidth=2)
// Dynamic Stop Loss and Take Profit Lines
plot(strategy.position_size != 0 ? slLevel : na, title="Stop Loss", color=color.new(color.red, 20), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size != 0 ? tpLevel : na, title="Take Profit", color=color.new(color.green, 20), style=plot.style_linebr, linewidth=2)
// Background Session Highlight
bgcolor(inSession and useSession ? color.new(color.blue, 95) : na, title="Trading Session Highlights")
// ==========================================
// 6. PRO DASHBOARD
// ==========================================
var table dash = table.new(position.top_right, 2, 5, border_color=color.new(color.gray, 80), border_width=1, frame_color=color.new(color.gray, 80), frame_width=1)
if barstate.islast
table.cell(dash, 0, 0, "PRO SCALPER", text_color=color.white, bgcolor=color.new(#0800ff, 20), text_size=size.small)
table.cell(dash, 1, 0, "STATUS", text_color=color.white, bgcolor=color.new(#0800ff, 20), text_size=size.small)
table.cell(dash, 0, 1, "Macro Trend", text_color=color.gray, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 1, 1, bullishTrend ? "BULL" : "BEAR", text_color=bullishTrend ? color.green : color.red, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 0, 2, "Volatility (ADX)", text_color=color.gray, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 1, 2, str.tostring(math.round(adx, 1)), text_color=trendStrong ? color.green : color.orange, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 0, 3, "Session Active", text_color=color.gray, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 1, 3, inSession ? "YES" : "NO", text_color=inSession ? color.green : color.red, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 0, 4, "Trade Status", text_color=color.gray, bgcolor=color.new(color.black, 0), text_size=size.small)
table.cell(dash, 1, 4, strategy.position_size > 0 ? "LONG" : strategy.position_size < 0 ? "SHORT" : "FLAT", text_color=strategy.position_size != 0 ? color.white : color.gray, bgcolor=strategy.position_size > 0 ? color.new(color.green, 30) : strategy.position_size < 0 ? color.new(color.red, 30) : color.new(color.black, 0), text_size=size.small)