下载市场交易日明细
官方原版
下载"1990-01-01"至"2022-03-10"是否交易日的明细。 保存为: From19900101To20220310.csv 文件
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)
rs = bs.query_trade_dates(start_date="1990-01-01", end_date="2022-03-10")
print('query_trade_dates respond error_code:'+rs.error_code)
print('query_trade_dates 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)
result.to_csv("./From19900101To20220310.csv", encoding="utf-8", index=False)
print(result)
bs.logout()
加工改造后
对query_trade_dates() 进行封装构造新的函数TradeDates2Csv()
TradeDates2Csv()
输入参数: input_start_date: 开始日期,格式: yyyy-mm-dd input_end_date: 结束日期,格式: yyyy-mm-dd input_path: 保存CSV文件路径
def TradeDates2Csv(input_start_date, input_end_date, input_path):
lg = bs.login()
print('login respond error_code:' + lg.error_code)
print('login respond error_msg:' + lg.error_msg)
rs = bs.query_trade_dates(start_date= input_start_date, end_date= input_end_date)
print('query_trade_dates respond error_code:' + rs.error_code)
print('query_trade_dates 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)
dirname, file_ab_name = os.path.split(input_path)
filename, file_ext_name = os.path.splitext(file_ab_name)
if file_ext_name.lower()== '.csv' :
result.to_csv(input_path, encoding="utf-8", index=False)
print(result)
bs.logout()
else:
print("请确认保存路径的文件名")
bs.logout()
为保存csv文件路径做路径拼接
输入参数: input_path_folder: 文件夹路径 input_file_name: 保存的文件名 input_ext_name = None 文件的拓展名默认为空
FilePathJoin()
def FilePathJoin(input_path_folder, input_file_name, input_ext_name = None):
if input_ext_name is None:
result_path = os.path.join(input_path_folder, input_file_name.strip())
else:
file_full_name = input_file_name.strip() + '.'+ input_ext_name.strip()
result_path = os.path.join(input_path_folder, file_full_name)
重新构造查询结果导出交易日明细
文件架构如下图所示:
源代码:
import baostock as bs
import pandas as pd
import os
import time
import datetime
import calendar
def TradeDates2Csv(input_start_date, input_end_date, input_path):
lg = bs.login()
print('login respond error_code:' + lg.error_code)
print('login respond error_msg:' + lg.error_msg)
rs = bs.query_trade_dates(start_date= input_start_date, end_date= input_end_date)
print('query_trade_dates respond error_code:' + rs.error_code)
print('query_trade_dates 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)
dirname, file_ab_name = os.path.split(input_path)
filename, file_ext_name = os.path.splitext(file_ab_name)
if file_ext_name.lower()== '.csv' :
result.to_csv(input_path, encoding="utf-8", index=False)
print(result)
bs.logout()
else:
print("请确认保存路径的文件名")
bs.logout()
def FilePathJoin(input_path_folder, input_file_name, input_ext_name = None):
if input_ext_name is None:
result_path = os.path.join(input_path_folder, input_file_name.strip())
else:
file_full_name = input_file_name.strip() + '.'+ input_ext_name.strip()
result_path = os.path.join(input_path_folder, file_full_name)
return result_path
def time_offset_cal(input_time, offset, time_type):
if time_type == 'year':
re_dt = input_time + relativedelta(years=offset)
return re_dt
elif time_type == 'week':
re_dt = input_time + datetime.timedelta(weeks=offset)
return re_dt
elif time_type == 'month':
re_dt = input_time + relativedelta(months=offset)
return re_dt
elif time_type == 'day':
re_dt = input_time + datetime.timedelta(days=offset)
return re_dt
elif time_type == 'hour':
re_dt = input_time + datetime.timedelta(hours=offset)
return re_dt
elif time_type == 'minute':
re_dt = input_time + datetime.timedelta(minutes=offset)
return re_dt
elif time_type == 'second':
re_dt = input_time + datetime.timedelta(seconds=offset)
return re_dt
elif time_type == 'microsecond':
re_dt = input_time + datetime.timedelta(microseconds=offset)
return re_dt
elif time_type == 'millisecond':
re_dt = input_time + datetime.timedelta(milliseconds=offset)
return re_dt
else:
print("error")
if __name__ == "__main__":
DstFolder = "./dstFolder/downTradeDates2Csv"
FileName = 'MarketTradeDay'
ExtName = 'csv'
config_start_day = '1990-01-01'
today_date = datetime.datetime.now()
config_end_day = time_offset_cal(today_date, -1, 'day').strftime('%Y-%m-%d')
str_ts = str(calendar.timegm(time.localtime()))
ResultFileName = FileName+ '#'+ config_end_day+ '@'+ str_ts
TradeDates2Csv(config_start_day, config_end_day, FilePathJoin(DstFolder, ResultFileName, ExtName))
|