布林线口诀的要点:
1、三线向上,中轨进,上轨出
2、三线向下,下轨进,中轨出
3、三轨走平,下轨进,上轨出
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_obv.py 如下
- # -*- coding: utf-8 -*-
- import os
- import sys
- import matplotlib.pyplot as plt
- import pandas as pd
- import tushare as ts
- import talib
-
- if len(sys.argv) ==2:
- code = sys.argv[1]
- else:
- print('usage: python talib_boll_obv.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['mid20'], df['lower'] = talib.BBANDS(close,
- timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
-
- # 量价指标: OBV (On-Balance Volume,净额成交量或叫能量潮指标)
- df['obv'] = talib.OBV(df.close, df.volume)
-
- # 取样 近期的数据
- d2 = df[ df['date'] >'2023-05-01']
- #print(d2.tail())
- print(" 向上突破 upper")
- print(d2[ d2.close > d2.upper ])
- print(" 向下突破 lower")
- print(d2[ d2.close < d2.lower ])
-
- d2.index = pd.to_datetime(d2.date)
- # 画股价-布林线图
- fig,axes = plt.subplots(2,1)
- d2[['close','upper','mid20','lower']].plot(ax=axes[0], grid=True, title=code)
- # 画股票OBV图
- d2[['obv']].plot(ax=axes[1], grid=True)
- plt.legend(loc='best', shadow=True)
- plt.show()
运行 python talib_boll_obv.py 002594
# 画基金净值-布林线图
编写 talib_boll.py 如下
- # -*- coding: utf-8 -*-
- import os
- import sys
- import tushare as ts
- import pandas as pd
- import matplotlib.pyplot as plt
- import numpy as np
- import talib
-
- if len(sys.argv) ==2:
- fcode = sys.argv[1]
- else:
- print('usage: python talib_boll.py fundcode ')
- sys.exit(1)
-
- if len(fcode) !=6:
- print('fund code length: 6')
- sys.exit(2)
-
- # 加这个两句 可以显示中文
- plt.rcParams['font.sans-serif'] = [u'SimHei']
- plt.rcParams['axes.unicode_minus'] = False
-
- f1 = fcode +'.csv'
- if not os.path.exists(f1):
- print("Error: %s not found." % f1)
- sys.exit(3)
-
- df = pd.read_csv(f1, parse_dates=True, index_col=0 )
- if len(df) <30:
- print(" len(df) <30 ")
- sys.exit(4)
-
- # 取基金净值
- jz = df['jz'].values
- # 计算布林线
- df['upper'], df['mid20'], df['lower'] = talib.BBANDS(jz,
- timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
- # 取样 近期的数据
- df2 = df[ df.index >'2022-01-01']
- print(df2.tail(5))
-
- # 画基金净值-布林线图
- df2[['jz','upper','mid20','lower']].plot(grid=True, title=fcode)
- plt.legend(loc='best', shadow=True)
- plt.show()
运行 python talib_boll.py 014423