Indicator · Pine Script v6 · On the chart

2x MA Moving Average

by piecioshka

This indicator plots two customizable moving averages directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv6current
Lines of code73
Settings10inputs
LicenceMIT
Last update12 days ago18 Oct 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

73 lines of Pine Script v6 from indicators/overlays/2x-ma/2x-ma.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

2x-ma.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Piotr Kowalski "piecioshka"
// Docs (EN): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/overlays/2x-ma/2x-ma.md
// Docs (PL): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/overlays/2x-ma/2x-ma.pl.md

//@version=6
indicator('2x MA = Moving Average', overlay = true, max_labels_count=500)

aLen = input(50, 'Length', group='Short')
aType = input.string(title='Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'], group='Short', defval='SMA')
aColor = input.color(color.rgb(56, 104, 174), 'Color', group='Short')
aSeries = switch aType
    'SMA' => ta.sma(close, aLen)
    'EMA' => ta.ema(close, aLen)
    'WMA' => ta.wma(close, aLen)
    'VWMA' => ta.vwma(close, aLen)
    'RMA' => ta.rma(close, aLen)
    => na

bLen = input(200, 'Length', group='Long')
bType = input.string(title='Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'], group='Long', defval='SMA')
bColor = input.color(color.rgb(215, 107, 52), 'Color', group='Long')
bSeries = switch bType
    'SMA' => ta.sma(close, bLen)
    'EMA' => ta.ema(close, bLen)
    'WMA' => ta.wma(close, bLen)
    'VWMA' => ta.vwma(close, bLen)
    'RMA' => ta.rma(close, bLen)
    => na

showLabels = input.bool(true, 'Show labels (length and type)', group='Labels')
showTrades = input.bool(true, 'Signal markers', group='Labels', tooltip='Markers at crosses: below the candle (long) at a golden cross (the short MA crosses the long one up), above the candle (sell) at a death cross. Shape and size in the Appearance group.')

gA = 'Appearance'
shapeName = input.string('Triangle', 'Marker shape', options=['Triangle', 'Arrow', 'Label arrow', 'Circle', 'Diamond'], group=gA, tooltip='Triangle - solid triangle.\nArrow - small thin arrow.\nLabel arrow - arrow-shaped label bubble.\nCircle / Diamond - neutral dot markers.')
sizeName  = input.string('Small', 'Marker size', options=['Tiny', 'Small', 'Normal', 'Large'], group=gA)

// Markers as labels - the shape is picked from an input, which plotshape() cannot do (its style must be a constant).
upStyle    = shapeName == 'Arrow' ? label.style_arrowup : shapeName == 'Label arrow' ? label.style_label_up : shapeName == 'Circle' ? label.style_circle : shapeName == 'Diamond' ? label.style_diamond : label.style_triangleup
downStyle  = shapeName == 'Arrow' ? label.style_arrowdown : shapeName == 'Label arrow' ? label.style_label_down : shapeName == 'Circle' ? label.style_circle : shapeName == 'Diamond' ? label.style_diamond : label.style_triangledown
markerSize = sizeName == 'Tiny' ? size.tiny : sizeName == 'Normal' ? size.normal : sizeName == 'Large' ? size.large : size.small

plot(aSeries, 'Short', color=color.new(aColor, 30), linewidth=2, force_overlay=true)
plot(bSeries, 'Long', color=color.new(bColor, 30), linewidth=2, force_overlay=true, linestyle=plot.linestyle_dotted)

updateLabel(label lbl, float y, string txt, color col) =>
    label result = lbl
    if na(result)
        result := label.new(bar_index, y, txt, style=label.style_label_left, color=color.new(color.black, 100), textcolor=col, force_overlay=true)
    label.set_xy(result, bar_index, y)
    label.set_text(result, txt)
    result

var label aLabel = na
var label bLabel = na

if showLabels and barstate.islast
    aLabel := updateLabel(aLabel, aSeries, str.tostring(aLen) + ' ' + aType, aColor)
    bLabel := updateLabel(bLabel, bSeries, str.tostring(bLen) + ' ' + bType, bColor)

crossUp = ta.crossover(aSeries, bSeries)
crossDn = ta.crossunder(aSeries, bSeries)

// Signal markers (as in Accumulation / Distribution Density):
// golden cross = green marker below the candle (long), death cross = red marker above (sell)
if showTrades and crossUp
    label.new(bar_index, na, '', yloc=yloc.belowbar, style=upStyle, color=color.green, size=markerSize, force_overlay=true)
if showTrades and crossDn
    label.new(bar_index, na, '', yloc=yloc.abovebar, style=downStyle, color=color.red, size=markerSize, force_overlay=true)

// --- Alerts ---
alertcondition(crossUp, 'MA: cross up',   'The short MA crossed the long one up (golden cross, bullish impulse)')
alertcondition(crossDn, 'MA: cross down', 'The short MA crossed the long one down (death cross, bearish impulse)')

Overview

Users can select independent lengths, colors, and calculation types, including SMA, EMA, WMA, VWMA, and RMA. When the faster moving average crosses the slower moving average, the script plots visual markers for golden and death crosses. Settings allow customization of marker shapes, marker sizing, and optional labels at the end of each line displaying the period and type.

Details

Published by

piecioshka 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

pinescriptpinescript-indicatorspinescript-strategiestradingtradingview

Settings

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

SettingTypeDefaultVariable
Lengthint50aLen
TypestringSMAaType
Colorcolorcolor.rgb(56, 104, 174)aColor
Lengthint200bLen
TypestringSMAbType
Colorcolorcolor.rgb(215, 107, 52)bColor
Show labels (length and type)booltrueshowLabels
Signal markersbooltrueshowTrades
Marker shapestringTriangleshapeName
Marker sizestringSmallsizeName
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