could someone help with divide by 0 error?

cboss

Newbie
Messages
6
Likes
0
I've been searching for an EL version of Chaikin Money Flow, I especially like the one in IB's Trader Workstation and have been trying to duplicate that.

The code below is the best I've come up with so far, and most of the time it works great, but randomly I get divide by 0 errors in the event log and the indicator will not display. Sometimes i can get it back by deleting all cached data and reloading the page, but it is a big problem for it to not be available.

I would be incredibly grateful if any of you could take a look through the code below and point out what the problem is - thank you in advance!!

Chris



{ Chaikin Money Flow }


inputs: len(21);

var: den(0), den2(0);

// Currencies don't always track volume, so this sets the value to zero if there isn't volume
den = summation(V,len);
den2 = H-L;

if den = 0 or den2 = 0 then begin
value1 = 0;
end
else begin
value1 = summation(((( C-L ) - ( H-C )) / ( H-L )) * V,len) / den;
end;

if value1 > 0 then begin
plot1(value1, "CMF_Hist", blue);
plot2(value1, "CMF_Line", blue);
end
else begin
plot1(value1, "CMF_Hist", red);
plot2(value1, "CMF_Line", red);
end;

plot5(0, "Mid");
 
I don't know this language but have you tried testing for 0 first and setting it to either 1 or null
 
sorry, i am not sure what that means...I tried the following changes and this did not work

{ Chaikin Money Flow }


inputs: len(21);

var: den(0), den2(0), den3(0);

// Currencies don't always track volume, so this sets the value to zero if there isn't volume
den = summation(V,len);
den2 = H-L;

if den = 0 or den2 = 0 then begin
den3=.00000001;
end
else begin
value1 = summation(((( C-L ) - ( H-C )) / ( H-L )) * V,len) / den3;
end;

if value1 > 0 then begin
plot1(value1, "CMF_Hist", blue);
plot2(value1, "CMF_Line", blue);
end
else begin
plot1(value1, "CMF_Hist", red);
plot2(value1, "CMF_Line", red);
end;

plot5(0, "Mid");
 
var: den(0), den2(0), den3(0);

den = summation(V,len);
den2 = H-L;

if den = 0 or den2 = 0 then begin
den3=.00000001;
end
else begin
value1 = summation(((( C-L ) - ( H-C )) / ( H-L )) * V,len) / den3;
end;

You're initialising den3 = 0 then setting it according to tests against den and den2 either being zero. What if neither den or den2 are zero? Then den3 will be 0 and you will get a divide by zero error
 
could you suggest alternative coding to the original that would fix it? i really know nothing about programming, although this language seems pretty straightforward.

anything would be greatly appreciated!!

Chris
 
HI,
If neither den or den2 is zero then den3 will be set to zero.
So the Error. There seems to be some logic missing when you tried to duplicate it from IB's workstation.

Can you post the IB's code for this indicator, so that we can help you to achieve it in Tradestation...

HTML:
Note:- Please post the code with the code Tag.
Thanks,
Mani.
 
Last edited:
I have this Chaikin indicator in my computer, I don't know if it is the same thing.

Code:
// Chaikin Osc
//
// more indicators at:
// http://www.tradersXchange.com/
//


inputs:
	FastLength( 3 ),
	SlowLength( 10 ),
	AlertLength( 14 ),
	ColorNormLength( 14 ),                                                       
	                                                                             
	UpColor( Yellow ),                                                             
	                             
	DnColor( Red ),                                                             
	                             
	GridForegroundColor( Black ) ;                                                
	                                                                              
variables:
	var0( 0 ),
	var1( 0 ),
	var2( 0 ),
	var3( 0 ) ;

{---------- end of variables ----------}

if CurrentBar = 1 then
	var0 = GetAppInfo( aiApplicationType ) ;
	
if BarType >= 2 then                              
	var2 = Volume 
else                                                                              
                                                                                 
	var2 = Ticks ;

var1 = ChaikinOsc( var2, FastLength, SlowLength ) ;

Plot1( var1, "ChaikinOsc" ) ;
Plot2( 0 , "ZeroLine" ) ;

condition1 = UpColor >= 0 and DnColor >= 0 ;                                           
if condition1 then
	begin
	var3 = NormGradientColor( var1, true, ColorNormLength, UpColor,
		 DnColor ) ;
	if var0 = 1 then                                
		SetPlotColor( 1, var3 )
	else if var0 > 1 then                                 
		begin
		SetPlotColor( 1, GridForegroundColor ) ;
		SetPlotBGColor( 1, var3 ) ;
		end ;
	end ;
		
                  
condition1 = LowestBar( C, AlertLength ) = 0 
and LowestBar( var1, AlertLength ) > 0;

if condition1 
 then 
	Alert( "Bullish divergence - new low not confirmed" ) 
else 
begin 
condition1 = HighestBar( C, AlertLength ) = 0 
and HighestBar( var1, AlertLength ) > 0 ;
if condition1 then
	Alert( "Bearish divergence - new high not confirmed" ) ;
end;
 
Last edited:
HI,
If neither den or den2 is zero then den3 will be set to zero.
So the Error. There seems to be some logic missing when you tried to duplicate it from IB's workstation.

Can you post the IB's code for this indicator, so that we can help you to achieve it in Tradestation...

HTML:
Note:- Please post the code with the code Tag.
Thanks,
Mani.

Mani,

IB will not give me the code to their version. The closest I have come is the code I posted originally above, and when it is working it charts out the same in TS as it does in IB's trader workstation.

It's just that randomly i get divide by 0 errors and the indicator disappears. For instance right now it will not load on my hourly ZN chart, but is working on an 80 tick chart of the same.

Chris
 
This is painful. I now nothing about tradestation or programming, but...

if den = 0 or den2 = 0 then begin ---- is giving you divide by zero errors then perhaps....

if den > 0 or den2 = 0 then begin.....will not.......
 
This is painful. I now nothing about tradestation or programming, but...

if den = 0 or den2 = 0 then begin ---- is giving you divide by zero errors then perhaps....

if den > 0 or den2 = 0 then begin.....will not.......


nothing painful about it....

the summation/divide function looks OUTSIDE of the bar into a RANGE that encompasses previous bars
... thus a divide by zero error can still occur if there is a zero in the previous bars.


let me know if you need further explanation.
 
Last edited:
Here is a screenshot of the error I am getting, "floating point invalid calculation".

The even line reads "floating point invalid numbers in calculation. This error sometimes indicates a division by zero error with a zero also in the numerator. Incorrect example: value1 = value2/value3. correct example: value1 = IFF(value 2=0,0, value2/value3);
 

Attachments

  • floating point.JPG
    floating point.JPG
    284.2 KB · Views: 1,262
Yeah, it's den3 that's the divisor not den anyway.

I guess the summation function isn't the culprit, so the only other thing I could suggest would be when the High and the Low are the same value.
 
Quick tip create a Div function to do the check, that way you can reuse it when ever you see this problem, something like this pesuedo code

double Div(double a, double b)
{
if a = 0 or b = 0
return 0;
return a / b;
}

this gets past the error and gives you a condition you can test.

double z = Div(a, b);
if z = 0 then MessageBox("Error")
 
ain't you dividing by H-L there?

"value1 = summation(((( C-L ) - ( H-C )) / ( H-L )) * V,len) / den3;"

You reckon high can't equal low for some reason?
 
Yeah, it's den3 that's the divisor not den anyway.

I guess the summation function isn't the culprit, so the only other thing I could suggest would be when the High and the Low are the same value.

LOL - didn't see that you'd put this in.

I reckon this is it - 100%.
 
Top