Indicator · Pine Script v5 · On the chart

MACD VWMA Strategy

by g-moe· code credited to goodgains

This strategy combines MACD and Supertrend to generate entries on the price chart.

View on GitHub

Repaints

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code86
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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

86 lines of Pine Script v5 from Tradingview/macd-vwma-strategy-v2.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

macd-vwma-strategy-v2.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("MACD VWMA Strategy", overlay = true)

// @keywords: macd-crossover, vwma-trend, supertrend-filter, mtf-supertrend, atr-trailing-stop, buy-sell-shapes, histogram-macd, signal-line-cross, oscillator-ma-type, overlay-signals, fast-slow-macd, security-higher-tf, filter-gated-entries, directional-bias, alert-conditions, trend-confirmation, dual-filter, intraday-signals, momentum-cross, entry-markers

// supertrend settings
use_supertrend      = input.bool(true, title = 'Supertrend Filter', group = 'Supertrend Settings', inline = 'ST')
st_timeframe        = input.timeframe("15", title = '', group = 'Supertrend Settings', inline = 'ST 2')
st_atrPeriod        = input.int(10,    "Length", minval = 1, group = 'Supertrend Settings', inline = 'ST 2')
st_factor           = input.float(3.0, "Factor",     minval = 0.01, step = 0.01, group = 'Supertrend Settings', inline = 'ST 2')



// macd settings
macd_src        = input(title="Source", defval=close, group = 'MACD Settings', inline = 'MACD')
fast_length     = input(title="Fast Length", defval=12, group = 'MACD Settings', inline = 'MACD')
slow_length     = input(title="Slow Length", defval=26, group = 'MACD Settings', inline = 'MACD')
signal_length   = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group = 'MACD Settings', inline = 'MACD')
sma_source      = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group = 'MACD Settings', inline = 'MACD')
sma_signal      = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group = 'MACD Settings', inline = 'MACD')



// calculate supertrend
pine_supertrend(factor, atrPeriod) =>
    src = hl2
    atr = ta.atr(atrPeriod)
    upperBand = src + factor * atr
    lowerBand = src - factor * atr
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])

    lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
    int direction = na
    float superTrend = na
    prevSuperTrend = superTrend[1]
    if na(atr[1])
        direction := 1
    else if prevSuperTrend == prevUpperBand
        direction := close > upperBand ? -1 : 1
    else
        direction := close < lowerBand ? 1 : -1
    superTrend := direction == -1 ? lowerBand : upperBand
    [superTrend[1], direction[1]]


[supertrend, direction] = request.security(syminfo.tickerid, st_timeframe, pine_supertrend(st_factor, st_atrPeriod), lookahead = barmerge.lookahead_on)
supertrend := barstate.isfirst ? na : supertrend
upTrend =    plot(direction < 0 ? supertrend : na, "Up Trend",   color = color.rgb(0, 0, 0), style = plot.style_linebr, display = use_supertrend ? display.all : display.none)
downTrend =  plot(direction < 0 ? na : supertrend, "Down Trend", color = color.rgb(0, 0, 0),   style = plot.style_linebr, display = use_supertrend ? display.all : display.none)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)

fill(bodyMiddle, upTrend,   color.new(color.blue, 90), fillgaps = false, display = use_supertrend ? display.all : display.none)
fill(bodyMiddle, downTrend, color.new(#000000, 90), fillgaps = false, display = use_supertrend ? display.all : display.none)



// calculate MACD
fast_ma = sma_source == "SMA" ? ta.sma(macd_src, fast_length) : ta.ema(macd_src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(macd_src, slow_length) : ta.ema(macd_src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal



// calculate signals
st_Buy      = direction < 0 or not use_supertrend
st_Sell     = direction >= 0 or not use_supertrend

macdBuy     = signal < 0 and ta.crossover(hist, 0)
macdSell    = signal > 0 and ta.crossunder(hist, 0)

buyCond     = st_Buy and macdBuy
sellCond    = st_Sell and macdSell

plotshape(buyCond, title = 'Buy', style = shape.triangleup, color = color.black, size = size.small, location = location.belowbar)
plotshape(sellCond, title = 'Sell', style = shape.triangledown, color = color.black, size = size.small, location = location.abovebar)

alertcondition(buyCond, title = 'Buy Signal', message = 'Buy Signal')
alertcondition(sellCond, title = 'Sell Signal', message = 'Sell Signal')

Overview

The MACD oscillator uses selected source, fast length 12, slow 26, signal 9 and EMA or SMA. Supertrend on higher timeframe 15 provides trend direction and ATR trailing stop. Signals plot as buy sell shapes on MACD crossover filtered by Supertrend. Main settings adjust Supertrend parameters, MACD lengths and MA types. It overlays trend lines and fills on the chart with entry alerts. It repaints on the live bar due to lookahead.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Oscillators & momentum, Moving averages, Volatility & bands

Markets

Any symbol

Timeframes

M15-H1, H4 and above

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

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

SettingTypeDefaultVariable
Supertrend Filterbooltrueuse_supertrend
st_timeframetimeframe15st_timeframe
Lengthint10st_atrPeriod
Factorfloat3.0st_factor
Sourcesourceclosemacd_src
Fast Lengthint12fast_length
Slow Lengthint26slow_length
Signal Smoothingint9signal_length
Oscillator MA TypestringEMAsma_source
Signal Line MA TypestringEMAsma_signal
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

114 stars

ADX Strategy (original) is an overlay indicator that identifies trend-following trade signals using the Average Directional Index (ADX) combined with a Simple Moving Average filter. A long signal is generated when the ADX rises above a user-defined trend level

Indicator v4 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Strategy (original)
56 stars

Double Screened Decision is a multi-timeframe trend and momentum indicator overlaid directly on the price chart. It dynamically selects a higher timeframe based on the active chart resolution to evaluate higher-timeframe MACD, Bollinger Bands, RSI, and Stochas

Indicator v5 TrendMoving averages Repaints
Updated 4 months ago · None stated GitHub repository of Double Screened Decision
24 stars

This indicator plots Bollinger Bands on the price chart alongside a baseline moving average to identify trend-aligned swing setups. It calculates trend bias by comparing the Bollinger Bands basis to a customizable moving average, such as a 200 EMA. Trade signa

Indicator v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of Bollinger Band Swing

Index Intraday Analyst

by chris-c-thomas
14 stars

Index Intraday Analyst overlays CBOE cash indices such as SPX, NDX, RUT, and DJX. It plots a multi-EMA ribbon, a session-anchored TWAP with standard-deviation bands, and session levels such as prior-day high, low, and close, the opening range, and the high and

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · MIT GitHub repository of Index Intraday Analyst

RePaNoCHa V4

by wocclyl
9 stars

RePaNoCHa V4 is an overlay alert indicator that plots a Tillson T3 moving average on the price chart and colors it by whether the line is rising or falling. It combines that slope with a range filter, ADX, parabolic SAR, RSI, and MACD to mark long and short si

Indicator v4 TrendOscillators & momentum
Updated 7 years ago · MIT GitHub repository of RePaNoCHa V4

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers