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
matlab的index函数,写论文第九天:MATLAB之rsindex函数相关推荐
- matlab仿真参考文献,学生仿真论文,关于《MATLAB仿真》课程教学改革和相关参考文献资料-免费论文范文...
导读:该文是关于学生仿真论文范文,为你的论文写作提供相关论文资料参考. 樊春霞 (南京邮电大学自动化学院,江苏 南京 210003) 摘 要: MATLAB软件是科学研究.工程技术以及管理决策等领域内 ...
- matlab用进退法写程序,进退法matlab程序
极小值点包含于区间 [ x(1) , x(3) ]或[ x(3) , x(1) ] (3)算法的 MATLAB 实现在 MATLAB 中编程实现的进退函数为: min JT 功能:用进退法求解.... ...
- vc只能调用matlab子函数,Vc++6.0调用matlab的数学库函数
Vc++6.0调用matlab的数学库函数 前段时间在摸索如何在VC++6.0当中调用Matlab的里的数学库函数.当时弄得我很是郁闷,现在想想,自已走了很多弯路,原来,是如些的简单.所以,与出来和大 ...
- 关于MATLAB中xlswrite函数写数据出现服务器异常情况的解决办法
关于MATLAB中xlswrite函数写数据出现服务器异常情况的解决办法 参考文章: (1)关于MATLAB中xlswrite函数写数据出现服务器异常情况的解决办法 (2)https://www.cn ...
- 最小二乘法,简明公式整理,数学证明,matlab程序(自写代码、lsqcurvefit函数、fminsearch函数)
目录 批处理最小二乘方法 递推最小二乘方法 带有遗忘因子的递推最小二乘方法 Matlab案例分析 自写代码 matlab之lsqcurvefit函数 matlab之fminsearch函数 附录1:递 ...
- matlab三天写论文!截面空间计量模型实战
matlab三天写论文!截面空间计量模型实战
- MATLAB:调用自己写的函数
自己编写了一个s函数,为m文件,问题来了,将这个文件保存在哪可以被直接接调用?具体解决方法: 1.打开主页上的[set path]选项,把自己写的函数所在的文件夹加到搜索路径 2.在matlab的安装 ...
- 【Matlab】Matlab图片清晰拷贝到论文中并去掉空白边距
问题描述 在写论文的时候,需要用到MATLAB生成的图片,但发现生成的图片在Word中十分模糊,并不清晰:同时,生成图片的四边存在很明显的空白边距,导致与论文上下文字的间距较大,严重影响美观.例如: ...
- 对于写论文和平时学习都有用的网站
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_32892383/article/ ...
最新文章
- struts2中使用标签操作静态方法等
- 注意了,又有企业因BTC费用高转向BCH
- avformat_alloc_output_context2 -22错误
- 在代码中向ReportViewer动态添加数据源
- SSD算法 模板 匹配
- matlab模拟gpd,如何用ARMA模型预测中国GDP
- language dropdown list - filter logic
- 自监督学习的一些思考
- java版 二叉树 所有递归和非递归遍历算法
- 电平转换电路_RS232电平和TTL电平有什么不同?如何转换?
- python异步线程算法应用_Python多线程----线程池以及线程实现异步任务
- php 导入excal,php导入excel php使用phpexcel导入excel文件
- 【代码审计】代码安全测试的方法
- av_rescale_q和av_rescale_q_rnd和av_rescale_rnd
- 百度收录批量查询-免费百度整站批量收录查询工具
- python 扫描二维码
- 拆弹专家(密码BFS)
- 【币圈止损】正确认知,合理运用
- macbook linux 双系统,macbookair双系统怎么切换使用?macbookair双系统切换使用的方法...
- php+vue基于微信小程序的叽喳音乐播放小程序