# Why variable getting NAN value

Hi
I am getting NAN value for a variable ccima which is defined as follow
kcci = cci(10);
ccima = ema(kcci,5);

The other part of the program worked ok and only this variable is causing problem. I found this out when i debug the program and display the variable using writeval(ccima,1.0).

The displayed value was {NAN}

I appreciate to understand

1. what the value meant in AFL
2. what are the possible reason causing this

Any tip to debug the problem as i do not believe it was a syntax problem.

Thanks a lot for help,

1 Like

As an added note re the variable ccima was only referred to in formulas and no effort was made to modify it in any way.

NAN is the result of division by zero. CCI uses "Mean deviation" in denominator. If it is zero you will get Nan (for example if prices don't move for 10 bars). 3 Likes

Dear Thomaz,
As usual, you are dead right again! Thank you so much for the prompt reply too.
When i change kcci=cci(11) problem is gone for all tickers for all bars. Anything above 10 seems working perfectly as well.

I however still cannot understand the following and hope for your guidance:

1. the kcci and ccima as an indicator display perfectly even with cci period <=10 .
2. i am using popular etfs such as spy and xle etc. 1hr chart to test my code so 10 zero bars very seldomly happens.

Many thanks for the great help anyways.

"Flat" periods can happen if use Foreign or Pad&align because bars without data are padded with previous (existing) bar value. This can create bars when data do not change for prolonged periods. CCI would return NAN because denominator is zero. If NAN occur in CCI output, then they will cause NANs futher along the way especially if you are using EMA (or any recursive filter). EMA is infinite impulse response filter and as such uses ALL past bars to calculate value (even if it says 10 bars).

You can prevent propagation of Nans using Nz function

``````kcci = Nz( CCI(10) ); // Nans to zero
``````
2 Likes

Tomasz,
Thank you for teaching me. You are such a great teacher.

1 Like