VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 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()
Member
avatar
加入于:
帖子: 126
声望: 14

因为策略写的乱七八糟的啊

同样一个条件内部,出现多次sell/cover,什么鬼?

看你的注释:

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

sell是平仓,short才是开空啊。

同一个判断条件内,你多次平空,接着开空,逻辑就不对

反正就是乱七八糟,没法看

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

沪公网安备 31011502017034号

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