topfun
November 13, 2021, 8:52am
#1
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.
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);
Add…
topfun
November 13, 2021, 11:14am
#4
@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
fxshrat
November 13, 2021, 11:41am
#6
If you need array period you may use AMA function.
periods = 20;
// THIS IS EMA using AMA function
x = AMA(C, 2 / (periods + 1));
2 Likes
beppe
November 13, 2021, 12:42pm
#7
@topfun , I suggest also to study this important past thread by @Tomasz :
As you know some functions accept variable periods
http://www.amibroker.com/guide/a_varperiods.html
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…
2 Likes
topfun
November 13, 2021, 3:16pm
#8
Thanks @TrendSurfer @fxshrat and @beppe for your time.
Have a nice weekend
2 Likes
Tomasz
November 14, 2021, 8:41am
#9
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 )
3 Likes
topfun
November 14, 2021, 10:49am
#10
Thank you @Tomasz , I can do it right now.
system
Closed
February 22, 2022, 10:49am
#11
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.