vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 1
声望: 1

首先vnpy已经是一个非常完备的框架了,此处还是要感谢vnpy团队的无私奉献与努力,框架中有着数据录制功能,能够解决实时数据落盘的问题,不过如果需要历史数据进行回测,那就只有购买米筐的数据了,但是不花钱办大事一直是我的一贯追求(-_-!),虽说论坛里也有朋友对接了聚宽的数据源,但是到底只能试用一年的时间,所以我找了天勤量化的数据下载工具

代码基于vnpy2.1.5:

from vnpy.app.data_manager.engine import ManagerEngine
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.engine import MainEngine
from vnpy.event import EventEngine

from tqsdk import TqApi, TqSim
from tqsdk.tools import DataDownloader
from contextlib import closing
from datetime import datetime, timedelta

import os
import time

"""
获取所有已经结束交易的代码
"""

创建Api

api = TqApi(TqSim())

所有已经结束交易的代码

all_code = []
for code, info in api._data["quotes"].items():
if code.startswith("KQ.m@"):
all_code.append(code)

关闭资源

api.close()

"""
下载所有的结束交易的代码的1min线数据
"""

创建Api

api = TqApi(TqSim())

准备任务容器

download_tasks = dict()

创建任务

for count, code in enumerate(all_code):

#显示进度
print(code, count + 1, "/", len(all_code))

#处理代码
exchange, file_name = code.replace("KQ.m@", "").split(".")
if exchange == "CZCE":
    exchange = "郑州商品交易所"
if exchange == "SHFE":
    exchange = "上海期货交易所"
if exchange == "DCE":
    exchange = "大连商品交易所"
if exchange == "CFFEX":
    exchange = "中国金融期货交易所"
if exchange == "INE":
    exchange = "上海国际能源交易中心"

#获取今天时间
year, month, day = map(int, datetime.now().strftime("%Y-%m-%d").split("-"))

#下载1mink线
if not os.path.exists("D:/期货数据/k线数据/%s/主连合约/" % (exchange)):
    os.makedirs("D:/期货数据/k线数据/%s/主连合约/" % (exchange))
download_tasks[code] = DataDownloader(
    api,
    symbol_list=[code],
    dur_sec=60,
    start_dt=datetime(year, month, day, 0, 0, 0) - timedelta(days=7),
    end_dt=datetime(year, month, day, 0, 0, 0),
    csv_file_name="D:/期货数据/k线数据/%s/主连合约/%s.csv" % (exchange, file_name)
)

使用with closing机制确保下载完成后释放对应的资源

with closing(api):
while not all([v.is_finished() for v in download_tasks.values()]):
api.wait_update()
print("progress: ", {k.replace("KQ.m@", ""): ("%.2f%%" % v.get_progress()) for k, v in download_tasks.items() if
v.get_progress() < 100.0})

event_engine = EventEngine()
main_engine = MainEngine(event_engine)

engine = ManagerEngine(main_engine, event_engine)

for root, _, files in os.walk("D:/期货数据/k线数据"):

if len(files) == 0:
    continue
for file in files:
    file_path = os.path.join(root, file)

    if "上海国际能源交易中心" in root:
        exchange = Exchange.INE
        exchange_code = "INE"
    elif "上海期货交易所" in root:
        exchange = Exchange.SHFE
        exchange_code = "SHFE"
    elif "中国金融期货交易所" in root:
        exchange = Exchange.CFFEX
        exchange_code = "CFFEX"
    elif "大连商品交易所" in root:
        exchange = Exchange.DCE
        exchange_code = "DCE"
    elif "郑州商品交易所" in root:
        exchange = Exchange.CZCE
        exchange_code = "CZCE"
    else:
        continue

    with open(file_path, "r", encoding="utf-8") as old_file, open(file, "w", encoding="utf-8") as new_file:
        for line in old_file:
            line = line.replace(".000000000", "")
            new_file.write(line)

    try:
        print("ready: %s" % file)
        engine.import_data_from_csv(
            file_path=file,
            symbol=file.replace(".csv", "9999"),
            exchange=exchange,
            interval=Interval.MINUTE,
            datetime_head="datetime",
            open_head="KQ.m@%s.%s.open" % (exchange_code, file.replace(".csv", "")),
            high_head="KQ.m@%s.%s.high" % (exchange_code, file.replace(".csv", "")),
            low_head="KQ.m@%s.%s.low" % (exchange_code, file.replace(".csv", "")),
            close_head="KQ.m@%s.%s.close" % (exchange_code, file.replace(".csv", "")),
            volume_head="KQ.m@%s.%s.volume" % (exchange_code, file.replace(".csv", "")),
            open_interest_head="KQ.m@%s.%s.open_oi" % (exchange_code, file.replace(".csv", "")),
            datetime_format="%Y-%m-%d %H:%M:%S"
        )
        print("load: %s" % file)
    except Exception as e:
        print("lost: %s" % file)
        print(e)
        continue

    os.remove(file)
    os.remove(file_path)

print("完毕!!!")

event_engine.stop()
main_engine.close()

Member
avatar
加入于:
帖子: 18
声望: 0

👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻

Member
avatar
加入于:
帖子: 64
声望: 3

天勤也收费了,没有白嫖的源了....

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号