[R-sig-finance] Need help to modify rsiTA function from fSeries
Neuro LeSuperHéros
neuro3000 at hotmail.com
Sun Apr 24 20:59:45 CEST 2005
Need help to modify rsiTA function from fSeries
Hello, in fSeries, this is the rsiTA function :
function (close, lag)
{
sumlag = function(x, lag) {
xs = x
for (i in 1:lag) {
x1 = c(x[1], x[1:(length(x) - 1)])
xs = xs + x1
x = x1
}
xs
}
close1 = c(close[1], close[1:(length(close) - 1)])
x = abs(close - close1)
x[close < close1] = 0
rsi = sumlag(x, lag)/sumlag(abs(close - close1), lag)
rsi[1] = rsi[2]
rsi
}
The function is correct and works fine but does not correspond to what the
market uses. Data providers (Bloomberg, Datastream) usually use a Smoothed
RS in the calculation. Basically, what it means is that calculation of the
first RS value is: divide the average gain by the average loss. All
subsequent RS calculations use the previous period's average gain and
average loss for smoothing purposes.
For a calculation example see this page:
http://www.stockcharts.com/education/IndicatorAnalysis/indic_RSI.html
I tried to change the function but it doesn't work. I get the same data as
rsiTA and this weird code at the end of the results:
attr(,"tsp")
[1] 0 261 1
Here's my feeble attempt at making a smoothed RSI function:
rsiTA2<-function (close, lag)
{
sumlag = function(x, lag) {
xs = x
for (i in 1:lag) {
x1 = c(x[1], x[1:(length(x) - 1)])
xs = xs + x1
x = x1
}
xs
}
close1 = c(close[1], close[1:(length(close) - 1)])
x = abs(close - close1)
x[close < close1] = 0
#This is what is changed to smooth the RS#############
rsi =((lag(sumlag(x, lag),1)*(lag-1)+ (close - close1)/lag)/((
lag(sumlag(abs(close - close1), lag),1)*(lag-1)+abs(close - close1))/lag)
rsi[1] = rsi[2]
rsi
}
rsiTA2(tsx at Data,14)
Can you help?
More information about the R-sig-finance
mailing list