Volatility Regime and Expansion

EVLabs · study · 120 行 · 点赞 29 · TradingView 原页

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

Pine Script

//@version=6
indicator("Volatility Regime and Expansion", overlay=false)
 
// ── ATR settings ──────────────────────────────────────────────────────────────
groupATR = "ATR Settings"
 
atrLength = input.int(14, "ATR Length", minval=1, group=groupATR)
percentileLength = input.int(100, "Percentile Lookback", minval=20, group=groupATR)
 
// ── Bollinger Band settings ───────────────────────────────────────────────────
groupBB = "Bollinger Band Settings"
 
bbLength = input.int(20, "Bollinger Length", minval=1, group=groupBB)
bbMultiplier = input.float(2.0, "Standard Deviation Multiplier", minval=0.1, step=0.1, group=groupBB)
 
// ── Regime settings ───────────────────────────────────────────────────────────
groupRegime = "Volatility Regime"
 
lowVolThreshold = input.float(25, "Low Volatility Threshold", minval=0, maxval=100, step=1, group=groupRegime)
highVolThreshold = input.float(75, "High Volatility Threshold", minval=0, maxval=100, step=1, group=groupRegime)
expansionThreshold = input.float(80, "Expansion Alert Threshold", minval=0, maxval=100, step=1, group=groupRegime)
confirmedBarsOnly = input.bool(true, "Signals Only After Bar Closes", group=groupRegime)
 
// ── Display settings ──────────────────────────────────────────────────────────
groupDisplay = "Display"
 
showRegimeBackground = input.bool(true, "Color Background By Regime", group=groupDisplay)
showStatsTable = input.bool(true, "Show Statistics Table", group=groupDisplay)
 
// ── ATR percentile ────────────────────────────────────────────────────────────
atrValue = ta.atr(atrLength)
atrPercent = close != 0 ? atrValue / close * 100 : na
atrPercentile = ta.percentrank(atrPercent, percentileLength)
 
// ── Bollinger Band width percentile ───────────────────────────────────────────
bbBasis = ta.sma(close, bbLength)
bbDeviation = ta.stdev(close, bbLength) * bbMultiplier
bbUpper = bbBasis + bbDeviation
bbLower = bbBasis - bbDeviation
 
bbWidthPercent = bbBasis != 0 ? (bbUpper - bbLower) / bbBasis * 100 : na
bbWidthPercentile = ta.percentrank(bbWidthPercent, percentileLength)
 
// ── Combined volatility score ─────────────────────────────────────────────────
volatilityScore = na(atrPercentile) or na(bbWidthPercentile) ? na : (atrPercentile + bbWidthPercentile) / 2
 
isLowVolatility = volatilityScore <= lowVolThreshold
isHighVolatility = volatilityScore >= highVolThreshold
 
regimeName = na(volatilityScore) ? "WARMING UP" :
     isHighVolatility ? "HIGH" :
     isLowVolatility ? "LOW" :
     "NORMAL"
 
regimeColor = na(volatilityScore) ? color.gray :
     isHighVolatility ? color.red :
     isLowVolatility ? color.aqua :
     color.orange
 
// ── Volatility events ─────────────────────────────────────────────────────────
barIsValid = not confirmedBarsOnly or barstate.isconfirmed
 
volatilityExpansion = ta.crossover(volatilityScore, expansionThreshold) and barIsValid
volatilityCompression = ta.crossunder(volatilityScore, lowVolThreshold) and barIsValid
 
// ── Plots ─────────────────────────────────────────────────────────────────────
plot(volatilityScore, "Combined Volatility Percentile", color=regimeColor, linewidth=2)
 
hline(lowVolThreshold, "Low Volatility Threshold", color=color.aqua, linestyle=hline.style_dotted)
hline(highVolThreshold, "High Volatility Threshold", color=color.red, linestyle=hline.style_dotted)
hline(expansionThreshold, "Expansion Threshold", color=color.orange, linestyle=hline.style_dashed)
hline(50, "Midpoint", color=color.new(color.gray, 70))
 
bgcolor(showRegimeBackground ? color.new(regimeColor, 92) : na)
 
plotshape(
     volatilityExpansion,
     title="Volatility Expansion",
     style=shape.triangleup,
     location=location.bottom,
     color=color.lime,
     text="EXPANSION",
     textcolor=color.lime,
     size=size.tiny)
 
plotshape(
     volatilityCompression,
     title="Volatility Compression",
     style=shape.triangledown,
     location=location.top,
     color=color.aqua,
     text="COMPRESSION",
     textcolor=color.aqua,
     size=size.tiny)
 
// ── Statistics table ──────────────────────────────────────────────────────────
var table statsTable = table.new(position.top_right, 2, 5, border_width=1)
 
if barstate.islast
    if showStatsTable
        table.cell(statsTable, 0, 0, "Volatility Metric", text_color=color.white, bgcolor=color.black)
        table.cell(statsTable, 1, 0, "Current Value", text_color=color.white, bgcolor=color.black)
 
        table.cell(statsTable, 0, 1, "Regime", text_color=color.white)
        table.cell(statsTable, 1, 1, regimeName, text_color=regimeColor)
 
        table.cell(statsTable, 0, 2, "ATR", text_color=color.white)
        table.cell(statsTable, 1, 2, na(atrPercent) ? "—" : str.tostring(atrPercent, "#.##") + "%", text_color=color.white)
 
        table.cell(statsTable, 0, 3, "ATR Percentile", text_color=color.white)
        table.cell(statsTable, 1, 3, na(atrPercentile) ? "—" : str.tostring(atrPercentile, "#.##"), text_color=color.white)
 
        table.cell(statsTable, 0, 4, "BB Width Percentile", text_color=color.white)
        table.cell(statsTable, 1, 4, na(bbWidthPercentile) ? "—" : str.tostring(bbWidthPercentile, "#.##"), text_color=color.white)
    else
        table.clear(statsTable, 0, 0, 1, 4)
 
// ── Alerts ────────────────────────────────────────────────────────────────────
alertcondition(volatilityExpansion, title="Volatility Expansion", message="Volatility has crossed above the expansion threshold.")
alertcondition(volatilityCompression, title="Volatility Compression", message="Volatility has entered the low-volatility zone.")

← 返回列表