TradingView scripts / 40 Moving Averages

40 SLOTS · 8 AVERAGE TYPES · CHART & HIGHER TIMEFRAMES

40 Moving Averages: Multi-Timeframe TradingView Indicator

Compare moving average types, lengths and timeframes in one workspace. Identify every line with numbered labels and an optional legend instead of stacking separate indicator instances.

Open 40 Moving Averages on TradingView ↗ (paid link)
SPY five-minute chart with moving averages, stepwise higher-timeframe lines, numbered labels and a legend of types, lengths, timeframes and values.
SPY five-minute chart comparing chart and completed higher-timeframe averages. Select the image for the full-size view. This illustration is not a strategy performance report.

One indicator, up to 40 independent averages

Each numbered slot has an enable switch, average type, length from 1 to 2000 and its own timeframe. Set line color and thickness under Settings → Style. The shared source selector offers Close, Open, High, Low, HL2, HLC3 and OHLC4; that source is evaluated in each slot’s calculation timeframe.

The published script is called Add 40 Moving Averages From Any Time Frame [CandleChasers]. Supported calculations are chart timeframe and higher timeframe. Lower-timeframe selections are not supported.

Supported moving average types

TypeCalculation
EMAExponential moving average.
SMASimple moving average.
WMALinearly weighted moving average.
RMA / SMMAWilder-style smoothing using Pine RMA. SMMA names this implementation; initialization can differ across platforms.
HMAHull moving average. Length 1 returns the selected source directly.
VWMAVolume-weighted moving average. Requires usable feed volume and is not VWAP.
DEMATwice the first EMA minus its second EMA stage.
TEMAThree times the first EMA minus three times the second stage plus the third stage.

No type is presented as universally superior. Comparing formulas is a visual research task, not evidence that one produces better trading outcomes.

Length belongs to the selected timeframe

A 20-period daily average uses daily candles, not 20 candles from a five-minute chart. Blank timeframe means chart timeframe; an equal-duration selection also uses chart calculation.

Calculation modeWhat you see
Chart timeframeThe average develops during the open chart candle.
Higher timeframeThe previous completed source candle’s average is carried into the next source period, producing deliberate steps.
Lower timeframeAn explanatory error identifies the unsupported slot.

Higher-timeframe lines do not follow the unfinished source candle. History offsets with lookahead_on prevent final future HTF values from leaking into earlier chart bars. All 40 enabled slots may use supported higher timeframes within the script’s conservative 40-slot request budget.

Changing the chart timeframe can turn a previously valid source selection into a lower timeframe. Review enabled slots if an error appears.

Read the SPY chart example

The supplied five-minute chart combines smooth chart-timeframe averages with stepwise completed higher-timeframe values. The legend distinguishes 5 chart from sources marked closed. One long DEMA shows Unavailable / warm-up: an enabled slot does not guarantee a usable value when source history is insufficient. This customized illustration uses more than the four default enabled lines.

Identify each line with labels and the legend

Right-edge labels identify slot number, type, length and timeframe; HTF marks higher-timeframe values. Hover for the snapshot value and calculation mode. Label size and horizontal offset are adjustable.

Labels match the built-in palette by default. Pine cannot read manual Style-tab color overrides, so changing a line’s color there does not automatically recolor its label. Keep default colors for matching or choose a neutral label color. Slot numbers distinguish lines when palette colors repeat.

The optional legend lists enabled slots, types, lengths, timeframes and values. It defaults off and offers three text sizes. Input values are hidden from the status line to preserve chart space.

Why labels can lag a developing line

Labels and the legend refresh at chart-bar close by default, while chart-timeframe lines continue updating live. Displays initialize on the last confirmed history candle. On an open candle, label positions and snapshot values can therefore lag the line.

Disable the bar-close display option for realtime label and legend updates. That changes display refresh; higher-timeframe values still use the previous completed source candle.

Start with the lines you need

  1. Add the indicator to a standard time-based candlestick chart.
  2. Begin with the four enabled chart EMAs: 9, 21, 50 and 200. The other 36 slots default off.
  3. Enable only the slots needed for your comparison.
  4. Select each type, length and chart or higher timeframe.
  5. Adjust color and thickness under Style.
  6. Use numbered labels or enable the legend to identify the lines.

Default lengths are example starting settings, not optimized trading parameters. A focused comparison can be easier to interpret than displaying all 40 lines at once.

Manage chart load

The script calculates the selected average family, skips disabled slots, limits extra EMA stages to DEMA/TEMA and reuses label objects. It uses 40 ordinary line plots rather than rebuilding short drawing fragments.

More enabled lines, HTF requests, long lengths and large datasets can increase load. Disable unused slots under Inputs. Hiding a plot under Style does not disable its calculation. Similar labels can overlap and a large legend can become tall. No specific runtime or speed improvement is guaranteed.

Common questions

Can I use a lower timeframe than my chart?

No. Use chart timeframe or a higher timeframe. The error names the offending slot; review selections after changing your chart timeframe.

Why are my higher-timeframe averages stepwise?

They use the previous completed source candle’s average and hold it through the next source period. They do not track the developing source candle.

Why does a label color differ from its line?

Manual Style-tab color changes cannot be read by Pine for label recoloring. Use the default palette or a neutral label color.

Is VWMA the same as VWAP?

No. This script’s VWMA is a volume-weighted moving average and requires usable volume.

Does the indicator provide crossover alerts or backtests?

No. It is a visual comparison indicator with no crossover signals, alerts, orders or strategy backtest.

Purpose and limitations

The average formulas are established methods. This implementation combines independent configuration, completed HTF handling, bounded plot/request usage, persistent identification and configurable display refresh in one comparison workflow. More averages do not inherently improve decisions.

Long lengths and multiple smoothing stages need sufficient source history. Missing volume can make VWMA unavailable. Feed revisions, loaded history and input changes can alter recalculated results. No verified win rate or trading edge is claimed.

Explore Support & Resistance for selective price levels, or Market State Engine for separate environment and trigger context.

View the published script ↗ (paid link)

As an Amazon Associate I earn from qualifying purchases.