Having trouble with volume counter. Wrong logic?

jackbravo

New member
Hi folks, I'm new to the forum and relatively new to thinkscript. I saw this awesome library here and decide to try out a few ideas.

Code:
``````#Counter
declare lower;
Def event = close >= close[1];
Def count = if event then count[1] + 1 else count[1];
Plot total = count;``````

My idea was to keep a running total of the accumulation and distribution volume based on the arbitrary measure that a green day volume is accumulation and red day volume is distribution. But I only want to add the % volume that is above/below the average. So for example if today is a green day, and volume is 50% of average, I only want to add 50% of the volume to the counter.

The logic I am trying to code is the following:
• Accumulation volume = if close > open, then add today's volume * (today's volume / average volume) to UpVolume counter
• Distribution volume = if close < open, then add today's volume * (today's volume / average volume) to DnVolume counter
Code:
``````declare lower;

def AvgVolume = average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #today's relative volume to average
def NewVolume = volume * RelVolume; #today's volume * Relative volume ratio

def UpVol = 0 #up volume counter initiated at 0
def DnVol = 0; #down volume counter initiated at 0

plot AccumVol = if close > open
then NewVolume+ UpVol[1] else UpVol[1] + 0; #if today is green, add today's Upvolume to yesterday's UpVol ounter, else add 0

plot DistVol = if Close < open
then DnVol + DnVol[1] else DnVol[1] +0; #if today is red,  add today's Downvolume to yesterday's DnVol counter``````

I think my definition of UpVol and DnVol is messed up. Or logic is messed up. Thanks for any help!

Last edited:
that can be done i beleive

Ok cool. What's happening now is not correct. It seems to be subtracting volume from the counter. I don't understand what's happening. Should only be positive values

Code:
``````declare lower;

def AvgVolume = average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #volume relative to average volume
def UpVol = volume * RelVolume; #up volume counter
def DnVol = volume * RelVolume; #down volume counter
def todays_vol= (volume(period = AggregationPeriod.day));

#Accumulation volume = if close > open, then add today's volume * (today's volume / average volume) to UpVolume counter
def Accumulation_volume = if close > open then ((todays_vol * (todays_vol / AvgVolume)) + UpVol) else 0;
#Distribution volume = if close < open, then add today's volume * (today's volume / average volume) to DnVolume counter
def Distribution_volume = if close < open then ((todays_vol * (todays_vol / AvgVolume)) + dnVol) else 0;

AddLabel (yes, "Accumulation_volume " +  (Accumulation_volume)  );
AddLabel (yes, "Distribution_volume " +  (Distribution_volume)  );``````

Wow, thank you for the time to do the whole thing! I was just looking for a pointer in the right direction. This is much better @XeoNoX! Thanks.

I ran the code. For some reason, it does not want to accumulate volume. The distribution volume looks good.

Code:
``````declare lower;

def AvgVolume = average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #volume relative to average volume
def UpVol = volume * RelVolume; #up volume counter
def DnVol = volume * RelVolume; #down volume counter
def todays_vol= (volume(period = AggregationPeriod.day));

#Accumulation volume = if close > open, then add today's volume * (today's volume / average volume) to UpVolume counter
def Accumulation_volume = if close > open then ((todays_vol * (todays_vol / AvgVolume)) + UpVol) else 0;
#Distribution volume = if close < open, then add today's volume * (today's volume / average volume) to DnVolume counter
def Distribution_volume = if close < open then ((todays_vol * (todays_vol / AvgVolume)) + dnVol) else 0;

AddLabel (yes, "Accumulation_volume " +  (Accumulation_volume)  );
AddLabel (yes, "Distribution_volume " +  (Distribution_volume)  );``````

I messed with the code a bit, to set the initial counter to 0. Here are the results:

Code:
``````declare lower;

def AvgVolume = average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #volume relative to average volume
def UpVol = 0; #up volume counter
def DnVol = 0; #down volume counter
def todays_vol= (volume(period = AggregationPeriod.day));

#Accumulation volume = if close > open, then add today's volume * (today's volume / average volume) to UpVolume counter
def Accumulation_volume = if close > open then ((todays_vol * (todays_vol / AvgVolume)) + UpVol) else 0;
#Distribution volume = if close < open, then add today's volume * (today's volume / average volume) to DnVolume counter
def Distribution_volume = if close < open then ((todays_vol * (todays_vol / AvgVolume)) + dnVol) else 0;

AddLabel (yes, "Accumulation_volume " +  (Accumulation_volume)  );
AddLabel (yes, "Distribution_volume " +  (Distribution_volume)  );``````

What I would like to do eventually if this part works is to subtract Accumulation - Distribution volume counters, or maybe create an oscillator, to judge the strength of the stock.

accumulation will work if the the current candle's close > open .... thats what you stated.

I guess what I was intending is the counter to be persistent for all the candles in the chart. I'd like to add all the volumes together of all the green candles in the chart. So there would be a running total. I went back through the code, and actually what I want to see is the counter. It is showing 0. I want to add today's accumulation_volume to the counter to keep a running total.

Code:
``````declare lower;

def AvgVolume = average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #volume relative to average volume
def UpVol = 0; #up volume counter
def DnVol = 0; #down volume counter
def todays_vol= (volume(period = AggregationPeriod.day));

#Accumulation volume = if close > open, then add today's volume * (today's volume / average volume) to UpVolume counter
def Accumulation_volume = if close > open then ((todays_vol * (todays_vol / AvgVolume)) + UpVol) else UpVol + 0;
#Distribution volume = if close < open, then add today's volume * (today's volume / average volume) to DnVolume counter
def Distribution_volume = if close < open then ((todays_vol * (todays_vol / AvgVolume)) + dnVol) else DnVol + 0;

