Nineth Day of writing paper: Rsindex function of matlab

Source: Internet
Author: User
Tags scalar

function rsi = Rsindex (Closep, Nperiods)
% input price vector, term (default is 14th), output RSI value%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) Percent 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 Fact Or is calculated by dividing the average of the gains by% the average of the losses within a specified period.%% RS = (average gains)/(average losses) percent Also, the first value of RSI, RSI (1), is a NaN in order to preserve the% Dimens Ions of closep.%% example:% load disney.mat% dis_rsi = Rsindex (dis_close);% plot (dis_rsi); percent See also N             Egvolidx, 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 $ $Da    TE: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 Vectorif 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 n    Ans is 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 to account any leading NaNs%, 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)):d idx));        Totalloss = SUM (Declines ((Didx-(nperiods-1)):d idx));        % Calculate RSI rs = totalgain./totalloss;    TRSI (DIDX) =-(+/(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:NPERIODSMustBeP        Osscalar ', ... ' Nperiods must be a positive scalar. '); else RSI = closep;end% [EOF]

Nineth Day of writing paper: Rsindex function of matlab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.