Is there a way to make Cumulative Volume Delta code below work in a scan?

New member
# Cumulative Volume Delta
#
# The length of the accumulation is user controlled. The cumulative bar
# is the sum of the deltas for the past 10 bars. Change that length to
# 252 (a year in days) then plot something like AAPL. Very interesting.
#
# LongShort
# 5.7.2019

declare lower;

input length = 10;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);
def Delt = buying - selling;

plot Delta = Delt;
Delta.AssignValueColor(if Delta > 0 then Color.GREEN else Color.RED);
Delta.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Delta.hide();

plot zero = 0;
zero.setDefaultColor(Color.BLUE);

plot CumulativeVolumeDelta = sum(Delta,length);
CumulativeVolumeDelta.AssignValueColor(if CumulativeVolumeDelta > 0 then Color.GREEN else Color.RED);
CumulativeVolumeDelta.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

# End Code

Solution
The error I receive is " Exactly one plot expected"
try this
declare lower;

input length = 10;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);
def Delt = buying - selling;

def Delta = Delt;

plot CumulativeVolumeDelta = sum(Delta,length);
I don't see why not, but what exactly would you be scanning for?

I don't see why not, but what exactly would you be scanning for?
It shows error " Exactly one plot expected". I would like to scan for stocks when Delta is above zero line for any time period.

Last edited:
The error I receive is " Exactly one plot expected"
try this
declare lower;

input length = 10;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);
def Delt = buying - selling;

def Delta = Delt;

plot CumulativeVolumeDelta = sum(Delta,length);

You would just have to delete everything underneath `plot Delta = Delt;`. But you would still likely need some type of condition depending on what you're after. Like, is it positive or negative, or over a certain level, and so on.

try this
declare lower;

input length = 10;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);
def Delt = buying - selling;

def Delta = Delt;

plot CumulativeVolumeDelta = sum(Delta,length);
@samer800 Thank a lot. However, this brings mixed result.

try this
declare lower;

input length = 10;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);
def Delt = buying - selling;

def Delta = Delt;

plot CumulativeVolumeDelta = sum(Delta,length);
Hi, instead of 'length' how can i get this cumulative delta for each regular trading day, so if I make a backtest of last 30 days, I can see the cumulative for each day starting a 9:30AM NYSE open and ending at 4:00PM ET? maybe using aggregation.period, I don't know. Thx.

Not the exact question you're looking for?

87k+ Posts
283 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?