通达信导出的csv中添加一行表头:date,time,open,high,low,close,volume,open_interest,turnover
通达信的数据和vnpy的数据录制规则不同,所以中间对通达信数据做了两个处理:
1、时间减1分钟;
2、夜盘的日期减1天。

注:代码基于vnpy2.7版本

import csv
from datetime import datetime, timedelta
from pytz import timezone

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

# 封装函数
def load_tdx_csv_to_mongodb(file_path: str, exchange: str, symbol:str, interval:str):

    with open(file_path, "rt") as f:
        buf = [line.replace("\0", "") for line in f]

    reader = csv.DictReader(buf, delimiter=",")

    bars = []
    start = None
    count = 0
    tz = timezone("Asia/Shanghai")

    for item in reader:

        dt = datetime.strptime(item["date"] + item["time"], '%Y/%m/%d%H%M')
        # minute - 1;
        dt = dt + timedelta(minutes=-1)
        # time > 15:00, day - 1
        if dt.hour > 15:
            dt = dt + timedelta(days=-1)
        dt = tz.localize(dt)

        bar = BarData(
            symbol=symbol,
            exchange=exchange,
            interval=interval,
            datetime=dt,

            open_price=float(item["open"]),
            high_price=float(item["high"]),
            low_price=float(item["low"]),
            close_price=float(item["close"]),
            volume=float(item["volume"]),
            turnover=float(item["turnover"]),
            open_interest=float(item["open_interest"]),

            gateway_name="DB",
        )
        bars.append(bar)

        # do some statistics
        count += 1
        if not start:
            start = bar.datetime
    end = bar.datetime
    print(f'{datetime.now()} Insert Bar start: {count} from {start} - {end}')

    # insert into database
    database: BaseDatabase = get_database()
    database.save_bar_data(bars)
    print(f'{datetime.now()} Insert Bar complete: {count} from {start} - {end}')

if __name__ == "__main__":

    # tdx csv表头:date,time,open,high,low,close,volume,open_interest,turnover
    load_tdx_csv_to_mongodb("D:\\new_tdx\\T0002\\export\\29#PL9.txt", Exchange.DCE, 'P99', Interval.MINUTE)
    load_tdx_csv_to_mongodb("D:\\new_tdx\\T0002\\export\\29#PL8.txt", Exchange.DCE, 'P88', Interval.MINUTE)
    load_tdx_csv_to_mongodb("D:\\new_tdx\\T0002\\export\\29#YL8.txt", Exchange.DCE, 'Y88', Interval.MINUTE)