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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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)  );
 
I ran the code. For some reason, it does not want to accumulate volume. The distribution volume looks good.

P7JnHEY.png


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:

saq0dIq.png


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.
 
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.

HRfwG7O.png



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:

jWuMrJt.png


Sbbtvm6.png

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:
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)

zwuRNUz.png


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:

jFZRGat.png



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?

Start a new thread and receive assistance from our community.

87k+ Posts
352 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top