Market Weather: Volatility Forecast

a.b · study · 65 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
// Market Weather: Volatility Forecast
// Open-source under the Mozilla Public License 2.0.
// Volatility comes in clusters: big candles are followed by big candles, quiet by quiet.
// This script measures that (fast ATR vs slow ATR) and shows it as weather: a colored ribbon along the bottom
// and a "breathing" channel that widens in a storm and tightens when calm. It forecasts SIZE of moves, not direction.
// All values are computed on closed bars only. Nothing repaints.
 
indicator("Market Weather: Volatility Forecast", shorttitle = "Market Weather", overlay = true, max_labels_count = 5)
 
// ── Inputs ──────────────────────────────────────────────────────────────
GRP = "Display"
showChan = input.bool(true, "Show expected-range channel", group = GRP, tooltip = "The channel is the typical high-to-low range of the next 10 bars, centered on a smooth average of price.")
showRib  = input.bool(true, "Show weather ribbon (bottom)", group = GRP)
showLbl  = input.bool(true, "Show forecast label", group = GRP)
 
// ── Weather engine (closed bars only) ───────────────────────────────────
float atrF  = ta.atr(5)
float atrS  = ta.atr(50)
float emaM  = ta.ema(close, 10)
float ratio = atrS > 0 ? atrF / atrS : na
 
var float stRatio = na
var float stAtr   = na
var float stMid   = na
if barstate.isconfirmed and not na(ratio)
    stRatio := ratio
    stAtr   := atrS
    stMid   := emaM
 
// 0 = warming up, 1 = calm, 2 = normal, 3 = building, 4 = storm
int lvl = na(stRatio) ? 0 : stRatio < 0.8 ? 1 : stRatio < 1.0 ? 2 : stRatio < 1.25 ? 3 : 4
 
// Typical high-to-low range of the next 10 bars, in slow-ATR units (fitted on 5 real datasets, see backtest notes).
float rClamp = math.min(math.max(nz(stRatio, 1.0), 0.6), 1.8)
float expR   = 1.83 + 1.73 * rClamp
float halfW  = 0.5 * expR * stAtr
float upper  = stMid + halfW
float lower  = stMid - halfW
 
// ── Theme ───────────────────────────────────────────────────────────────
color calmC  = #4FC3F7
color normC  = #8A94A8
color buildC = #FFB300
color stormC = #FF3D71
color wCol   = lvl == 1 ? calmC : lvl == 3 ? buildC : lvl == 4 ? stormC : normC
 
// ── Drawing ─────────────────────────────────────────────────────────────
pU = plot(showChan and lvl > 0 ? upper : na, "Expected range: top", color = color.new(wCol, 25), linewidth = 1, style = plot.style_linebr)
pL = plot(showChan and lvl > 0 ? lower : na, "Expected range: bottom", color = color.new(wCol, 25), linewidth = 1, style = plot.style_linebr)
fill(pU, pL, color = color.new(wCol, 90), title = "Expected range")
 
plotshape(showRib and lvl > 0, "Weather ribbon", style = shape.square, location = location.bottom, color = wCol, size = size.tiny)
 
// Forecast label: pinned just above the ribbon at the bottom of the chart (does not cover price).
bool lblOn = barstate.islast and showLbl
plotshape(lblOn and lvl == 4, "Label: storm", style = shape.labeldown, location = location.bottom, color = color.new(stormC, 10), textcolor = #0B0E14, text = "STORM: BIG MOVES LIKELY\nWider stops, smaller size", size = size.normal, offset = -3)
plotshape(lblOn and lvl == 3, "Label: building", style = shape.labeldown, location = location.bottom, color = color.new(buildC, 10), textcolor = #0B0E14, text = "BUILDING: MOVES GETTING BIGGER\nWider stops, smaller size", size = size.normal, offset = -3)
plotshape(lblOn and lvl == 2, "Label: normal", style = shape.labeldown, location = location.bottom, color = color.new(normC, 10), textcolor = #0B0E14, text = "NORMAL WEATHER\nPlan stops as usual", size = size.normal, offset = -3)
plotshape(lblOn and lvl == 1, "Label: calm", style = shape.labeldown, location = location.bottom, color = color.new(calmC, 10), textcolor = #0B0E14, text = "CALM: SMALL MOVES FOR NOW\nCalm never lasts. Stay ready", size = size.normal, offset = -3)
 
// ── Alerts (set to "Once Per Bar Close") ────────────────────────────────
alertcondition(lvl == 4 and lvl[1] != 4, "Storm: volatility is high", "Volatility just turned stormy on {{ticker}} {{interval}}. Expect big candles: wider stops, smaller size.")
alertcondition(lvl == 1 and lvl[1] != 1, "Calm: volatility is low", "Volatility just turned calm on {{ticker}} {{interval}}. Quiet markets often wake up fast.")
alertcondition(lvl >= 3 and lvl[1] < 3, "Storm building", "Volatility is building on {{ticker}} {{interval}}.")

← 返回列表