VeighNa量化社区
你的开源社区量化交易平台
leo-lee's Avatar
Member
离线
4 帖子
声望: 0

写了一个很简单的“黎明之星”的蜡烛形态策略(多空点以及止盈止损位置如下图所示)

入场方式用的是停止单。但是策略跑出来后,开仓点,止盈,止损都是乱七八糟的。求大家帮忙看下我哪里出问题了,感激!!

description

description

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



class morning_star(CtaTemplate):

    author = "Leo_Monster"


    fixed_size=1
    wining_ratio=1.1
    sl_pip=0.00050



    sl_entry_long=0
    sp_entry_long=0

    sl_entry_short=0
    sp_entry_short=0

    entry_price = 0

    parameters = ['fixed_size','wining_ratio','sl_pip']
    variables = ['sl_entry_long','sp_entry_long','sl_entry_short','sp_entry_short','entry_price']


    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,window=1,interval=Interval.HOUR,on_window_bar=self.on_60min_bar) 
        self.am = ArrayManager()


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

    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_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

    def on_bar(self, bar: BarData): 
        self.bg.update_bar(bar)


    def on_60min_bar(self,bar: BarData):
        am = self.am
        am.update_bar(bar) 

        if not self.am.inited: 
            return

        self.up_trend = self.am.low_array[-2] < self.am.low_array[-1] and\
            self.am.low_array[-2] < self.am.low_array[-3] and\
                self.am.high_array[-1] > self.am.high_array[-2] and\
                     self.am.high_array[-3] > self.am.high_array[-2]


        self.down_trend = self.am.high_array[-2] > self.am.high_array[-3] and\
            self.am.high_array[-2] > self.am.high_array[-1] and\
                self.am.low_array[-3] < self.am.low_array[-2] and\
                    self.am.low_array[-1] < self.am.low_array[-2]




        if self.pos == 0:
            if self.up_trend:
                self.entry_price=self.am.high_array[-1]

                self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
                self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点

                self.buy(self.entry_price,1,True) #做多头仓位

                self.sell(self.sl_entry_long,abs(self.pos),True)#止损
                self.sell(self.sp_entry_long,abs(self.pos)) #止盈

            if self.down_trend:

                self.entry_price=self.am.low_array[-1]

                self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
                self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点

                self.sell(self.entry_price,1,True) #做空头仓位

                self.cover(self.sl_entry_short,abs(self.pos),True) #止损
                self.cover(self.sp_entry_short,abs(self.pos)) #止盈



        if self.pos < 0:
            if self.up_trend:

                self.entry_price=self.am.high_array[-1] #入场价
                self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
                self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点

                self.cover(bar.close_price,abs(self.pos)) #平空头仓

                self.buy(self.entry_price,1,True) #做多头仓位

                self.sell(self.sl_entry_long,abs(self.pos),True) #止损
                self.sell(self.sp_entry_long,abs(self.pos)) # 止盈

            if self.down_trend:

                self.entry_price=self.am.low_array[-1] #入场价
                self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
                self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点

                self.cover(bar.close_price,abs(self.pos)) #平掉之前所持有的空头仓

                self.sell(self.entry_price,1,True) #再做空

                self.cover(self.sl_entry_short,abs(self.pos),True) #止损
                self.cover(self.sp_entry_short,abs(self.pos)) #止盈

        if self.pos > 0:
            if self.up_trend:
                self.entry_price=self.am.high_array[-1]

                self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
                self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点

                self.sell(bar.close_price,abs(self.pos)) #平掉之前的多头仓

                self.buy(self.entry_price,1,True) #再开一个多头仓

                self.sell(self.sl_entry_long,abs(self.pos),True) #止损
                self.sell(self.sp_entry_long,abs(self.pos)) #止盈



                self.buy(bar.close_price,1)

            if self.down_trend:

                self.entry_price=self.am.low_array[-1] #入场价
                self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
                self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点

                self.sell(bar.close_price,abs(self.pos)) #平多头仓位

                self.sell(self.entry_price,1,True) # 下空头仓位

                self.cover(self.sl_entry_short,abs(self.pos),True) #止损
                self.cover(self.sp_entry_short,abs(self.pos)) #止盈


        self.put_event()

青青子荆 wrote:

  1. 删掉def on_bar()后面的normal
  2. 你用了小时的bar,因此需要在策略文件前面加上一行 from vnpy.trader.constant import Interval
    这就去试一下,非常感谢!!!

大家好,我是名正在不断摸索学习的 “小学生“ 。 最近遇到了点小问题。我尝试写一个price action类型的策略(如下图所示)
description
当最新一根K线突破内包线(inside bar)的时候,就做空或者做多;处于着急测试的原因,我这个策略只写了做多部分。

问题就是:
当我打开vnpy ui进行回测的时候,无法进行回测(如下图所示)
description

等了很长时间没有反应就又点了一下回测,就显示这样的图片

description

但当我不通过打开vnpy客户端而是用backtesting文件回测的时候,却是可以跑的。

为了防止是数据方面的问题,我用同样的数据测试了下自带的几个strategy,并没有问题,所以问题肯定出在我写的这个代码上了。(我把代码attach在下面了)个人猜测会不会是因为我调用K线的方式有问题导致数据没法读取?
求各位大神们指点下。万分感谢
Regards,
Leo

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

class three_bar(CtaTemplate):
    author = "Leo_Monster"

    fixed_size=1
    parameters=["fixed_size"]
    variables=[]


    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,window=1,interval=Interval.HOUR) 
        self.am = ArrayManager()


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

    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.bg.update_tick(tick)

    def on_bar(self, bar: BarData): normal

        self.am.update_bar(bar) 

        if not self.am.inited: 
            return

        if self.pos ==0:
            if self.am.high_array[-2]>self.am.high_array[-1] and\
                self.am.low_array[-2]<self.am.low_array[-1]:
                self.buy(bar.open_price,self.fixed_size)

    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

不好意思刚开始学习,想请问下如何下载oanda外汇历史数据的?我使用策略回测的时候显示历史数据下载失败。。
求帮忙解答一下感激!!

© 2015-2022 微信 18391752892
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

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