OPEN-SOURCE SCRIPT

Adaptive Trend Envelope [BackQuant]

505
Adaptive Trend Envelope [BackQuant]

Overview
Adaptive Trend Envelope is a volatility-aware trend-following overlay designed to stay responsive in fast markets while remaining stable during slower conditions. It builds a dynamic trend spine from two exponential moving averages and surrounds it with an adaptive envelope whose width expands and contracts based on realized return volatility. The result is a clean, self-adjusting trend structure that reacts to market conditions instead of relying on fixed parameters.

This indicator is built to answer three core questions directly on the chart:
  • Is the market trending or neutral?
  • If trending, in which direction is the dominant pressure?
  • Where is the dynamic trend boundary that price should respect?


Core trend spine
At the heart of the indicator is a blended trend spine:
  • A fast EMA captures short-term responsiveness.
  • A slow EMA captures structural direction.
  • A volatility-based blend weight dynamically shifts influence between the two.


When short-term volatility is low relative to long-term volatility, the fast EMA has more influence, keeping the trend responsive. When volatility rises, the blend shifts toward the slow EMA, reducing noise and preventing overreaction. This blended output is then smoothed again to form the final trend spine, which acts as the structural backbone of the system.

Volatility-adaptive envelope
The envelope surrounding the trend spine is not based on ATR or fixed percentages. Instead, it is derived from:
  • Log returns of price.
  • An exponentially weighted variance estimate.
  • A configurable multiplier that scales envelope width.


This creates bands that automatically widen during volatile expansions and tighten during compression. The envelope therefore reflects the true statistical behavior of price rather than an arbitrary distance.

Inner hysteresis band
Inside the main envelope, an inner band is constructed using a hysteresis fraction. This inner zone is used to stabilize regime transitions:
  • It prevents rapid flipping between bullish and bearish states.
  • It allows trends to persist unless price meaningfully invalidates them.
  • It reduces whipsaws in sideways conditions.


Trend regime logic
The indicator operates with three regime states:
  • Bullish
  • Bearish
  • Neutral


Regime changes are confirmed using a configurable number of bars outside the adaptive envelope:
  • A bullish regime is confirmed when price closes above the upper envelope for the required number of bars.
  • A bearish regime is confirmed when price closes below the lower envelope for the required number of bars.
  • A trend exits back to neutral when price reverts through the trend spine.


This structure ensures that trends are confirmed by sustained pressure rather than single-bar spikes.

Active trend line
Once a regime is active, the indicator plots a single dominant trend line:
  • In a bullish regime, the lower envelope becomes the active trend support.
  • In a bearish regime, the upper envelope becomes the active trend resistance.
  • In neutral conditions, price itself is used as a placeholder.


This creates a simple, actionable visual reference for trend-following decisions.

Directional energy visualization
The indicator uses layered fills to visualize directional pressure:
  • Bullish energy fills appear when price holds above the active trend line.
  • Bearish energy fills appear when price holds below the active trend line.
  • Opacity gradients communicate strength and persistence rather than binary states.


A subtle “rim” effect is added using ATR-based offsets to give depth and reinforce the active side of the trend without cluttering the chart.

Signals and trend starts
Discrete signals are generated only when a new trend regime begins:
  • Buy signals appear at the first confirmed transition into a bullish regime.
  • Sell signals appear at the first confirmed transition into a bearish regime.


Signals are intentionally sparse. They are designed to mark regime shifts, not every pullback or continuation, making them suitable for higher-quality trend entries rather than frequent trading.

Candle coloring
Optional candle coloring reinforces regime context:
  • Bullish regimes tint candles toward the bullish color.
  • Bearish regimes tint candles toward the bearish color.
  • Neutral states remain visually muted.


This allows the chart to communicate trend state even when the envelope itself is partially hidden or de-emphasized.

Alerts
Built-in alerts are provided for key trend events:
  • Bull trend start.
  • Bear trend start.
  • Transition from trend to neutral.
  • Price crossing the trend spine.


These alerts support hands-off trend monitoring across multiple instruments and timeframes.

How to use it for trend following

Trend identification
  • Only trade in the direction of the active regime.
  • Ignore counter-trend signals during confirmed trends.


Entry alignment
  • Use the first regime signal as a structural entry.
  • Use pullbacks toward the active trend line as continuation opportunities.


Trend management
  • As long as price respects the active envelope boundary, the trend remains valid.
  • A move back through the spine signals loss of trend structure.


Market filtering
  • Periods where the indicator remains neutral highlight non-trending environments.
  • This helps avoid forcing trades during chop or compression.


Adaptive Trend Envelope is designed to behave like a living trend structure. Instead of forcing price into static rules, it adapts to volatility, confirms direction through sustained pressure, and presents trend information in a clean, readable form that supports disciplined trend-following workflows.

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.