Code covered by the BSD License  

Highlights from
Technical Analysis Tool

image thumbnail

Technical Analysis Tool

by

 

29 Mar 2006 (Updated )

GUI for viewing various simple technical analysis indicators of a time series

sma(data,period)
function out = sma(data,period)
% Function to calculate the simple moving average of a data set
% 'data' is the vector to operate on.  The first element is assumed to be
% the oldest data.
% 'period' is the number of periods over which to calculate the average
%
% Example:
% out = sma(data,period)
%

% Error check
if nargin ~= 2
    error([mfilename,' requires 2 inputs.']);
end
[m,n]=size(data);
if ~(m==1 || n==1)
    error(['The data input to ',mfilename,' must be a vector.']);
end
if (numel(period) ~= 1) || (mod(period,1)~=0)
    error('The period must be a scalar integer.');
end
if length(data) < period
    error('The length of the data must be at least the specified ''period''.');
end

% calculate the SMA
out = filter(ones(1,period),period,data);
out(1:period-1) = nan; % these are just the filter buffer filling

Contact us