利用天勤量化下载期货全品种的tick信息历史行情

原文:下载期货全品种的tick信息历史行情!!!

from datetime import datetime
from contextlib import closing
from tqsdk import TqApi, TqSim
from tqsdk.tools import DataDownloader
import os
import time
import zipfile

print("请将所需下载合约,写入记事本,1.txt")
zhouqi=input("请输入需要下载的周期,0代表tick,60代表1分钟,86400代表日线")
qishi=input("请输入起始年月日,比如2019-01-01:")
zhongzhi=input("请输入终止年月日,比如2019-01-01:")
while True:
    print("请将所需下载合约,写入记事本,1.txt")
    try:
        qishi=qishi.split("-")
        zhongzhi=zhongzhi.split("-")
        f=open("1.txt","r")
        datazong=f.read()
        datazong=datazong.split(",")
        hehe=os.listdir()
        datazong=[     x    for x in datazong  if x+".csv" not in hehe]
        if datazong==[]:
            break
        datazong=[ datazong[x:x+2]  for x in (0,len(datazong),2)]
        for data in datazong:
            print("wunai")
            download_tasks = {}
            api = TqApi(TqSim())
            for x in data:
                download_tasks[x]=DataDownloader(api, symbol_list=[x], dur_sec=int(zhouqi),start_dt=datetime(int(qishi[0]), int(qishi[1]), int(qishi[2])), end_dt=datetime(int(zhongzhi[0]), int(zhongzhi[1]), int(zhongzhi[2])), csv_file_name=x+".csv")
            with closing(api):
                while not all([v.is_finished() for v in download_tasks.values()]):
                    time.sleep(0.1)
                    api.wait_update(deadline=1)
                    print("progress: ", { k:("%.2f%%" % v.get_progress()) for k,v in download_tasks.items() })
            for x in data:
                zp = zipfile.ZipFile(x+'.zip','w',zipfile.ZIP_DEFLATED)
                zp.write(x+".csv")
                zp.close()
                f=open(x+".csv","w")
                f.close()
    except:
        time.sleep(60)

发表评论