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

执行了self.cover()之后,self.pos不应该为0了么,为什么还继续执行self.pos>0那个分支,导致数组出现越界的情况。

def on_bar(self, bar: BarData):
    """
    Callback of new bar data update.
    """
    if self.pos > 0:
        if bar.close_price < self.bars1[1].low_price:
            self.cover(bar.close_price, 1)
            self.bars1 = []
            self.bars = []
        elif bar.close_price > 2 * abs(self.bars[0] - self.bars1[1].low_price):
            self.cover(bar.close_price, 1)
            self.bars1 = []
            self.bars = []
        else:
            pass

    elif self.pos == 0:
        if len(self.bars1) < 2:
            gains = (bar.close_price - bar.open_price) / bar.open_price
            if gains > self.point:
                self.bars1.append(bar)
            else:
                self.bars1 = []
        else:
            self.buy(bar.open_price, 1)
            # 买入价
            self.bars.append(bar.open_price)
    self.put_event()


以上代码出现这个问题,是哪里写错了?怎么解决?希望大家能帮助一下,谢谢。
Exception in thread Thread-4:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\python\vnpy-2.0.6\vnpy\app\cta_backtester\engine.py", line 158, in run_backtesting
engine.run_backtesting()
File "D:\python\vnpy-2.0.6\vnpy\app\cta_strategy\backtesting.py", line 294, in run_backtesting
func(data)
File "D:\python\vnpy-2.0.6\vnpy\app\cta_strategy\backtesting.py", line 709, in new_bar
self.strategy.on_bar(bar)
File "D:\python\vnpy-2.0.6\vnpy\app\cta_strategy\strategies\my_gao_strategy2.py", line 72, in on_bar
if bar.close_price < self.bars1[1].low_price:
IndexError: list index out of range

vnpy-2.0.7里的vnpy包下面没有event,trader,gateway等包和模块,导致run.py无法导入这些模块,这是什么原因?

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

沪公网安备 31011502017034号

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