SmartQuant Discussion
http://www.smartquant.com/forums/

EMA Code
http://www.smartquant.com/forums/viewtopic.php?f=60&t=10618
Page 1 of 1

Author:  azuric [ Mon Oct 21, 2013 11:09 am ]
Post subject:  EMA Code

Hi,

Can anyone post some code on building an Exponential Moving Average. I know it is a pre-built incidator but I am building one that updates using OnTrade not OnBar.

I know the formula is 2/(n+1) * Price + (1-2/(n+1)) * EMA(t-1)

but not sure what the first value is, i.e how do you get EMA(t-1) for the first value.

Any help would be great.

Author:  bean3178 [ Sat Nov 02, 2013 3:30 am ]
Post subject:  Re: EMA Code

I use this code to calculate an EMA in my strategy. If the prior value is unknown, then I use 0. After a number of iterations your EMA should be correct.

Author:  azuric [ Sat Nov 02, 2013 9:22 am ]
Post subject:  Re: EMA Code

thanks I will try that.

Author:  maddy [ Mon Nov 04, 2013 11:56 am ]
Post subject:  Re: EMA Code

Hi bean3178

Where do I find the code?

Regards
Maddy

bean3178 wrote:
I use this code to calculate an EMA in my strategy. If the prior value is unknown, then I use 0. After a number of iterations your EMA should be correct.

Author:  bean3178 [ Wed Nov 06, 2013 4:24 am ]
Post subject:  Re: EMA Code

EMA = Price(t) * k + EMA(y) * (1 – k)
t = today, y = yesterday, N = number of days in EMA, k = 2/(N+1)

Constructor/one-time:

Code:
EMAk = 2.0 / (eLen + 1);
myEMA = new TimeSeries();


In your calculate/interative function:

Code:
if (myEMA.Count > 1)
    myEMA.Add(Input.GetDateTime(index), myEMA.Last*EMAk + myEMA.Ago(1)*(1-EMAk));
else
     myEMA.Add(Input.GetDateTime(index), 0);


You may need to tweak this a bit, but this should be it in principle.

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/