Correlation RATIO

veip7rades · study · 69 行 · 点赞 7 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Correlation Analyzer: VIX vs Benchmark", shorttitle="Corr VIX/Bench", overlay=false, precision=2, max_bars_back=5000)
 
// ── Inputs ────────────────────────────────────────────
symA        = input.symbol("", "Ticker A (empty = chart symbol, e.g. your benchmark)")
symB        = input.symbol("CBOE:VIX", "Ticker B")
lenValue    = input.int(1, "Period: value", minval=1, maxval=5000)
lenUnit     = input.string("Years", "Period: unit", options=["Bars", "Days", "Months", "Years"])
useReturns  = input.bool(true, "Use percentage returns (recommended)")
smoothLen   = input.int(1, "Smoothing (1 = none)", minval=1)
highTh      = input.float(0.7, "High correlation threshold", minval=0, maxval=1, step=0.05)
lowTh       = input.float(-0.7, "Negative correlation threshold", minval=-1, maxval=0, step=0.05)
 
// ── Period → bars conversion (based on chart timeframe) ──
// Days = calendar days; months = 1/12 of a year; max 10 years
barsPerYear = timeframe.isdaily   ? 252.0 / timeframe.multiplier :
              timeframe.isweekly  ? 52.0  / timeframe.multiplier :
              timeframe.ismonthly ? 12.0  / timeframe.multiplier :
              (23400.0 / timeframe.in_seconds()) * 252.0   // intraday estimate (6.5h/day)
 
years = lenUnit == "Days" ? lenValue / 365.25 : lenUnit == "Months" ? lenValue / 12.0 : lenUnit == "Years" ? float(lenValue) : na
 
lenRaw = lenUnit == "Bars" ? lenValue : int(math.round(math.min(years, 10.0) * barsPerYear))
len    = math.min(5000, math.max(2, lenRaw))
 
// ── Data ──────────────────────────────────────────────
tfA = symA == "" ? syminfo.tickerid : symA
 
getSeries(sym) =>
    request.security(sym, timeframe.period,
         useReturns ? close / close[1] - 1 : close,
         gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
 
a = getSeries(tfA)
b = getSeries(symB)
 
// ── Calculation ───────────────────────────────────────
corr    = ta.correlation(a, b, len)
corrOut = smoothLen > 1 ? ta.sma(corr, smoothLen) : corr
 
// ── Plot ──────────────────────────────────────────────
col = corrOut >= highTh ? color.new(color.green, 0) :
      corrOut <= lowTh  ? color.new(color.red, 0)   :
      color.new(color.gray, 0)
 
plot(corrOut, "Correlation", color = col, linewidth = 2, style = plot.style_line)
 
pTop  = hline(1,    "+1",  color = color.new(color.gray, 70), linestyle = hline.style_dotted)
pHi   = hline(highTh, "High", color = color.new(color.green, 50), linestyle = hline.style_dashed)
pZero = hline(0,    "0",   color = color.new(color.gray, 40))
pLo   = hline(lowTh, "Low", color = color.new(color.red, 50), linestyle = hline.style_dashed)
pBot  = hline(-1,   "-1",  color = color.new(color.gray, 70), linestyle = hline.style_dotted)
 
fill(pHi, pTop, color = color.new(color.green, 90))
fill(pBot, pLo, color = color.new(color.red, 90))
 
// ── Table with current value ──────────────────────────
var table t = table.new(position.top_right, 1, 1, bgcolor = color.new(color.black, 70))
if barstate.islast
    txt = (symA == "" ? syminfo.ticker : symA) + " vs " + symB + "\n" +
          str.tostring(corrOut, "#.##") + "\nwindow: " + str.tostring(lenValue) + " " + str.lower(lenUnit) +
          " (" + str.tostring(len) + " bars)"
    if bar_index < len
        txt += "\n⚠ not enough history"
    table.cell(t, 0, 0, txt, text_color = color.white, text_size = size.normal)
 
// ── Alerts ────────────────────────────────────────────
alertcondition(ta.crossover(corrOut, highTh), "Correlation above high threshold")
alertcondition(ta.crossunder(corrOut, lowTh), "Correlation below negative threshold")

← 返回列表