I am trying to call some TA-lib(https://github.com/mrjbq7/ta-lib) functions through Polars so that the multiple stocks' technical indicators could possibly be calculated through Polars' parallel computing framework.
Here is the sample code
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()
])
The method 2 code snippet however failed to run and the error message was:
TypeError Traceback (most recent call last)
Input In [5], in <cell line: 17>()
14 def kama30() -> pl.Expr:
15 return talib.KAMA(pl.col("Close"), 30)
17 pl_df2 = pl_df.select([
18 pl.col("Close"),
---> 19 kama30()
20 ])
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.<locals>.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 someone could advise how this could be done properly.
Thanks!
There is no need to define a Polars function expression, all you need is to use the map function. Here is the working code