I need to calculate a Simple Moving Average with period = 10.
How can I do this in Matlab?
I am using movavg(series,1,20,0) but I am not sure if this is correct.
What should I use for lead and lag?
Thanks,
Miguel
--------------------------------------------------------------------------
Miguel:
I don't know that function. I use conv() or conv2(). Just use ones
(10,1) for the kernel. Pretty easy to use.
Good luck,
ImageAnalyst
ones(10,1)/10
filt1=[1/3 1/3 1/3]; %3 coefficients
The other will have length 10 and have filter coefficients
filt2=[1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10]; %10 coefficients
You are then filtering your input data with these FIR filters.
series=randn(100,1); % create some random data
output_filt1=filter(filt1,1,series); %filtering some random data
output_filt2=filter(filt2,1,series);
If you now plot that data, you will see that both filtered versions are smoother than the input data, but that output_filt2 is smoother than output_filt1 because you have used a longer moving average filter. I don't think you want your 'lead' input variable to be 1, because that isn't giving you anything. I'm not an economics person, but one application of using these moving averages of different lengths is to compare the actual data against the moving averages of different length (one short or leading, and one longer or lagging) and see where the actual market data falls in relation to those different moving averages. This is used to make inferences about the general direction of the time series (market). Changing the control parameter gives you weighted moving averages or exponential.
Hope that helps,
wayne
"Miguel Moura" <mdNOSP...@gmREMOVEail.com> wrote in message <guahs5$lgk$1...@fred.mathworks.com>...
I need to use the Simple Moving Average in its normal form because I created a C# NET library to do it. And I am using this library in Matlab and checking the performance.
I would like to calculate the SMA using Matlab function to validate the values.
In theory the SMA values should be the same either using the C# Library SMA or the Matlab SMA, right?
In C# my SMA is as follows:
public static Double[] SMA(Double[] series, Int32 period)
{
// Check arguments
Int32 length = series.Length;
if (length == 0) throw new ArgumentException("Series cannot be
empty");
if (period > length) throw new ArgumentException("Period
cannot be greater than series length");
// Calculate simple moving average
Double[] sma = new Double[length];
sma[0] = series[0];
double sum = sma[0];
for (int bar = 1; bar < length; bar++)
{
if (bar < period)
{
sum += series[bar];
sma[bar] = sum / (bar + 1);
}
else
{
sma[bar] = sma[bar - 1] + (series[bar] - series[bar -
period]) / period;
}
}
return sma;
}
I am using SMA as an example for testing.
Thanks,
Miguel
sma[0] = series[0];
double sum = sma[0];
for (int bar = 1; bar < length; bar++)
{
if (bar < period)
{
sum += series[bar];
sma[bar] = sum / (bar + 1);
}
else
{
sma[bar] = sma[bar - 1] + (series[bar] - series[bar -
period]) / period;
}
}
In Matlab (quick translation):
sma(1)=series(1);
for j=2:length(series)-1
if j<=period
sma(j)=sum(series(1:j))/(j+1);
else
sma(j)=sma(j-1)+(series(j)-series(j-period))/period;
end
end
But you get the essentially the same results if you just use filter() with an FIR filter consisting of a vector of length period with coefficients (1/10)
series=randn(100,1);
h=ones(10,1);
h=h./10;
sma_matlab=filter(h,1,series);
period=10;
sma(1)=series(1);
for j=2:length(series)-1
if j<=period
sma(j)=sum(series(1:j))/(j+1);
else
sma(j)=sma(j-1)+(series(j)-series(j-period))/period;
end
end
plot(sma_matlab,'b','linewidth',2);
hold on;
plot(sma,'r');
There are some start-up effects to deal with in your method, but you get the picture. The nice thing about Matlab is that some great developers have done a lot of work for you. You get to reap the fruits of their labor.
Hope that helps,
wayne
"Miguel Moura" <mdNOSP...@gmREMOVEail.com> wrote in message <gubrt2$l1$1...@fred.mathworks.com>...
period=10 in the code snippet below.
wayne
"Wayne King" <wmki...@gmail.com> wrote in message <gubuip$7s8$1...@fred.mathworks.com>...
> Hi Miguel, you can translate your C code easily into matlab. Taking the relevant part of your C-code
>
> sma[0] = series[0];
>
> double sum = sma[0];
> for (int bar = 1; bar < length; bar++)
> {
> if (bar < period)
> {
> sum += series[bar];
> sma[bar] = sum / (bar + 1);
> }
> else
> {
> sma[bar] = sma[bar - 1] + (series[bar] - series[bar -
> period]) / period;
> }
> }
>
> In Matlab (quick translation):
>
> sma(1)=series(1);
> for j=2:length(series)-1
> if j<=period
> sma(j)=sum(series(1:j))/(j+1);
> else
> sma(j)=sma(j-1)+(series(j)-series(j-period))/period;
> end
> end
>
> But you get the essentially the same results if you just use filter() with an FIR filter consisting of a vector of length period with coefficients (1/10)
>
> series=randn(100,1);
> h=ones(10,1);
> h=h./10;
> sma_matlab=filter(h,1,series);
> period ;
I am creating a financial model. I do the testing in Matlab but at real time I will use C# since it has been difficult to connect Matlab to the API and to be honest most API use .NET or C++.
So real time it will be a C# .NET WPF Application.
For testing it will be Matlab.
For coerency both systems should use the same methods for calculation.
So either I create the algorithms in C# and create a .NET 3.5 library to be used in Matlab.
Or I create everything in Matlab, compile to NET (which I think it is possible) to use into the WPF application.
What would you advice me? Maybe this latest option?
I think it will probably save me a lot of work ...
But what about performance?
But how can I compile for example that code into a NET library?
Any advice on this is very welcome.
Thank You,
Miguel
"Wayne King" <wmki...@gmail.com> wrote in message <gubuvu$71g$1...@fred.mathworks.com>...
> sorry miguel a crazy character appear for my statement
>
> period in the code snippet below.
Is the movavg function backward looking, as in it is appropriate for financial analysis?
My results to a simple ema trading model were better than I expected so I wanted to check: for a backward looking 10 period exponential moving average is movavg(data,10,10,'e'); correct?
Kind Regards
Ralph
wayne
"Ralph " <ral...@gmail.com> wrote in message <h2ad61$3st$1...@fred.mathworks.com>...
Much appreciated
Ralph
[short,long] = movavg(data,10,10,'e');
Usually, people pick different values for those moving averages.
Hope that helps,
wayne
"Ralph " <ral...@gmail.com> wrote in message <h2atdf$6sc$1...@fred.mathworks.com>...
Don't trust the EMA that Matlab implements. It isn't the traditional moving average that is used in finance. In fact I don't know if their version is ever used. In other words it's flat out wrong IMO.
Here's what Matlab uses:
% compute exponential moving average
% calculate smoothing constant (alpha)
alphas = 2/(period+1);
% first exponential average is first price
b(1) = asset(1);
% preallocate matrices
b = [b;zeros(r-period,1)];
% lagging average
% For large matrices of input data, FOR loops are more efficient
% than vectorization.
% leading average
for j = period:r-1;
b(j+2-period) = b(j-period+1)+alphas*(asset(j+2-period)-b(j-period+1));
end
First off, the line:
b(1) = asset(1);
is not good, for example what if your data looked like this 1, 4, 6 , 20, 45
then ask Matlab to compute a 5 period EMA and it gives you 1 as the first pt.
much better is to use SMA for the first point,
b(1) = mean(asset(1:period));
and it doesn't stop there!
look at the actual EMA calculation:
b(j+2-period) = b(j-period+1)+alphas*(asset(j+2-period)-b(j-period+1));
asset(j+2-period) is the price X periods ago when in reality it should be today's price. Every reference I've seen gives the formula:
EMA_today = EMA_yest + alpha*(PRICE_today - EMA_yest)
And for comparison Matlab:
EMA_today = EMA_yest + alpha*("PRICE #period days ago" - EMA_yest)
the correct line should read:
b(j+2-period) = b(j-period+1)+alphas*(asset(j+1)-b(j-period+1));
This is a pretty serious blunder and can really throw off your results as it did in my case. Can't believe this has never been addressed.
Michael