量化自动化交易python学习笔记之(一)BaoStock使用A股K线数据股票代码sh.60000,四年历史数据,用于后期追溯测试和策略可行性
import baostock as bs import pandas as pd #### 登陆系统 #### lg = bs.login() stockCode = "sh.600000" #股票代码 start_date = '2017-06-01' #开始时间 end_date = '2021-12-31' #结束时间 frequency="d" #frequency="d"取日k线, adjustflag="3"#adjustflag="3"默认不复权 # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) #### 获取历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节 rs = bs.query_history_k_data_plus(stockCode, "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST", start_date , end_date, frequency , adjustflag) 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("F:/"+stockCode+"history_k_data"+end_date+"-"+end_date+".csv",encoding="gbk" ,index=False ) print(result) #### 登出系统 #### bs.logout()