2
3

More than 1 year has passed since last update.

Google Colaboratory TA-Lib のエラー

Posted at

Google Colaboratory TA-Lib のエラー

Google Colaboratory の Python のバージョンが 3.9 にアップグレードされたことに伴うエラー

import talib as ta

実行時

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
__init__.pxd in numpy.import_array()

RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf . Check the section C-API incompatibility at the Troubleshooting ImportError section at https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility for indications on how to solve this problem .

During handling of the above exception, another exception occurred:

/usr/local/lib/python3.9/dist-packages/talib/__init__.py in <module>
     91 
     92 
---> 93 from ._ta_lib import (
     94     _ta_initialize, _ta_shutdown, MA_Type, __ta_version__,
     95     _ta_set_unstable_period as set_unstable_period,

talib/_func.pxi in init talib._ta_lib()

__init__.pxd in numpy.import_array()

ImportError: numpy.core.multiarray failed to import

なぜが import でエラーが出ます。
numpyをアップデートしても同じ。

pandas_ta の利用

こちらのライブラリを代わりに利用します。

!pip install pandas_ta

利用例はこんな感じ

TA-Lib のメソッドで大字だったのものは、pandas_taでは小文字になるようです。

import mplfinance as mpf
import datetime as dt
import pandas_ta as ta

df = get_stock_data(3099) # 三越伊勢丹HD
df["ma5"]   = ta.sma(df["Close"], 5) 
df["ma25"]  = ta.sma(df["Close"], 25) 
df["ma75"]  = ta.sma(df["Close"], 75) 

cdf = df[dt.datetime(2021,4,30):dt.datetime(2022,2,20)]
apd = { "MA5": mpf.make_addplot(cdf["ma5"], color="blue"),
         "MA25": mpf.make_addplot(cdf["ma25"], color="green"),
         "MA75": mpf.make_addplot(cdf["ma75"], color="red") }

fig, axes = mpf.plot(cdf, type="candle", figratio=(2,1), addplot=list(apd.values()), returnfig=True)
axes[0].legend([None]*(len(apd)+2))
handles = axes[0].get_legend().legendHandles
axes[0].legend(handles=handles[2:], labels=list(apd.keys()))
fig.show()

スクリーンショット 2023-04-13 3.52.56.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3