Celestial Mean Reversion Envelopes [Pineify]Celestial Mean Reversion Envelopes
This indicator identifies mean reversion opportunities by wrapping an adaptive moving average in standard deviation envelopes and signaling when price snaps back inside after piercing a band. Rather than using a fixed-period moving average as the baseline, the central line adapts its speed based on how frequently price is setting new highest highs or lowest lows — it tracks price quickly in trending conditions and almost freezes in ranges, so the bands shift organically with market character.
Key Features
Adaptive mean that responds to trend intensity rather than time alone — sluggish during consolidation, responsive during strong moves
Standard deviation envelopes calibrated to actual recent volatility, not fixed ATR multiples
Buy and sell signals generated on band crossunders/crossovers, confirming the reversal rather than anticipating it
Translucent overbought/oversold shading between the mean and each band for quick visual context
Built-in alerts for both reversion directions
How It Works
The calculation runs in two stages: first building the adaptive mean, then constructing the envelopes around it.
Extreme tracking — On each bar, the indicator checks whether a new highest high or lowest low has formed over the lookback window. Bars where a fresh extreme appears are marked with a value of 1; all other bars get 0. The SMA of these binary values over the same window gives the fraction of recent bars that produced a new extreme.
Squaring the fraction — Raising that fraction to the power of 2 produces a nonlinear smoothing coefficient. When trends are strong and new extremes appear on most bars, the coefficient approaches 1 and the adaptive mean tracks price closely. In a choppy range where few new extremes form, the coefficient collapses near zero and the mean barely moves. This technique is inspired by TRAMA (Trend Regularity Adaptive Moving Average) by e2e4mfck.
Adaptive mean update — Each bar the mean nudges toward the source price by the amount determined by the coefficient. The result is an average that effectively switches between "responsive" and "parked" behavior depending on what the market is doing.
Standard deviation envelopes — The upper and lower bands are placed at ±(StdDev × multiplier) from the adaptive mean, where StdDev is computed over the same lookback period. This makes the band width proportional to recent volatility: wider when price has been swinging, tighter during quiet periods.
Signal generation — A buy signal fires when source crossesunder the lower band (price dipped below, then closed back above it). A sell signal fires on a crossover of the upper band. The crossunder/crossover logic requires price to actually breach and then retrace — a bar that merely touches the band without closing through it does not trigger.
How the Components Work Together
The adaptive mean solves a problem that conventional envelope indicators ignore: when a market trends hard, a static EMA or SMA falls behind, making the upper band a poor reference for "too far, too fast." Because the adaptive mean accelerates during trends, the envelopes stay anchored to current price levels rather than lagging. This means the bands are more likely to represent genuine statistical extremes rather than just momentum riding.
The standard deviation layer adds a second dimension. Instead of a fixed pip or percentage offset, the band width expands when the market is volatile and contracts when it is calm — naturally suppressing signals during low-volatility compression and allowing wider moves during active sessions before flagging exhaustion.
Together these two layers create a filter that roughly says: "price reached a statistically unusual distance from where the trend currently sits, then pulled back." That combination reduces fakeout signals compared to using static bands on a lagging baseline.
Trading Ideas and Insights
On higher timeframes (daily, 4H), buy signals at the lower band that coincide with a key support level or volume spike may offer higher-confidence entries. Look for the adaptive mean to be flattening — it suggests the trend is pausing rather than reversing.
In intraday trading, signals that appear after a sharp impulsive leg tend to perform better than signals generated inside a choppy range. The adaptive mean will often be steeply sloped after an impulse, indicating the signal is against the micro-trend — exercise more caution and use tighter risk.
When price oscillates between the bands repeatedly without triggering signals, the market is likely in a low-volatility squeeze. A breakout attempt that immediately pulls back (triggering a sell or buy signal) at the edge of that range can mark the failed breakout early.
The gradient fill zones serve as a running reference for where price stands relative to the mean. Price persistently in the upper (red) fill with a rising adaptive mean suggests a strong trend; consider fading only when price crosses back into the neutral zone.
Past performance of any signal pattern does not guarantee future results. Always combine signals with broader context — structure, volume, and higher-timeframe bias. These signals indicate potential exhaustion; they do not predict reversal magnitude.
Unique Aspects
The squaring of the trend-regularity fraction is the core differentiator. Most adaptive averages use linear coefficients; squaring creates a much sharper distinction between trending and ranging states, so the mean spends more time "frozen" during ranges and snaps to price quickly when momentum genuinely kicks in.
Signals require price to close back inside the band, not just touch it — this one-bar confirmation step reduces noise from wicks that briefly pierce a band and immediately reverse without a real close-to-close move.
Band width is purely standard-deviation based rather than ATR-derived, which means the scaling responds to the actual statistical dispersion of the source series rather than the high-low range. On instruments with many gaps this can produce meaningfully different widths than ATR bands.
How to Use
Add the indicator to any chart. It overlays directly on the price pane.
The blue line is the adaptive mean. When it is rising steeply the market is in an upward trending mode; when flat or slightly sloped, it is ranging.
The red-shaded zone above the mean is the overbought area; the green-shaded zone below is the oversold area. Price spending extended time in one zone suggests momentum, not necessarily exhaustion.
A green BUY label below a bar means price closed back above the lower band after briefly breaking it — potential reversion entry. A red SELL label above a bar means the opposite.
To set alerts, use the "Buy Alert" or "Sell Alert" conditions from the indicator's alert panel (Once Per Bar Close recommended to avoid premature triggers on intrabar wicks).
Customization
Adaptive Mean Length (default: 99) — Controls both the highest/lowest lookback and the SMA averaging window for the smoothing coefficient. Higher values slow the mean considerably and widen bands; lower values increase reactivity but also produce more frequent and less reliable signals.
Envelope Multiplier (default: 2.5) — Scales the standard deviation distance. 2.0 suits instruments with tighter typical ranges; raise to 3.0+ on highly volatile assets to avoid constant band touches that don't represent genuine extremes.
Source (default: close) — Change to hl2 or hlc3 to incorporate high and low into the baseline; close is typically sufficient for most reversion setups.
Color inputs — Adjust bullish/bearish/mean colors and toggle the gradient fill on or off depending on visual preference.
Conclusion
Celestial Mean Reversion Envelopes pairs an adaptive mean that adjusts its responsiveness to trend regularity with volatility-scaled deviation bands, targeting the specific moment when a stretched move closes back inside its statistical boundary. The approach is best suited to traders who wait for confirmation — the crossunder/crossover trigger ensures you're acting on a completed reversal bar, not an open wick. As with any mean-reversion tool, it works best when context confirms the extension is exhaustion rather than breakout continuation.
Indicador Pine Script®






















