ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

Failed to use ta-lib as Polars expression

Open peascod206 opened this issue 3 years ago • 2 comments

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!

peascod206 avatar Apr 13 '22 08:04 peascod206

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 avatar Apr 15 '22 00:04 mrjbq7

Contributions welcome!

mrjbq7 avatar Apr 15 '22 00:04 mrjbq7

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)),
    ]
)

mrjbq7 avatar Sep 14 '22 16:09 mrjbq7