Division by zero error during syntax verification ?

Getting error when trying to use CMF in exploration , anyone know how to resolve?:

function CMF(r1)
{
/* 
Chaikin Money Flow Indicator


*/  

Graph0=Sum(IIf( H>L && V>0,((( C-L )-( H-C )) / ( H-L ))*V,0),r1) / Sum
(V,r1);
dynamic_color = IIf( Graph0> 0, colorGreen, colorRed ); 
Plot(Graph0, "Chaikin Money Flow ("+r1+")", dynamic_color, styleHistogram | styleThick); 
}

image

The error is not in the code you have displayed - its earlier.

If your denominator is zero, you can add small value to denominator such as:

+1e-9

Search the forum (use magnifying glass :mag:in upper right corner) to find already existing solutions first as there are many posts & solutions about - Warning 505

Skate

1 Like

First this is NOT error, this is warning - the difference is crucial: Basics: Error, warning, notice

@skate - the error IS exactly in place when it is displayed, i.e. in this line:

Graph0=Sum(IIf( H>L && V>0,((( C-L )-( H-C )) / ( H-L ))*V,0),r1) / Sum
(V,r1);

This particular division has VOLUME in the denominator. If you apply such indicator to data without volume (such as for example currencies or some indices) then you will divide by zero.

There are also other problems with this code (using obsolete graph0 reserved variable and Plot at the same time - you should NOT do that). If you are using Plot() function (correct) you should NOT be using graph0 reserved variable.

Proper formula would look like this:

function CMF(r1)
{
    nom = Sum( V * ( ( C-L )-( H-C )) / ( H-L ), r1);
    denom = Sum( V, r1 ); // this can be zero if there is no volume in data

    eps = 1e-9; // small value to prevent division by zero in case when volume is zero
	ind = nom / (eps + denom);
	
	dynamic_color = IIf( ind> 0, colorGreen, colorRed ); 
	Plot( ind, "Chaikin Money Flow ("+r1+")", dynamic_color, styleHistogram | styleThick); 
}
3 Likes