ta-lib github项目地址:https://github.com/mrjbq7/ta-lib
mac-intel芯片:
brew install ta-lib
linux:
tar -xzf ta-lib-0.4.0-src.tar.gz cd ta-lib/ ./configure --prefix=/usr make sudo make install
其他系统(windows,mac-m1)请参考官网安装方法:https://github.com/mrjbq7/ta-lib#dependencies
pip install TA-Lib
新建一个python文件可以尝试运行下面的代码:
我的代码中使用efinance
来获取数据,首先pip安装efinance
:
pip install efinance
import matplotlib.pyplot as plt import talib as ta import pandas as pd def get_k_data() -> pd.DataFrame: import efinance # 这里需要 pip install efinance stock_code = '600519' # 股票代码,茅台 k_dataframe: pd.DataFrame = efinance.stock.get_quote_history(stock_code, beg='20100101', end='20100201', ) k_dataframe = k_dataframe.iloc[:, :9] k_dataframe.columns = ['name', 'code', 'date', 'open', 'close', 'high', 'low', 'volume', 'turnover'] k_dataframe.index = pd.to_datetime(k_dataframe.date) k_dataframe.drop(['name', 'code', "date"], axis=1, inplace=True) return k_dataframe if __name__ == '__main__': k_data = get_k_data() ema_value = ta.EMA(k_data['close'], timeperiod=5) # 开始绘图 plt.plot(k_data['close'], label="close") plt.plot(ema_value, label="EMA") plt.legend() plt.show()
这里通过收盘价,借助talib模块得到EMA的曲线,效果图如下: