Amibroker code for EMA? (or convert formula from Metastock to Amibroker?) - Aussie Stock Forums

# Thread: Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

1. ## Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

Does anyone know the full AFL code to create a custom EMA?

Alternatively, if somone could help me convert the following Metastock formula, it would be most appreciated!

Pds:= 200;

Rate:=2 / (Pds+1);

M:=If(Cum(1)=1, C, PREV*(1-Rate) + C*Rate );

ValueWhen(1,Cum(1)>=Pds,M);

2. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

What's wrong with the built-in EMA function?

Code:
```// variable period EMA
function cEMA( array, period )
{ // by T.Janeczko, http://www.amibroker.com/guide/h_understandafl.html
// initialize first value
vaexp[ 0 ] = array[ 0 ];
for( i = 1; i < BarCount; i++ ) {
// calculate the value of smoothing factor
factor = 2/(period[ i ] + 1 );
// calculate the value of i-th element of array
// using this bar close ( close[ i ] ) and previous average value ( vaexp[ i - 1 ] )
vaexp[ i ] = factor * array[ i ] + ( 1 - factor ) * vaexp[ i - 1 ];
}
return vaexp;
}```

Code:
```// variable period EMA
function varperEMA( array, period )
{// by T.Janeczko
return AMA( array, 2 / ( period + 1 ) );
}```

3. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

I'm experiencing the same problem as is mentioned here: http://forum.metastock.com/Discussio...lue#post150586

When I calculate EMA using Amibroker's default EMA, it gives me a different value to what is calculated in both Excel and Metastock (which match each other) and the difference is VERY wide. I'm trying to figure out why this might be... and from what I can figure out so far, I suspect it might something to do with the initial EMA value. I'm not even entirely sure which is correct... Metastock/Excel OR Amibroker...

Many thanks for the code.. I'm not familar with functions in AB though... how do I get it to plot?

Originally Posted by trash
What's wrong with the built-in EMA function?

Code:
```// variable period EMA
function cEMA2( array, period )
{ // by T.Janeczko, http://www.amibroker.com/guide/h_understandafl.html
// initialize first value
vaexp[ 0 ] = array[ 0 ];
for( i = 1; i < BarCount; i++ ) {
// calculate the value of smoothing factor
factor = 2/(period[ i ] + 1 );
// calculate the value of i-th element of array
// using this bar close ( close[ i ] ) and previous average value ( vaexp[ i - 1 ] )
vaexp[ i ] = factor * array[ i ] + ( 1 - factor ) * vaexp[ i - 1 ];
}
return vaexp;
}```

Code:
```// variable period EMA
function varperEMA( array, period )
{// by T.Janeczko
return AMA( array, 2 / ( period + 1 ) );
}```

4. ## Re: Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

I've worked out how to plot it, so please disregard my last question - thanks.

The resulting plot from the code you gave is now only a little bit different from the Excel/Metastock values (but is still very different from the default Amibroker EMA).

5. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Also, I note that the EMA using that code changes completely as I zoom in and out of the chart... very strange!

Originally Posted by trash
What's wrong with the built-in EMA function?

Code:
```// variable period EMA
function cEMA( array, period )
{ // by T.Janeczko, http://www.amibroker.com/guide/h_understandafl.html
// initialize first value
vaexp[ 0 ] = array[ 0 ];
for( i = 1; i < BarCount; i++ ) {
// calculate the value of smoothing factor
factor = 2/(period[ i ] + 1 );
// calculate the value of i-th element of array
// using this bar close ( close[ i ] ) and previous average value ( vaexp[ i - 1 ] )
vaexp[ i ] = factor * array[ i ] + ( 1 - factor ) * vaexp[ i - 1 ];
}
return vaexp;
}```

Code:
```// variable period EMA
function varperEMA( array, period )
{// by T.Janeczko
return AMA( array, 2 / ( period + 1 ) );
}```

6. ## Re: Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

Originally Posted by richardwilson
I've worked out how to plot it, so please disregard my last question - thanks.

