一个免费、开源的证券数据服务平台
安装
pip install baostock -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
获取A股K线数据
import baostock as bs import pandas as pd #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。 # 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag # 周月线指标:date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg rs = bs.query_history_k_data_plus("sz.000001", "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date='2020-10-10', end_date='2020-10-20', frequency="d", adjustflag="3") print('query_history_k_data_plus respond error_code:'+rs.error_code) print('query_history_k_data_plus respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 结果集输出到csv文件 #### result.to_csv("k_data.csv", index=False) print(result) #### 登出系统 #### bs.logout()
结果
>python test.py
login success!
login respond error_code:0
login respond error_msg:success
query_history_k_data_plus respond error_code:0
query_history_k_data_plus respond error_msg:success
date code open high low ... adjustflag turn tradestatus pctChg isST
0 2020-10-12 sz.000001 15.2200 16.0500 15.2100 ... 3 0.820000 1 4.743100 0
1 2020-10-13 sz.000001 15.9000 16.1100 15.7700 ... 3 0.468300 1 1.006300 0
2 2020-10-14 sz.000001 16.0400 16.1200 15.8000 ... 3 0.341400 1 -0.186800 0
3 2020-10-15 sz.000001 16.2000 16.9200 16.1500 ... 3 0.824500 1 3.306300 0
4 2020-10-16 sz.000001 16.5600 17.3700 16.5400 ... 3 1.079900 1 3.260900 0
5 2020-10-19 sz.000001 17.3000 18.1000 17.3000 ... 3 1.038900 1 2.222200 0
6 2020-10-20 sz.000001 17.4800 17.6000 17.2500 ... 3 0.494700 1 0.343200 0
[7 rows x 14 columns]
logout success!
使用mpl_finance画K线图
import baostock as bs import pandas as pd import mplfinance as mpf #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。 # 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag # 周月线指标:date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg rs = bs.query_history_k_data_plus("sz.000001", "date,code,open,high,low,close", start_date='2020-05-10', end_date='2020-10-20', frequency="d", adjustflag="3") print('query_history_k_data_plus respond error_code:'+rs.error_code) print('query_history_k_data_plus respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 结果集输出到csv文件 #### result.to_csv("k_data.csv", index=False) #### 登出系统 #### bs.logout() data = result.loc[:, ['date','open', 'high', 'low', 'close']] data=data.apply(pd.to_numeric, errors='ignore') data.set_index('date', inplace=True) data.index = pd.DatetimeIndex(data.index) mpf.plot(data, type='candle', style='charles')