From: happydude on
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);


P.S. this was one of the posts I looked up http://groups.google.com/group/comp.soft-sys.matlab/tree/browse_frm/thread/7b5c0b3146432dd9/58e9d04b885a576a?rnum=11&_done=/group/comp.soft-sys.matlab/browse_frm/thread/7b5c0b3146432dd9/48bdf7f81cd8f197%3Ftvc%3D1%26#doc_a1c5b8de7a7c428a

this is also where I got the above filter code
http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/1d8d10d5b835550d?tvc=2&q=exponential+moving+average+filter
From: Matthew Whitaker on
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(a)hotmail.com> wrote in message <hdv3c3$5um$1(a)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);
>
>
> P.S. this was one of the posts I looked up http://groups.google.com/group/comp.soft-sys.matlab/tree/browse_frm/thread/7b5c0b3146432dd9/58e9d04b885a576a?rnum=11&_done=/group/comp.soft-sys.matlab/browse_frm/thread/7b5c0b3146432dd9/48bdf7f81cd8f197%3Ftvc%3D1%26#doc_a1c5b8de7a7c428a
>
> this is also where I got the above filter code
> http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/1d8d10d5b835550d?tvc=2&q=exponential+moving+average+filter
From: happydude on
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?
From: Matthew Whitaker on
"happydude " <anonymousse(a)hotmail.com> wrote in message <he1oep$fs6$1(a)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;
pad = repmat(todaysClose(1),daysBack,1);
todaysClose = [pad;todaysClose];
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')
From: happydude on
thanks i'll give it a shot :)