I made some more investigation and I think tsmovavg based solution has a major drawback i.e. will not work for real time application. Lets assume you have 10k bars and you calculate ema on it. Than a new bar comes and calculate again 10k would be very time costly - much easier to calculate just last bar.
But in this case this code will not work because instead of use x-1 data
for initial value (so last data point of 1st calculation of ema) it will use some average values.
in other words it means that ema calculated on 10k+1 bars in one pass will have different value than ema calculated on last bar and it should not be a case.
You should format your code like this,
vout = indicators([hi,lo,cl],'kdj',k,d)
vout = output vector
hi = high prices
lo = low prices
cl = closing prices
k = number of periods for %K
d = number of periods for %D
In order to extract fpctk, fpctd, and jline, please use the following code:
fpctk = vout(:,1);
fpctd = vout(:,2);
jline = vout(:,3);
1. All inputs must be vertically oriented
2. All outputs are returned vertically oriented
3. This code is intended to be used for hundreds or thousands of points of data. Normally you cannot use any of these functions for 1 single point. This function will require at least 1 more point of data for each of the hi, lo, and cl inputs than the maximum of the k or d inputs.
Thanks for the feedback Alejandro! I'm glad everything is working out for you.
At the moment I do not have access to Matlab in order to make this change but as soon as I do, I will add in your suggestion.
How to use the code on KDJ? Please,
Say, I have tick data(tick) and like to has KDJ(9,2,3). Thanks,
I did try, but get error.
>> [fpctk,fpctd,jline] = indicators([2500,2490,2495],'kdj',9,2);
??? Error using ==> indicators
Too many output arguments.
22 Oct 2013
Functions for calling the statistical package R from within MATLAB.
I have a problem doing
evalR('gfit = garch(ddta, order=c(1,1))')
evalR('garchcoef = coef(gfit)')
garchcoef = getRdata('garchcoef');
The garchcoef are not the same im matlab than in R. Do you know why ?
Thanks for your help,
Nate, I want to thank you a lot for sharing your code because it works great and saved me a lot of time.
I've been working with some of these indicators and found differences between your RSI calculation and another very popular version (used in platforms like MT4), where the second and subsequent totalGain/totalLoss values depend on their previous values providing a smoother result -similar to what is done in exponential moving averages. Here you find a detailed explanation: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi
The following modification over your code provides the smoothed RSI:
% Calculate the RSI of the non-nan closing prices. Ignore first non-nan
% vin b/c it is a reference point. Take into account any leading nans
% that may exist in vin vector.
trsi = nan(size(diffdata, 1)-numLeadNans, 1);
% Pre-allocating totalGain and totalLoss using 'trsi' size as reference.
totalGain = zeros(size(trsi,1),1);
totalLoss = zeros(size(trsi,1),1);
% First values are different than others
totalGain(period) = sum(advances(1:period))/period;
totalLoss(period) = sum(declines(1:period))/period;
% Subsequent totalGain/Loss values
for i1 = period+1:size(trsi, 1)
totalGain(i1) = (totalGain(i1-1)*(period-1)+advances(i1))/period;
totalLoss(i1) = (totalLoss(i1-1)*(period-1)+declines(i1))/period;
%Calculate RS and RSI
rs = totalGain ./ totalLoss;
for i2 = period:size(trsi,1)
trsi(i2) = 100 - (100 / (1+rs(i2)));
An improvement could be a "method option" in the formula to choose one of both versions of RSI; one method with your original RSI version and a second one with the smoothed RSI version.