To prevent TradingView from pinging you every 5 minutes while the correlation remains high, you need to trigger the alert only on the exact bar where the threshold is breached.
We do this by checking if the danger state is true on the current bar, but was false on the previous bar (danger_ag and not danger_ag[1]).
Code: Select all
//@version=5
indicator("Gold Inventory Risk Auditor + Correlation + Alerts", overlay=true)
// --- INPUTS: Risk Windows ---
tier1_time = input.session("08:25-08:45", "Tier-1 Data Window", group="Time Risk Zones")
swap_time = input.session("16:45-17:15", "Overnight Swap / Close", group="Time Risk Zones")
fomc_time = input.session("13:55-14:15", "Late NY Event", group="Time Risk Zones")
// --- INPUTS: Correlation Tracking ---
corr_len = input.int(20, "Correlation Lookback (Bars)", group="Stacked Risk Matrix")
sym_ag = input.symbol("OANDA:XAGUSD", "Silver Ticker", group="Stacked Risk Matrix")
sym_uj = input.symbol("OANDA:USDJPY", "USD/JPY Ticker", group="Stacked Risk Matrix")
corr_thresh = input.float(0.80, "Danger Threshold (Absolute)", step=0.05, group="Stacked Risk Matrix")
// --- LOGIC: Session Detection ---
in_tier1 = time(timeframe.period, tier1_time, "America/New_York")
in_swap = time(timeframe.period, swap_time, "America/New_York")
in_fomc = time(timeframe.period, fomc_time, "America/New_York")
// --- LOGIC: Correlation ---
close_ag = request.security(sym_ag, timeframe.period, close)
close_uj = request.security(sym_uj, timeframe.period, close)
corr_ag = ta.correlation(close, close_ag, corr_len)
corr_uj = ta.correlation(close, close_uj, corr_len)
danger_ag = corr_ag >= corr_thresh
danger_uj = corr_uj <= -corr_thresh
// --- VISUALS: Paint the Danger Zones ---
bgcolor(in_tier1 or in_fomc ? color.new(color.red, 85) : na, title="Macro Event Risk")
bgcolor(in_swap ? color.new(color.blue, 85) : na, title="Overnight Swap Risk")
// --- DASHBOARD: Weekly Audit & Live Risk ---
var table audit_board = table.new(position.bottom_right, 2, 6, border_width=1, border_color=color.gray)
if barstate.islast
table.cell(audit_board, 0, 0, "XAU DESK AUDIT", text_color=color.white, bgcolor=color.rgb(40, 40, 40), text_halign=text.align_left)
table.cell(audit_board, 1, 0, "STATUS", text_color=color.white, bgcolor=color.rgb(40, 40, 40))
table.cell(audit_board, 0, 1, "Flat Before Tier-1", text_color=color.gray, text_halign=text.align_left)
table.cell(audit_board, 1, 1, "Review", text_color=color.red, bgcolor=color.new(color.red, 90))
table.cell(audit_board, 0, 2, "Overnight Swap Leftovers", text_color=color.gray, text_halign=text.align_left)
table.cell(audit_board, 1, 2, "Review", text_color=color.blue, bgcolor=color.new(color.blue, 90))
table.cell(audit_board, 0, 3, "XAG/USD Correlation", text_color=color.white, text_halign=text.align_left)
color_ag = danger_ag ? color.red : color.gray
text_ag = danger_ag ? "STACKED (" + str.tostring(corr_ag, "#.##") + ")" : str.tostring(corr_ag, "#.##")
table.cell(audit_board, 1, 3, text_ag, text_color=color_ag, bgcolor=color.new(color_ag, 90))
table.cell(audit_board, 0, 4, "USD/JPY Correlation", text_color=color.white, text_halign=text.align_left)
color_uj = danger_uj ? color.red : color.gray
text_uj = danger_uj ? "STACKED (" + str.tostring(corr_uj, "#.##") + ")" : str.tostring(corr_uj, "#.##")
table.cell(audit_board, 1, 4, text_uj, text_color=color_uj, bgcolor=color.new(color_uj, 90))
table.cell(audit_board, 0, 5, "Broker vs. Entry Costs", text_color=color.gray, text_halign=text.align_left)
table.cell(audit_board, 1, 5, "Pending", text_color=color.orange, bgcolor=color.new(color.orange, 90))
// --- ALERTS: Automated Risk Pings ---
// Triggers only when crossing into the danger zone, preventing alert spam
alertcondition(danger_ag and not danger_ag[1], title="Silver Correlation Warning", message="XAU/XAG correlation breached limits. Do not stack risk.")
alertcondition(danger_uj and not danger_uj[1], title="USD/JPY Correlation Warning", message="XAU/USDJPY inverse correlation breached limits. Do not stack risk.")