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

大佬们,cta策略移动止损和止盈是这样写的吗?我设置的fixed_size是3,但是打出来log有时候self.pos是6或者-6,stop=true不应该是停止单吗,但是委托记录里显示的全部是限价单

self.cancel_all()
 ...
if self.pos > 0:
            self.intra_trade_high = max(bar.high_price,self.intra_trade_high)
            self.intra_trade_low = bar.low_price
            if self.isShort():
                self.short_entry_price = bar.close_price
                self.sell(self.short_entry_price,self.fixed_size)
                self.short(self.short_entry_price,self.fixed_size)
                # print("Sell and short")

            if self.enable_take_profit:
                self.sell(self.long_entry_price*(1+self.long_take_profit_perc*0.01),abs(self.pos))
            if self.enable_stop_loss:
                self.long_stop_loss_price = self.intra_trade_high*(1-self.perc_long_stop_loss*0.01)
                self.sell(self.long_stop_loss_price,abs(self.pos),stop=True)
        elif self.pos == 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = bar.low_price
            if self.isLong():
                self.long_entry_price = bar.close_price
                self.buy(self.long_entry_price, self.fixed_size)
            elif self.isShort():
                self.short_entry_price = bar.close_price
                self.short(self.short_entry_price,self.fixed_size)
                # print("short")
        elif self.pos <0:
            self.intra_trade_low = min(bar.low_price,self.intra_trade_low)
            self.intra_trade_high = bar.high_price
            if self.isLong():
                self.long_entry_price = bar.close_price
                self.cover(self.long_entry_price,self.fixed_size)
                self.buy(self.long_entry_price,self.fixed_size)
                # print("cover and buy")
            if self.enable_take_profit:
                self.cover(self.short_entry_price*(1-self.short_take_profit_perc*0.01),abs(self.fixed_size))
            if self.enable_stop_loss:
                self.short_stop_loss_price = self.intra_trade_low*(1+self.perc_short_stop_loss*0.01)
                self.cover(self.short_stop_loss_price,abs(self.pos),stop=True)

原版只适用于能被60整除的,改成计数算法后遇到数据缺失一段的情况,后面的计数好像就会混乱,改成这样是不是更好呢?

if not (bar.datetime.hour*60+bar.datetime.minute+1)%self.window:
                finished = True

原来代码:

if not (bar.datetime.minute + 1) % self.window:
               finished = True

CTA策略,log打印出来了,委托记录那里也显示的全部成交,结果下个bar走到这里还是-1,然后又cover和buy了一次

if m[-1]>m[-2]:
            print("pos: ",self.pos)
            if self.pos == 0:
                self.buy(bar.close_price, 1)
                print("%s buy"%(bar.datetime))
            elif self.pos <0:
                print("cover and buy")
                self.cover(bar.close_price,1)
                self.buy(bar.close_price,1)

不应该是45,30吗?

self.bg = NewBarGenerator(self.on_bar,window= 45,on_window_bar= self.on_45min_bar,interval=Interval.MINUTE)

if self.interval == Interval.MINUTE:
            # # x-minute bar
            # if not (bar.datetime.minute + 1) % self.window:
            #     finished = True
            if self.last_bar and bar.datetime.minute != self.last_bar.datetime.minute:

                self.interval_count += 1

                if not self.interval_count % self.window:
                    finished = True
                    self.interval_count = 0

这是回测时自己打印出来的log
crossunder max
pos:1
Sell max: 323.889693 323.889693 min: 312.919660 312.919660
short max: 323.889693 323.889693 min: 312.919660 312.919660
可以看到pos是1,然后sell然后short,但是成交记录里看不到这个交易

我看源码BarData里好像没有保存当前bar的index,am.open, am.high什么的返回的都是最近100个bar的,是要自己每次count++吗?

一直报错
close = am.close pre_max = self.pre_max pre_min = self.pre_min if(close[-1]>=pre_max[-1] and close[-2] <=pre_max[-2]): if self.postion > 0: self.sell(bar.close_price,1)

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

沪公网安备 31011502017034号

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