本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0
// © cfdtools
//
// Volatility Regime Meter (ATR Percentile)
// Ranks today's ATR against its own recent history, sorts every bar into a
// compressed, normal or expanded volatility regime, marks the bar where a
// long compression ends, and keeps simple statistics on how long
// compressions usually last and how much of the lookback each regime filled.
indicator("Volatility Regime Meter (ATR Percentile)", shorttitle = "Vol Regime", overlay = false, precision = 1)
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
GRP_CALC = "Calculation"
GRP_REG = "Regimes"
GRP_VIS = "Display"
GRP_TBL = "Table"
atrLen = input.int(14, "ATR length", minval = 1, group = GRP_CALC)
normMode = input.string("ATR % of price", "Measure", options = ["ATR % of price", "Raw ATR"], group = GRP_CALC, tooltip = "ATR % of price keeps the reading comparable when price has moved a long way. Raw ATR uses price units.")
rankLen = input.int(250, "Percentile lookback (bars)", minval = 20, maxval = 4000, group = GRP_CALC)
smoothLen = input.int(1, "Smoothing (bars)", minval = 1, maxval = 50, group = GRP_CALC, tooltip = "1 means no smoothing.")
lowTh = input.float(20.0, "Compressed below percentile", minval = 1, maxval = 49, group = GRP_REG)
highTh = input.float(80.0, "Expanded above percentile", minval = 51, maxval = 99, group = GRP_REG)
minSqueeze = input.int(5, "Minimum compressed bars before a release mark", minval = 1, group = GRP_REG)
showBg = input.bool(true, "Shade pane background by regime", group = GRP_VIS)
colorBars = input.bool(false, "Colour chart bars by regime", group = GRP_VIS)
showMarks = input.bool(true, "Mark compression releases", group = GRP_VIS)
cLow = input.color(#2962FF, "Compressed colour", group = GRP_VIS)
cMid = input.color(#787B86, "Normal colour", group = GRP_VIS)
cHigh = input.color(#FF6D00, "Expanded colour", group = GRP_VIS)
showTbl = input.bool(true, "Show table", group = GRP_TBL)
tblPos = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = GRP_TBL)
tblSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = GRP_TBL)
// ─────────────────────────────────────────────────────────────────────────────
// Calculation
// ─────────────────────────────────────────────────────────────────────────────
isPct = normMode == "ATR % of price"
atrRaw = ta.atr(atrLen)
measure = isPct ? atrRaw / close * 100.0 : atrRaw
measureS = ta.sma(measure, smoothLen)
pct = ta.percentrank(measureS, rankLen)
// -1 = warming up, 0 = compressed, 1 = normal, 2 = expanded
int regime = na(pct) ? -1 : pct < lowTh ? 0 : pct > highTh ? 2 : 1
var int runLen = 0
runLen := regime == regime[1] ? runLen + 1 : 1
prevRun = nz(runLen[1])
enteredLow = regime == 0 and regime[1] != 0
enteredHigh = regime == 2 and regime[1] != 2
leftLow = regime[1] == 0 and regime > 0
release = leftLow and prevRun >= minSqueeze
// Compression spell statistics
var int spells = 0
var int spellBarsSum = 0
var int longestSpell = 0
if leftLow
spells += 1
spellBarsSum += prevRun
longestSpell := math.max(longestSpell, prevRun)
lowShare = nz(math.sum(regime == 0 ? 1 : 0, rankLen)) / rankLen * 100.0
highShare = nz(math.sum(regime == 2 ? 1 : 0, rankLen)) / rankLen * 100.0
// ─────────────────────────────────────────────────────────────────────────────
// Plots
// ─────────────────────────────────────────────────────────────────────────────
regColor = regime == 0 ? cLow : regime == 2 ? cHigh : cMid
plot(pct, "ATR percentile", color = regColor, linewidth = 2)
hline(highTh, "Expanded level", color = color.new(cHigh, 40), linestyle = hline.style_dashed)
hline(50, "Midline", color = color.new(color.gray, 70), linestyle = hline.style_dotted)
hline(lowTh, "Compressed level", color = color.new(cLow, 40), linestyle = hline.style_dashed)
bgcolor(showBg and regime >= 0 ? color.new(regColor, 88) : na, title = "Regime background")
barcolor(colorBars and regime >= 0 ? regColor : na, title = "Regime bar colour")
plotshape(showMarks and release, "Compression release", shape.triangleup, location.bottom, cHigh, size = size.tiny)
// ─────────────────────────────────────────────────────────────────────────────
// Table
// ─────────────────────────────────────────────────────────────────────────────
tPos = switch tblPos
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
tSize = switch tblSize
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
var table tbl = table.new(tPos, 2, 7, bgcolor = color.new(#131722, 10), border_color = color.new(color.gray, 70), border_width = 1)
cellRow(r, string label, string value, color valueColor) =>
table.cell(tbl, 0, r, label, text_color = color.silver, text_size = tSize, text_halign = text.align_left)
table.cell(tbl, 1, r, value, text_color = valueColor, text_size = tSize, text_halign = text.align_right)
if showTbl and barstate.islast
regName = regime == 0 ? "Compressed" : regime == 2 ? "Expanded" : regime == 1 ? "Normal" : "Warming up"
measTxt = isPct ? str.tostring(measure, "#.###") + "%" : str.tostring(measure, format.mintick)
avgSpell = spells > 0 ? str.tostring(spellBarsSum * 1.0 / spells, "#.#") + " bars" : "n/a"
cellRow(0, "Volatility regime", regName, regColor)
cellRow(1, isPct ? "ATR % of price" : "ATR", measTxt, color.white)
cellRow(2, "Percentile", na(pct) ? "n/a" : str.tostring(pct, "#"), regColor)
cellRow(3, "Bars in this regime", str.tostring(runLen), color.white)
cellRow(4, "Time compressed / expanded", str.tostring(lowShare, "#") + "% / " + str.tostring(highShare, "#") + "%", color.white)
cellRow(5, "Average compression", avgSpell, color.white)
cellRow(6, "Longest compression", spells > 0 ? str.tostring(longestSpell) + " bars" : "n/a", color.white)
// ─────────────────────────────────────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(enteredLow, "Entered compressed regime", "Volatility Regime Meter: ATR percentile moved into the compressed regime.")
alertcondition(enteredHigh, "Entered expanded regime", "Volatility Regime Meter: ATR percentile moved into the expanded regime.")
alertcondition(release, "Compression release", "Volatility Regime Meter: a compression of the minimum length has ended.")