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

Member
avatar
加入于:
帖子: 25
声望: 0

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

Member
avatar
加入于:
帖子: 25
声望: 0

我使用vn.py中原有的TestStrategy策略进行回测,也出现了下面的这个问题,求助啊。

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 282, in run_backtesting
self.callback(data)
TypeError: 'NoneType' object is not callable

Administrator
avatar
加入于:
帖子: 4500
声望: 320

mohu wrote:

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

这个报错是你的策略代码问题,做以上操作的时候,你要先检查下self.bars1中数据量有多少,是否有最少2个了,你才能用下标1取到数据

Member
avatar
加入于:
帖子: 25
声望: 0

用Python的交易员 wrote:

mohu wrote:

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

这个报错是你的策略代码问题,做以上操作的时候,你要先检查下self.bars1中数据量有多少,是否有最少2个了,你才能用下标1取到数据
是我使用cover()错了,使用cover致使pos又加1了,不过cover()是平多还是平空?代码是平空,为什么在回测记录哪里看是方向是多。

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

沪公网安备 31011502017034号

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