The resulting plot from the code you gave is now only a little bit different from the Excel/Metastock values (but is still very different from the default Amibroker EMA).
I'm sorry to say but you do not have much of a clue whatsoever. The built-in function outputs the same result as other ones and same one as Excel calculation. It just starts period later. That's why at start only it differs a bit.

7. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Originally Posted by richardwilson
Also, I note that the EMA using that code changes completely as I zoom in and out of the chart... very strange!
There is nothing strange.
Once again you just prove one thing that is having no clue of the software you are using. How can you gain more knowledge resulting in having more clue? Reading the documentation!

Anyway in case you have a short attention span the posts of this thread may help a little bit already https://groups.yahoo.com/neo/groups/.../topics/177938
But to be honest I doubt it since ignorance is the king of Internet.

8. ## Re: Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

Here is another proof that the calculation is in-line with Excel for example using same start method contrary to your claims it wouldn't be the case.

attached is the Excel file from Stockcharts.com. Import the close prices into AB and apply built-in EMA 10 as of the Excel file. Results will be in-line.

9. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Originally Posted by richardwilson
Also, I note that the EMA using that code changes completely as I zoom in and out of the chart... very strange!
read "Important notes" section here http://www.amibroker.com/kb/2008/07/03/quickafl/

10. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

You are right.. I dont have a clue whatsoever.. have never used Amibroker before until today... so if this is the general attitude towards helping community members.. then I think I will give this software a miss. Thanks for replying anyway.

Originally Posted by trash
read "Important notes" section here http://www.amibroker.com/kb/2008/07/03/quickafl/

11. ## Re: Amibroker code for EMA? (or convert formula from Metastock to Amibroker?)

And I find it rather rude annoying other people with implications like "this is wrong and that is strange" instead of first reading documentation of a software if someone has no clue at all.

You being a helping community member? Have I missed something?
What have you helped so far? That's quite funny. AFAICS I'm the only one who has helped you so far. I am the one who has provided the input to you but not you to yourself. You have just gotten your lazy butt creamed with free info that you could have easily found out yourself.

BTW, I don't care if you use this or that software as I'm not the developer of any commercial software. I really couldn't care less.
And as I see it, you will remember my "rant" much better instead of some butt licking posts and I'm sure you will rather avoid further redundant posts but instead you got encouraged to better study reasons before implying something and making a fool out of yourself. So actually everything has a good side not just a bad one.

12. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Originally Posted by richardwilson
You are right.. I dont have a clue whatsoever.. have never used Amibroker before until today... so if this is the general attitude towards helping community members.. then I think I will give this software a miss. Thanks for replying anyway.
Richard don't be turned off. All boards have people who "live" on them (or even more than one under different names) and feel compelled to respond to all questions. If annoyed just ignore and somebody else will answer your questions.

Have you actually switched to AB or are you searching for the right software (or combination of software) that fits your needs?

13. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Originally Posted by richardwilson
You are right.. I dont have a clue whatsoever.. have never used Amibroker before until today... so if this is the general attitude towards helping community members.. then I think I will give this software a miss. Thanks for replying anyway.
Hi Richard, if you're not looking for too complicated stuff, you might want to check out my threads:

Converting Metastock EOD data to MT4 history files for MT4 charting

and

World's First Metatrader 4 ASX EOD Stock Market Scanner/Screener/Filter

Cheers,

Andrew.

14. ## Re: Amibroker code for Exp. Mov. Avge? (or convert formula from Metastock to Amibroke

Originally Posted by ajcode
Hi Richard, if you're not looking for too complicated stuff, you might want to check out my threads:

Converting Metastock EOD data to MT4 history files for MT4 charting

and

World's First Metatrader 4 ASX EOD Stock Market Scanner/Screener/Filter

Cheers,

Andrew.
What's that gotta do with EMA topic?

And what's that gotta do with "not too complicated"?
Those threads are the opposite of "not too complicated".