Calculating relative strength index over a period
Your code is already pretty Pythonic.

There is a bug in your code. If no losses were reported in that window then losses == 0.0 which when you append to rsi will throw a ZeroDivisionError.

There are two suggestions I would make:


Use zip (or izip if possible)

The zip function takes two iterables and combines them. It essentially returns:

&gt;&gt;&gt;zip([1,2,3], ['a','b','c'])
[(1, 'a'), (2, 'b'), (3, 'c')]


We can use this instead of directly indexing through j and j-1.
If-Else

Currently your if-statement looks like this:

if diff &gt; 0:
    gains += diff
elif diff &lt; 0: 
    losses += abs(diff)


I would recommend making the elif into a simple else. This is because anything that the elif won't catch is if diff == 0 and since losses == losses + 0, it won't effect losses. Also, you remove a comparision using a simple else.


With these suggestions taken into account (plus the simple bug fix), we can refactor your code a little bit:

def get_rsi(self, period):
    rsi=[]
    for i in range(len(self.hist_d)-period):
        gains = 0.0
        losses = 0.0

        window = self.hist_d[i:i+period+1]
        for year_one, year_two in zip(window, window[1:]):
            diff = year_two - year_one

            if diff &gt; 0:
                gains += diff
            else:
                losses += abs(diff)

        # Check if `losses` is zero. If so, `100/(1 + RS)` will practically be 0.
        if not losses:
            rsi.append(100.00)
        else:
            rsi.append(round(100 - (100 / (1 + gains / losses)), 2))

    return rsi