Cumulative Volume Indicator




I’ve received dozens of requests for this style of Cumulative Volume indicator for TradingView over the past year since I released my RVOL By Time and standard RVOL indicators.

After studying Pine Script deeply and using a bit of old fashioned trial and error over the past few months, I finally got it to work!

And now I’m proud to present the Cumulative Volume indicator you’ve been waiting for.

How The Cumulative Volume Indicator Works

This script adds up the cumulative volume throughout any given trading day (the blue & green volume bars) on any given intraday timeframe on any given market.

Then it compares that cumulative volume value to the total volume of the previous day AND the average cumulative volume of the previous X days for whatever time of day the current bar is printing on.

By default it compares today’s cumulative volume to yesterday’s, but you can set the Lookback higher in the settings menu if desired.

It even takes into consideration random gaps in price action (unlike my RVOL By Time indicator) – so as far as I can tell, it’s as accurate as I can possibly make it.

However it does not work on extended hours for stocks so don’t use it if you have pre-market hours enabled. And there is an inbuilt limitation to how many bars a script can reference on TradingView, so on 1-minute timeframes and lower your Lookback period will be limited and may crash the script if you set it too high.

The PURPLE LINE is the total volume of the previous day.
The GRAY COLUMN is the cumulative volume of the previous day.
The COLORED COLUMN is the current cumulative volume for the current bar.

The BLACK LINE is the Previous Cumulative Volume Line which will draw a black line representing the previous day’s cumulative volume only if today’s CV exceeds the average – this is useful for comparing exactly how far today’s cumulative volume exceeds the previous day’s.

The ORANGE NUMBER is the ratio of today’s CV compared to the average (CV ÷ Average CV).

If the current cumulative volume is less than the previous day’s cumulative volume for that time of day, the bar is blue.

If the current cumulative volume is greater than the previous day’s cumulative volume for that time of day, the bar is dark green.

If the current cumulative volume is greater than the previous day’s total volume, the bar is bright green.

This script also comes with alert functionality.

You can set a Cumulative Volume Alert which will trigger an alert if the current bar’s cumulative volume exceeds the average cumulative volume for that time of day.

You can also set a Total Cumulative Volume Alert which will trigger an alert only if the current bar’s cumulative volume exceeds the total volume of the previous day.

And you can set a CV Ratio Alert which will trigger an alert if today’s CV ÷ Average CV >= Specified Ratio.

Apart from that, the script is fairly self-explanatory so I hope you enjoy using it!

The source code is public & open source so feel free to “borrow” any code you like.

Good luck with your trading, and feel free to drop any suggestions or bug reports and I’ll do my best to help out.

Kind regards,
Matthew


Pine Script Basics Course

Cumulative Volume Source Code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading
// www.pinescriptmastery.com
// Last Updated: 11th November, 2020
// @version=4
study(title="Cumulative Volume+", shorttitle="CV☯", overlay=false, format=format.volume)

// Get user input
lookback = input(title="Lookback Days:", type=input.integer, defval=1)
ratioToTriggerAlert = input(title="CV ratio to trigger alert:", type=input.integer, defval=5)

// Declare our cumulative volume variables
var cumulativeVolume = 0.0
var previousDaysVolume = 0.0

// Declare our bar counting variables
var startHour = -1
var barsSinceLastDay = 0
var barsPerLastDay = 0
extraBar = false

// Set large max bars back buffer size for cumulativeVolume to prevent runtime error
max_bars_back(cumulativeVolume, 5000)

// Check if this bar is the beginning of a new day
newDay = change(time("D"))

// Add 1 to our bar counter
barsSinceLastDay := barsSinceLastDay + 1

// Determine if we need to reset our cumulative volume counter
if newDay
    startHour := hour
    previousDaysVolume := cumulativeVolume
    cumulativeVolume := volume
    barsPerLastDay := barsSinceLastDay
    // If this occurs, then a market opened an hour earlier than usual (eg. extra bar on SPX500 at 17:00 2nd November 2020)
    // By adding +1 to barsPerLastDay we accomodate for that extra bar in our calculations.
    if startHour[1] - startHour == 1
        extraBar := true
        barsPerLastDay := barsPerLastDay + 1
    barsSinceLastDay := 0
else
    cumulativeVolume := cumulativeVolume + volume
    
// Get total volume over our lookback period
totalVolume = 0.0
for i = 1 to lookback by 1
    totalVolume := totalVolume + cumulativeVolume[i * barsPerLastDay]
        
// Calculate average cumulative volume for this bar
avgVolume = totalVolume / lookback

// Calculate relative volume percentage for this bar
volumePercent = volume / avgVolume

// Draw data to the chart
plot(extraBar ? na : avgVolume, color=color.gray, transp=50, style=plot.style_columns, title="Previous Cumulative Volume")    
plot(previousDaysVolume, style=plot.style_line, color=color.purple, title="Previous Day's Volume")
plot(cumulativeVolume, style=plot.style_columns, color=cumulativeVolume > previousDaysVolume ? color.lime : cumulativeVolume > avgVolume ? color.green : color.blue, title="Cumulative Volume")
plot(extraBar or newDay or cumulativeVolume < avgVolume ? na : avgVolume, color=color.black, transp=0, style=plot.style_linebr, title="Previous Cumulative Volume Line")
plot(floor(cumulativeVolume / avgVolume), color=color.orange, transp=100, title="Cumulative Volume Ratio")

// Trigger alert if current cumulative volume exceeds previous day's total volume
alertcondition(cumulativeVolume > previousDaysVolume, title="Total Cumulative Volume Alert!", message="Today's cumulative volume has exceeded yesterday's total volume for {{ticker}}")

// Trigger alert if current cumulative volume exceeds previous day's cumulative volume at the same time of day
alertcondition(cumulativeVolume > avgVolume, title="Cumulative Volume Alert!", message="Today's cumulative volume has exceeded yesterday's cumulative volume at this time of day for {{ticker}}")

// Trigger alert if current cumulative volume ratio exceeds the specified ratio
alertcondition(floor(cumulativeVolume / avgVolume) >= ratioToTriggerAlert, title="CV Ratio Alert!", message="Today's cumulative volume ratio has exceeded {{plot_4}} for {{ticker}}")

Last Updated: 11th November, 2020


Free Premium Charts!

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments