Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help using talib in a Custom Factors in Pipelines - it might be a BUG - - Exception: inputs are all NaN

Hi guys,
I'm not able to make it works properly, because it continuously throws an Exception: inputs are all NaN... Any ideas? How I could use talib.ADX as a Custom Factor without this try:-except ??
I think this might be a bug in quantopian talib implementation, don`t u?
Thanks

# Create custom factor subclass to calculate ADX(11) for screening stocks  
class AdxFactor(CustomFactor):  
    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close, USEquityPricing.high, USEquityPricing.low]  
    window_length=11  
    def compute(self, today, assets, out, close_p, highs, lows):  
        adx = np.empty(len(close_p.T), dtype=np.float64)  
        i=0  
        while i<len(close_p.T):  
            try:  
                adx[i] = talib.ADX(highs.T[i], lows.T[i], close_p.T[i], timeperiod=11)[-1]  
            except Exception as e:  
                print ("Unexpected error: {0}".format(e))  
            i=i+1  
        out[:] = adx  
4 responses

Are you trying to loop through all the stocks calculating the ADX with talib? That would seem, to me, to be an extraordinarily slow way to do it. Anthony Garner recently posted a vectorized efficiency ratio calc, which I think reduces to ADX under some conditions?

Maybe you can add a check for all NaN close price values before sending them to the talib function.

while i<len(close_p.T):  
            if not np.isnan(close_p.T[i]).all():  

This might work.

class Momentum(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 20

    def compute(self, today, assets, out, values):  
        def RSI(close, timeperiod=14):  
            return ta.RSI(close, timeperiod=timeperiod)  
        results = np.apply_along_axis(RSI,1,values)  
        out[:] = results[-1]  
class ADX_Dir_Ind(CustomFactor):  
    inputs=[USEP.high,USEP.low,USEP.close]  
    true_length=14  
    window_length=true_length+true_length  
    def compute(self, today, assets, out, high, low, close):  
        anynan =np.isnan(close).any(axis=0)  
        for col_ix, have_nans in enumerate(anynan):  
            if have_nans:  
                out[col_ix] = np.nan  
                continue  
            results = talib.ADX(  
                high[:, col_ix],  
                low[:, col_ix],  
                close[:, col_ix],  
                timeperiod=self.true_length)  
            out[col_ix] = results[-1]