This upgraded version acts as a visual execution engine. It introduces a Heads-Up Display (HUD) for real-time state tracking, explicit risk mapping (Entry, Stop, Target lines), and programmatic alerts for automated prop-firm copiers.
Code: Select all
//@version=5
indicator("XAU Institutional Invalidation Engine", overlay=true, max_lines_count=100, max_labels_count=100, max_boxes_count=50)
// ================= Core Parameters =================
atrLength = input.int(14, title="ATR Period", group="1. Volatility Baseline")
impulseMult = input.float(2.2, title="Displacement Threshold (x ATR)", step=0.1, group="1. Volatility Baseline")
volumeFilter = input.bool(true, title="Require Volume Expansion", group="1. Volatility Baseline")
timeStopBars = input.int(4, title="Velocity Decay Limit (Bars)", group="2. Invalidation Matrix")
rrTarget = input.float(2.0, title="Expected R-Multiple", step=0.1, group="2. Invalidation Matrix")
// ================= Market Data =================
atr = ta.atr(atrLength)
body = math.abs(close - open)
volMA = ta.sma(volume, 20)
volValid = volumeFilter ? (volume > volMA) : true
// ================= Setup Detection =================
isBullImpulse = close > open and body > (atr * impulseMult) and volValid
isBearImpulse = open > close and body > (atr * impulseMult) and volValid
// ================= State Management =================
var bool isActive = false
var int direction = 0 // 1 = Long, -1 = Short
var float priceStop = na
var float priceEntry = na
var float priceTarget = na
var int barCount = na
var line stopLine = na
var line targetLine = na
// ================= Execution Engine =================
if not isActive
if isBullImpulse or isBearImpulse
isActive := true
direction := isBullImpulse ? 1 : -1
priceEntry := close
priceStop := isBullImpulse ? low : high
riskDistance = math.abs(priceEntry - priceStop)
priceTarget := isBullImpulse ? (priceEntry + (riskDistance * rrTarget)) : (priceEntry - (riskDistance * rrTarget))
barCount := 0
// Draw Risk Zones
stopLine := line.new(bar_index, priceStop, bar_index + timeStopBars, priceStop, color=color.red, width=2, style=line.style_dashed)
targetLine := line.new(bar_index, priceTarget, bar_index + timeStopBars, priceTarget, color=color.new(color.teal, 0), width=2, style=line.style_dashed)
// Box the impulse origin
box.new(bar_index[1], priceEntry, bar_index, priceStop, color=color.new(direction == 1 ? color.green : color.red, 80), border_color=color.new(color.gray, 50))
alert(direction == 1 ? "XAU LONG IMPULSE" : "XAU SHORT IMPULSE", alert.freq_once_per_bar)
// ================= Trade Management =================
if isActive
barCount += 1
// Extend lines for active visuals
line.set_x2(stopLine, bar_index + 1)
line.set_x2(targetLine, bar_index + 1)
// Invalidation Conditions
hitStop = (direction == 1 and low <= priceStop) or (direction == -1 and high >= priceStop)
hitTarget = (direction == 1 and high >= priceTarget) or (direction == -1 and low <= priceTarget)
hitTime = (barCount >= timeStopBars) and not hitTarget and not hitStop
if hitStop
label.new(bar_index, priceStop, "STRUCTURAL INVALIDATION", style=direction == 1 ? label.style_label_up : label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
alert("XAU STRUCTURAL STOP", alert.freq_once_per_bar)
isActive := false
else if hitTime
label.new(bar_index, close, "VELOCITY DECAY (TIME STOP)", style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.small)
alert("XAU TIME STOP", alert.freq_once_per_bar)
isActive := false
else if hitTarget
label.new(bar_index, priceTarget, "TARGET REALIZED", style=direction == 1 ? label.style_label_down : label.style_label_up, color=color.teal, textcolor=color.white, size=size.small)
isActive := false
// ================= HUD Dashboard =================
var table display = table.new(position.top_right, 2, 4, border_width = 1, border_color = color.new(color.gray, 80))
if barstate.islast
table.cell(display, 0, 0, "SYSTEM STATE", text_color=color.white, bgcolor=color.new(color.gray, 80))
table.cell(display, 1, 0, isActive ? "ACTIVE" : "SCANNING", text_color=isActive ? color.yellow : color.gray, bgcolor=color.new(color.black, 0))
table.cell(display, 0, 1, "T-MINUS (TIME STOP)", text_color=color.white, bgcolor=color.new(color.gray, 80))
table.cell(display, 1, 1, isActive ? str.tostring(timeStopBars - barCount) : "-", text_color=color.white, bgcolor=color.new(color.black, 0))
table.cell(display, 0, 2, "CURRENT SPREAD RISK", text_color=color.white, bgcolor=color.new(color.gray, 80))
// Approximate spread monitoring (Requires real-time tick data; simulated visually via difference in high/close proxy if needed, but best left as manual execution check in Pine)
table.cell(display, 1, 2, "CHECK B-BOOK", text_color=color.red, bgcolor=color.new(color.black, 0))