from
TillsonT3
by Alexandros Leontitsis
Tillson smoothing moving average and chart.
|
| ema=Xmovavg(asset,windowlength)
|
function ema=Xmovavg(asset,windowlength)
%Syntax: ema=Xmovavg(asset,windowlength)
%_______________________________________
%
% Calculates the Exponential Moving Average of a financial time series. It
% can also plot its chart.
%
% ema is the exponential moving average.
% asset is the security data.
% windowlength is the length of the time window.
%
%
% Reference:
% Adapted from Trey Johnson's C/C++ code. Thanks Trey, you have done a great
% job!!!
%
%
% Alexandros Leontitsis
% Department of Education
% University of Ioannina
% 45110 - Dourouti
% Ioannina
% Greece
%
% University e-mail: me00743@cc.uoi.gr
% Lifetime e-mail: leoaleq@yahoo.com
% Homepage: http://www.geocities.com/CapeCanaveral/Lab/1421
%
% 16 December 2003.
if nargin<1 | isempty(asset)==1
error('You should provide a time series.');
else
% asset must be a vector
if min(size(asset))>1
error('Invalid time series.');
end
asset=asset(:);
end
if nargin<2 | isempty(windowlength)==1
windowlength=4;
else
% windowlength must be scalar
if sum(size(windowlength))>2
error('windowlength must be scalar.');
end
% windowlength must be an integer
if round(windowlength)-windowlength~=0
error('windowlength must be an integer.');
end
% windowlength must be greater than or equal to 2
if windowlength<2
error('windowlength must be greater than or equal to 2');
end
end
% Calculate the Exponential Moving Average factor
fac=2/(1+windowlength);
% Initialize the loop
sum1=0;
sum2=0;
for i=windowlength+1:length(asset)
sum1=sum1+fac*(asset(i)-sum1);
sum2=sum2+fac*(1-sum2);
% Calculate the Exponential Moving Agerage
ema(i-windowlength,1)=sum1/sum2;
end
if nargout==0
% If there are no output arguments, make a plot
r=length(asset);
rema=length(ema);
plot(1:r,asset,(r-rema+1:r),ema)
end
|
|
Contact us at files@mathworks.com