OPEN-SOURCE SCRIPT

Adaptive MA constructor [lastguru]

Updated
Adaptive Moving Averages are nothing new, however most of them use EMA as their MA of choice once the preferred smoothing length is determined. I have decided to make an experiment and separate length generation from smoothing, offering multiple alternatives to be combined. Some of the combinations are widely known, some are not. This indicator is based on my previously published public libraries and also serve as a usage demonstration for them. I will try to expand the collection (suggestions are welcome), however it is not meant as an encyclopaedic resource, so you are encouraged to experiment yourself: by looking on the source code of this indicator, I am sure you will see how trivial it is to use the provided libraries and expand them with your own ideas and combinations. I give no recommendation on what settings to use, but if you find some useful setting, combination or application ideas (or bugs in my code), I would be happy to read about them in the comments section.

The indicator works in three stages: Prefiltering, Length Adaptation and Moving Averages.

Prefiltering is a fast smoothing to get rid of high-frequency (2, 3 or 4 bar) noise.

Adaptation algorithms are roughly subdivided in two categories: classic Length Adaptations and Cycle Estimators (they are also implemented in separate libraries), all are selected in Adaptation dropdown. Length Adaptation used in the Adaptive Moving Averages and the Adaptive Oscillators try to follow price movements and accelerate/decelerate accordingly (usually quite rapidly with a huge range). Cycle Estimators, on the other hand, try to measure the cycle period of the current market, which does not reflect price movement or the rate of change (the rate of change may also differ depending on the cycle phase, but the cycle period itself usually changes slowly).
  • Chande (Price) - based on Chande's Dynamic Momentum Index (CDMI or DYMOI), which is dynamic RSI with this length
  • Chande (Volume) - a variant of Chande's algorithm, where volume is used instead of price
  • VIDYA - based on VIDYA algorithm. The period oscillates from the Lower Bound up (slow)
  • VIDYA-RS - based on Vitali Apirine's modification of VIDYA algorithm (he calls it Relative Strength Moving Average). The period oscillates from the Upper Bound down (fast)
  • Kaufman Efficiency Scaling - based on Efficiency Ratio calculation originally used in KAMA
  • Deviation Scaling - based on DSSS by John F. Ehlers
  • Median Average - based on Median Average Adaptive Filter by John F. Ehlers
  • Fractal Adaptation - based on FRAMA by John F. Ehlers
  • MESA MAMA Alpha - based on MESA Adaptive Moving Average by John F. Ehlers
  • MESA MAMA Cycle - based on MESA Adaptive Moving Average by John F. Ehlers, but unlike Alpha calculation, this adaptation estimates cycle period
  • Pearson Autocorrelation* - based on Pearson Autocorrelation Periodogram by John F. Ehlers
  • DFT Cycle* - based on Discrete Fourier Transform Spectrum estimator by John F. Ehlers
  • Phase Accumulation* - based on Dominant Cycle from Phase Accumulation by John F. Ehlers


Length Adaptation usually take two parameters: Bound From (lower bound) and To (upper bound). These are the limits for Adaptation values. Note that the Cycle Estimators marked with asterisks(*) are very computationally intensive, so the bounds should not be set much higher than 50, otherwise you may receive a timeout error (also, it does not seem to be a useful thing to do, but you may correct me if I'm wrong).

The Cycle Estimators marked with asterisks(*) also have 3 checkboxes: HP (Highpass Filter), SS (Super Smoother) and HW (Hann Window). These enable or disable their internal prefilters, which are recommended by their author - John F. Ehlers. I do not know, which combination works best, so you can experiment.

Chande's Adaptations also have 3 additional parameters: SD Length (lookback length of Standard deviation), Smooth (smoothing length of Standard deviation) and Power (exponent of the length adaptation - lower is smaller variation). These are internal tweaks for the calculation.

Length Adaptaton section offer you a choice of Moving Average algorithms. Most of the Adaptations are originally used with EMA, so this is a good starting point for exploration.
  • SMA - Simple Moving Average
  • RMA - Running Moving Average
  • EMA - Exponential Moving Average
  • HMA - Hull Moving Average
  • VWMA - Volume Weighted Moving Average
  • 2-pole Super Smoother - 2-pole Super Smoother by John F. Ehlers
  • 3-pole Super Smoother - 3-pole Super Smoother by John F. Ehlers
  • Filt11 -a variant of 2-pole Super Smoother with error averaging for zero-lag response by John F. Ehlers
  • Triangle Window - Triangle Window Filter by John F. Ehlers
  • Hamming Window - Hamming Window Filter by John F. Ehlers
  • Hann Window - Hann Window Filter by John F. Ehlers
  • Lowpass - removes cyclic components shorter than length (Price - Highpass)
  • DSSS - Derivation Scaled Super Smoother by John F. Ehlers


There are two Moving Averages that are drown on the chart, so length for both needs to be selected. If no Adaptation is selected (None option), you can set Fast Length and Slow Length directly. If an Adaptation is selected, then Cycle multiplier can be selected for Fast and Slow MA.

More information on the algorithms is given in the code for the libraries used. I am also very grateful to other TradingView community members (they are also mentioned in the library code) without whom this script would not have been possible.
Release Notes
Added Delay Fast and Slow options to shift the Moving Average by a number of bars. For example, now you can intersect an MA with itself (same length) in the past. If the specified value is lower than 1, it is interpreted as a multiplier of the MA length.
Release Notes
library update: fixed vidyaRS calculation
Release Notes
In case Length Adaptation is enabled, but Slow or Fast Cycle is 0, use static Length for that Moving Average instead
Release Notes
New MA added: Relative Strength Super Smoother based on Vitali Apirine's RS EMA, but with Super Smoother. Looks like an excellent dynamic MA, deserving its own place in the collection!
autocorrelationDFTdominantcycleehlershannMESA Adaptive Moving Average (MAMA)Moving AveragessupersmootherWave Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Tips in TradingView Coins are appreciated
Also on:

Disclaimer