Hi,
There is a built-in factor RSI. This is useful. But for me, many times I do want to have the RSI values of n-days ago.
I wrote a custom factor for this purpose. Hope you enjoy it. Any critics and sujustions are welcome.
Cheers
Thomas
Here is the code:
def offset_rsi_factor(days, **kwargs):
kwargs['window_length'] += days
class OffsetRSI(CustomFactor):
def compute(self, today, assets, out, closes):
diffs = np.diff(closes, axis=0)
ups = np.nanmean(np.clip(diffs[:-days], 0, np.inf), axis=0)
downs = abs(np.nanmean(np.clip(diffs[:-days], -np.inf, 0), axis=0))
out[:] = 100.0 - (100.0 / (1.0 + (ups / downs)))
return OffsetRSI(**kwargs)
How to use it? Simply as follow:
rsi_offset_1 = offset_rsi_factor(1, inputs=[USEquityPricing.close], window_length=context.rsi_lookback)