COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Exponential Moving Average without For Loop

• Email
• Follow

```Hello, I am trying to find the rolling 30 day EMA for a time series without using a for loop (I have a lot of data).

As an example/test this is something like what I want (below) but I'm finding that my end result is not really close to how it should look. When I put it together in Excel or with a for loop it comes out correctly but I am in the dark if I this is using filter correctly below.

Can anyone help?

todaysClose = cumsum(randn(100,1));
daysBack = 30;
alpha = 2 / (daysBack + 1); %calculate smoothing factor "alpha"

%prepare a coefficient for the filter function
coefficient = repmat(alpha,1,daysBack).^(1:daysBack);
coefficient = coefficient/sum(coefficient);

EMA = filter(coefficient, 1, todaysClose);

this is also where I got the above filter code
```
 0

See related articles to this posting

```try this code:
todaysClose = cumsum(randn(100,1));
daysBack = 30;
alpha = 2 / (daysBack + 1); %calculate smoothing factor "alpha"
coefficient = repmat(1-alpha,1,daysBack).^(1:daysBack); %note 1-alpha
EMA = filter(coefficient, sum(coefficient), todaysClose);
plot(todaysClose)
hold on
plot(EMA,'r')

Hope this helps
Matt W

"happydude " <anonymousse@hotmail.com> wrote in message <hdv3c3\$5um\$1@fred.mathworks.com>...
> Hello, I am trying to find the rolling 30 day EMA for a time series without using a for loop (I have a lot of data).
>
> As an example/test this is something like what I want (below) but I'm finding that my end result is not really close to how it should look. When I put it together in Excel or with a for loop it comes out correctly but I am in the dark if I this is using filter correctly below.
>
> Can anyone help?
>
> todaysClose = cumsum(randn(100,1));
> daysBack = 30;
> alpha = 2 / (daysBack + 1); %calculate smoothing factor "alpha"
>
> %prepare a coefficient for the filter function
> coefficient = repmat(alpha,1,daysBack).^(1:daysBack);
> coefficient = coefficient/sum(coefficient);
>
> EMA = filter(coefficient, 1, todaysClose);
>
>
>
> this is also where I got the above filter code
```
 0

```thanks for this. Seems quite close but still can be quite different from the traditional EMA as used in finance.

from a limited number of simulations it seems to be quite different from the EMA for about 60 datapoints or so ...

any ideas why this might happen?

nb - the traditional EMA uses an SMA as an initial value because the EMA formula calls for an initial EMA value.. how does the Filter function get around this?
```
 0

```"happydude " <anonymousse@hotmail.com> wrote in message <he1oep\$fs6\$1@fred.mathworks.com>...
> thanks for this. Seems quite close but still can be quite different from the traditional EMA as used in finance.
>
> from a limited number of simulations it seems to be quite different from the EMA for about 60 datapoints or so ...
>
> any ideas why this might happen?
>
> nb - the traditional EMA uses an SMA as an initial value because the EMA formula calls for an initial EMA value.. how does the Filter function get around this?

The answer is that filter does not get around it.
For the first 30 points the filter will go off the leading edge of the todaysClose vector. Those 'values' past the edge are set to 0. This will distort at least the first 30 points of your EMA.
You can see the effect by having a constant close price.

todaysClose = ones(100,1)*100;
daysBack = 30;
alpha = 2 / (daysBack + 1); %calculate smoothing factor "alpha"
coefficient = repmat(1-alpha,1,daysBack).^(1:daysBack); %note 1-alpha
EMA = filter(coefficient, sum(coefficient), todaysClose);
plot(todaysClose)
hold on
plot(EMA,'r')

You could pad the leading edge of the array by replicating the first value out daysBack values and then strip it off. That might help.
So:
todaysClose = cumsum(randn(100,1));
daysBack = 30;
alpha = 2 / (daysBack + 1); %calculate smoothing factor "alpha"
coefficient = repmat(1-alpha,1,daysBack).^(1:daysBack); %note 1-alpha
EMA = filter(coefficient, sum(coefficient), todaysClose);
EMA = EMA(31:end); %remove the pad
plot(todaysClose(31:end))
hold on
plot(EMA,'r')
```
 0

```thanks i'll give it a shot :)
```
 0

```"happydude " <anonymousse@hotmail.com> wrote in message <he3krm\$glm\$1@fred.mathworks.com>...
> thanks i'll give it a shot :)

all built in: http://www.mathworks.com/access/helpdesk/help/toolbox/finance/tsmovavg.html
```
 0