AddLabel (yes, "UpVol " +  (UpVol)  );
AddLabel (yes, "DnVol " +  (DnVol)  );``````

I changed the code above to show UpVol and DnVol. If Accumulation_volume, then add to UpVol, else add 0 to UpVol. Yet it still display 0, instead of keeping a running total of all the past days with positive volume.

My intention is this:

Thanks for any help! I find counters to be very difficult. Or maybe counter is not the right tool to keep a running total, I'm not sure.

Last edited:
i have no clue how they are calculating the numbers, i can count and see the 10 reds but i dont know what they represent. if you happen to get the full formula or explanation of the study it can be coded. you are having a issue with the "counter" because "upcounter" and "dncounter" and possibly "counter" or "upcounter running total" and ""upcounter running total"" are not being defined/explained. it sounds like upcounter and downcounter and counter are 3 seperate functions. you are saying counter but mean upcounter or downcounter then you should say upcounter and downdounter as the way you describe makes counter appears like a separate function (maybe it is, i have no clue). The way the picture describes makes "DNcounter running total" and "upcounter running total" appear as separate functions as well.

Last edited:
@XeoNoX It's a study idea that I came up with. Clearly, I could not describe it unambiguously. I'll try again

Theory: Stocks with more volume on up days than down days are stronger.

Indicator:

1. Have a variable that keeps running total of upvolume
• day close greater than open
• volume from that day times volume from that day divided by average volume equals upvolume
2. Have a variable that keeps running total of downvolume
• day close less than open
• volume from that day times volume from that day divided by average volume equals downvolume
Logic:
• UpVolume counter : variable that keeps running total of upvolume
• DnVolume counter: variable that keeps running total of downvolume
Day's relative volume: volume * (volume / average volume)

If day close > open, then volume * (volume/avg volume) + UpVolume counter (accumulation)
If day close < open, then volume * (volume/avg volume) + DnVolume counter (distribution)

Code:
``````#Upvolume counter: variable that keeps running total of upvolume
UpVol = 0; #set inititial counter at 0

#DnVolume counter: variable that keeps running total of downvolume
DnVol = 0; #set initial counter at 0

#Day's relative volume
AvgVolume = average (volume, 10);
RelVol = volume * volume / AvgVolume;

#If day's close > open, then add the day's relative volume to UpVolume counter
def accumulation = if close > open then RelVol + UpVol;

#If day's close < open, then add the day's relative volume to DnVolume counter
def distribution = if close < open then RelVol + DnVol;``````

Last edited:
If day close > open, then volume * (volume/avg volume) + UpVolume counter (accumulation)
If day close < open, then volume * (volume/avg volume) + DnVolume counter (distribution)

Do you mean ?

(accumulation) = day close > open, then volume * (volume/avg volume) + UpVolume counter
(distribution) = day close < open, then volume * (volume/avg volume) + DnVolume counter

or do you mean

UpVolume counter multiplied by accumulation
DnVolume counter multiplied by distribution

or do you mean

upvolume counter is accumulation
dnvolume counter is distribution

Apologies. The parentheses were confusing.

accumulation = day close > open, then volume * (volume/avg volume) + UpVolume counter
distribution = day close < open, then volume * (volume/avg volume) + DnVolume counter

I would like to add the accumulation to the UpVolume counter if the day is bullish, and the distribution to the DnVolume counter if the day is bearish.

Perhaps I should use another word rather than accumulation/distribution. The name of the variable doesn't matter to me. In the end I just want to add the day's relative volume to the UpVolume counter if the day is bullish. And add the day's relative volume to the DnVolume counter if the day is bearish.

Here is a chart example:

I just made an indicator with only the relative volume variable. What I would like to do is keep a running sum all the blue volume bars into the Upvolume counter, and all the red volume bars into the Dnvolume counter.

Last edited:
You're saying:

accumulation = day close > open, then volume * (volume/avg volume) + UpVolume counter
distribution = day close < open, then volume * (volume/avg volume) + DnVolume counter

then you state...

I just made an indicator with only the relative volume variable. What I would like to do is keep a running sum all the blue volume bars into the Upvolume counter, and all the red volume bars into the Dnvolume counter.

You are showing me a picture with what appears to be RELATIVE VOLUME however your example formula equation uses avg volume

There is a difference between average volume and relative volume, you picture is different from what your formula is stating... and there bye i will give you a script for what you previously defined as average vol.

As per your defined AVERAGE VOL

Code:
``````def AvgVolume = Average (volume, 10); #average volume
def RelVolume = volume / AvgVolume; #volume relative to average volume

def todays_vol = (volume(period = AggregationPeriod.DAY));

#accumulation = day close > open, then volume * (volume/avg volume) + UpVolume counter
def A_volume = if close > open then ((todays_vol * (todays_vol / AvgVolume)) ) else A_volume[1];
#distribution = day close < open, then volume * (volume/avg volume) + DnVolume counter
def D_volume = if close < open then ((todays_vol * (todays_vol / AvgVolume)) ) else D_volume[1];

#adds all the bars that you defined as accumulation distribution on the chart
def Upvol =  round(totalsum(A_volume),0);
def Dnvol =  round(totalsum(D_volume),0);

AddLabel (yes, "Upvol " +  Upvol  );
AddLabel (yes, "DnVol " +  Dnvol  );``````

I just made an indicator with only the relative volume variable. What I would like to do is keep a running sum all the blue volume bars into the Upvolume counter, and all the red volume bars into the Dnvolume counter.

You will need to define upvolume and dnvolume and then use sumtotal to add all the charted volume bars that meet your defined syntax.

good luck

Last edited:

Not the exact question you're looking for?

87k+ Posts
240 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?