Celestial Mean Reversion Envelopes [Pineify]

Pineify · study · 94 行 · 点赞 410 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
 
//
// Celestial Mean Reversion Envelopes
// ------------------------------------
// Uses an adaptive moving average (inspired by TRAMA) as the central baseline,
// then wraps it in standard deviation envelopes to mark statistically stretched
// price zones. Signals fire when price crosses back inside the outer band —
// the crossunder/crossover acts as the "snap back" confirmation trigger.
//
// Adaptive mean states:
//   1. Trending — new highest highs or lowest lows appear; the AMA tracks price closely
//   2. Ranging  — no new extremes; smoothing factor drops near zero, AMA freezes
//   3. Signal   — price pierces the band and immediately reverses back inside
//
 
//@version=6
indicator("Celestial Mean Reversion Envelopes [Pineify]", overlay=true, max_labels_count=500)
 
//---------------------------------------------------------------------------------------------------------------------}
// INPUT SETTINGS
//---------------------------------------------------------------------------------------------------------------------{
grp_core  = "Core Settings"
grp_style = "Aesthetics & Colors"
 
length = input.int(99, "Adaptive Mean Length", minval=2, group=grp_core,
     tooltip="Lookback window for highest/lowest tracking and the SMA smoothing factor. Higher values make the mean slower and the envelopes wider; lower values increase sensitivity but introduce more whipsaw signals.")
mult = input.float(2.5, "Envelope Multiplier", minval=0.5, step=0.5, group=grp_core,
     tooltip="Scales the standard deviation band width. 2.0–2.5 captures most normal price swings; raise it on volatile instruments to reduce noise.")
src = input.source(close, "Source", group=grp_core,
     tooltip="Price field fed into the adaptive mean and standard deviation calculations.")
 
bullCss   = input.color(#089981, "Bullish Reversion (Buy)",  inline="css1", group=grp_style)
bearCss   = input.color(#f23645, "Bearish Reversion (Sell)", inline="css1", group=grp_style)
meanCss   = input.color(#2962ff, "Mean Baseline",            inline="css2", group=grp_style)
showFills = input.bool(true, "Show Gradient Fills",          inline="css2", group=grp_style,
     tooltip="Toggles the translucent overbought/oversold shading between the mean and each outer band.")
 
//---------------------------------------------------------------------------------------------------------------------}
// CALCULATIONS
//---------------------------------------------------------------------------------------------------------------------{
// Track when a new highest high or lowest low is set.
// sign() + max() collapses each bar to 1 (new extreme) or 0 (no new extreme).
hh = math.max(math.sign(ta.change(ta.highest(length))), 0)
ll = math.max(math.sign(ta.change(ta.lowest(length)) * -1), 0)
 
// trendSignal is 1 on bars where price reaches a new extreme in either direction.
// SMA over `length` bars gives the fraction of recent bars with new extremes.
// Squaring creates a nonlinear response: the coefficient drops sharply in ranges
// and rises steeply during sustained trends — adapting mean responsiveness automatically.
// [Source: TRAMA by e2e4mfck]
trendSignal = math.max(hh, ll)
tc = math.pow(ta.sma(trendSignal, length), 2)
 
// Adaptive moving average: only moves when tc is high (trending market).
// In flat markets tc ≈ 0, so ama barely updates; it drifts toward price only
// when new price extremes are consistently being made.
var float ama = na
ama := nz(ama + tc * (src - nz(ama, src)), src)
 
// Standard deviation of source gives a statistically grounded band width.
// Multiplying by `mult` lets traders calibrate how far price must stretch
// before a reversion signal is considered meaningful.
dev   = ta.stdev(src, length) * mult
upper = ama + dev
lower = ama - dev
 
//---------------------------------------------------------------------------------------------------------------------}
// SIGNALS
//---------------------------------------------------------------------------------------------------------------------{
// Crossunder below the lower band: price broke through support but the close
// snapped back inside, suggesting the extension was rejected.
buySignal  = ta.crossunder(src, lower)
 
// Crossover above the upper band: price broke through resistance but
// pulled back inside, suggesting the breakout failed and sellers absorbed supply.
sellSignal = ta.crossover(src, upper)
 
//---------------------------------------------------------------------------------------------------------------------}
// PLOTTING & AESTHETICS
//---------------------------------------------------------------------------------------------------------------------{
p_upper = plot(upper, "Upper Band",      color.new(bearCss, 30), 2)
p_mean  = plot(ama,   "Celestial Mean",  color.new(meanCss, 0),  2)
p_lower = plot(lower, "Lower Band",      color.new(bullCss, 30), 2)
 
fill(p_upper, p_mean, showFills ? color.new(bearCss, 85) : na, title="Overbought Area")
fill(p_mean, p_lower, showFills ? color.new(bullCss, 85) : na, title="Oversold Area")
 
plotshape(buySignal  ? low  : na, "Reversion Buy",  shape.labelup,   location.belowbar, color.new(bullCss, 0), 0, text="▲\nBUY",  textcolor=color.white, size=size.small)
plotshape(sellSignal ? high : na, "Reversion Sell", shape.labeldown, location.abovebar, color.new(bearCss, 0), 0, text="SELL\n▼", textcolor=color.white, size=size.small)
 
alertcondition(buySignal,  "Buy Alert",  "Price stretched below the lower envelope. Potential Mean Reversion UP.")
alertcondition(sellSignal, "Sell Alert", "Price stretched above the upper envelope. Potential Mean Reversion DOWN.")

← 返回列表