Compounding inside a for loop

Hello there, could you please let me know why the compounding of SumSq += (in the code below) returns null while Diff has a value? I am sure I am making some mistakes there but I am new to AFl and could not figure it out. Thank you in advance.

 for( i = 0; i < Segments; i++ )
			  Diff = RR[i] - AvgRR;
			  SumSq +=  (Diff * Diff);

Your code example is incomplete!
See here

So playing guessing games... either you initialized SumSq with Null or RR has Null element. AvgRR and Segments are unknown too.

BTW, are you sure you need looping? AFAICS, you don't need it.
There are functions for summing up array/array segments: Cum, Sum, SumSince

Also read here:

Because you did NOT initialize SumSq with ZERO and did not check if RR[i] and/or AvgRR is Null !

Proper code would look like this:

SumSq  = 0; // before adding, you have to start with known value
for( i = 0; i < Segments; i++ )
			  Diff = Nz( RR[i] - AvgRR ); // change Nulls to zero
			  SumSq +=  (Diff * Diff);

Thank you Sir.

SumSqp was initialized as I always do in my code but I was only showing a snippet so I keep the question concise. What did the trick was the function Nz(). This Awesome! I am in love with AFL.

Thank you for creating such a wonderful software. I wish I came across Amibroker long ago.

I wish you and yours a wonderful holiday.

Thank you fxshrat for your reply.

I agree with you; my question could have been clearer and more complete.

Also, you're right, in most cases, in AFL one can do without loops. Will try that too. I am just very new to AFL.

Happy holidays to you and yours.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.