I am trying to use the TA-Lib in python on Ubuntu 12.04 as described in the official documentation. However, when using pandas DataFrame
s or Series
, as shown in multiple examples on different sources, I get the following TypeError
:
Traceback (most recent call last): File "test1.py", line 14, in analysis['rsi'] = ta.RSI(spy.Close) TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Series)
When executing e.g. this code:
import pandas.io.data as data
import pandas as pd
import talib as ta
import matplotlib.pyplot as plt
# Download SP500 data with pandas
spyidx = data.get_data_yahoo('SPY', '2013-01-01')
analysis = pd.DataFrame(index = spyidx.index)
analysis['rsi'] = ta.RSI(spyidx.Close)
What went wrong?
For pandas
>= 0.13.0:
Passing a Series directly to a cython function expecting an ndarray type will no long work directly, you must pass Series.values
So before TA-lib
revises its API to accommodate the newer pandas
versions, you need to use Series.values
or DataFrame.values
.
First you need to use abstract functions:
import talib.abstract as ta
instead of
import talib as ta
Second make sure you use correct names:
ta_serie = pd.DataFrame({
'high': _1_minute_tf.max_price,
'open': _1_minute_tf.open_price,
'close': _1_minute_tf.close_price,
'low': _1_minute_tf.min_price
})
Finally, enjoy:ta.SAR(ta_serie, window)
will give you what you expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With