if then error when no volume

naquan24

Member
Good afternoon.....I'm having an issue trying to have this formula calculate.

(volume [1] + volume [2] + volume [3] + volume [4] + volume [5]) / 5 > volume.

I know the reason I'm getting this error is because when there is no volume, it doesn't calculate.

Is there way I can say if (volume [1] + volume [2] + volume [3] + volume [4] + volume [5]) / 5 = ERROR then (volume [1] + volume [2] + volume [3] + volume [4] + volume [4]) / 4 OR can I tell it to remove the one with no volume out of the equation?
 
Solution
I was trying to indicate a high volume...but in the morning time, the system uses the premarket volume in the calculation which is extremely low or in some cases, none at all.....so using a 5 minute schedule, when I say calculate volume [3] at 9:30am, its going into the premarket and if there is no volume 15 minutes into the premarket, then it was returning an error and not calculating at all.

please post your complete code and tell us what stocks you are looking at.

if you use an offset of 3, tos should go back 3 bars and read data. it is possible, going back 3 bars doesn't exist at the time period you think it should be, if some time periods had no trades,

this post is a good example of why we want people to post the...
Looking at what your solution for error trapping is, I'm not certain it will do better than the first... I can't think of many places there will be volume for the last 4 but not 5 bars... but that's up to you.

To solve the 'what to do when there's no data' question, I suggest using the IsNaN function like so:
Code:
def more_than_average_volume = 
    if !isNaN(VOLUME) then 
        if Average(VOLUME, 5) < VOLUME then 1 
        else 0 
    else 0;

The isNaN() function (https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN) is used in this case with a bang before it (!) so that it reads "if volume is NOT not-a-number then do the calculation else make it zero".

I know, I know don't use no double negatives, but whoever wrote programming languages like this ignored usual grammar rules for the sake of logic. Grammar and Logic never really saw eye-to-eye on much.

To this part of your question:
OR can I tell it to remove the one with no volume out of the equation?

There really is no way to do this in Thinkscript directly. You can go to the trouble of creating a new series with volume information in it and then use that series for your calculations:

Code:
def myVolume = if !isNaN(VOLUME) then VOLUME else 0;
def condition = if (myVolume [1] + myVolume [2] + myVolume [3] + myVolume [4] + myVolume [5]) / 5 > myVolume then 1 else 0;
but be careful because if the last (most recent) myVolume is 0 you'll have a divide by zero result on your hands.

Hope that helps,

mashume
 
If you could provide an example of a symbol, with the date, time, and aggregation, of where this error is happening it would help a lot.

Something like this, I think, should be able to dynamically remove the vacant volume bars from the equation, assuming they are intermittent.

Ruby:
def Length = 5;
def Vol = if !Volume then No else Volume;
def NoVolLength = Length - sum(!Vol,Length);
def AvgVol = if !NoVolLength then 0 else sum(Vol,Length) / NoVolLength;
def Result = AvgVol > Fold Index = 0 to Length With V = Double.NaN while isNaN(V) do GetValue(Volume,Index);
# or you could just do
#Def Result = AvgVol > Vol
# depending on how you wish to go about it

But, I would need a specific sample to truly test it on.
 
Good afternoon.....I'm having an issue trying to have this formula calculate.

(volume [1] + volume [2] + volume [3] + volume [4] + volume [5]) / 5 > volume.

I know the reason I'm getting this error is because when there is no volume, it doesn't calculate.

Is there way I can say if (volume [1] + volume [2] + volume [3] + volume [4] + volume [5]) / 5 = ERROR then (volume [1] + volume [2] + volume [3] + volume [4] + volume [4]) / 4 OR can I tell it to remove the one with no volume out of the equation?


what stocks and what timeframes are you looking at to see errors ?
if volume is 0, adding 0 shouldn't cause an error.

i usually don't analyze a question, i just read it and then try to make a solution.
so when you said there is an error with the formula, i put that notion in my head and made something to work around it.
for a minute, i was thinking that at bar#1 , trying to read volume[1] would cause an error, but it doesn't.
when i was done and looked at the values in my test bubbles, i didn't see any errors....


here is a study with your code and 2 other verions for calculating an average.

ver0 your original formula
ver1 checks each variable to see if it is an error. if an error, then = 0.
ver2 uses a fold loop, to be able to look back at any quantity of bars and read data until it reads an error.

it draws 3 bubbles under the bars. each bubble is a different set of formulas to find an average, ver0, ver1, ver2.


Ruby:
# ver0 -----------------------------------

def v = volume;
def vavg0 = (v[1] + v[2] + v[3] + v[4] + v[5]) / 5;
def isvabove0 = if vavg0 > volume then 1 else 0;

addchartbubble(1,low,
"ver 0" + "\n" +
vavg0 + " avg\n" +
volume + "  v\n" +
isvabove0 + " above?"
, color.cyan, no);


