ricequant / rqalpha-mod-ctp

RQAlpha 对接 ctp 的扩展 Mod。通过启用该 Mod 来实现期货策略的实盘交易

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Portfolio里面所有position的amount都没有正确更新为持仓数量

kid143 opened this issue · comments

环境:

  • rqalpha==3.0.6
  • rqalpha-mod-ctp==0.2.0
  • python==3.4.5

现象:

  1. 实盘中出现以下错误:
Traceback (most recent call last):
  File "TraderApi.pyx", line 841, in ctp._TraderApi.TraderSpi_OnRtnTrade (ctp/TraderApi.cpp:21981)
  File "/home/nsls/anaconda2/envs/rqalpha-env/lib/python3.4/site-packages/rqalpha_mod_ctp/ctp/api.py", line 297, in OnRtnTrade
    self.gateway.on_trade(trade_dict)
    │                     └ {'quantity': 3, 'order_book_id': 'IC1709', 'style': ORDER_TYPE.LIMIT, 'order_id': 1, 'price': 6398.0, 'exchange_id': b'CFFEX', '...
    └ <rqalpha_mod_ctp.ctp.api.CtpTdApi object at 0x7f4572ad5a68>
  File "/home/nsls/anaconda2/envs/rqalpha-env/lib/python3.4/site-packages/rqalpha_mod_ctp/ctp/trade_gateway.py", line 169, in on_trade
    trade_dict.order_id, trade_dict.price, trade_dict.amount,
    │                    │                 └ {'quantity': 3, 'order_book_id': 'IC1709', 'style': ORDER_TYPE.LIMIT, 'order_id': 1, 'price': 6398.0, 'exchange_id': b'CFFEX', '...
    │                    └ {'quantity': 3, 'order_book_id': 'IC1709', 'style': ORDER_TYPE.LIMIT, 'order_id': 1, 'price': 6398.0, 'exchange_id': b'CFFEX', '...
    └ {'quantity': 3, 'order_book_id': 'IC1709', 'style': ORDER_TYPE.LIMIT, 'order_id': 1, 'price': 6398.0, 'exchange_id': b'CFFEX', '...
  File "/home/nsls/anaconda2/envs/rqalpha-env/lib/python3.4/site-packages/rqalpha_mod_ctp/ctp/data_dict.py", line 43, in __getattr__
    return self.__getitem__(item)
           │                └ 'amount'
           └ {'quantity': 3, 'order_book_id': 'IC1709', 'style': ORDER_TYPE.LIMIT, 'order_id': 1, 'price': 6398.0, 'exchange_id': b'CFFEX', '...
KeyError: 'amount'
  1. context.portfolilo里面的所有的position的amount都没有正确更新

推测是CTP的trade_gateway的成交量属性搞错了。

ctp/trade_gateway.py的on_trade方法:

    def on_trade(self, trade_dict):
        self.on_debug('交易回报: %s' % str(trade_dict))
        if self._data_update_date != date.today():
            self._cache.cache_trade(trade_dict)
        else:
            account = Environment.get_instance().get_account(trade_dict.order_book_id)

            if trade_dict.trade_id in account._backward_trade_set:
                return

            order = self._cache.get_cached_order(trade_dict)
            commission = cal_commission(trade_dict, order.position_effect)
            trade = Trade.__from_create__(
                trade_dict.order_id, trade_dict.price, trade_dict.amount,
                trade_dict.side, trade_dict.position_effect, trade_dict.order_book_id, trade_id=trade_dict.trade_id,
                commission=commission, frozen_price=trade_dict.price)

            order.fill(trade)
            self._env.event_bus.publish_event(RqEvent(EVENT.TRADE, account=account, trade=trade))

这个地方创建trade对象的时候,trade_dict的amount属性应该改为quantity(trade_dict.quantity),CTP接口返回里面没有amount属性,因此CTP的回调TraderSpi_OnRtnTrade报错。

自己手动修改site-packages里面对应部分的代码以后,不再出现这个错误。

@kid143 问下 是这样改么? 谢谢
image