本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("VIX Term Structure", shorttitle="VIX Term Struture", overlay=false, precision=2)
// Thresholds
strongContango = input.float(3.0, "Strong Contango %", step=0.1)
lowVix = input.float(16.0, "Low VIX", step=0.5)
elevatedVix = input.float(22.0, "Elevated VIX", step=0.5)
highVix = input.float(28.0, "High VIX", step=0.5)
useVIX3M = input.bool(true, "Use VIX vs VIX3M (steadier)")
smoothLen = input.int(5, "Smoothing length", minval=1)
// Dashboard Settings
dashPos = input.string("Top Right", "Dashboard Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
dashSize = input.string("Small", "Dashboard Text Size", options=["Tiny", "Small", "Normal", "Large"])
// VIX Box Settings
vixBoxPos = input.string("Top Left", "VIX Box Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
vixBoxSize = input.string("Normal", "VIX Box Size", options=["Tiny", "Small", "Normal", "Large"])
// Legend Settings
legendPos = input.string("Bottom Left", "Legend Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
legendSize = input.string("Tiny", "Legend Text Size", options=["Tiny", "Small", "Normal", "Large"])
// Arrow Settings
showArrows = input.bool(true, "Show Action Arrows")
showLegend = input.bool(true, "Show Arrow Legend")
// Data
vix = request.security("VIX", timeframe.period, close)
vix3m = request.security("VIX3M", timeframe.period, close)
vx1 = request.security("VX1!", timeframe.period, close)
vx2 = request.security("VX2!", timeframe.period, close)
// Percentage
rawPct = useVIX3M ? (vix3m / vix - 1) * 100 : (vx2 / vx1 - 1) * 100
pct = ta.ema(rawPct, smoothLen)
// Regimes
tsRegime = pct >= strongContango ? "Strong Contango" : pct > 0 ? "Mild Contango" : pct > -1.5 ? "Flat" : "Backwardation"
vixRegime = vix < lowVix ? "Low" : vix < elevatedVix ? "Normal" : vix < highVix ? "Elevated" : "High"
// Combined bias (still used for arrows, colors, and action guidance)
bias = pct >= strongContango and vix < elevatedVix ? "Best Short Vol" : pct > 0 and vix < highVix ? "Short Vol OK" : pct > 0 and vix >= highVix ? "Cautious Short Vol" : pct <= 0 and vix < elevatedVix ? "Neutral / Reduce" : "Avoid Short Vol"
// Color based on term structure regime
regimeColor = pct >= strongContango ? color.lime : pct > 0 ? color.green : pct > -1.5 ? color.orange : color.red
// Plot
plot(pct, "Term Structure %", color=pct >= 0 ? color.green : color.red, linewidth=2)
plot(rawPct, "Raw %", color=color.new(color.gray, 70), linewidth=1)
// Scale levels
hline(0, "0", color=color.gray, linestyle=hline.style_solid)
hline(3, "+3", color=color.new(color.green, 50), linestyle=hline.style_dotted)
hline(6, "+6", color=color.new(color.green, 70), linestyle=hline.style_dotted)
hline(-1.5, "-1.5", color=color.new(color.orange, 50), linestyle=hline.style_dotted)
hline(-3, "-3", color=color.new(color.red, 60), linestyle=hline.style_dotted)
// Background
bgcolor(color.new(regimeColor, 88))
// Action Arrows (still driven by combined bias)
plotshape(showArrows and bias == "Best Short Vol", title="Best Short Vol", style=shape.arrowup, location=location.bottom, color=color.lime, size=size.small)
plotshape(showArrows and bias == "Short Vol OK", title="Short Vol OK", style=shape.arrowup, location=location.bottom, color=color.green, size=size.small)
plotshape(showArrows and bias == "Cautious Short Vol", title="Cautious", style=shape.circle, location=location.bottom, color=color.yellow, size=size.small)
plotshape(showArrows and bias == "Neutral / Reduce", title="Neutral", style=shape.circle, location=location.bottom, color=color.orange, size=size.small)
plotshape(showArrows and bias == "Avoid Short Vol", title="Avoid", style=shape.arrowdown, location=location.bottom, color=color.red, size=size.small)
// Helpers
getPos(posStr) =>
posStr == "Top Left" ? position.top_left : posStr == "Top Center" ? position.top_center : posStr == "Top Right" ? position.top_right : posStr == "Middle Left" ? position.middle_left : posStr == "Middle Center" ? position.middle_center : posStr == "Middle Right" ? position.middle_right : posStr == "Bottom Left" ? position.bottom_left : posStr == "Bottom Center" ? position.bottom_center : position.bottom_right
getSize(sizeStr) =>
sizeStr == "Tiny" ? size.tiny : sizeStr == "Small" ? size.small : sizeStr == "Normal" ? size.normal : size.large
// Dashboard
dashPosition = getPos(dashPos)
dashTextSize = getSize(dashSize)
var table dash = table.new(dashPosition, 2, 5, bgcolor=color.new(color.black, 15), border_width=1)
if barstate.islast
table.cell(dash, 0, 0, "Regime", text_color=color.white, text_size=dashTextSize)
table.cell(dash, 1, 0, tsRegime, text_color=regimeColor, text_size=dashTextSize)
table.cell(dash, 0, 1, "Term Structure", text_color=color.gray, text_size=dashTextSize)
table.cell(dash, 1, 1, str.tostring(pct, "#.##") + "%", text_color=color.white, text_size=dashTextSize)
table.cell(dash, 0, 2, "Source", text_color=color.gray, text_size=dashTextSize)
table.cell(dash, 1, 2, useVIX3M ? "VIX / VIX3M" : "VX2! / VX1!", text_color=color.white, text_size=dashTextSize)
table.cell(dash, 0, 3, "Options", text_color=color.gray, text_size=dashTextSize)
table.cell(dash, 1, 3, bias == "Best Short Vol" ? "Aggressive short premium" : bias == "Short Vol OK" ? "Normal short premium" : bias == "Cautious Short Vol" ? "Small defined-risk only" : bias == "Neutral / Reduce" ? "Reduce / pause shorts" : "Avoid short vol", text_color=color.white, text_size=dashTextSize)
table.cell(dash, 0, 4, "Equities", text_color=color.gray, text_size=dashTextSize)
table.cell(dash, 1, 4, bias == "Best Short Vol" or bias == "Short Vol OK" ? "Normal risk" : bias == "Cautious Short Vol" or bias == "Neutral / Reduce" ? "Reduced risk" : "Defensive", text_color=color.white, text_size=dashTextSize)
// VIX Box
vixPosition = getPos(vixBoxPos)
vixTextSize = getSize(vixBoxSize)
var table vixBox = table.new(vixPosition, 1, 2, bgcolor=color.new(color.black, 10), border_width=2, border_color=color.gray)
if barstate.islast
table.cell(vixBox, 0, 0, "VIX", text_color=color.gray, text_size=size.tiny)
table.cell(vixBox, 0, 1, str.tostring(vix, "#.##"), text_color=vix >= highVix ? color.red : vix >= elevatedVix ? color.orange : color.lime, text_size=vixTextSize)
// Legend
legendPosition = getPos(legendPos)
legendTextSize = getSize(legendSize)
var table legend = table.new(legendPosition, 2, 6, bgcolor=color.new(color.black, 10), border_width=1)
if barstate.islast and showLegend
table.cell(legend, 0, 0, "Arrow", text_color=color.gray, text_size=legendTextSize)
table.cell(legend, 1, 0, "Meaning", text_color=color.gray, text_size=legendTextSize)
table.cell(legend, 0, 1, "↑ Lime", text_color=color.lime, text_size=legendTextSize)
table.cell(legend, 1, 1, "Aggressive short vol / Normal equity risk", text_color=color.white, text_size=legendTextSize)
table.cell(legend, 0, 2, "↑ Green", text_color=color.green, text_size=legendTextSize)
table.cell(legend, 1, 2, "Normal short vol / Normal equity risk", text_color=color.white, text_size=legendTextSize)
table.cell(legend, 0, 3, "● Yellow", text_color=color.yellow, text_size=legendTextSize)
table.cell(legend, 1, 3, "Small defined-risk shorts / Reduced equity risk", text_color=color.white, text_size=legendTextSize)
table.cell(legend, 0, 4, "● Orange", text_color=color.orange, text_size=legendTextSize)
table.cell(legend, 1, 4, "Reduce or pause shorts / Reduced equity risk", text_color=color.white, text_size=legendTextSize)
table.cell(legend, 0, 5, "↓ Red", text_color=color.red, text_size=legendTextSize)
table.cell(legend, 1, 5, "Avoid short vol / Defensive equities", text_color=color.white, text_size=legendTextSize)
// Alerts
alertcondition(bias == "Best Short Vol", "Best Short Vol", "VIX: Best Short Vol environment")
alertcondition(bias == "Avoid Short Vol", "Avoid Short Vol", "VIX: Avoid Short Vol")