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.
Your period is 200 but you have a sum of three values divided by that period. BarCount is 3 but period is 200.
13*3/200 = around 0.2
If you want to reduce period if BarCount is less than period
Filter = 1;
per = 200;
bi = BarIndex();
AddColumn(bi,"Number of Bars",1.0);
AddColumn(MA(C,IIf(bi<per, bi, per)),"MA200");
Or if you want to consider only symbols which have required BarCount (otherwise output being Null).
Filter = 1;
per = 200;
my_ma = IIf(BarCount >= per, MA(C,per), Null);
@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
@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'.
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:
As you know some functions accept variable periods
If you need some function that is not on that list, you can usually easily implement variable-period version using existing functions, for example:
Variable period ROC
function VarROC( array, periods )
prev = Ref( array, -periods );
return 100*(array - prev)/prev;
Variable period RSI
function VarRSI( array, periods )
Chg = array - Ref( array, -1 );
pc = Max( Chg, 0 );
nc = Ma…
@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 )
@Tomasz , I can do it right now.
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.