1.在python等环境安装好的情况下,下载安装jqdatasdk,提供下载到本地安装方式:
到https://github.com/JoinQuant/jqdatasdk
下载文件到本地
2.cmd到解压文件夹下运行 python setup.py install
3.到python环境中写代码(本人使用pycharn python IDE)
代码参考如下:
import jqdatasdk
import os
import pandas as pd
jqdatasdk.auth("user","password")
#起始时间设置
date_start = []
date_end = []
for date_str in range(2016,2020):
date_start.append(str(date_str)+"-01-01")
date_end.append(str(date_str)+"-12-31")
#获取文件夹下文件函数
def file_name(file_dir):
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.csv':
L.append(file.split(".")[0])
return L
if __name__ == '__main__':
# feature_index = jqdatasdk.get_all_securities(['futures'] # 获取所有期货标的
# feature_index["index1"] = feature_index.index
# feature_index.to_excel("所有期货合约.xlsx",index=False)#将标的保存至本地,根据标的起始日期 添加此标的需要下载的年份数据是否存在
feature_index = pd.read_excel("所有期货合约.xlsx")
index_1 = list(feature_index["index1"])
name_1 = list(feature_index["name"])
c_name = list(feature_index["display_name"])
for d in range(len(date_start)-1):
try:
os.makedirs("期货1分钟数据/"+date_start[d].split("-")[0])
except:
pass
new_index1 = feature_index[feature_index["is_"+date_start[d].split("-")[0]]==1]
print(len(new_index1))
already_index = file_name("期货1分钟数据/"+date_start[d].split("-")[0])
new_index = list(set(list(new_index1["name"]))-set(already_index))
print(len(new_index))
for i in new_index:
sub = feature_index[feature_index["name"]==i]
df = jqdatasdk.get_price(security = list(sub["index1"])[0], start_date=date_start[d],end_date=date_start[d+1], frequency='1m', fields=['open', 'close', 'high', 'low', 'volume', 'money'],skip_paused=True)
if len(df)>0:
df["name"] = list(sub["name"])[0]
df["time"] = df.index
df = df.loc[:,["name","time",'open', 'close', 'high', 'low', 'volume', 'money']]
df.to_csv("期货1分钟数据/"+date_start[d].split("-")[0]+"/"+list(sub["name"])[0]+".csv",index=False)
保存结果如下:
期货标的文件如下: