SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Sep 28, 2020 5:48 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: EMA Code
PostPosted: Mon Oct 21, 2013 11:09 am 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 236
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.


Top
 Profile  
 
 Post subject: Re: EMA Code
PostPosted: Sat Nov 02, 2013 3:30 am 
Offline

Joined: Sat Jul 13, 2013 3:57 am
Posts: 52
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.


Top
 Profile  
 
 Post subject: Re: EMA Code
PostPosted: Sat Nov 02, 2013 9:22 am 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 236
thanks I will try that.


Top
 Profile  
 
 Post subject: Re: EMA Code
PostPosted: Mon Nov 04, 2013 11:56 am 
Offline

Joined: Wed Sep 18, 2013 10:50 am
Posts: 67
Location: London
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.


Top
 Profile  
 
 Post subject: Re: EMA Code
PostPosted: Wed Nov 06, 2013 4:24 am 
Offline

Joined: Sat Jul 13, 2013 3:57 am
Posts: 52
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group