Pine Script Volume Price Trend (VPT)

Master this insightful momentum indicator in TradingView's Pine Script that combines price changes and volume to confirm trends and anticipate price movements by tracking cumulative money flow.

Last Updated on: Expertise: Intermediate

What is the Volume Price Trend (VPT)?

The Volume Price Trend (VPT), sometimes referred to as Price-Volume Trend (PVT), is a momentum indicator that essentially measures the money flow into or out of an asset. It is a cumulative indicator that adds or subtracts a portion of daily volume to a running total, based on the percentage change in the security's price. The core idea behind VPT is similar to On-Balance Volume (OBV) but with a nuanced difference: while OBV adds/subtracts *all* volume based on a simple up/down close, VPT adjusts the volume added/subtracted based on the magnitude of the price change.

A rising VPT indicates that trading volume is higher on days when the price increases, suggesting strong buying pressure and accumulation. A falling VPT indicates higher volume on days when the price decreases, suggesting strong selling pressure and distribution. VPT is primarily used to:

In Pine Script, VPT offers a valuable perspective on the health of a trend by weighing volume by price movement, making it a key tool for pine script strategies focused on volume analysis.

Components and Calculation

The calculation of the Volume Price Trend is cumulative:

  1. Daily Price Change Ratio: Calculate the percentage change of the current closing price from the previous closing price.
    `Price Change Ratio = (Current Close - Previous Close) / Previous Close`
    * Handle `Previous Close == 0` to avoid division by zero.
  2. Volume Contribution: Multiply the `Price Change Ratio` by the current `Volume`.
    `Volume Contribution = Price Change Ratio * Current Volume`
  3. Volume Price Trend (VPT): This is a running, cumulative total of the `Volume Contribution`.
    `VPT = Previous VPT + Current Volume Contribution`

The initial value of VPT is typically zero or the first bar's volume contribution.

Basic Volume Price Trend (VPT) Implementation in Pine Script

Pine Script v5 provides a convenient built-in function `ta.vpt()` for calculating the Volume Price Trend.


//@version=5
indicator("My Volume Price Trend (VPT)", overlay=false) // overlay=false to plot in a separate pane

// Calculate VPT using the built-in function
// ta.vpt() implicitly uses 'close' for price comparison and 'volume' for calculation
vptValue = ta.vpt(close, volume)

// Plot the VPT line
plot(vptValue, title="VPT", color=color.blue, linewidth=2)

// Optional: Add a Moving Average of VPT to act as a signal line
// Common lengths for VPT MA are 10, 20, or 30
vptMALength = input.int(20, title="VPT MA Length", minval=1)
vptMA = ta.sma(vptValue, vptMALength)

// Plot the VPT Moving Average (signal line)
plot(vptMA, title="VPT MA", color=color.orange, linewidth=1)

// Plot the Zero Line as a key reference point
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted)

Magnitude Matters: Unlike OBV, VPT considers *how much* price moved. A small price change with high volume will impact VPT less than a large price change with the same volume, reflecting the efficiency of the move.

Practical Volume Price Trend (VPT) Trading Strategies

1. Divergence (Key Strategy)

Divergence between price and VPT is one of the most powerful signals, suggesting a weakening trend and potential reversal, as the money flow is not confirming the price action's strength.


//@version=5
strategy("VPT Divergence Strategy", overlay=true)

vptValue = ta.vpt(close, volume)

// Plot VPT in a separate pane
plot(vptValue, "VPT", color=color.blue, display=display.pane_only)
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted, display=display.pane_only)

// Simple divergence detection (conceptual, robust detection requires advanced pivot logic)
// This example looks for recent higher/lower swings in price and VPT.

// Bullish Divergence: Price lower low, VPT higher low
bullishDiv = close < close[1] and close[1] < close[2] and vptValue > vptValue[1] and vptValue[1] > vptValue[2]

// Bearish Divergence: Price higher high, VPT lower high
bearishDiv = close > close[1] and close[1] > close[2] and vptValue < vptValue[1] and vptValue[1] < vptValue[2]

// Plot shapes on the chart to indicate divergence
plotshape(bullishDiv, title="Bullish VPT Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish VPT 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)

2. Trend Confirmation and Strength

VPT can confirm the strength and direction of a price trend. Ideally, price and VPT should move in the same direction, indicating healthy money flow supporting the trend.


//@version=5
strategy("VPT Trend Confirmation", overlay=true)

vptValue = ta.vpt(close, volume)
vptMALength = input.int(20, "VPT MA Length", minval=1)
vptMA = ta.sma(vptValue, vptMALength) // Moving Average of VPT

plot(vptValue, "VPT", color=color.blue, display=display.pane_only)
plot(vptMA, "VPT MA", color=color.orange, display=display.pane_only)

// Price trend filter (e.g., using a long-term EMA)
priceMALength = input.int(50, "Price MA Length", minval=1)
priceMA = ta.ema(close, priceMALength)

// Conditions for trend confirmation based on VPT and Price
// Long: Price above MA AND VPT above its MA (or rising)
longConfirm = close > priceMA and vptValue > vptMA and vptValue > vptValue[1]

// Short: Price below MA AND VPT below its MA (or falling)
shortConfirm = close < priceMA and vptValue < vptMA and vptValue < vptValue[1]

if (longConfirm)
    strategy.entry("Long Confirm", strategy.long)

if (shortConfirm)
    strategy.entry("Short Confirm", strategy.short)

// Basic exit: Price crosses back over price MA OR VPT crosses its MA
strategy.close("Long Confirm", when=close < priceMA or vptValue < vptMA)
strategy.close("Short Confirm", when=close > priceMA or vptValue > vptMA)

3. Zero-Line Crossovers (Bias Shift)

While often used for confirmation, a decisive crossover of the zero line by VPT can indicate a shift in the overall money flow bias, sometimes preceding a price trend change.


//@version=5
strategy("VPT Zero Line Crossover Strategy", overlay=true)

vptValue = ta.vpt(close, volume)

plot(vptValue, "VPT", color=color.blue, display=display.pane_only)
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted, display=display.pane_only)

// Long entry: VPT crosses above zero
longCondition = ta.crossover(vptValue, 0)

// Short entry: VPT crosses below zero
shortCondition = ta.crossunder(vptValue, 0)

if (longCondition)
    strategy.entry("Long VPT Cross", strategy.long)

if (shortCondition)
    strategy.entry("Short VPT Cross", strategy.short)

// Basic exit: opposite signal
strategy.close("Long VPT Cross", when=shortCondition)
strategy.close("Short VPT Cross", when=longCondition)

Optimizing Volume Price Trend (VPT) Performance

To get the most from the Volume Price Trend in Pine Script:

Money Flow Strength: VPT highlights whether money is flowing into or out of an asset, and critically, how much conviction (proportional to price change) that flow has.

Common Volume Price Trend (VPT) Pitfalls

Conclusion

The Volume Price Trend (VPT) is an insightful and effective momentum indicator available in Pine Script for TradingView. By uniquely combining price changes with volume, it provides traders with a deeper understanding of the underlying money flow and conviction behind price movements. While its greatest strength lies in identifying powerful divergences with price (signaling potential trend reversals), it also excels at confirming existing trends and validating shifts in market sentiment. By understanding its calculation, thoughtfully utilizing its signals, and integrating it strategically with price action and other technical analysis tools, you can leverage VPT to enhance your pine script strategies and gain a clearer understanding of smart money activity.