这里更新《30天解锁python量化开发》课程中与现版本(2.6.0)不符的地方,如果还有其他跟课程相关的问题,欢迎在该贴进行补充。

(1)由于2.6.0版本将一些应用模块进行了剥离,因此如果课程中提及的目录不在vnpy目录下,可以在vnpy同级目录,也就是site-packages目录下寻找

(2) 课程中的代码如果出现导入包错误,如from vnpy.gateway.ctp import CtpGateway,说明该包已经被剥离出去了,应该修改为from vnpy_ctp import CtpGateway。其他同样被剥离出去的包也应该用类似的方式导入即:from vnpy_xxx import xxxx

(3)课时36-载入CSV历史数据中的load_data_36代码应修改为:

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 get_database

database_manager = get_database()
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)}")

(3)课时44-日志引擎中代码run_44应该修改为:

# flake8: noqa
from vnpy.event import EventEngine

from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp

from vnpy.gateway.ctp import CtpGateway
from vnpy_ctastrategy import CtaStrategyApp
from vnpy_ctastrategy.base import EVENT_CTA_LOG


def main():
    """"""
    qapp = create_qapp()

    event_engine = EventEngine()

    main_engine = MainEngine(event_engine)
    main_engine.add_gateway(CtpGateway)
    main_engine.add_app(CtaStrategyApp)

    log_engine = main_engine.get_engine("log")
    event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event)

    main_window = MainWindow(main_engine, event_engine)
    main_window.showMaximized()

    qapp.exec()


if __name__ == "__main__":
    main()