matlab的index函数,写论文第九天:MATLAB之rsindex函数

function rsi = rsindex(closep, nperiods)%输入价格向量、期限(默认为14日),输出rsi值

%RSINDEX Relative Strength Index (RSI).

% RSINDEX calculates the Relative Strength Index (RSI). The RSI is calculated

% based on a default 14-period period.

%

% RSI = rsindex(CLOSEP)

% RSI = rsindex(CLOSEP, NPERIODS)

%

% Optional Inputs: NPERIODS

%

% Inputs:

% CLOSEP - Nx1 vector of closing prices.

%

% Optional Inputs:

% NPERIODS - Scalar value of the number of periods. The default is period

% is 14.

%

% Outputs:

% RSI - Nx1 vector of the relative strength index

%

% Note: The RS factor is calculated by dividing the average of the gains by

% the average of the losses within a specified period.

%

% RS = (average gains) / (average losses)

%

% Also, the first value of RSI, RSI(1), is a NaN in order to preserve the

% dimensions of CLOSEP.

%

% Example:

% load disney.mat

% dis_RSI = rsindex(dis_CLOSE);

% plot(dis_RSI);

%

% See also NEGVOLIDX, POSVOLIDX.

% Reference: Murphy, John J., Technical Analysis of the Futures Market,

% New York Institute of Finance, 1986, pp. 295-302

% Copyright 1995-2006 The MathWorks, Inc.

% $Revision: 1.1.6.3 $ $Date: 2006/03/21 07:01:38 $

% Check input arguments.

switch nargin

case 1

nperiods = 14;

case 2

if numel(nperiods) ~= 1 || mod(nperiods, 1) ~= 0

error('Ftseries:rsindex:NPERIODSMustBeScalar', ...

'NPERIODS must be a scalar integer.');

elseif nperiods > length(closep)

error('Ftseries:rsindex:NPERIODSTooLarge1', ...

'NPERIODS is too large for the number of data points.');

end

otherwise

error('Ftseries:rsindex:InvalidNumberOfInputArguments', ...

'Invalid number of input arguments.');

end

% Check to make sure closep is a column vector

if size(closep, 2) ~= 1

error('Ftseries:rsindex:ClosepMustBeColumnVect', ...

'Closing prices must be a column vector.');

end

% Check for data sufficiency.

if length(closep) < nperiods

error('Ftseries:rsindex:NPERIODSTooLarge2', ...

'NPERIODS is too large for the number of data points.');

end

% Calculate the Relative Strength index (RSI).

if (nperiods > 0) && (nperiods ~= 0)

% Determine how many nans are in the beginning

nanVals = isnan(closep);

firstVal = find(nanVals == 0, 1, 'first');

numLeadNans = firstVal - 1;

% Create vector of non-nan closing prices

nnanclosep = closep(~isnan(closep));

% Take a diff of the non-nan closing prices

diffdata = diff(nnanclosep);

priceChange = abs(diffdata);

% Create '+' Delta vectors and '-' Delta vectors

advances = priceChange;

declines = priceChange;

advances(diffdata < 0) = 0;

declines(diffdata >= 0) = 0;

% Calculate the RSI of the non-nan closing prices. Ignore first non-nan

% closep b/c it is a reference point. Take into account any leading nans

% that may exist in closep vector.

trsi = nan(size(diffdata, 1)-numLeadNans, 1);

for didx = nperiods:size(diffdata, 1)

% Gains/losses

totalGain = sum(advances((didx - (nperiods-1)):didx));

totalLoss = sum(declines((didx - (nperiods-1)):didx));

% Calculate RSI

rs = totalGain ./ totalLoss;

trsi(didx) = 100 - (100 / (1+rs));

end

% Pre allocate vector taking into account reference value and leading nans.

% length of vector = length(closep) - # of reference values - # of leading nans

rsi = nan(size(closep, 1)-1-numLeadNans, 1);

% Populate rsi

rsi(~isnan(closep(2+numLeadNans:end))) = trsi;

% Add leading nans

rsi = [nan(numLeadNans+1, 1); rsi];

elseif nperiods < 0

error('Ftseries:rsindex:NPERIODSMustBePosScalar', ...

'NPERIODS must be a positive scalar.');

else

rsi = closep;

end

% [EOF]

原文:http://blog.csdn.net/liangzuojiayi/article/details/51330602

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值