[tech-spec] RSI

  • From: BBands <BBands@xxxxxxxxxxxxxxxxxx>
  • To: "'tech-spec@xxxxxxxxxxxxx'" <tech-spec@xxxxxxxxxxxxx>
  • Date: Fri, 15 Oct 2004 10:03:19 -0700

Here is the snippet of Python code from Crusher that calcs RSI. I believe it
does so correctly. I coded this because it thought it was useful and indeed
I do use it and find it to be of value. A xy plot of returns versus
indicator values reveals nothing. If simple answers were sufficient...

Have a nice weekend.

    --jab

    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])  

Other related posts:

  • » [tech-spec] RSI