本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =====================================================================
// Gold–DXY Correlation — Light
// Version: 1.0
//
// Rolling correlation between Gold (XAU/USD) and the US Dollar Index
// (DXY), computed on RETURNS (ta.change) — the only mathematically
// honest way to measure correlation between two price series of
// different orders of magnitude. Ships with a sub-pane chart, a zero
// line, ±0.5 regime bands, and a compact top-right dashboard.
//
// No lookahead. Pine v6 strict.
// =====================================================================
indicator("Gold–DXY Correlation — Light", shorttitle = "Gold-DXY Corr",
overlay = false, precision = 3,
max_labels_count = 50)
// =============================
// INPUTS
// =============================
grpSrc = "Sources"
srcSym = input.symbol("OANDA:XAUUSD", "Primary symbol", group = grpSrc,
tooltip = "Default: Gold spot. Change to any symbol you want to correlate against DXY.")
dxySym = input.symbol("TVC:DXY", "Reference symbol (DXY)", group = grpSrc,
tooltip = "Default: US Dollar Index. You can point this at any other reference, e.g. US10Y, SPX, BTC.")
useCurrentForSrc = input.bool(true, "Use current chart as primary", group = grpSrc,
tooltip = "If true, the current chart is used as the primary symbol (ignores 'Primary symbol' above). Recommended.")
grpCalc = "Calculation"
lenCorr = input.int(50, "Correlation length (bars)", minval = 5, maxval = 500, group = grpCalc,
tooltip = "Number of bars in the rolling window. 50 is standard on intraday; try 100 on daily.")
useLogRet = input.bool(false, "Use log returns", group = grpCalc,
tooltip = "If true, returns are computed as log(P_t / P_{t-1}). Otherwise simple returns (P_t / P_{t-1} - 1).")
grpUI = "Display"
showZeroLine = input.bool(true, "Show zero line", group = grpUI)
showBands = input.bool(true, "Show ±0.5 regime bands", group = grpUI)
showDash = input.bool(true, "Show dashboard", group = grpUI)
dashPos = input.string("top_right", "Dashboard position", options = ["top_right", "top_left", "bottom_right", "bottom_left", "middle_right"], group = grpUI)
dashSize = input.string("small", "Dashboard size", options = ["tiny", "small", "normal"], group = grpUI)
// =============================
// DATA FETCH
// =============================
string srcFinal = useCurrentForSrc ? syminfo.tickerid : srcSym
srcClose = request.security(srcFinal, timeframe.period, close, ignore_invalid_symbol = true, lookahead = barmerge.lookahead_off)
dxyClose = request.security(dxySym, timeframe.period, close, ignore_invalid_symbol = true, lookahead = barmerge.lookahead_off)
// =============================
// RETURNS
// =============================
srcRet = useLogRet ? math.log(srcClose / nz(srcClose[1], srcClose)) : ta.change(srcClose) / nz(srcClose[1], 1.0)
dxyRet = useLogRet ? math.log(dxyClose / nz(dxyClose[1], dxyClose)) : ta.change(dxyClose) / nz(dxyClose[1], 1.0)
// =============================
// ROLLING CORRELATION
// =============================
// Guard: if either series has no data, correlation is na.
haveSrc = not na(srcClose) and not na(srcClose[lenCorr])
haveDxy = not na(dxyClose) and not na(dxyClose[lenCorr])
corr = (haveSrc and haveDxy) ? ta.correlation(srcRet, dxyRet, lenCorr) : na
// =============================
// PLOT
// =============================
colUp = color.new(#EF5350, 0) // positive corr with DXY = unusual for Gold (red = tension)
colDown = color.new(#26A69A, 0) // negative corr = expected = calm
colMid = color.new(#FFA726, 0)
corrCol = not na(corr) ? (corr >= 0.5 ? colUp : corr <= -0.5 ? colDown : colMid) : color.gray
plot(corr, "Rolling correlation", color = corrCol, linewidth = 2)
hline(showZeroLine ? 0 : na, "Zero", color = color.new(color.gray, 40), linestyle = hline.style_dotted)
hline(showBands ? 0.5 : na, "+0.5", color = color.new(#EF5350, 60), linestyle = hline.style_dashed)
hline(showBands ? -0.5 : na, "-0.5", color = color.new(#26A69A, 60), linestyle = hline.style_dashed)
// =============================
// DASHBOARD
// =============================
f_pos(string p) =>
p == "top_left" ? position.top_left :
p == "top_right" ? position.top_right :
p == "bottom_left" ? position.bottom_left :
p == "bottom_right" ? position.bottom_right : position.middle_right
f_size(string s) =>
s == "tiny" ? size.tiny :
s == "normal" ? size.normal : size.small
var table dash = table.new(position = f_pos(dashPos), columns = 2, rows = 5,
bgcolor = color.new(color.black, 80), border_color = color.new(color.gray, 60), border_width = 1)
string regimeTxt = na(corr) ? "loading" : corr >= 0.5 ? "STRONG POSITIVE" : corr <= -0.5 ? "STRONG NEGATIVE" : corr >= 0.2 ? "mild positive" : corr <= -0.2 ? "mild negative" : "decorrelated"
color regimeCol = na(corr) ? color.gray : corr >= 0.5 ? #EF5350 : corr <= -0.5 ? #26A69A : corr >= 0.2 or corr <= -0.2 ? #FFA726 : color.silver
if showDash and barstate.islast
table.clear(dash, 0, 0, 1, 4)
table.cell(dash, 0, 0, "Gold-DXY Corr", text_color = color.white, text_size = f_size(dashSize), bgcolor = color.new(color.black, 40))
table.cell(dash, 1, 0, str.format("n={0}", lenCorr), text_color = color.white, text_size = f_size(dashSize), bgcolor = color.new(color.black, 40))
table.cell(dash, 0, 1, "Primary", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 1, useCurrentForSrc ? syminfo.ticker : srcSym, text_color = color.white, text_size = f_size(dashSize))
table.cell(dash, 0, 2, "Reference", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 2, dxySym, text_color = color.white, text_size = f_size(dashSize))
table.cell(dash, 0, 3, "Correlation", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 3, na(corr) ? "—" : str.tostring(corr, "0.000"), text_color = regimeCol, text_size = f_size(dashSize))
table.cell(dash, 0, 4, "Regime", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 4, regimeTxt, text_color = regimeCol, text_size = f_size(dashSize))
// =============================
// ALERTS
// =============================
crossDown05 = not na(corr) and not na(corr[1]) and corr[1] > -0.5 and corr <= -0.5
crossUp05 = not na(corr) and not na(corr[1]) and corr[1] < 0.5 and corr >= 0.5
crossDownZero = not na(corr) and not na(corr[1]) and corr[1] > 0 and corr <= 0
crossUpZero = not na(corr) and not na(corr[1]) and corr[1] < 0 and corr >= 0
alertcondition(crossDown05, "Correlation flipped to STRONG NEGATIVE", "Rolling correlation crossed below -0.5.")
alertcondition(crossUp05, "Correlation flipped to STRONG POSITIVE", "Rolling correlation crossed above +0.5.")
alertcondition(crossDownZero, "Correlation crossed zero DOWN", "Rolling correlation crossed below 0.")
alertcondition(crossUpZero, "Correlation crossed zero UP", "Rolling correlation crossed above 0.")