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

用 python run.py和nohup python run.py &都试了

大佬们,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)

用Python的交易员 wrote:

cover只是发出买入平仓委托,并不保证能成交。

想提高成交概率可以超价下单,比如:

self.cover(bar.close_price + 10, 1)

那么为什么委托记录里显示全部成交呢,我记得之前会显示未成交的

原版只适用于能被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)

用Python的交易员 wrote:

建议检查下你加载的第一条数据时间戳,可能是01而不是00

我把window改成5时,发现加载第一条数据时因为lastbar是空,interval_count没有计数,所以第一个5m bar用了6个1m bar算的,导致后面的都向后移动了一个bar,把这一行改成这样就正常了,请问这是bug吗?

if (self.last_bar and bar.datetime.minute != self.last_bar.datetime.minute) or not self.last_bar:
                self.interval_count += 1
                if not self.interval_count % self.window:
                    finished = True
                    self.interval_count = 0

不应该是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

还有这段日志
2020-09-22 08:16:00+08:06
crossunder max
1
Sell max: 344.710000 343.229092 min: 336.531125 337.440000

short max: 344.710000 343.229092 min: 336.531125 337.440000

2020-09-22 08:28:00+08:06
crossunder max
1
Sell max: 345.027145 345.027145 min: 336.446130 336.446130

short max: 345.027145 345.027145 min: 336.446130 336.446130
sell和short之后为什么紧跟着的日志里pos还是1呢,不应该是-1吗?

这是代码
`

            # print("%s"%(bar.datetime))
            print("crossunder max ")
            print("pos: ",self.pos)
            if self.pos > 0:
                self.sell(bar.close_price,1)
                self.short(bar.close_price,1)
                print("Sell max: %f %f  min: %f %f \n"%(pre_max[-1],pre_max[-2],pre_min[-1],pre_min[-2]))
                print("short max: %f %f  min: %f %f \n"%(pre_max[-1],pre_max[-2],pre_min[-1],pre_min[-2]))
            if self.pos == 0:
                self.short(bar.close_price,1)
                print("short max: %f %f  min: %f %f \n"%(pre_max[-1],pre_max[-2],pre_min[-1],pre_min[-2]))

`

这是回测时自己打印出来的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++吗?

xiaohe wrote:

用print就行了,可以用run.py打开vnstation
多谢

xiaohe wrote:

应该是你在拿一列数和一个值相比较。你可以试着打印一下这几个变量或者打印它们的类型看看
怎么打印呢,self.write_log好像没用

请问有办法debug吗 或者打log

File "c:\vnstudio\lib\site-packages\vnpy\app\cta_strategy\backtesting.py", line 286, in run_backtesting
self.callback(data)
File "C:\Users\79119\strategies\pf.py", line 581, in on_bar
if(close[-1]>=pre_max[-1] and close[-2] <=pre_max[-2]):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

一直报错
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号

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