TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.27k stars 1.72k forks source link

Polars talib: Problem with NaN input for calculation of chained indicators #585

Closed collinsethans closed 1 year ago

collinsethans commented 1 year ago

For a newly listed stock (IPO), initially due to fewer trading days, calculation of an indicator produces only NaN. Calculation of a 2nd indicator on top of the 1st one raises an exception stating: inputs are all NaN. See Snippet #1 below.

However, as trading days increase beyond timeperiod of the indicator (Case B), the 1st indicator produces it's first valid value (after the sequence of NaN-s). In such a case, calculation of the 2nd indicator on top of it successfully executes without any exception. See Snippet #2 below.

# Snippet #1, simplified example:

import polars as pl

>>> df['close']
shape: (9,)
Series: 'close' [f32]
[
    0.15
    0.15
    0.15
    0.15
    0.1
    0.15
    0.2
    0.2
    0.15
]

>>> rsi_14 = ta.RSI(df['close'], timeperiod=14)
>>> rsi_14
shape: (9,)
Series: '' [f64]
[
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
]

# 2nd indicator on RSI
>>> ta.MA(rsi_14, timeperiod=5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/aux/apps_install/mc3/envs/pl1/lib/python3.10/site-packages/talib/__init__.py", line 64, in wrapper
    result = func(*_args, **_kwds)
  File "talib/_func.pxi", line 3414, in talib._ta_lib.MA
  File "talib/_func.pxi", line 68, in talib._ta_lib.check_begidx1
Exception: inputs are all NaN

Case B:

Snippet #2, When the stock has atleast 15 days of data:

>>> rsi_14 = ta.RSI(df['close'], timeperiod=14)
>>> rsi_14
shape: (15,)
Series: '' [f64]
[
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    50.0
]

# Successful execution, with acceptance of initial NaN in input.
>>> ta.MA(rsi_14, timeperiod=5)
shape: (15,)
Series: '' [f64]
[
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
    NaN
]

Since it's a natural progression from initial days to more trading days (case B), there shoun't be an exception here and 2nd indicator should just return with all NaN-s.

Problems due to this exception:

Possible fix: Avoid exception and return a series with all NaN. If backward compatibility is a concern (unlikely), then an argument ip_all_nan_exception with default True can be used. We will set it to False.

wukan1986 commented 1 year ago

I also have the same problem

mrjbq7 commented 1 year ago

Is the suggestion to silently handle inputs that are all NaN to just return outputs that are all NaN?

wukan1986 commented 1 year ago

Yes, just return all NaN, not to throw exception

mrjbq7 commented 1 year ago

I made this change and released it as 0.4.27. Please give it a try?

wukan1986 commented 1 year ago

@mrjbq7 Great Job! Thank you very much.