vn.py官网
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 17
声望: 0

RT
附代码,能帮我看下是哪里出了问题吗?萌新第一次提问,还请大神斧正。

`from typing import Any
from vnpy.app.cta_strategy import (
CtaTemplate, BarGenerator, ArrayManager,
)
from vnpy.trader.object import (
BarData
)
from vnpy.trader.constant import Interval

class Bollinger_bias(CtaTemplate):

author = 'SteveZhao'

# 定义参数
period = 480
band_width = 1.5
bias_pct = 0.1

# 定义变量
median0, median1 = 0.0, 0.0
upper0, upper1 = 0.0, 0.0
lower0, lower1 = 0.0, 0.0

parameters = ["period", "band_width", "bias_pct"]
variables = ["median0", "median1", "upper0", "upper1", "lower0", "lower1"]

def __init__(self, cta_engine: Any, strategy_name: str, vt_symbol: str, setting: dict):
    super.__init__(cta_engine, strategy_name, vt_symbol, setting)
    self.bg = BarGenerator(self.on_bar, window=15, on_window_bar=self.on_15min_bar, interval=Interval.MINUTE)
    self.am = ArrayManager()

def on_init(self):
    self.write_log("策略初始化")
    self.load_bar(480)

def on_start(self):
    self.write_log("策略启动")

def on_stop(self):
    self.write_log("策略结束")

def on_bar(self, bar: BarData):
    self.bg.update_bar(bar)

def on_15min_bar(self, bar:BarData):
    self.cancel_all()

    am = self.am
    am.update_bar(bar)
    if not am.inited:
        return

    # 计算技术指标
    median = am.sma(self.period, array=True)
    self.median0 = median[-1]
    self.median1 = median[-2]

    self.boll_up, self.boll_down = am.boll(self.period, self.band_width)
    self.upper0 = self.boll_up[-1]
    self.upper1 = self.boll_up[-2]
    self.lower0 = self.boll_down[-1]
    self.lower1 = self.boll_down[-2]

    close0, close1 = am.close[-1], am.close[-2]
    cross_over = (close0 >= self.upper0 and close1 < self.upper1)
    cross_below = (close0 <= self.lower0 and close1 > self.lower1)

    if cross_over:
        if (close0 / self.median0 - 1) <= self.bias_pct:
            price = bar.close_price * 1.02
            if not self.pos:
                self.buy(price, 1)
            elif self.pos < 0:
                self.cover(price, 1)
                self.buy(price, 1)


    elif cross_below:
        if (close0 / self.median0 - 1) <= self.bias_pct:
            price = bar.close_price * 0.98
            if not self.pos:
                self.short(price, 1)
            elif self.pos > 0:
                self.sell(price, 1)
                self.short(price, 1)

    self.put_event()

`
图形界面上跑回测没有什么反应
description

Member
avatar
加入于:
帖子: 2747
声望: 190

底层报错了,请用run.py或者在cmd运行命令行python -m vnstation再重复之前的操作看一下底层报错信息

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

description

Member
avatar
加入于:
帖子: 343
声望: 25

super后面没有加括号,请参考示例策略核对。

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

沪公网安备 31011502017034号

【用户协议】
【隐私政策】