Indicator · Pine Script v4 · On the chart

Moving Average Extension

by krosenfeld7

This indicator calculates a multi-timeframe moving average using Simple, Exponential, Hull, Volume Weighted, or Weighted moving average types.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code68
Settings4inputs
LicenceApache-2.0
Last update3 years ago12 May 2023

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

68 lines of Pine Script v4 from Moving_Average_Extension.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

Moving_Average_Extension.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © krosenfeld7

//@version=4
study("Moving Average Extension", overlay = true)

resolution = input(title = "Moving Average Resolution", 
     type  = input.resolution, defval = "D")
length     = input(title = "Moving Average Length",
     type  = input.integer, defval = 140, minval = 1)
source     = input(title = "Moving Average Source",
     type  = input.source, defval = close)
maType     = input(title = "Moving Average Type", 
     type  = input.string, defval = "SMA", options = 
     ["EMA", "HMA", "SMA", "VWMA", "WMA"])

ema  = ema(source,  length)
hma  = hma(source,  length)
vwma = vwma(source, length)
wma  = wma(source,  length)
sma  = sma(source,  length)

emaRes = security(syminfo.tickerid, resolution, 
 ema[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, 
 barmerge.lookahead_off)
hmaRes = security(syminfo.tickerid, resolution, 
 hma[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, 
 barmerge.lookahead_off)
vwmaRes = security(syminfo.tickerid, resolution, 
 vwma[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, 
 barmerge.lookahead_off)
wmaRes = security(syminfo.tickerid, resolution, 
 wma[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, 
 barmerge.lookahead_off)
smaRes = security(syminfo.tickerid, resolution, 
 sma[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, 
 barmerge.lookahead_off)

movingAvgRes = smaRes

if maType == "EMA"
    movingAvgRes := emaRes
else if maType == "HMA"
    movingAvgRes := hmaRes
else if maType == "VWMA"
    movingAvgRes := vwmaRes
else if maType == "WMA"
    movingAvgRes := wmaRes

value = log(source / movingAvgRes)

closePlot = plot(close, color = color.new(color.white, transp = 100))

mAvgPlot = plot(movingAvgRes, color = color.new(value < 0 ? color.red : 
     color.green, transp = 0), linewidth = 3)
plot(value, color = color.new(value < 0 ? color.red : 
     color.green, transp = 100))
fill(mAvgPlot, closePlot, color = color.new(value < 0 ? color.red : 
     color.green, transp = 75))

var line1 = line.new(0,0,0,0)
line.set_x1(line1, bar_index)
line.set_x2(line1, bar_index)
line.set_y1(line1, movingAvgRes)
line.set_y2(line1, close)
line.set_style(line1, line.style_dotted)
line.set_width(line1, 3)
line.set_color(line1, color.white)

Overview

Moving Average Extension computes the logarithmic distance between the asset price and the selected moving average to determine extension. Plots are rendered directly on the price chart with color-coded fills indicating whether price is above or below the average, alongside a dynamic dotted connection line on the current bar. Note that historical and real-time bars process multi-timeframe data differently, causing repainting.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
Moving Average ResolutiontimeframeDresolution
Moving Average Lengthint140length
Moving Average Sourcesourceclosesource
Moving Average TypestringSMAmaType
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

257 stars

This indicator analyzes short-term price action on index futures and options charts by applying Rational Quadratic kernel smoothing to detect sideways consolidation ranges and potential breakouts. It processes pivot highs and lows over customizable loopback pe

Indicator v5 Support & resistanceTrend Repaints
Updated 7 weeks ago · None stated GitHub repository of SPX 30M - Trading Chart

Ichimoku Cloud

by harryguiacorn
220 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud

MA Envelope (MAE)

by mihakralj
99 stars

The MA Envelope indicator calculates upper and lower channel bands shifted by a user-defined percentage around a baseline moving average. It supports Simple Moving Average, Exponential Moving Average, and Weighted Moving Average calculation types. The script p

Indicator v6 Moving averagesSupport & resistance
Updated 4 weeks ago · Apache-2.0 GitHub repository of MA Envelope (MAE)
56 stars

This indicator calculates an Exponential Moving Average and a Zero-Lag Moving Average to identify market trends on the price chart. It plots both moving averages and fills the space between them with color to highlight trend direction. Upon a crossover or cros

Indicator v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of No-Lag MA Trend Levels

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