TA-Lib / ta-lib-python

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

Failed to use ta-lib as Polars expression #511

Closed peascod206 closed 2 years ago

peascod206 commented 2 years ago

I am trying to use ta-lib as a Polars expression so that I could possibly leverage the cool parallel processing feature of Polars. Here is the sample code I used to do this

###################### import talib import polars as pl import yfinance as yf

tesla = yf.Ticker('TSLA') tesla_data = tesla.history(period="1Y") tesla_data["Date"]=tesla_data.index pl_df = pl.from_pandas(tesla_data[["Date", "Open", "High", "Low", "Close", "Volume"]])

Method 1. Using ta-lib as a direct function call.

mv_kama = talib.KAMA(pl_df["Close"], 30)

Method 2. Using ta-lib as Polars expression

def kama30() -> pl.Expr: return talib.KAMA(pl.col("Close"), 30)

pl_df2 = pl_df.select([ pl.col("Close"), kama30() ]) ################################

In method 2, ta-lib KAMA function is wrapped as a Polars expression. The code however failed to run properly and the error msg is:

Input In [5], in kama30() 14 def kama30() -> pl.Expr: ---> 15 return talib.KAMA(pl.col("Close"), 30)

File C:\ProgramData\Anaconda3\envs\Charm3.9\lib\site-packages\talib__init__.py:64, in _wrapper..wrapper(*args, *kwds) 61 _args = args 62 _kwds = kwds ---> 64 result = func(_args, **_kwds) 66 # check to see if we got a streaming result 67 first_result = result[0] if isinstance(result, tuple) else result

TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Expr)

Appreciate if anyone could advise how this should be done properly.

Thanks!

mrjbq7 commented 2 years ago

We don't support polars.Expr right now -- currently only supporting polars.Series and polars.DataFrame... and that support converts those to numpy.ndarray before calling the talib functions.

mrjbq7 commented 2 years ago

Contributions welcome!

mrjbq7 commented 2 years ago

Here's how to do it with polars.Expr, using any of the TA-Lib releases:

df.select(
    [
        pl.col("Close").map(lambda s: ta.KAMA(s, 30)),
    ]
)