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

写了一个网格交易的策略,回测的时候,最大回撤都无穷大了,请问是怎么回事呢?
description

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

class GridStrategy(CtaTemplate):
""""""

author = "Fighter"

#定义参数

initial_price = 1.0
step_price = 1.0 
step_volume = 1.0
max_pos = 4

vt_orderid = ""
pos = 0
parameters = ["initial_price", "step_price", "step_volume", "max_pos"]
variables = ["pos", "vt_orderid"]

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):
    """
    Callback when strategy is inited.
    """
    self.write_log("策略初始化")
    self.load_bar(1)
    self.pos = 0

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

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

def on_tick(self, tick: TickData):
    """
    Callback of new tick data update.
    """
    self.bg.update_tick(tick)

def on_bar(self, bar: BarData):
    """
    Callback of new bar data update.
    """
    #self.bg.update_bar(bar)
    #self.cancel_all()

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

    if self.initial_price > bar.close_price:
    # 价格在基准价之上时
    # 计算当前K线收盘价与初始价的距离
        target_buy_distance = (self.initial_price - bar.close_price) / self.step_price
    # 计算当前价位应该持有的仓位,取整。再和最大仓位置比较
        target_buy_position = min(math.floor(target_buy_distance) * self.step_volume, self.max_pos)
    # 当前应该持有的仓位减去原有持仓就是该再买入的仓位
        target_buy_volume = target_buy_position - self.pos
        # Buy when price dropping
        if target_buy_volume > 0:
            self.buy(bar.close_price, target_buy_volume)
    # Sell when price rising
        elif target_buy_volume < 0:
            self.sell(bar.close_price, target_buy_volume)

    elif self.initial_price < bar.close_price:

    # 价格在基准价之下时
        target_buy_distance = (bar.close_price - self.initial_price) / self.step_price
        target_buy_position = - min(math.floor(target_buy_distance) * self.step_volume, self.max_pos)
        target_buy_volume = target_buy_position - self.pos

        # Buy when price dropping
        if target_buy_volume > 0:
            self.buy(bar.close_price, target_buy_volume)
        # Sell when price rising
        elif target_buy_volume < 0:
            self.sell(bar.close_price, target_buy_volume)


    # Update UI
    #self.put_variables_event()


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()

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

可以自己在策略里打印排查看看

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

xiaohe wrote:

可以自己在策略里打印排查看看
请问在回测引擎里面怎么打印呢

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

xiaohe wrote:

可以自己在策略里打印排查看看
搞定了,谢谢

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

沪公网安备 31011502017034号

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