Indicator · Pine Script v5 · On the chart

Multiple Moving Averages

by sbtnc

Plots five moving averages on the price chart in one script, defaulting to exponential averages of 9, 20, 50, 100 and 200 bars in graded blue shades.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code130
Settings26inputs
LicenceMPL-2.0
Last update9 months ago28 Jan 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

130 lines of Pine Script v5 from Multiple Moving Averages.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Multiple Moving Averages.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sbtnc
// Created: 2019-01-05
// Last modified: 2023-05-09
// Version 2.0

// @version=5
indicator("Multiple Moving Averages", "MAs", true)


//--------------------------------------------------------------------
//#region                      Constants
//--------------------------------------------------------------------

var DEFAULT_LINEWIDTH   = 1
var DEFAULT_COLOR1      = color.new(#c9daf8, 0)
var DEFAULT_COLOR2      = color.new(#a4c2f4, 4)
var DEFAULT_COLOR3      = color.new(#6d9eeb, 8)
var DEFAULT_COLOR4      = color.new(#3c78d8, 12)
var DEFAULT_COLOR5      = color.new(#0b5394, 16)

//#endregion


//--------------------------------------------------------------------
//#region                        Inputs
//--------------------------------------------------------------------

// Groups

g1 = "#1"
g2 = "#2"
g3 = "#3"
g4 = "#4"
g5 = "#5"
g6 = "Preferences"


// Tooltips

tt_timeframe    = "You can display a Moving Average from a higher timeframe. " +
                     "Note: It will be automatically hidden when switching the chart to a timeframe higher than its timeframe."


// Inputs

i_type_1        = input.string      ("EMA", "Type", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"],    group=g1)
i_length1       = input.int         (9,     "Length", 1,                                            group=g1)
i_source1       = input             (close, "Source",                                               group=g1)
i_offset1       = input.int         (0,     "Offset",                                               group=g1)
i_timeframe1    = input.timeframe   ("",    "Timeframe", tooltip=tt_timeframe,                      group=g1)

i_type_2        = input.string      ("EMA", "Type", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"],    group=g2)
i_length2       = input.int         (20,    "Length", 1,                                            group=g2)
i_source2       = input             (close, "Source",                                               group=g2)
i_offset2       = input.int         (0,     "Offset",                                               group=g2)
i_timeframe2    = input.timeframe   ("",    "Timeframe", tooltip=tt_timeframe,                      group=g2)

i_type_3        = input.string      ("EMA", "Type", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"],    group=g3)
i_length3       = input.int         (50,    "Length", 1,                                            group=g3)
i_source3       = input             (close, "Source",                                               group=g3)
i_offset3       = input.int         (0,     "Offset",                                               group=g3)
i_timeframe3    = input.timeframe   ("",    "Timeframe", tooltip=tt_timeframe,                      group=g3)

i_type_4        = input.string      ("EMA", "Type", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"],    group=g4)
i_length4       = input.int         (100,   "Length", 1,                                            group=g4)
i_source4       = input             (close, "Source",                                               group=g4)
i_offset4       = input.int         (0,     "Offset",                                               group=g4)
i_timeframe4    = input.timeframe   ("",    "Timeframe", tooltip=tt_timeframe,                      group=g4)

i_type_5        = input.string      ("EMA", "Type", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"],    group=g5)
i_length5       = input.int         (200,   "Length", 1,                                            group=g5)
i_source5       = input             (close, "Source",                                               group=g5)
i_offset5       = input.int         (0,     "Offset",                                               group=g5)
i_timeframe5    = input.timeframe   ("",    "Timeframe", tooltip=tt_timeframe,                      group=g5)

i_enableGaps    = input             (true,  "Wait for timeframe closes",                            group=g6)

//#endregion


//--------------------------------------------------------------------
//#region                       Functions 
//--------------------------------------------------------------------

// @function Get the Moving Average (MA)
// @returns float
f_getMa(string _type, float _source, _length) =>
    switch _type
        "SMA"           => ta.sma(_source, _length)
        "EMA"           => ta.ema(_source, _length)
        "SMMA (RMA)"    => ta.rma(_source, _length)
        "WMA"           => ta.wma(_source, _length)
        "VWMA"          => ta.vwma(_source, _length)


// @function Check if a timeframe is higher or equal than the chart's one
// @returns bool
f_canDisplay(_tf) =>
    timeframe.in_seconds(_tf) >= timeframe.in_seconds()

//#endregion


//--------------------------------------------------------------------
//#region                         Logic
//--------------------------------------------------------------------

var gaps = i_enableGaps ? barmerge.gaps_on : barmerge.gaps_off

ma1 = request.security(syminfo.tickerid, i_timeframe1, f_getMa(i_type_1, i_source1, i_length1), gaps=gaps)
ma2 = request.security(syminfo.tickerid, i_timeframe2, f_getMa(i_type_2, i_source2, i_length2), gaps=gaps)
ma3 = request.security(syminfo.tickerid, i_timeframe3, f_getMa(i_type_3, i_source3, i_length3), gaps=gaps)
ma4 = request.security(syminfo.tickerid, i_timeframe4, f_getMa(i_type_4, i_source4, i_length4), gaps=gaps)
ma5 = request.security(syminfo.tickerid, i_timeframe5, f_getMa(i_type_5, i_source5, i_length5), gaps=gaps)

//#endregion


//--------------------------------------------------------------------
//#region                   Plotting & styling
//--------------------------------------------------------------------

plot(f_canDisplay(i_timeframe1) ? ma1 : na, "#1", DEFAULT_COLOR1, DEFAULT_LINEWIDTH, offset=i_offset1)
plot(f_canDisplay(i_timeframe2) ? ma2 : na, "#2", DEFAULT_COLOR2, DEFAULT_LINEWIDTH, offset=i_offset2)
plot(f_canDisplay(i_timeframe3) ? ma3 : na, "#3", DEFAULT_COLOR3, DEFAULT_LINEWIDTH, offset=i_offset3)
plot(f_canDisplay(i_timeframe4) ? ma4 : na, "#4", DEFAULT_COLOR4, DEFAULT_LINEWIDTH, offset=i_offset4)
plot(f_canDisplay(i_timeframe5) ? ma5 : na, "#5", DEFAULT_COLOR5, DEFAULT_LINEWIDTH, offset=i_offset5)

//#endregion

Overview

Each line has its own type from SMA, EMA, SMMA, WMA or VWMA, plus length, source, offset and an optional higher timeframe. A preference controls whether higher-timeframe lines wait for the timeframe to close. It has no signals or alerts.

Details

Published by

sbtnc on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pine-scriptpinescriptpinescript-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
TypestringEMAi_type_1
Lengthint9i_length1
Sourcesourceclosei_source1
Offsetint0i_offset1
Timeframetimeframei_timeframe1
TypestringEMAi_type_2
Lengthint20i_length2
Sourcesourceclosei_source2
Offsetint0i_offset2
Timeframetimeframei_timeframe2
TypestringEMAi_type_3
Lengthint50i_length3
Show all 26 settings
SettingTypeDefaultVariable
Sourcesourceclosei_source3
Offsetint0i_offset3
Timeframetimeframei_timeframe3
TypestringEMAi_type_4
Lengthint100i_length4
Sourcesourceclosei_source4
Offsetint0i_offset4
Timeframetimeframei_timeframe4
TypestringEMAi_type_5
Lengthint200i_length5
Sourcesourceclosei_source5
Offsetint0i_offset5
Timeframetimeframei_timeframe5
Wait for timeframe closesbooltruei_enableGaps
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

947 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 12 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
280 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 3 days ago · MIT GitHub repository of NVDA Trend Ribbon
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 7 weeks ago · None stated GitHub repository of Donald SPX - Options Trading
228 stars

This indicator computes moving average and RSI bias signals across the current timeframe and four higher timeframes. It sums biases from the MA and RSI to derive an overall bullish or bearish signal. The results appear in a configurable table that displays eac

Indicator v6 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Template

More Moving averages scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers