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
Hope this helps
Matt W
"happydude " <anony...@hotmail.com> wrote in message <hdv3c3$5um$1...@fred.mathworks.com>...
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))
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?
My guess is that it's because you've screwed up.
But you haven't shown us your code, so how could we know?