Logo
OFFLINEPIXEL
/ pinescript-strategies / pine-script-volume-weighted-macd

$ Pinescript Volume-Weighted MACD

Master this advanced momentum indicator in TradingView's Pinescript that integrates volume into its core calculation, providing more robust trend and momentum signals for enhanced trading strategies.

500+ Clients Helped
100% Satisfaction
Live Trading Ready
⚠️
Trading financial markets carries risk. All content (PineScript code, indicators, strategies) on this website is for educational purposes only. Past performance is not indicative of future results. By using any code or information from this site, you agree that you are solely responsible for your trading decisions. The author disclaims all liability for any losses incurred. To gain from experts experiences, You can always try our Invite Only Scripts
01

What is Volume-Weighted MACD?

Volume-Weighted Moving Average Convergence Divergence (VW-MACD) is an advanced variation of the traditional MACD indicator. While the standard MACD uses Exponential Moving Averages (EMAs) of price, the VW-MACD replaces these with Volume-Weighted Moving Averages (VWMAs). By incorporating volume into the calculation of its underlying moving averages, VW-MACD aims to provide more reliable and conviction-based signals.

The core philosophy behind VW-MACD is that significant price movements should be accompanied by significant volume. If volume is low, a price move might lack conviction. By weighting price averages by volume, VW-MACD prioritizes price action that occurs on higher trading activity, theoretically filtering out "noise" that happens on lower volume.

VW-MACD is primarily used to:

  • Confirm price trends: A rising VW-MACD line above its signal line confirms a strong uptrend supported by volume.
  • Identify momentum shifts: Crossovers of the VW-MACD line and its signal line indicate shifts in volume-weighted momentum.
  • Spot divergences: Divergences between price and VW-MACD can provide early warnings of potential trend reversals, emphasizing the lack of volume conviction behind price movements.
  • Filter out weak signals: Signals generated by VW-MACD might be considered more robust than those from standard MACD, as they are confirmed by volume.
02

Components and Calculation

The calculation of Volume-Weighted MACD closely mirrors that of the standard MACD, but with a critical difference: the use of Volume-Weighted Moving Averages (VWMAs) instead of Exponential Moving Averages (EMAs) for the main MACD line components.

  1. Volume-Weighted Fast Length Moving Average (VWMA_Fast):
    `VWMA_Fast = ta.vwma(close, Fast_Length)` (Typically 12 periods)
  2. Volume-Weighted Slow Length Moving Average (VWMA_Slow):
    `VWMA_Slow = ta.vwma(close, Slow_Length)` (Typically 26 periods)
  3. VW-MACD Line: The difference between the VWMA_Fast and VWMA_Slow.
    `VW_MACD_Line = VWMA_Fast - VWMA_Slow`
  4. VW-MACD Signal Line: An Exponential Moving Average (EMA) of the VW-MACD Line. Note that this is a *standard EMA*, not volume-weighted, applied to the VW-MACD Line itself.
    `VW_Signal_Line = ta.ema(VW_MACD_Line, Signal_Length)` (Typically 9 periods)
  5. VW-MACD Histogram: The difference between the VW-MACD Line and the VW-MACD Signal Line.
    `VW_Histogram = VW_MACD_Line - VW_Signal_Line`
03

Basic Volume-Weighted MACD Implementation in Pinescript

pine-script@terminal
//@version=5
indicator("My Volume-Weighted MACD", overlay=false) // overlay=false to plot in a separate pane

// Inputs for VW-MACD lengths
fastLength = input.int(12, title="Fast Length", minval=1)
slowLength = input.int(26, title="Slow Length", minval=1)
signalLength = input.int(9, title="Signal Length", minval=1)

// Calculate Volume-Weighted Fast and Slow Moving Averages
vwmaFast = ta.vwma(close, fastLength)
vwmaSlow = ta.vwma(close, slowLength)

// Calculate VW-MACD Line
vwMacdLine = vwmaFast - vwmaSlow

// Calculate VW-MACD Signal Line (EMA of VW-MACD Line)
vwSignalLine = ta.ema(vwMacdLine, signalLength)

// Calculate VW-MACD Histogram
vwHistogram = vwMacdLine - vwSignalLine

// Plotting the VW-MACD components
plot(vwMacdLine, title="VW-MACD Line", color=color.blue, linewidth=2)
plot(vwSignalLine, title="VW-MACD Signal", color=color.orange, linewidth=1)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)

// Plotting the Histogram with color logic
// Colors for histogram: green for rising, red for falling
histColor = vwHistogram >= 0 ? (vwHistogram > vwHistogram[1] ? color.new(color.teal, 20) : color.new(color.blue, 20)) : (vwHistogram < vwHistogram[1] ? color.new(color.red, 20) : color.new(color.maroon, 20))

