This script uses request.security to pull data for both your Gold and Yen tickers simultaneously, regardless of which chart you are currently viewing. It builds a live, dynamic risk matrix on your screen.
Code: Select all
//@version=5
indicator("Institutional Thematic Risk Matrix", overlay=true, max_lines_count=100)
// =========================================================================
// INPUTS: THE RISK DESK PARAMETERS
// =========================================================================
var string GRP_CONFIG = "1. Instrument Configuration"
tkrXAU = input.symbol("OANDA:XAUUSD", title="Gold Ticker", group=GRP_CONFIG)
tkrJPY = input.symbol("OANDA:USDJPY", title="Yen Ticker", group=GRP_CONFIG)
var string GRP_RISK = "2. Capital & Risk Allocation"
acctBalance = input.float(100000.0, title="Account Equity ($)", group=GRP_RISK)
maxSessionRisk = input.float(1.0, title="Max Session Risk (%)", step=0.1, group=GRP_RISK, tooltip="Total risk allowed across all correlated assets for this session.")
secondaryCap = input.float(15.0, title="Secondary Allocation Cap (%)", step=1.0, group=GRP_RISK, tooltip="Max percentage of the session risk budget allocated to the secondary theme.")
var string GRP_THEME = "3. The Session Intent (07:00 Fix)"
primaryTheme = input.string("XAU (Metals)", title="Primary Driver", options=["XAU (Metals)", "JPY (Rates)", "FLAT (Cash)"], group=GRP_THEME)
var string GRP_VOL = "4. Volatility Parity Settings"
atrLength = input.int(14, title="ATR Length", group=GRP_VOL)
stopMultiplier = input.float(1.5, title="Stop Distance (ATR Multiplier)", step=0.1, group=GRP_VOL)
// =========================================================================
// CALCULATIONS: VOLATILITY & CAPITAL ALLOCATION
// =========================================================================
// Fetch ATR for both instruments to ensure volatility parity
[xauClose, xauAtr] = request.security(tkrXAU, timeframe.period, [close, ta.atr(atrLength)])
[jpyClose, jpyAtr] = request.security(tkrJPY, timeframe.period, [close, ta.atr(atrLength)])
// Calculate Risk Budgets (in Dollars)
float totalRiskCapital = acctBalance * (maxSessionRisk / 100)
float primaryRiskCapital = totalRiskCapital * (1 - (secondaryCap / 100))
float secondaryRiskCapital = totalRiskCapital * (secondaryCap / 100)
// Assign Risk Capital based on the 07:00 Sticky Note
float xauCapitalLimit = 0.0
float jpyCapitalLimit = 0.0
string xauStatus = ""
string jpyStatus = ""
if primaryTheme == "XAU (Metals)"
xauCapitalLimit := primaryRiskCapital
jpyCapitalLimit := secondaryRiskCapital
xauStatus := "DOMINANT"
jpyStatus := "SCALP ONLY"
else if primaryTheme == "JPY (Rates)"
xauCapitalLimit := secondaryRiskCapital
jpyCapitalLimit := primaryRiskCapital
xauStatus := "SCALP ONLY"
jpyStatus := "DOMINANT"
else
xauCapitalLimit := 0.0
jpyCapitalLimit := 0.0
xauStatus := "LOCKED"
jpyStatus := "LOCKED"
// Calculate Position Sizing (Units) based on Volatility (Stop Distance)
// Formula: Risk $ / (Stop Distance in Price terms)
float xauStopDist = xauAtr * stopMultiplier
float jpyStopDist = jpyAtr * stopMultiplier
float xauMaxUnits = xauCapitalLimit / xauStopDist
float jpyMaxUnits = jpyCapitalLimit / jpyStopDist
// =========================================================================
// HUD RENDERING: THE RISK MANAGER UI
// =========================================================================
// Institutional styling: Dark backgrounds, monospaced text, strict colors
var table riskHUD = table.new(position.top_right, 4, 5, bgcolor=color.rgb(15, 15, 15, 10), border_width=1, border_color=color.rgb(50, 50, 50, 0))
if barstate.islast
// Headers
table.cell(riskHUD, 0, 0, "INSTRUMENT", text_color=color.gray, text_size=size.small, bgcolor=color.black)
table.cell(riskHUD, 1, 0, "THEME ROLE", text_color=color.gray, text_size=size.small, bgcolor=color.black)
table.cell(riskHUD, 2, 0, "CAPITAL AT RISK", text_color=color.gray, text_size=size.small, bgcolor=color.black)
table.cell(riskHUD, 3, 0, "MAX SIZE (UNITS)", text_color=color.gray, text_size=size.small, bgcolor=color.black)
// XAU Row
c_xau = primaryTheme == "XAU (Metals)" ? color.rgb(255, 204, 0) : color.gray
table.cell(riskHUD, 0, 1, tkrXAU, text_color=c_xau, text_weight=font.weight_bold)
table.cell(riskHUD, 1, 1, xauStatus, text_color=c_xau, text_size=size.small)
table.cell(riskHUD, 2, 1, "$" + str.tostring(math.round(xauCapitalLimit)), text_color=c_xau)
table.cell(riskHUD, 3, 1, str.tostring(math.round(xauMaxUnits, 2)), text_color=c_xau, text_weight=font.weight_bold)
// JPY Row
c_jpy = primaryTheme == "JPY (Rates)" ? color.rgb(0, 204, 255) : color.gray
table.cell(riskHUD, 0, 2, tkrJPY, text_color=c_jpy, text_weight=font.weight_bold)
table.cell(riskHUD, 1, 2, jpyStatus, text_color=c_jpy, text_size=size.small)
table.cell(riskHUD, 2, 2, "$" + str.tostring(math.round(jpyCapitalLimit)), text_color=c_jpy)
table.cell(riskHUD, 3, 2, str.tostring(math.round(jpyMaxUnits, 2)), text_color=c_jpy, text_weight=font.weight_bold)
// Total Exposure Row
table.cell(riskHUD, 0, 3, "SESSION TOTAL", text_color=color.white, text_size=size.small, bgcolor=color.rgb(30, 30, 30))
table.cell(riskHUD, 1, 3, "BUDGET: " + str.tostring(maxSessionRisk) + "%", text_color=color.white, text_size=size.small, bgcolor=color.rgb(30, 30, 30))
table.cell(riskHUD, 2, 3, "$" + str.tostring(math.round(xauCapitalLimit + jpyCapitalLimit)), text_color=color.white, text_weight=font.weight_bold, bgcolor=color.rgb(30, 30, 30))
table.cell(riskHUD, 3, 3, "N/A", text_color=color.gray, text_size=size.small, bgcolor=color.rgb(30, 30, 30))
// Operational Directive
table.cell(riskHUD, 0, 4, "PROTOCOL: FLAT ENTIRE BOOK BEFORE CHANGING PRIMARY THEME.", text_color=color.rgb(255, 50, 50), text_size=size.small, text_weight=font.weight_bold)
table.merge_cells(riskHUD, 0, 4, 3, 4)