CTA策略测试平多时QAPositon第658行报错
发布于 5 个月前 作者 lovingfantasy 203 次浏览 来自 报bug
  • 测试环境:Docker部署的QA环境
  • QA相关的依赖版本:
    • QAStrategy 0.0.19
    • quantaxis 1.9.11 -测试代码如下图image.png -我的代码

	from QAStrategy import QAStrategyCTABase
	import QUANTAXIS as QA
	import uuid
	
	class CCI(QAStrategyCTABase):
    def on_bar(self, bar):
        res = self.cci()
        if res.CCI[-1] < -100:
            #多头信号触发,先判断是否有空头持仓,如果有就平多
            if self.positions.volume_short > 0:
                print(res.iloc[-1])
                print('SELL SHORT')
                self.send_order('SELL', 'CLOSE', price=bar['close'], volume=1)
            if self.positions.volume_long == 0:
                print(res.iloc[-1])
                print('LONG')
                self.send_order('BUY', 'OPEN', price=bar['close'], volume=1)
                print(self.positions.volume_long)
        elif res.CCI[-1] > 100:
            # 空头信号触发,先判断是否有多头持仓,如果有就平空
            if self.positions.volume_long > 0:
                print(res.iloc[-1])
                print('BUY CLOSE')
                self.send_order('BUY', 'CLOSE', price=bar['close'], volume=1)
            if self.positions.volume_short == 0:
                print(res.iloc[-1])
                print('SHORT')
                self.send_order('SELL', 'OPEN', price=bar['close'], volume=1)
    def cci(self):
        return QA.QA_indicator_CCI(self.market_data, 61)
    def risk_check(self):
        pass
	strategy_id =str(uuid.uuid4())
	strategy = CCI(code='RBL8',frequence='30min',strategy_id=strategy_id,portfolio='F_CTA_RB_30min_CCI',user_cookie='test001')
	strategy.run_backtest()
  • 报错信息
	File "D:\Python37\lib\site-packages\QUANTAXIS\QAMarket\QAPosition.py", line 658, in update_pos
	  (self.volume_short-amount)/self.volume_short
 	 ZeroDivisionError: ('division by zero', 'occurred at index (2020-01-16 22:30:00, RBL8)')
  • 定位到QAPosition-655到660代码
        elif towards == ORDER_DIRECTION.BUY_CLOSE:
            # 有昨仓先平昨仓
            self.position_cost_short = self.position_cost_short * \
                (self.volume_short-amount)/self.volume_short
            self.open_cost_short = self.open_cost_short * \
                (self.volume_short-amount)/self.volume_short
  • 策略代码中:如果有多单,则平多单的判断,进入到BUY_CLOSE的代码中,而此时self.volume_short必然是为0的,是否逻辑上有问题?
2 回复

写错了 buyopen -> sellclose sellopen-> buyclose

第一个是direction 第二个是offset

@Faithforus 谢谢!我理解错了。

回到顶部