Search code examples
pythonpython-3.xpandasta-libtechnical-indicator

TA-lib Exception: inputs are all NaN error?


I'm trying to run a backtest using zipline and I use TA-lib for some of my technical analysis. My dataset is enormous (gigabites in size). Because I wanted my data to work in zipline, I have a lot of data that has zeros as all the values for years at a time just so that the company data can be read in to zipline (as zipline requires you to have a trading calendar of the entire duration that your strategy is trading for).

Error message:

_func.pxi in talib._ta_lib.MACD()

_func.pxi in talib._ta_lib.check_begidx1()

Exception: inputs are all NaN

I've heard that other people are also having this error with TA-lib and that there isn't a good way to fix it. How could I fix this? Would it require me to create my own functions for MACD?

I find it surprising that a library as big as TA-lib can't deal with datasets that are over a certain size.


Solution

  • If I understand you correctly. When the indicators are calculated, a certain period is needed, for example, for a Bollinger, at least a period of 20. So anything less than 20 will be NaN. So you need to check your list for these values.

    import math
    x = float('nan')
    math.isnan(x)
    
    return True or False
    import numpy as np
    values = [float('nan'), np.nan, 55, "string", lambda x : x]
    for value in values:
       print(f"{repr(value):<8} : {is_nan(value)}")
    

    Here are the settings of the magdi that are highlighted it is necessary to calculate the period. Anything less than this will be NaN, when you try to calculate something you have nothing as input, that's why you get this error. Here's an example from the exchange.

    def MACD(data, fastperiod, slowperiod, signalperiod):
        macd, macdsignal, macdhist = [], [], []
    
        fast_ema = EMA(data, fastperiod)
        slow_ema = EMA(data, slowperiod)
    
        diff = []
    
        for k, fast in enumerate(fast_ema):
            if math.isnan(fast) or math.isnan(slow_ema[k]):
               macd.append(math.nan)
               macdsignal.append(math.nan)
            else:
              macd.append(fast-slow_ema[k])
              diff.append(macd[k])
    
        diff_ema = EMA(diff, signalperiod)
        macdsignal = macdsignal + diff_ema
    
        for k, ms in enumerate(macdsignal):
            if math.isnan(ms) or math.isnan(macd[k]):
               macdhist.append(math.nan)
            else:
               macdhist.append(macd[k] - macdsignal[k])
    
        return macd, macdsignal, macdhist
    macd, macdsignal, macdhist = MACD(closes, 12, 26, 9)
    

    This function calculates macd. If you pass a list of 100 values, then macd - 12 values will be nan, macdsignal - 26 will be nan, macdhist - 9 will be nan.