Code: Select all
//@version=5
strategy("Pro Structural Reclaim [PA Scalp]", overlay=true, initial_capital=10000, currency=currency.USD, commission_type=strategy.commission.cash_per_order, commission_value=3)
// =========================================================================
// 1. INPUTS & CONFIGURATION
// =========================================================================
grp1 = "1. Risk & Trade Management"
riskPct = input.float(1.0, title="Risk per Trade (%)", step=0.1, group=grp1)
rrRatio = input.float(2.0, title="Risk/Reward Target", step=0.5, group=grp1)
grp2 = "2. Session Filter"
useSession = input.bool(true, title="Use Session Filter?", group=grp2)
tradeSession = input.session("0800-1700", title="Trading Session (e.g. London/NY)", group=grp2)
inSession = not useSession or not na(time(timeframe.period, tradeSession))
grp3 = "3. Price Action Microstructure (M15)"
leftBars = input.int(5, title="Pivot Left (Impulse Leg)", minval=1, group=grp3)
rightBars = input.int(2, title="Pivot Right (Confirmation)", minval=1, tooltip="Tighter = earlier reclaim detection. 2 or 3 is optimal.", group=grp3)
grp4 = "4. Higher Timeframe Bias (H4/D1)"
htfRes = input.timeframe("240", title="HTF Resolution", group=grp4)
// =========================================================================
// 2. HIGHER TIMEFRAME BIAS
// =========================================================================
// We calculate the HTF EMA and pull it down without lookahead bias
htfEma = ta.ema(close, 20)
htfBiasFilter = request.security(syminfo.tickerid, htfRes, htfEma, lookahead=barmerge.lookahead_off)
isBullishHTF = close > htfBiasFilter
isBearishHTF = close < htfBiasFilter
// =========================================================================
// 3. STATE MACHINE & MICROSTRUCTURE DETECTION
// =========================================================================
// Detect standard pivots
pl = ta.pivotlow(low, leftBars, rightBars)
ph = ta.pivothigh(high, leftBars, rightBars)
// State variables for tracking the PA setup
var float lastPL = na
var float prevPL = na
var float lastPH = na
var float prevPH = na
var bool isLongSetupActive = false
var bool isShortSetupActive = false
var float triggerLevel = na
var float invalidationLevel = na
// Visual state pointers (for garbage collection)
var line triggerLine = na
var line slLine = na
// --- LONG SETUP LOGIC (Higher Lows) ---
if not na(pl)
prevPL := lastPL
lastPL := pl
// Condition: Clean Higher Low + Bullish HTF Context
if lastPL > prevPL and isBullishHTF
isLongSetupActive := true
isShortSetupActive := false
invalidationLevel := lastPL
// The trigger is the highest point reached during the 'rightBars' confirmation period
triggerLevel := ta.highest(high, rightBars)
// Clean up old visuals
if not na(triggerLine)
line.delete(triggerLine)
if not na(slLine)
line.delete(slLine)
// Draw new active setup levels
triggerLine := line.new(bar_index, triggerLevel, bar_index + 1, triggerLevel, color=color.new(color.teal, 0), style=line.style_dashed, width=2)
slLine := line.new(bar_index, invalidationLevel, bar_index + 1, invalidationLevel, color=color.new(color.gray, 0), style=line.style_solid, width=1)
// --- SHORT SETUP LOGIC (Lower Highs) ---
if not na(ph)
prevPH := lastPH
lastPH := ph
// Condition: Clean Lower High + Bearish HTF Context
if lastPH < prevPH and isBearishHTF
isShortSetupActive := true
isLongSetupActive := false
invalidationLevel := lastPH
// The trigger is the lowest point reached during the 'rightBars' confirmation period
triggerLevel := ta.lowest(low, rightBars)
// Clean up old visuals
if not na(triggerLine)
line.delete(triggerLine)
if not na(slLine)
line.delete(slLine)
// Draw new active setup levels
triggerLine := line.new(bar_index, triggerLevel, bar_index + 1, triggerLevel, color=color.new(color.red, 0), style=line.style_dashed, width=2)
slLine := line.new(bar_index, invalidationLevel, bar_index + 1, invalidationLevel, color=color.new(color.gray, 0), style=line.style_solid, width=1)
// =========================================================================
// 4. DYNAMIC VISUAL EXTENSIONS & SETUP CANCELLATION
// =========================================================================
// Extend lines dynamically if a setup is active
if isLongSetupActive or isShortSetupActive
line.set_x2(triggerLine, bar_index + 1)
line.set_x2(slLine, bar_index + 1)
// KILL SWITCH: Void the setup if invalidation is hit BEFORE the reclaim
if isLongSetupActive and low <= invalidationLevel
isLongSetupActive := false
line.set_color(triggerLine, color.new(color.gray, 70)) // Fade out voided setup
if isShortSetupActive and high >= invalidationLevel
isShortSetupActive := false
line.set_color(triggerLine, color.new(color.gray, 70)) // Fade out voided setup
// =========================================================================
// 5. EXECUTION & RISK SIZING
// =========================================================================
// Calculate Position Size based on absolute distance to stop loss
getQty(slPrice) =>
riskAmount = strategy.equity * (riskPct / 100)
distanceToSL = math.abs(close - slPrice)
qty = distanceToSL > 0 ? (riskAmount / distanceToSL) : 0
qty
// Execute Reclaims
triggerLongReclaim = isLongSetupActive and close > triggerLevel and inSession and strategy.position_size == 0
if triggerLongReclaim
qty = getQty(invalidationLevel)
tpPrice = close + ((close - invalidationLevel) * rrRatio)
strategy.entry("Long", strategy.long, qty=qty)
strategy.exit("Exit Long", "Long", stop=invalidationLevel, limit=tpPrice)
// Clear state after execution
isLongSetupActive := false
line.set_style(triggerLine, line.style_solid) // Solidify line on execution
triggerShortReclaim = isShortSetupActive and close < triggerLevel and inSession and strategy.position_size == 0
if triggerShortReclaim
qty = getQty(invalidationLevel)
tpPrice = close - ((invalidationLevel - close) * rrRatio)
strategy.entry("Short", strategy.short, qty=qty)
strategy.exit("Exit Short", "Short", stop=invalidationLevel, limit=tpPrice)
// Clear state after execution
isShortSetupActive := false
line.set_style(triggerLine, line.style_solid) // Solidify line on execution
// =========================================================================
// 6. CHART CONTEXT
// =========================================================================
plot(htfBiasFilter, color=isBullishHTF ? color.new(color.teal, 50) : color.new(color.red, 50), linewidth=2, title="HTF Bias")
bgcolor(inSession ? na : color.new(color.gray, 95), title="Out of Session Dimming")