Back to Community
Pipeline built in factor RSI does not work for short look back

The built in factor for RSI does not match talib RSI. This is most evident for short look back periods.

I believe this is because the built in RSI factor uses a simple moving average of gains and losses, while the talib RSI implementation likely uses an exponential moving average of gains and losses.

For short look back periods, you can easily get values of 0 or 100 for for RSI using SMA while it is not possible (or very unlikely I would think) to get such extreme values with an EMA implementation like talib.RSI.

Would it be possible to add a parameter to the built in RSI to select SMA or EMA? To make it back compatible, the default could be set to SMA.

Otherwise I find myself adding the RSI factor outside of the pipeline which really slows down the back test. I did see a post that talks about how to use talib with pipeline but it seems very complicated to implement and more than I care to take on. Also I don't really care to program my own version of RSI from scratch as a custom factor. RSI is a complicated function as it deal with streaks and uses EMAs ; I would probably get it wrong.

Loading notebook preview...
3 responses

Here's a graphical comparison of the 2-period RSI for talib vs.pipeline RSI built in factor.
I hope it makes the point a bit more clearly.

If anybody has custom factor RSI that matches (or comes close to matching) talib RSI and is willing to share, I would appreciate it.

Loading notebook preview...

yes. It is incorrect, especially for the short period. The problem I think is the smoothing period/decay period. If you want to build your own one, you may need 10/15 days data for the smoothing of 3 days rsi.

OK, so after a bit more searching, I found the following post in which mhp provides a custom factor implementation of RSI based on Welles Wilders version.

It lines up exactly with talib RSI if I shift the talib RSI series by 1 day.
I guess the 1 day shift makes sense as pipeline is dealing with yesterday's data?

class WRSI(CustomFactor):
# Relative Strength Index using Wilder's smoothing
# (default RSI factor is Cutler's RSI which uses SMA smoothing)
inputs = (USEquityPricing.close,)
params = {'rsi_len' : 14,}
window_length = 80 # allow at least 4 times RSI len for proper smoothing
def compute(self, today, assets, out, close, rsi_len):
# TODO: figure out how this can be coded without a loop
rsi = []
for col in close.T:
rsi.append(talib.RSI(col, timeperiod=rsi_len)[-1])
out[:] = rsi

Loading notebook preview...