Skip to content
Prev 312 / 15274 Next

Need help to modify rsiTA function from fSeries

On 4/24/05, Neuro LeSuperH?ros <neuro3000@hotmail.com> wrote:
<snip>
Perhaps...

First, the reference is Welles Wilders' "New Concepts in Technical
Trading Systems".

This indicator is often calculated inaccurately. The problems most
often seen are incorrect calculation of the averages.

Here is a Python snippet from Crusher that calcs RSI. I have no idea
whether it coincides with the sources you cite, but it does the job
correctly, or at least as Welles said it should be done.

    def rsi(self):
        """RSI"""
        # checked 2004-10-15, OK
        self.rsi = []
        length = config['indconsts.rsi']
        up = dn = 0.0
        # find the first value
        for line in range(0, length):
            if self.close[length - line] > self.close[length - line - 1]:
                up += self.close[length - line] - self.close[length - line - 1]
            if self.close[length - line] < self.close[length - line - 1]:
                dn += self.close[length - line - 1] - self.close[length - line]
            # pad the runup with 0
            if line < (length - 1):
                self.rsi.append(0.0)
        up = up / length
        dn = dn / length
        try:
            self.rsi.append(100 - 100 / (1 + up / dn))
        except ZeroDivisionError:
            self.rsi.append(0.0)
        # use Wilder's average technique to find the remaining values
        for line in range(length, len(self.date)):
            if self.close[line] > self.close[line - 1]:
                up = ((self.close[line] - self.close[line - 1]) + up *
(length - 1)) / length
            else:
                up = up / length * (length - 1)
            if self.close[line] < self.close[line - 1]:
                dn = ((self.close[line - 1] - self.close[line]) + dn *
(length - 1)) / length
            else:
                dn = dn / length * (length - 1)
            try:
                self.rsi.append(100 - 100 / (1 + up / dn))
            except ZeroDivisionError:
                self.rsi.append(0.0)
        if config['general.debug'] == 1:
            for line in range(len(self.date)):
                print '%4d %10s %8.2f %8.2f' % (line, self.date[line],
self.close[line], self.rsi[line])

Sorry, I haven't the time to translate it into R, but it could serve
as a template for you to do so.

Good trading,

    jab