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

description

description

陈老师,我模仿你课程中cuatro策略来写自己的跨周期策略。
on_15min_bar中的代码和你写的一模一样,为何我回测时却报错了呢?

Administrator
avatar
加入于:
帖子: 4550
声望: 325

self.am15.update_bar(bar)

这句代码里的am15是你自定义的一个ArrayManager?检查下update_bar函数的入参是不是写错了

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

是的,self.am15.update_bar(bar)这句代码里的am15是我自定义的一个ArrayManager。
因为是跨周期,所以定义了两个,一个am5、一个am15.

另外,陈老师,【检查下update_bar函数的入参是不是写错了】这句话看不懂啥意思,我是新手。能不能说的再详细一些呢?
(我直接点击了代码中的update_bar,跳转到了class ArrayManager(object):这个文件中,里面的东西我没动)

Administrator
avatar
加入于:
帖子: 4550
声望: 325

你这里的update_bar报错,是说缺少关键字参数,而vn.py自身定义的ArrayManager.update_bar函数是没有用到的,所以想问下你是否自行修改了这个函数。

贴下你的self.am15的初始化创建的代码?

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

我没有修改vnpy自带的内容,我也怕弄坏了。所以只是在写自己的策略代码。
我现在回测是各种报错,索性把所有的代码都发上来吧,
麻烦老师帮忙看一下。

from vnpy.app.cta_strategy import(
CtaTemplate,
BarGenerator,
ArrayManager,
TickData,
BarData,
OrderData,
TradeData,
StopOrder
)

class TestTwoTime(CtaTemplate):
""""""
author = "vnpy"

fast_window = 30
slow_window = 200
last_length = 155
volume_ma = 200
atr_length = 30
atr_length_ma = 200
volume_multiple = 2
atr_mutiple = 4
fixed_size = 1
fixed_sl = 200

fast_ma = 0
slow_ma = 0
ma_trend = 0
volume_value = 0
atr_value = 0
volume_value = 0
intra_trade_high = 0
intra_trade_low = 0
long_stop = 0
short_stop = 0
entity = 0
bodong = 0

parameters = [
    "fast_window",
    "slow_window",
    "last_length",
    "volume_ma",
    "atr_length",
    "atr_length_ma",
    "volume_multiple",
    "atr_mutiple",
    "fixed_size",
    "fixed_sl"
]

variables = [
    "fast_ma",
    "slow_ma",
    "ma_trend",
    "volume_value",
    "atr_value",
    "volume_value",
    "intra_trade_high",
    "intra_trade_low",
    "long_stop",
    "short_stop",
    "entity",
    "bodong"
]

def __init__(
    self,
    cta_engine,
    strategy_name: str,
    vt_symbol: str,
    setting: dict,
):
    super().__init__(cta_engine, strategy_name, vt_symbol, setting)

    self.bg5 = BarGenerator(self.on_bar, 5, self.on_5min_bar)
    self.bg15 = BarGenerator(self.on_bar, 15, self.on_15min_bar)

    self.am5 = ArrayManager()
    self.am15 = ArrayManager

def on_init(self):
    """
    Callback when strategy is inited. 
    """
    self.write_log("策略初始化")
    self.load_bar(10)

def on_start(self):
    """
    Callback when strategy is started. 
    """
    self.write_log("策略启动")

def on_stop(self):
    """
    Callback when strategy is stopped. 
    """
    self.write_log("策略停止")

def on_tick(self, tick: TickData):
    """
    Callback of new tick data update. 
    """
    self.bg5.update_tick(tick)
    print("tick合成到bg5中")

def on_bar(self, bar: BarData):
    """
    Callback of new bar data update. 
    """
    self.bg5.update_bar(bar)    
    self.bg15.update_bar(bar)    
    print("bar合成bg5、bg15")

def on_5min_bar(self, bar: BarData):
    """"""
    self.cancel_all()
    print("取消全部委托")

    self.am5.update_bar(bar)

    if not self.am5.inited or not self.am15.inited:
        return

    # 计算atr指标
    atr_array = self.am5.atr(self.atr_length, array=True)
    self.atr_value = atr_array[-1]
    self.atr_ma = atr_array[-self.atr_length_ma-1:-1].mean()
    print("计算了atr指标")

    # 计算成交量
    volume = self.am5.volume
    self.volume_value = volume[-1]
    self.volume_ma = volume[-self.last_length-1:-1].mean()
    print("计算了成交量指标")

    # 计算K线实体和总波动
    self.entity = abs(bar.close_price - bar.open_price)
    print("计算K线实体部分")
    self.bodong = max(self.am5.close[-2], bar.high_price) - min(self.am5.close[-2], bar.low_price)
    print("计算K线总的波动")


    # 计算仓位
    if not self.pos:
        self.intra_trade_high = bar.high_price
        self.intra_trade_low = bar.low_price
        self.long_stop = 0
        self.short_stop = 0

        if self.ma_trend > 0 and self.am5.close[-1] >= self.am5.close[-self.last_length-1:-1].max():
            if self.atr_value > self.atr_ma and self.volume_value > self.volume_ma * 2 and self.entity / self.bodong >= 0.6:
                self.buy(self.bar.close_price, self.fixed_size, stop=True)

        elif self.ma_trend < 0 and self.am5.close[-1] <= self.am5.close[-self.last_length-1:-1].min():
            if self.atr_value > self.atr_ma and self.volume_value > self.volume_ma * 2 and self.entity / self.bodong >= 0.6:
                self.short(self.bar.close_price, self.fixed_size, stop=True)

    # 多头持仓
    elif self.pos > 0:
        self.intra_trade_high = max(self.intra_trade_high, bar.close_price)
        self.long_stop = max(self.buy - self.fixed_sl, self.intra_trade_high - self.atr_value * self.atr_multiple)
        self.sell(self.long_stop, self.fixed_size, stop=True)
        print("有多单时,多单平仓")

    # 空头持仓
    else:
        self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
        self.short_stop = min(self.short + self.fixed_sl, self.intra_trade_low + self.atr_value * self.atr_multiple)
        self.cover(self.short_stop, self.fixed_size, stop=True)
        print("有空单时,开空单")

    self.put_event()
    print("刷新下界面")

def on_15min_bar(self, bar: BarData):
    """"""
    self.am15.update_bar(bar)
    print("将bar更新,并推送到am15中")

    if not self.am15.inited:
        return

    self.fast_ma = self.am15.sma(self.fast_window)
    self.slow_ma = self.am15.sma(self.slow_window)
    print("计算了均线多空")

    if self.fast_ma > self.slow_ma:
        self.ma_trend = 1

    elif self.fast_ma < self.slow_ma:
        self.ma_trend = -1

    else:
        self.ma_trend = 0

    self.put_event()
    print("刷新截面")

def on_order(self, order: OrderData):
    """
    Callback of new order data update.
    """
    pass

def on_trade(self, trade: TradeData):
    """
    Callback of new trade data update. 
    """
    self.put_event()  
    print("刷新trade界面")

def on_stop_order(self, stop_order: StopOrder):
    """
    Callback of stop order update. 
    """
    pass
Administrator
avatar
加入于:
帖子: 4550
声望: 325

self.am15 = ArrayManager()

漏了后面的括号。。。。你得把类实例化成对象才能用啊

Administrator
avatar
加入于:
帖子: 4550
声望: 325

粗心大意害死人啊,看你折腾这么多天

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

沪公网安备 31011502017034号

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