Get value of MA (200) when not enough bar

Hi All,

I have the same situation when I tried to get the value of MA (200) of ticker which has less than 200 bars. I read this suggestion form @fxshrat in this post. But AB not allow to use EMA with array

Moving Average is calculating when there are not enough bars

here is my script :

bi = BarIndex () -1 ;
per = 200 ;

periods = IIf ( bi <= per ,bi,per );

Ema1 = EMA (C,periods);

printf ("\n ema1 " + ema1);
printf ("\n Barcount" + BarCount);
printf ("\n Barindex" + BarIndex());


Can you please to help.

You are not doing it the correct way as per @fxshrat's code. Look more closely at the code.

@TrendSurfer thank for your time.

Absolutely, I did read word by word . The problem is it can go in MA function but for EMA it doesn't work. So that is my early question in the first post.
Please to see the screen shot, the part come with MA is OK, EMA has error


1 Like

@topfun, actually after looking at it more closely the issue is with using 'EMA'.

It appears either your way or @fxshrat way the code will work with 'MA' or 'WMA' but not with 'EMA'.

1 Like

If you need array period you may use AMA function.

periods = 20;

// THIS IS EMA using AMA function
x = AMA(C, 2 / (periods + 1));

@topfun, I suggest also to study this important past thread by @Tomasz:


Thanks @TrendSurfer @fxshrat and @beppe for your time.

Have a nice weekend


Instead of EMA that accepts only constant period, you should use AMA that accepts variable period:

AMA( array, 2/(period+1) )

is equivalent of

EMA( array, period )


Thank you @Tomasz , I can do it right now.

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