VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 44
声望: 0

平时回测和研究用脚本比较多,下载数据从ui下载不是很方便,就写了个脚本,可以更新数据多个/单个合约数据:

from datetime import datetime
from tzlocal import get_localzone

from vnpy.trader.object import HistoryRequest
from vnpy.trader.database import database_manager
from vnpy.trader.constant import Interval, Exchange

from vnpy_pro.data.source.tdxdata import tdxdata_client
from vnpy_pro.data.tdx.tdx_common import get_future_contracts

# 下载合约
futures = ["AG", "AP", "AU", "BU", "CF", "CU", "JD",
           "MA", "NI", "OI", "P", "RB", "RU", "SR", "TA"]
# futures = ["AG", "AP", "AU", "BU", "CF", "CU", "JD",
#            "MA", "NI", "OI", "P", "RB", "RU", "SR", "TA"]

future_contracts = get_future_contracts()
if tdxdata_client.init():
    print("数据服务器登录成功")
else:
    print("数据服务器登录失败")
    import sys
    sys.exit(0)

for future in futures:
    symbol = future.upper() + "99"
    exchange = Exchange.__dict__[future_contracts[future]["exchange"]]
    interval = Interval.MINUTE

    # 查询数据库中的最新数据
    # start = datetime(2010, 6, 1)
    # 增量更新数据
    bar = database_manager.get_newest_bar_data(symbol, exchange, interval)
    if bar:
        start = bar.datetime
    else:
        start = datetime(2012, 1, 1)

    # 下载数据
    req = HistoryRequest(
        symbol,
        exchange,
        start,
        datetime.now(),
        interval=interval
    )
    data = tdxdata_client.query_history(req)

    # 写入数据库
    if data:
        database_manager.save_bar_data(data)
        print(f"{symbol}更新完成:{data[0].datetime} -- {data[-1].datetime}")

# 更新contracts字典
# tdxdata_client.tdx_api.update_mi_contracts()

download_or_update_data.py

Member
avatar
加入于:
帖子: 50
声望: 1

vnpy_pro 这个包是自己安装的吗?

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

晴空 wrote:

vnpy_pro 这个包是自己安装的吗?
是扩展的,代码在我github上有哈

Member
avatar
加入于:
帖子: 50
声望: 1

找到了,多谢哈

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

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】