import baostock as bs
import pandas as pd

#function description:  get stock history information  from baostock by API
#
# 登陆系统
lg = bs.login()
# 显示登陆返回信息
print('login respond error_code:'+lg.error_code)
print('login respond  error_msg:'+lg.error_msg)

# # 获取沪深300成分股
# rs = bs.query_hs300_stocks()
# print('query_hs300 error_code:'+rs.error_code)
# print('query_hs300  error_msg:'+rs.error_msg)
#
# # 打印结果集
# hs300_stocks = []
# while (rs.error_code == '0') & rs.next():
#     # 获取一条记录，将记录合并在一起
#     hs300_stocks.append(rs.get_row_data())
# result = pd.DataFrame(hs300_stocks, columns=rs.fields)
# # 结果集输出到csv文件
# result.to_csv("./data/stock/hs300_stocks.csv", encoding="gbk", index=False)
# print(result)


#### 获取沪深A股历史K线数据 ####
# 详细指标参数，参见“历史行情指标参数”章节；“分钟线”参数与“日线”参数不同。“分钟线”不包含指数。
# 分钟线指标：date,time,code,open,high,low,close,volume,amount,adjustflag
# 周月线指标：date,code,open,high,low,close,volume,amount,adjustflag,turn,pctChg

stock = "000001"
file_name = "SZ"+stock+".csv"

rs = bs.query_history_k_data_plus("sz."+stock,
    "Date,Close,High,Low,Open,Volume",
    # "date,close,high,low,open,volume",
    # "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
    start_date='2022-07-01', end_date='2023-09-14',
    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("./data/stock1/"+file_name, index=False)
print(result)


# 登出系统
bs.logout()