# ver1 -----------------------------------

#you could error trap each variable, then add them up and calculate the average.
 #def v = volume;
def barsback = 5;
def v1 = if isnan(v[1]) then 0 else v[1];
def v2 = if isnan(v[2]) then 0 else v[2];
def v3 = if isnan(v[3]) then 0 else v[3];
def v4 = if isnan(v[4]) then 0 else v[4];
def v5 = if isnan(v[5]) then 0 else v[5];
def vsum1 = v1+v2+v3+v4+v5;
def vavg1 = floor(vsum1/barsback);
def isvabove1 = if ( vavg1 > v ) then 1 else 0;

addchartbubble(1,low,
"ver 1" + "\n" +
vsum1 + " sum\n" + 
vavg1 + " avg\n" +
volume + "  v\n" +
isvabove1 + " above?"
, color.yellow, no);


# ver2 -----------------------------------

#def v = volume;
#input barsback = 5;
def bn = barnumber();
def vsum2;
if bn > barsback then {
  vsum2 = sum( v[1], barsback);
} else {
  vsum2 = fold i = 1 to barsback+1
  with p
  while !isnan(v)
  do p + getvalue(v, i);
}

def vavg2 = floor(vsum2/barsback);
def isvabove2 = if ( vavg2 > v ) then 1 else 0;

addchartbubble(1,low,
"ver 2" + "\n" +
vsum2 + " sum\n" + 
vavg2 + " avg\n" +
volume + "  v\n" +
isvabove2 + " above?"
, color.green, no);
#
 
Looking at what your solution for error trapping is, I'm not certain it will do better than the first... I can't think of many places there will be volume for the last 4 but not 5 bars... but that's up to you.

To solve the 'what to do when there's no data' question, I suggest using the IsNaN function like so:
Code:
def more_than_average_volume =
    if !isNaN(VOLUME) then
        if Average(VOLUME, 5) < VOLUME then 1
        else 0
    else 0;

The isNaN() function (https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN) is used in this case with a bang before it (!) so that it reads "if volume is NOT not-a-number then do the calculation else make it zero".

I know, I know don't use no double negatives, but whoever wrote programming languages like this ignored usual grammar rules for the sake of logic. Grammar and Logic never really saw eye-to-eye on much.

To this part of your question:


There really is no way to do this in Thinkscript directly. You can go to the trouble of creating a new series with volume information in it and then use that series for your calculations:

Code:
def myVolume = if !isNaN(VOLUME) then VOLUME else 0;
def condition = if (myVolume [1] + myVolume [2] + myVolume [3] + myVolume [4] + myVolume [5]) / 5 > myVolume then 1 else 0;
but be careful because if the last (most recent) myVolume is 0 you'll have a divide by zero result on your hands.

Hope that helps,

mashume
I was trying to indicate a high volume...but in the morning time, the system uses the premarket volume in the calculation which is extremely low or in some cases, none at all.....so using a 5 minute schedule, when I say calculate volume [3] at 9:30am, its going into the premarket and if there is no volume 15 minutes into the premarket, then it was returning an error and not calculating at all.
 
I was trying to indicate a high volume...but in the morning time, the system uses the premarket volume in the calculation which is extremely low or in some cases, none at all.....so using a 5 minute schedule, when I say calculate volume [3] at 9:30am, its going into the premarket and if there is no volume 15 minutes into the premarket, then it was returning an error and not calculating at all.

please post your complete code and tell us what stocks you are looking at.

if you use an offset of 3, tos should go back 3 bars and read data. it is possible, going back 3 bars doesn't exist at the time period you think it should be, if some time periods had no trades,

this post is a good example of why we want people to post the complete study, not just a line or 2.
i am pretty sure something else is causing your code issues, because what you are saying doesn't seem right to me.
a candle can't exist with 0 volume. every candle has some volume. if it didn't, the candle wouldn't exist. in after hours times,
the quantity of AH candles will vary on every stock on every day. you can't look back x bars into premarket, on multiple days, and read from the same time period.
anyway, what i am saying is, if you use a positive offset on a variable, it should read a valid value. unless the offset is > 2000, which is the limit.

if you are using some method to read a variable, from a specific time period, that could cause problems in after hours.

here is a test code, that looks for 0 volume bars and bars at 9:15am.
on low daily volume stocks, i don't see a bar at 9:15. i do with csco, daily vol > 10m.
i don't see any errors with this.

i used the max offset and it read data.

Code:
# chg bubble to green if 9:15 time exists
# chg bubble to yellow if volume is 0

def abc = if secondstillTime(0915) == 0 then 1 else 0;

def v= volume;
def off = 1999;
addchartbubble(1, v[off], v[off] + "\n" + abc, ( if abc then color. green else if v[off] == 0 then color.yellow else color.gray), yes);
#
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
293 Online
Create Post

Similar threads

Similar threads

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