plot(vwHistogram, title="VW-MACD Histogram", style=plot.style_columns, color=histColor)
$ ✓ Compiled successfully
04

Conviction Filter

Conviction Filter

VW-MACD implicitly filters out weak price moves that aren't supported by significant volume, making its signals potentially more reliable than standard MACD.

05

Practical Volume-Weighted MACD Trading Strategies

06

1. Crossovers (Trend & Momentum Shifts)

pine-script@terminal
//@version=5
strategy("VW-MACD Crossover Strategy", overlay=true)

fastLength = input.int(12, title="Fast Length", minval=1)
slowLength = input.int(26, title="Slow Length", minval=1)
signalLength = input.int(9, title="Signal Length", minval=1)

vwmaFast = ta.vwma(close, fastLength)
vwmaSlow = ta.vwma(close, slowLength)
vwMacdLine = vwmaFast - vwmaSlow
vwSignalLine = ta.ema(vwMacdLine, signalLength)
vwHistogram = vwMacdLine - vwSignalLine

// Plot for visualization (display=display.pane_only plots in a separate sub-window)
plot(vwMacdLine, "VW-MACD Line", color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color.orange, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

histColor = vwHistogram >= 0 ? (vwHistogram > vwHistogram[1] ? color.new(color.teal, 20) : color.new(color.blue, 20)) : (vwHistogram < vwHistogram[1] ? color.new(color.red, 20) : color.new(color.maroon, 20))

plot(vwHistogram, "VW-MACD Histogram", style=plot.style_columns, color=histColor, display=display.pane_only)

// Long entry: VW-MACD Line crosses above Signal Line
longCondition = ta.crossover(vwMacdLine, vwSignalLine)

// Short entry: VW-MACD Line crosses below Signal Line
shortCondition = ta.crossunder(vwMacdLine, vwSignalLine)

if (longCondition)
    strategy.entry("Long VW-MACD", strategy.long)

if (shortCondition)
    strategy.entry("Short VW-MACD", strategy.short)

// Basic exit: opposite signal
strategy.close("Long VW-MACD", when=shortCondition)
strategy.close("Short VW-MACD", when=longCondition)
$ ✓ Compiled successfully
07

2. Divergence (Key Reversal Signal with Volume Confirmation)

pine-script@terminal
//@version=5
strategy("VW-MACD Divergence Strategy", overlay=true)

fastLength = input.int(12, title="Fast Length", minval=1)
slowLength = input.int(26, title="Slow Length", minval=1)
signalLength = input.int(9, title="Signal Length", minval=1)

vwmaFast = ta.vwma(close, fastLength)
vwmaSlow = ta.vwma(close, slowLength)
vwMacdLine = vwmaFast - vwmaSlow
vwSignalLine = ta.ema(vwMacdLine, signalLength)
vwHistogram = vwMacdLine - vwSignalLine

// Plot for visualization
plot(vwMacdLine, "VW-MACD Line", color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color.orange, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

histColor = vwHistogram >= 0
    ? (vwHistogram > vwHistogram[1]
        ? color.new(color.teal, 20)
        : color.new(color.blue, 20))
    : (vwHistogram < vwHistogram[1]
        ? color.new(color.red, 20)
        : color.new(color.maroon, 20))

plot(vwHistogram, "VW-MACD Histogram", style=plot.style_columns, color=histColor, display=display.pane_only)

// Simple divergence detection (conceptual)
// Bullish Divergence: Price lower low, VW-MACD higher low
bullishDiv = close < close[1] and close[1] < close[2]
    and vwMacdLine > vwMacdLine[1] and vwMacdLine[1] > vwMacdLine[2]

// Bearish Divergence: Price higher high, VW-MACD lower high
bearishDiv = close > close[1] and close[1] > close[2]
    and vwMacdLine < vwMacdLine[1] and vwMacdLine[1] < vwMacdLine[2]

plotshape(bullishDiv, title="Bullish VW-MACD Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish VW-MACD Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

if (bullishDiv)
    strategy.entry("Long Divergence", strategy.long)
if (bearishDiv)
    strategy.entry("Short Divergence", strategy.short)

// Basic exit after a few bars or on opposite signal
strategy.exit("Long Divergence Exit", from_entry="Long Divergence", profit=close*0.02, loss=close*0.01)
strategy.exit("Short Divergence Exit", from_entry="Short Divergence", profit=close*0.02, loss=close*0.01)
$ ✓ Compiled successfully
08

3. Histogram Analysis (Momentum Strength)

pine-script@terminal
//@version=5
strategy("VW-MACD Histogram Analysis", overlay=true)

fastLength = input.int(12, title="Fast Length", minval=1)
slowLength = input.int(26, title="Slow Length", minval=1)
signalLength = input.int(9, title="Signal Length", minval=1)

vwmaFast = ta.vwma(close, fastLength)
vwmaSlow = ta.vwma(close, slowLength)
vwMacdLine = vwmaFast - vwmaSlow
vwSignalLine = ta.ema(vwMacdLine, signalLength)
vwHistogram = vwMacdLine - vwSignalLine

// Plot for visualization
plot(vwMacdLine, "VW-MACD Line", color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color.orange, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

histColor = vwHistogram >= 0
    ? (vwHistogram > vwHistogram[1]
        ? color.new(color.teal, 20)
        : color.new(color.blue, 20))
    : (vwHistogram < vwHistogram[1]
        ? color.new(color.red, 20)
        : color.new(color.maroon, 20))

plot(vwHistogram, "VW-MACD Histogram", style=plot.style_columns, color=histColor, display=display.pane_only)

// Conditions for histogram momentum
increasingBullishMomentum = vwHistogram > 0 and vwHistogram > vwHistogram[1]
decreasingBullishMomentum = vwHistogram > 0 and vwHistogram < vwHistogram[1]
increasingBearishMomentum = vwHistogram < 0 and vwHistogram < vwHistogram[1]
decreasingBearishMomentum = vwHistogram < 0 and vwHistogram > vwHistogram[1]

// Example strategy using histogram signals
// Entry on increasing momentum, exit on decreasing momentum
if (increasingBullishMomentum)
    strategy.entry("Long Hist Inc", strategy.long)
if (increasingBearishMomentum)
    strategy.entry("Short Hist Inc", strategy.short)

if (decreasingBullishMomentum)
    strategy.close("Long Hist Inc")
if (decreasingBearishMomentum)
    strategy.close("Short Hist Inc")
$ ✓ Compiled successfully
09

Optimizing Volume-Weighted MACD Performance

To get the most from Volume-Weighted MACD in Pinescript:

  • Parameter Tuning: Experiment with the `fastLength`, `slowLength`, and `signalLength` inputs. While 12, 26, 9 are standard, different assets or timeframes might benefit from slightly adjusted values.
  • Focus on Divergence: Like the traditional MACD, divergences between price and VW-MACD are often the most reliable signals for anticipating reversals. Pay close attention to these.
  • Combine with Price Action: Always confirm VW-MACD signals with price action. A bullish crossover is stronger if it aligns with a break of resistance or a bullish candlestick pattern.
  • Trend Context: Use VW-MACD in the context of the larger trend. In a strong uptrend, focus on bullish crossovers and bounces from the zero line. In a downtrend, look for bearish crossovers.
  • Volume Data Quality: Since VW-MACD heavily relies on volume, ensure the asset you are trading has reliable and consistent volume data.
  • Filtering with Zero Line: Signals occurring closer to or crossing the zero line often indicate stronger momentum shifts. Signals far from the zero line might indicate overextended conditions.
10

Enhanced Reliability

Enhanced Reliability

By factoring in volume, VW-MACD aims to reduce false signals and provide more robust indications of momentum and trend shifts compared to its price-only counterpart.

11

Common Volume-Weighted MACD Pitfalls

  • Lag: Despite being volume-weighted, VW-MACD is still a lagging indicator. It will confirm trends after they have already begun.
  • Whipsaws in Choppy Markets: In highly volatile and non-trending (choppy) markets, VW-MACD can generate frequent crossovers and false signals, leading to whipsaws.
  • Over-Optimization: Excessive parameter tuning for historical data can lead to an indicator that performs poorly in live trading (curve fitting).
  • Volume Data Quality: Inaccurate or inconsistent volume data can significantly distort the VW-MACD's readings and make it unreliable.
  • Not a Standalone Indicator: VW-MACD provides valuable insights but should never be used in isolation. It works best as a confirming or filtering indicator within a broader trading strategy.
12

Conclusion

Conclusion

The Volume-Weighted MACD is an enhanced and insightful momentum indicator available in Pinescript for TradingView. By integrating Volume-Weighted Moving Averages into its core calculation, it aims to provide more robust and conviction-based signals compared to the traditional MACD. While it still operates as a lagging indicator, its ability to filter out price moves not supported by significant volume makes its crossovers, divergences, and histogram analysis potentially more reliable. By understanding its calculation, thoughtfully tuning its parameters, and integrating it strategically with price action and other technical analysis tools, you can leverage Volume-Weighted MACD to enhance your Pinescript strategies and gain a clearer understanding of market momentum backed by underlying volume conviction.

Enhance Your Trading

Get a high-performance Pinescript analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Get Pinescript Strategy