```"Bwana " <bwana.mukubwa@gmail.com> wrote in message <i1fpb3\$noh\$1@fred.mathworks.com>...
> "happydude " <anonymousse@hotmail.com> wrote in message <he3krm\$glm\$1@fred.mathworks.com>...
> > thanks i'll give it a shot :)
>
> all built in: http://www.mathworks.com/access/helpdesk/help/toolbox/finance/tsmovavg.html

Anyone know why the filter function described above gives a different output to that of the built in movavg function?
```
 0
Reply davidtra84 (10) 3/14/2011 3:50:08 PM

```On Mar 15, 4:50=A0am, "david " <davidtr...@gmail.com> wrote:
> "Bwana " <bwana.muku...@gmail.com> wrote in message <i1fpb3\$no...@fred.ma=
thworks.com>...
> > "happydude " <anonymou...@hotmail.com> wrote in message <he3krm\$gl...@f=
red.mathworks.com>...
> > > thanks i'll give it a shot :)
>
> > all built in:http://www.mathworks.com/access/helpdesk/help/toolbox/fina=
nce/tsmovav...
>
> Anyone know why the filter function described above gives a different out=
put to that of the built in movavg function?

My guess is that it's because you've screwed up.
But you haven't shown us your code, so how could we know?
```
 0
Reply mulgor (3011) 3/14/2011 6:59:44 PM

7 Replies
1262 Views

Similar Articles

12/7/2013 12:26:00 AM
[PageSpeed]

Similar Artilces:

--without-mysql
My php test page shows mysql is not enabled. How do I enable php with mysql support --with-mysql? I am using rpms

methods with and without !
, for example, exits without running the exit handlers. Hal

without shell
hi all, can any linux command be invoked/ executed without using shell (bash) ? what abt security concerns ? regards, KM On Sun, 12 Jun 2005 23:16:35 +0530, km wrote: > hi all, > > can any linux command be invoked/ executed without using shell (bash) ? py> import os py> status = os.system("ls") Prints the output of ls and stores the exit code into status. py> file_list = os.popen("ls").read() Stores the output of ls into file_list. Or see the module "commands". > what abt security concerns ? Yes, there are serious security concerns. You should be *very* careful about executing strings generated by users. You probably don't want your program executing something like os.system("rm -rf /"). -- Steven. On 2005-06-12, km <km@mrna.tn.nic.in> wrote: > can any linux command be invoked/executed without using shell (bash)? Yes -- for some values of "linux command". You can execute anything that's not a bash internal or a bash script without using bash. > what abt security concerns? What about them? -- Grant Edwards grante Yow! I'm young

With or Without You
WITH OR WITHOUT YOUUUUUUUUU WITH OR WITHOUT YOUUUUUUUUUUUUUUUOUOUOUUUUUUUUUUUUUUUUU In comp.os.linux.advocacy, Simon <some_spare_id@yahoo.com> wrote on 23 Jan 2004 06:10:17 -0800 <82ca3381.0401230610.53138d83@posting.google.com>: > WITH OR WITHOUT YOUUUUUUUUU > > WITH OR WITHOUT YOUUUUUUUUUUUUUUUOUOUOUUUUUUUUUUUUUUUUU The good news is that the Song Crowbar(tm) is not strictly necessary for this post, as I have *no* idea what tune you're crooning here. The bad news is that I'm still going to employ its alternate purpose because you're being so silly. :-P Followups. -- #191, ewill3@earthlink.net It's still legal to go .sigless.

Keylistener without GUI?
Hi! Does anyone know a way how to capture keyboard events from a console program without using java.awt.events? I tried: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; s = br.readLine(); But the problem is that I have to press the enter key each time. Thanks in advance Ivan Ivan Bout <ivan2@home.nl> wrote: > Does anyone know a way how to capture keyboard events from a > console program without using java.awt.events? There is no way. At that point, key events are delivered by the operating system to the console window that's.../Technical Trainer MindIQ Corporation Op Sat, 11 Dec 2004 08:47:53 -0700, schreef Chris Smith: > Ivan Bout <ivan2@home.nl> wrote: >> Does anyone know a way how to capture keyboard events from a >> console program without using java.awt.events? > > There is no way. At that point, key events are delivered by the > operating system to the console window that's running your application. > It's up to that console window to decide when to communicate with your > application via System.in. At best, you can find out when a key is > typed

Standalone applications without MCR
Is it possible to install standalone applications that have been made using MATLAB on target computers that do not have MCR? No. Why do you ask? Is installing MCR a problem for you? Why would that be any more of a problem to install that your own custom app?