Cause of Errnous EMA Return Value

Hi ,

I have an issue , where the code below, EMA function below returns -1.#J , in exploration mode, and I am not quite sure what is causing it.

	hhPer = HHV(H,PeriodRange);
        myC = C;
	Range1 =  hhPer - myC;
	Range2  = (HHV (H,PeriodRange) -LLV (L,PeriodRange));
	Range = (Range1/Range2)*100;
	EMA1 = EMA(Range,PeriodEMA1);

Thank you

That normally occurs when you divide by zero. So at a guess, your Range2 is returning a zero value for some or all of your bars. Perhaps you’re seeing it just at the start of the time series where there’s fewer bars than your PeriodRange, so it’s not able to calculate it yet?

alright let me chek the output of each variable. much appreciated @HelixTrader.

Yes you’were right , i ended up doing the following to negate 0 at the denominator

Range3 = IIf(Range2>0,Range2,1000000000);
Range = (Range1/Range3)*-100;

thank you @HelixTrader

1 Like

No problem @fourier. Another approach is just to add a very tiny amount far to the right of the decimal point, to the denominator so it avoids it resolving as an absolute zero value.

Range = (Range1 / (Range2 + 1e-9)) * 100;

Or you can wrap divisions inside Nz() function: Nz( Range1/Range2 );

2 Likes

@Tomasz,

Why should functions like HHV() , LLV() produce values other than null for bars before the defined period
its like having the MA(C, pds) returning MA ( C, n ) for each bar when n< pds
it just causes confusion, false signals, and extra line of code to fix .

bi =  BarIndex(); 
hh = HHV ( H , pds =  10) ;
//hh = IIf ( BarIndex () >= pds -1, hh, Null ); 
Buy = High > Ref ( hh , -1 );  
Filter = 1 ; 
AddColumn ( BarIndex(), "barindex" );
AddColumn ( hh, "hhv");
AddColumn ( buy, "buy");

image