vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 1
声望: 0

这是一个利用2小时k线,上下穿60日均线来决定买入和卖出的策略。数据是自己导入的,经其他策略测试,没有问题。
结果却只有一笔成交,不知道错在哪里,望大神指点一下,不胜感激!

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

class DemoStrategy(CtaTemplate):

#定义参数
day_window = 60
hour_window = 2
fixed_size = 1
#定义变量
day_window_ma = 0

parameters = [
    "day_window",
    "hour_window",
    "fixed_size"
]
variables = [
    "day_window_ma"
]

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_day = BarGenerator(self.on_bar, 1, self.on_day_bar, interval=Interval.DAILY)
    self.bg_hour = BarGenerator(self.on_bar, self.hour_window, self.on_hour_bar, interval=Interval.HOUR)
    self.am = ArrayManager()

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

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

def on_stop(self):
    """策略停止"""
    self.write_log("策略停止")

def on_tick(self, tick: TickData):
    self.bg_day.update_tick(tick)
    self.bg_hour.update_tick(tick)

def on_bar(self, bar:BarData):
    """K线更新"""
    self.bg_day.update_bar(bar)
    self.bg_hour.update_bar(bar)


def on_hour_bar(self, bar:BarData):


    if not self.am.inited:
        return

    self.day_window_ma = self.am.close_array[-self.day_window:-1].mean()

    cross_over = (bar.open_price <= self.day_window_ma) and (bar.close_price > self.day_window_ma)
    cross_below = (bar.open_price >= self.day_window_ma) and (bar.close_price < self.day_window_ma)

    if cross_over:
        price = bar.close_price + 2
        if self.pos == 0:
            self.buy(price, self.fixed_size)
        elif self.pos < 0:
            self.cover(price, abs(self.pos))
            self.buy(price, self.fixed_size)
    elif cross_below:
        price = bar.close_price - 2
        if self.pos == 0:
            self.short(price, self.fixed_size)
        elif self.pos > 0:
            self.sell(price, abs(self.pos))
            self.short(price, self.fixed_size)

    self.put_event()

def on_day_bar(self, bar:BarData):

    self.am.update_bar(bar)


    `

description

description

Member
avatar
加入于:
帖子: 2621
声望: 157

情自己根据策略逻辑自行在策略里打印变量的变化看看吧

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

沪公网安备 31011502017034号