P. S.:
Be aware that talib doesn't handle nans very well, so in order to avoid exceptions you need to get rid of them first. In my version I would do something like this:
class STOCH_RSI_FK(CustomFactor):
inputs = [USEquityPricing.close]
window_length = PERIOD + FK + FD + 1
def compute(self, today, assets, out, C):
# Set all values for a symbol to zero if it has nans before passing them to talib:
C[np.isnan(C).any(),:] = 0
stoch_rsi_fk = pd.DataFrame(C).apply(lambda x: talib.STOCHRSI(x, PERIOD, FK, FD, FDMA)[0][-1], axis=0)
out[:] = stoch_rsi_fk
In Vladimir's version you could do it that way:
class STOCH_RSI_FK(CustomFactor):
inputs = [USEquityPricing.close]
window_length = PERIOD + FK + FD + 1
def compute(self, today, assets, out, close):
stoch_rsi_fk = []
for C in close.T:
# If there are any nans, just append nan and don't pass them to talib
if np.isnan(C).any():
stoch_rsi_fk.append(np.nan)
continue
StochRsiFk = talib.STOCHRSI(C, PERIOD, FK, FD, FDMA)[0][-1]
stoch_rsi_fk.append(StochRsiFk)
out[:] = stoch_rsi_fk
And if you want both fastk and fastd as outputs of the same CustomFactor, you can that too
class STOCH_RSI(CustomFactor):
inputs = [USEquityPricing.close]
window_length = PERIOD + FK + FD + 1
# If you want 2 outputs, you need to add them here:
outputs = ['fastk', 'fastd']
def compute(self, today, assets, out, C):
# Set all values for a symbol to zero if it has nans:
C[np.isnan(C).any(),:] = 0
df = pd.DataFrame(C)
fastk = df.apply(lambda x: talib.STOCHRSI(x, PERIOD, FK, FD, FDMA)[0][-1], axis=0)
fastd = df.apply(lambda x: talib.STOCHRSI(x, PERIOD, FK, FD, FDMA)[1][-1], axis=0)
out[:].fastk = fastk
out[:].fastd = fastd
fastk, slowk = STOCH_RSI()