本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Sigma Heatmap — colours candles by how far price (ohlc4) is stretched from its
// rolling mean, measured in standard deviations.
// The window adapts to volatility: `Base length` bars in a normal ATR regime,
// shorter when ATR runs above its long-term average, longer when it runs below.
// σ is always computed from 24h (ETH) back-adjusted data, so an RTH chart shows
// the same values, just fewer bars.
indicator("Sigma Stretch", shorttitle = "σ Stretch", overlay = true, max_bars_back = 500)
// ── Inputs ──────────────────────────────────────────────────────────────────
sig2 = input.float(2.0, "Sigma 2 level", minval = 0.5, step = 0.1, group = "Levels")
sig3 = input.float(3.0, "Sigma 3 level", minval = 0.5, step = 0.1, group = "Levels")
colNeutral = input.color(color.rgb(51, 51, 51), "Neutral", group = "Colors")
colSig2 = input.color(color.rgb(110, 110, 110), "Sigma 2", group = "Colors")
colSig3 = input.color(color.rgb(255, 255, 255), "Sigma 3", group = "Colors")
baseLen = input.int(50, "Base length", minval = 5, group = "Adaptive window")
minLen = input.int(15, "Min length", minval = 2, group = "Adaptive window")
maxLen = input.int(100, "Max length", minval = 5, maxval = 500, group = "Adaptive window")
atrLen = input.int(14, "ATR length", minval = 1, group = "Adaptive window")
atrAvgLen = input.int(200, "ATR average length", minval = 2, group = "Adaptive window")
if sig3 <= sig2
runtime.error("Sigma 3 level must be above Sigma 2 level.")
if minLen > maxLen
runtime.error("Min length must not exceed Max length.")
// ── Stretch in sigmas ──────────────────────────────────────────────────────
// Until the ATR average is available the regime counts as normal (1.0).
f_sigma() =>
atrNow = ta.atr(atrLen)
regime = nz(atrNow / ta.sma(atrNow, atrAvgLen), 1.0)
winLen = int(math.max(minLen, math.min(maxLen, math.round(baseLen / regime))))
nz((ohlc4 - ta.sma(ohlc4, winLen)) / ta.stdev(ohlc4, winLen))
// Always computed on 24h (ETH), back-adjusted data, so switching the chart to
// RTH or toggling B-ADJ does not change the values.
ethTick = ticker.modify(syminfo.tickerid, session = session.extended, backadjustment = backadjustment.on)
sigma = nz(request.security(ethTick, timeframe.period, f_sigma(), lookahead = barmerge.lookahead_off))
stretch = math.abs(sigma)
// Between the levels the colour only drifts 35 % of the way towards Sigma 3,
// so a full Sigma 3 bar is a clear step up rather than the end of a smooth fade.
SIG2_DRIFT = 0.35
mixCh(a, b) => a + (b - a) * SIG2_DRIFT
colSig2Top = color.rgb(mixCh(color.r(colSig2), color.r(colSig3)), mixCh(color.g(colSig2), color.g(colSig3)), mixCh(color.b(colSig2), color.b(colSig3)))
barCol = stretch >= sig3 ? colSig3 :
stretch >= sig2 ? color.from_gradient(stretch, sig2, sig3, colSig2, colSig2Top) :
colNeutral
plotcandle(open, high, low, close, "σ Candles", color = barCol, wickcolor = barCol, bordercolor = barCol)
plot(sigma, "σ", display = display.data_window)