vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 2
声望: 0

小弟有EURUSD的1分钟线历史数据,想存入VNPY自带数据库
CSV文件的数据格式如下:

<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>
EURUSD,20010102,230100,0.9507,0.9507,0.9507,0.9507,4
EURUSD,20010102,230200,0.9506,0.9506,0.9505,0.9505,4
EURUSD,20010102,230300,0.9505,0.9507,0.9505,0.9506,4
EURUSD,20010102,230400,0.9506,0.9506,0.9506,0.9506,4
EURUSD,20010102,230500,0.9506,0.9506,0.9506,0.9506,4
EURUSD,20010102,230600,0.9506,0.9506,0.9506,0.9506,4
EURUSD,20010102,230700,0.9505,0.9507,0.9505,0.9507,4
EURUSD,20010102,230800,0.9507,0.9507,0.9507,0.9507,4

教程写入数据库的程序\load_data_36.py如下

import csv
import pytz
from datetime import datetime

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

CHINA_TZ = pytz.timezone("Asia/Shanghai")

with open("if_data.csv") as f:
reader = csv.DictReader(f)

bars = []
for d in reader:
    dt = datetime.strptime(d["datetime"], "%Y-%m-%d %H:%M:%S")
    dt = CHINA_TZ.localize(dt)

    bar = BarData(
        symbol=d["symbol"],
        exchange=Exchange(d["exchange"]),
        interval=Interval.MINUTE,
        datetime=dt,
        open_price=d["open"],
        high_price=d["high"],
        low_price=d["low"],
        close_price=d["close"],
        volume=d["volume"],
        open_interest=d["open_interest"],
        gateway_name="DB"
    )
    bars.append(bar)

database_manager.save_bar_data(bars)
print(f"完成数据插入,起始点{bars[0].datetime},结束点{bars[-1].datetime},总数据量{len(bars)}")

请问我该如何改写程序?谢谢各位大神回复

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

我自己已经完成了 哈哈 新手第一次完成改写
需要在CSV文件中 补充两个字段exchange 和 open_interest

代码如下

import csv
import pytz
from datetime import datetime

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

CHINA_TZ = pytz.timezone("Asia/Shanghai")

with open("data.csv") as f:
reader = csv.DictReader(f)

bars = []
for d in reader:
    dt = datetime.strptime(d["<DTYYYYMMDD>"]+d["<TIME>"].zfill(6), "%Y%m%d%H%M%S")
    dt = CHINA_TZ.localize(dt)

    bar = BarData(
        symbol=d["<TICKER>"],
        exchange=Exchange(d["<EXCHANGE>"]),
        interval=Interval.MINUTE,
        datetime=dt,
        open_price=d["<OPEN>"],
        high_price=d["<HIGH>"],
        low_price=d["<LOW>"],
        close_price=d["<CLOSE>"],
        volume=d["<VOL>"],
        open_interest=d["<OPEN_INTEREST>"],
        gateway_name="DB"
    )
    bars.append(bar)

database_manager.save_bar_data(bars)
print(f"完成数据插入,起始点{bars[0].datetime},结束点{bars[-1].datetime},总数据量{len(bars)}")
© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号