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

这里更新《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()
Member
avatar
加入于:
帖子: 6
声望: 0

第23课

用CSV读取数据并写入数据库

from datetime import datetime
from csv import DictReader

with open("rb_data.csv") as f:
reader = DictReader(f)
bars = []
for row in reader:
dt = datetime.strptime(row["datetime"], "%Y-%m-%d %H:%M:%S")
bar = BarData(
symbol=row["symbol"],
exchange=Exchange(row["exchange"]),
datetime=DB_TZ.localize(dt),
interval=Interval.MINUTE,
open_price=float(row["open"]),
high_price=float(row["high"]),
low_price=float(row["low"]),
close_price=float(row["close"]),
volume=row["volume"],
open_interest=row["open_interest"],
gateway_name="DB"
)
bars.append(bar)

get_database.save_bar_data(bars)

这里报错
AttributeError: 'function' object has no attribute 'save_bar_data'

Member
avatar
加入于:
帖子: 1468
声望: 105

漏了括号:

get_database().save_bar_data(bars)
Member
avatar
加入于:
帖子: 1
声望: 0

仔细 不错

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

赞一个

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

沪公网安备 31011502017034号

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