vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 13
声望: 0

各位大神好,研究vnpy有一段时间了自己尝试这写了一个策略想跑一跑试试,结果自己写的策略跑回测无成交记录查看良久后无果,希望大家帮忙看看。
强行把template.py中的self.trading改成True后有成交了,但是策略中的self.pos成交之后没有成交回报。版本是2.07.谢谢各位了

一下为策略源码,执行过程中无报错。

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


class HoushuliStrategy(CtaTemplate):
    author = "投机的男孩"
    fast_window = 10
    slow_window = 20

    shanggui = 0.0
    shangzhong = 0.0

    zhonggui = 0.0
    xiazhong = 0.0
    xiagui = 0.0

    shanggui_cj=[0,0]
    shangzhong_cj=[0,0]
    zhonggui_cj=[0,0]
    xiazhong_cj=[0,0]
    xiagui_cj=[0,0]

    parameters = ["fast_window", "slow_window"]
    variables = ["shanggui", "shangzhong", "zhonggui", "xiazhong","zhonggui","shanggui_cj","shangzhong_cj","zhonggui_cj","xiazhong_cj","xiagui_cj"]
    #print(cta_engine, strategy_name, vt_symbol, setting,self.trading)

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super(HoushuliStrategy, self).__init__(
            cta_engine, strategy_name, vt_symbol, setting
        )
        #print(cta_engine, strategy_name, vt_symbol, setting,self.trading)
        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)

    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.
        """
        if self.pos==0:
            self.zhonggui=bar.close_price
            self.shangzhong=self.zhonggui+100
            self.shanggui=self.shangzhong+100
            self.xiazhong=self.zhonggui-100
            self.xiagui=self.xiazhong-100

        if bar.close_price>=self.zhonggui and bar.close_price<self.shangzhong and self.zhonggui_cj[0]==0:
            chengjiao=self.buy(bar.close_price,1)
            self.zhonggui_cj[0]=1
            if self.zhonggui_cj[1]==1:
                self.sell(bar.close_price,1)
                self.zhonggui_cj[1]=0
        #print(bar.close_price,self.zhonggui,self.shangzhong,self.zhonggui_cj,self.pos)
        if bar.close_price<self.zhonggui and bar.close_price>self.xiazhong and self.zhonggui_cj[1]==0:
            self.short(bar.close_price,1)
            self.zhonggui_cj[1]=1
            if self.zhonggui_cj[0]==1:
                self.cover(bar.close_price,1)
                self.zhonggui_cj[0]==0

        if bar.close_price>=self.shangzhong and bar.close_price<self.shanggui and self.sahngzhong_cj[0]==0:
            self.buy(bar.close_price,1)
            self.shangzhong_cj[0]=1
            if self.shangzhong_cj[1]==1:
                self.sell(bar.close_price,1)
                self.shangzhong_cj[1]=0
        if bar.close_price<self.shangzhong and bar.close_price>self.zhonggui and self.shangzhong_cj[1]==0:
            self.short(bar.close_price,1)
            self.shangzhong_cj[1]=1
            if self.shangzhong_cj[0]==1:
                self.cover(bar.close_price,1)
                self.zhonggui_cj[0]==0

        if bar.close_price>=self.shanggui and self.shanggui_cj[0]==0:
            self.buy(bar.close_price,1)
            self.shanggui_cj[0]=1
            if self.shanggui_cj[1]==1:
                self.sell(bar.close_price,1)
                self.shanggui_cj[1]=0
        if bar.close_price<=self.shanggui and bar.close_price>self.shangzhong and self.sahnggui_cj[1]==0:
            self.short(bar.close_price,1)
            self.shanggui_cj[1]=1
            if self.shanggui_cj[0]==1:
                self.cover(bar.close_price,1)
                self.shanggui_cj[0]==0


        if bar.close_price>=self.xiazhong and bar.close_price<self.zhonggui and self.xiazhong_cj[0]==0:
            self.buy(bar.close_price,1)
            self.xiazhong_cj[0]=1
            if self.zhonggui_cj[1]==1:
                self.sell(bar.close_price,1)
                self.xiazhong_cj[1]=0
        if bar.close_price<=self.xiazhong and bar.close_price>self.xiagui and self.xiazhong_cj[1]==0:
            self.short(bar.close_price,1)
            self.xiazhong_cj[1]=1
            if self.xiazhong_cj[0]==1:
                self.cover(bar.close_price,1)
                self.xiazhong_cj[0]==0


        if bar.close_price>=self.xiagui and bar.close_price<self.xiazhong and self.xiazhong_cj[0]==0:
            self.buy(bar.close_price,1)
            self.xiagui_cj[0]=1
            if self.xiagui_cj[1]==1:
                self.sell(bar.close_price,1)
                self.xiagui_cj[1]=0
        if bar.close_price<self.xiagui and self.xiagui_cj[1]==0:
            self.short(bar.close_price,1)
            self.xiagui_cj[1]=1
            if self.xiagui_cj[0]==1:
                self.cover(bar.close_price,1)
                self.xiagui_cj[0]==0

        if self.pos==3 and bar.close_price>self.zhonggui+500:
            self.cover(bar.close_price,self.pos)
            self.shanggui_cj=[0,0]
            self.shangzhong_cj=[0,0]
            self.zhonggui_cj=[0,0]
            self.xiazhong_cj=[0,0]
            self.xiagui_cj=[0,0]
        if self.pos==-3 and bar.close_price<self.zhonggui-500:
            self.sell(bar.close_price,self.pos)
            self.shanggui_cj=[0,0]
            self.shangzhong_cj=[0,0]
            self.zhonggui_cj=[0,0]
            self.xiazhong_cj=[0,0]
            self.xiagui_cj=[0,0]


        self.put_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
加入于:
帖子: 173
声望: 18

可以重载on_trade函数实现成交回报,比如最简单的,可以

print(trade)
Member
avatar
加入于:
帖子: 13
声望: 0

方里明 wrote:

可以重载on_trade函数实现成交回报,比如最简单的,可以

print(trade)

您好方先生,我还是不太明白其中的原理,实例策略中的DoubleMaStratege完全不用实现也可以做到有成交回报,而我的策略,基本是照抄的面板为何他能而我不能。而且这么改源码会不会在实盘中出现严重的后果?

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

我按照方先生的方法试了一下重载之后self.pos依然不会有变化还是 0

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        if trade.direction.value=='多':
            if trade.offset.value=='开':
                self.pos=self.pos+1
            else:
                self.pos=self.pos-1
        else:
            if trade.offset.value=='开':
                self.pos=self.pos-1
            else:
                self.pos=self.pos+1
        self.put_event()

这让我很郁闷

Administrator
avatar
加入于:
帖子: 1908
声望: 85

不要去修改self.pos字段,这个是由策略引擎负责管理的策略仓位数据,手动修改会导致逻辑错乱

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

用Python的交易员 wrote:

不要去修改self.pos字段,这个是由策略引擎负责管理的策略仓位数据,手动修改会导致逻辑错乱

麻烦交易员老师帮忙看看吧,这个问题困扰了我一星期了一直没有解决。

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

用命令行模式运行进行调试先。肯定能找出问题。

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

yy10112001 wrote:

用命令行模式运行进行调试先。肯定能找出问题。

一直都是命令行方式运行的。 没安装Station

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

横权霸 wrote:

yy10112001 wrote:

用命令行模式运行进行调试先。肯定能找出问题。

一直都是命令行方式运行的。 没安装Station

用vscode或者pycharm加载运行,下断点进行调试啊。一步步跟着走往下看肯定能找到问题。
你这个变量命名太不规范了,估计其他人也没什么耐心往下看

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