논문 작성 7일차: MATLAB의 movavg 함수
3355 단어 matlab
function [short,long] = movavg(asset,lead,lag,alpha)
%MOVAVG Leading and lagging moving averages chart.
% [SHORT,LONG] = MOVAVG(ASSET,LEAD,LAG,ALPHA) plots leading and lagging
% moving averages. ASSET is the security data, LEAD is the number of
% samples to use in leading average calculation, and LAG is the number
% of samples to use in the lagging average calculation. ALPHA is the
% control parameter which determines what type of moving averages are
% calculated. ALPHA = 0 (default) corresponds to a simple moving average,
% ALPHA = 0.5 to a square root weighted moving average, ALPHA = 1
% to a linear moving average, ALPHA = 2 to a square weighted moving
% average, etc. To calculate the exponential moving averages,
% let ALPHA = 'e'.
%
% MOVAVG(ASSET,3,20,1) plots linear 3 sample leading and 20 sample
% lagging moving averages.
%
% [SHORT,LONG] = MOVAVG(ASSET,3,20,1) returns the leading and lagging
% average data without plotting it.
%
% See also BOLLING, HIGHLOW, CANDLE, POINTFIG.
% Copyright 1995-2006 The MathWorks, Inc.
% $Revision: 1.6.2.3 $ $Date: 2006/06/16 20:09:55 $
if nargin < 4
alpha = 0; % Default is simple moving average
end
if nargin < 3
error('finance:movavg:missingInputs',sprintf('Please input asset, lead, and lag.'))
end
[m,n] = size(asset);
if m > 1 & n > 1
error('finance:movavg:invalidInputSize',sprintf('Please specify input data as row or column vectors.'))
end
if lead > lag
error('finance:movavg:badLeadInput',sprintf('Lead argument must be less than or equal to lag argument.'))
end
asset = asset(:);
r = length(asset);
if lead < 1 | lead > r | lag < 1 | lag > r
error('finance:movavg:badLeadLagInput',sprintf('Lead and lag arguments must be positive <= %1.0f.',r))
end
if lower(alpha) == 'e'
%lower
% compute exponential moving average
% calculate smoothing constant (alpha)
alphas = 2/(lead+1);
alphal = 2/(lag+1);
% first exponential average is first price
a(1) = asset(1);
b(1) = asset(1);
% preallocate matrices
a = [a;zeros(r-1,1)];
b = [b;zeros(r-1,1)];
% lagging average
% For large matrices of input data, FOR loops are more efficient
% than vectorization.
for j = 2:r
a(j) = a(j-1) + alphal*(asset(j) - a(j-1));
end
% leading average
for j = 2:r
b(j) = b(j-1) + alphas*(asset(j) - b(j-1));
end
else
% compute general moving average (ie simple, linear, etc)
% build weighting vectors
i = 1:lag;
wa(i) = (lag - i + 1).^alpha./sum([1:lag].^alpha);
i = 1:lead;
wb(i) = (lead - i + 1) .^alpha/sum([1:lead].^alpha);
% build moving average vectors by filtering asset through weights
a = filter(wa,1,asset);
b = filter(wb,1,asset);
end
if nargout == 0
% If no output arguments, plot moving averages
h = plot(1:r-lag+1,asset(lag:r),1:r-lag+1,a(lag:r),1:r-lag+1,b(lag:r));
if get(0,'screendepth') > 1
cls = get(gca,'colororder');
set(h(1),'color',cls(1,:))
set(h(2),'color',cls(2,:))
set(h(3),'color',cls(3,:))
end
else
% output data to workspace
short = b;
long = a;
end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【MATLAB】변수를 스크립트에 저장MATLAB에서 계산 결과를 저장할 때 MAT 파일, Excel 등의 파일에 저장하는 것이 좋을 것이라고 생각하지만 변수의 유형에 따라 스크립트에 내보낼 수 있습니다. MATLAB을 사용해 10년 정도가 됩니다만, ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.