Error 33 - custom function

function selective_average(src,len)
    sum1=0.0; count1=0;
        if (not IsNull(src[BarCount-i]))
            count1 = count1 + 1;
        if (count1==len)
    mean1 = sum1/len;
    return mean1;

I'm new to AFL. Above is my AFL code.

I get Error 33. "The identifier is already in use. You attempted to assign value to the identifier representing a function. If you want to return value from a function, you should use RETURN statement instead."

Can someone help identify what am I doing wrong?

The function as it is posted above does NOT generate any error message. However, the way it is written it does not make sense to me. Maybe you explain in plain English what you really want to achieve.

I'm trying to create a moving average, where null values are omitted. Say if an array is [10, 5, null, 15], the answer should be (10+5+15)/3 = 10

I identified the error. I was trying to use the same identifier for a different function and it's return variable. It was calling the above function for a calculation and the error got triggered mentioning this function.

Thanks Tomasz for the help. I'm becoming a huge fan of amibroker every further day. It's less than a week since I started. I wish I tried amibroker long back.

Writing a loop for that, is unnecessary, use Nz() instead:

selective_average = MA( Nz( src, 0 ), len );

P.S. Categorized list of functions will help you in the long run along with the User Guide.


thanks for this!

Also, I'm looking for an equivalent function to pinescript's fixnan(x). "For a given series replaces NaN values with previous nearest non-NaN value."

I couldn't find anything similar in the forum or the documentation. Can you help?

That is not omitting... because then zero is included into MA calculation.

Omitting values is achieved by e.g SparseCompress (and then expanding via SparseExpand again).


len = 20;
array = C;
cond = NOT IsNull(array);

sc_array = SparseCompress(cond, array);
    sc_ma = MA(sc_array, len);
se_ma = SparseExpand(cond, sc_ma);

selective_average = se_ma;
// or if connecting 
// selective_average = Valuewhen(cond, se_ma);

Thanks @fxshrat
This worked. Can you help me write fixnan() as well? "For a given series replaces NaN values with previous nearest non-NaN value."

Do you mean Nan or Null?

x = ValueWhen(NOT IsNan(array), array);

x = ValueWhen(NOT IsNull(array), array);

Thanks again. I meant Nan only. This will help.