python 安装使用 TA_lib
安装主要在 http://www.lfd.uci.edu/~gohlke/pythonlibs/
这个网站找到
TA_Lib-0.4.24-cp310-cp310-win_amd64.whl
pip install /pypi/TA_Lib-0.4.24-cp310-cp310-win_amd64.whl
编写 talib_boll.py 如下
# -*- coding: utf-8 -*-
import os
import sys
import matplotlib.pyplot as plt
import pandas as pd
import tushare as ts
import talib as ta
if len(sys.argv) ==2:
code = sys.argv[1]
else:
print('usage: python stock_price.py stockcode ')
sys.exit(1)
if len(code) !=6:
print('stock code length: 6')
sys.exit(2)
# help(ts.get_k_data) 了解参数
df = ts.get_k_data(code, start='2023-01-01')
if len(df) <30:
print(" len(df) <30 ")
sys.exit(2)
# 取收盘价
close = df['close'].values
# 计算布林线
df['upper'], df['mid'], df['lower'] = ta.BBANDS(close,
timeperiod=10, nbdevup=2, nbdevdn=2, matype=0)
# 取样 近期的数据
d2 = df[ df['date'] >'2023-07-01']
#print(d2.tail())
d2.index = pd.to_datetime(d2.date)
# 画股价-布林线图
fig,axes = plt.subplots(2,1)
d2[['close','upper','mid','lower']].plot(ax=axes[0], grid=True, title=code)
# 画股票成交量图
d2[['volume']].plot(ax=axes[1], grid=True)
plt.show()
运行 python talib_boll.py 002594