[R] fSeries rsiTA. Need help to modify function

Neuro LeSuperHéros neuro3000 at hotmail.com
Wed Apr 20 17:15:33 CEST 2005


fSeries rsiTA.  Need help to modify function

Hello,  in fSeries, the rsiTA function is this:

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 dividing  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 code at the end of the results:

attr(,"tsp")
[1]   0 261   1

Here's my feeble attempt:

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)

Any idea?




More information about the R-help mailing list