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

from vnpy_ctastrategy import (CtaTemplate,StopOrder,TickData,BarData,TradeData,OrderData,BarGenerator,ArrayManager)

class DemoStrategy(CtaTemplate):
fast_window =10
slow_window =20
fast_ma0 = 0
slow_ma0 = 0
fast_ma1 = 0
slow_ma1 = 0
parameters = ["fast_window", "slow_window"]
variables = ["fast_ma0","slow_ma0","fast_ma1","slow_ma1"]

def __init__(self,cta_engine,strategy_name,vt_symbol,setting):
    super().__init__(cta_engine,strategy_name,vt_symbol,setting)
    self.bg = BarGenerator(self.on_bar)
    self.am = ArrayManager()

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

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

def on_tick(self, tick: TickData):
    self.bg.update_tick(tick)

def on_bar(self, bar: BarData):
    am=self.am
    if not am.inited:
        return
    fast_ma=am.sma(self.fast_window,array=True)
    self.fast_ma0=fast_ma[-1]
    self.fast_ma1=fast_ma[-2]
    slow_ma = am.sma(self.slow_window, array=True)
    self.slow_ma0 = slow_ma[-1]
    self.slow_ma1 = slow_ma[-2]
    cross_over=(self.fast_ma0>self.slow_ma0 and self.fast_ma1<self.slow_ma1)
    cross_below=(self.slow_ma0>self.fast_ma0 and self.fast_ma1>self.slow_ma1)
    if cross_over:
        price=bar.close_price+5
        if self.pos==0:
            self.buy(price,1)
        elif self.pos<0:
            self.cover(price,1)
            self.buy(price,1)
    elif cross_below:
        price = bar.close_price - 5
        if self.pos == 0:
            self.short(price, 1)
        elif self.pos > 0:
            self.sell(price, 1)
            self.short(price, 1)
    self.put_event()
def on_order(self, order: OrderData):
    pass
def on_trade(self, trade: TradeData):
    self.put_event()
def on_stop_order(self, stop_order: StopOrder):
    pass
def on_stop(self):
    self.write_log("策略停止")
    self.put_event()

这是我写的代码,运行的时候没有任何报错,却没有产生任何交易记录

description
崩溃,研究好多天了,都没搞懂

Member
avatar
加入于:
帖子: 4552
声望: 277

自己在策略内部对变量和委托函数进行打印排查即可

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

沪公网安备 31011502017034号

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