Pine Script Volume-Weighted MACD

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

Last Updated on: Expertise: Advanced

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:

In Pine Script, implementing VW-MACD involves leveraging the `ta.vwma()` function and applying it in a similar structure to the traditional MACD.

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`

The default lengths for VW-MACD are commonly 12, 26, and 9, consistent with the standard MACD.

Basic Volume-Weighted MACD Implementation in Pine Script

Pine Script v5 makes this relatively straightforward using `ta.vwma()` and `ta.ema()`.


//@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=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)
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.

Practical Volume-Weighted MACD Trading Strategies

1. Crossovers (Trend & Momentum Shifts)

The most common strategy involves crossovers between the VW-MACD Line and its Signal Line. These indicate shifts in volume-weighted momentum and can be used for trend confirmation or early entry/exit signals.


//@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=color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color=color.orange, display=display.pane_only)
hline(0, "Zero Line", color=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)

2. Divergence (Key Reversal Signal with Volume Confirmation)

Divergence between price and the VW-MACD is a highly regarded signal, suggesting a weakening trend and potential reversal, with the added conviction that volume is not supporting the price action.


//@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=color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color=color.orange, display=display.pane_only)
hline(0, "Zero Line", color=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)

3. Histogram Analysis (Momentum Strength)

The histogram component of VW-MACD provides insights into the strength of the volume-weighted momentum. Expanding histogram bars in the direction of the trend indicate increasing momentum, while contracting bars suggest weakening momentum.


//@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=color.blue, display=display.pane_only)
plot(vwSignalLine, "VW-MACD Signal", color=color.orange, display=display.pane_only)
hline(0, "Zero Line", color=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")

Optimizing Volume-Weighted MACD Performance

To get the most from Volume-Weighted MACD in Pine Script:

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.

Common Volume-Weighted MACD Pitfalls

Conclusion

The Volume-Weighted MACD is an enhanced and insightful momentum indicator available in Pine Script 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 pine script strategies and gain a clearer understanding of market momentum backed by underlying volume conviction.