vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 29
声望: 3

尝试了一下跟踪其他软件手动下单,订单在vnpy中的处理过程,cta_gateway.py -->gateway.py-->eventengine,这三个位置都用log打印出来,看看发生了什么。
cta_gateway.py中,添加打印如下:

    def onRtnOrder(self, data: dict):
        """
        Callback of order status update.
        """
        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            self.order_data.append(data)
            return

        frontid = data["FrontID"]
        sessionid = data["SessionID"]
        order_ref = data["OrderRef"]
        orderid = f"{frontid}_{sessionid}_{order_ref}"

        order = OrderData(
            symbol=symbol,
            exchange=exchange,
            orderid=orderid,
            type=ORDERTYPE_CTP2VT[data["OrderPriceType"]],
            direction=DIRECTION_CTP2VT[data["Direction"]],
            offset=OFFSET_CTP2VT[data["CombOffsetFlag"]],
            price=data["LimitPrice"],
            volume=data["VolumeTotalOriginal"],
            traded=data["VolumeTraded"],
            status=STATUS_CTP2VT[data["OrderStatus"]],
            time=data["InsertTime"],
            gateway_name=self.gateway_name
        )
        self.gateway.on_order(order)

        self.sysid_orderid_map[data["OrderSysID"]] = orderid

        # 无问西东 
        logger.info(f"gateway.on_order:{order}") 
        logger.info(f"data["OrderSysID"]:{data["OrderSysID"]}")

gateway.py中 on_event ,添加打印如下:

    def on_event(self, type: str, data: Any = None) -> None:
        """
        General event push.
        """
        event = Event(type, data)
        self.event_engine.put(event)

        # 无问西东 
        logger.info(f"gateway.py on_event event.type:{event.type}") 
        logger.info(f"gateway.py on_event event.data:{event.data}")

eventengine中,添加打印如下:

    def _run(self) -> None:
        """
        Get event from queue and then process it.
        """
        while self._active:
            try:
                event = self._queue.get(block=True, timeout=1)
                # 无问西东 
                logger.info(f"_run event_engine event.type:{event.type}") 
                logger.info(f"_run event_engine event.data:{event.data}")

                self._process(event)
            except Empty:
                pass

1、请问在gateway.on_order函数中,为什么要推送两次?一次是EVENT_ORDER,一次是EVENT_ORDER + order.vt_orderid,两个的作用分别是什么?

    def on_order(self, order: OrderData) -> None:
        """
        Order event push.
        Order event of a specific vt_orderid is also pushed.
        """
        self.on_event(EVENT_ORDER, order)
        self.on_event(EVENT_ORDER + order.vt_orderid, order)

2、打印出来的结果如下,会发现EVENT_ORDER的event在eventengine中的_run中的队列中被提取掉,并被后续的mainengine处理,而EVENT_ORDER + order.vt_orderid对应的事件在_run函数中没有被提取,也就是没有被打印出来,导致后续所有的下单(包括vnpy自带的下单控件下的单就没有响应,可能是跟队列中,有没被提取的事件导致阻塞有关)。请问为什么平时vnpy自带的下单控件下单对应的EVENT_ORDER + order.vt_orderid事件能被处理,而用其他软件下单对应的EVENT_ORDER + order.vt_orderid事件不能被提取?:

2021-03-10 22:58:33.213 | INFO     | vnpy.trader.gateway:on_event:99 - gateway.py on_event event.type:eOrder.

2021-03-10 22:58:33.213 | INFO     | vnpy.event.engine:_run:66 - _run event_engine event.type:eOrder.

2021-03-10 22:58:33.227 | INFO     | vnpy.trader.gateway:on_event:100 - gateway.py on_event event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.SUBMITTING: '提交中'>, datetime=datetime.datetime(2021, 3, 10, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.227 | INFO     | vnpy.event.engine:_run:67 - _run event_engine event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.SUBMITTING: '提交中'>, datetime=datetime.datetime(2021, 3, 10, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.227 | INFO     | vnpy.trader.gateway:on_event:99 - gateway.py on_event event.type:eOrder.CTP.21_-647510950_000000000056

2021-03-10 22:58:33.227 | INFO     | vnpy.trader.engine:process_order_event:400 - mainengine event.type:eOrder.

2021-03-10 22:58:33.227 | INFO     | vnpy.trader.gateway:on_event:100 - gateway.py on_event event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.SUBMITTING: '提交中'>, datetime=datetime.datetime(2021, 3, 10, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.227 | INFO     | vnpy.trader.engine:process_order_event:401 - mainengine event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.SUBMITTING: '提交中'>, datetime=datetime.datetime(2021, 3, 10, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.243 | INFO     | vnpy.app.cta_strategy.engine:process_order_event:178 - cta_engine event.type:eOrder.

2021-03-10 22:58:33.243 | INFO     | vnpy.app.cta_strategy.engine:process_order_event:179 - cta_engine event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.SUBMITTING: '提交中'>, datetime=datetime.datetime(2021, 3, 10, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.259 | INFO     | vnpy.trader.gateway:on_event:99 - gateway.py on_event event.type:eOrder.

2021-03-10 22:58:33.259 | INFO     | vnpy.trader.gateway:on_event:100 - gateway.py on_event event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.NOTTRADED: '未成交'>, datetime=datetime.datetime(2021, 3, 11, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')

2021-03-10 22:58:33.259 | INFO     | vnpy.trader.gateway:on_event:99 - gateway.py on_event event.type:eOrder.CTP.21_-647510950_000000000056

2021-03-10 22:58:33.259 | INFO     | vnpy.trader.gateway:on_event:100 - gateway.py on_event event.data:OrderData(gateway_name='CTP', symbol='m2105', exchange=<Exchange.DCE: 'DCE'>, orderid='21_-647510950_000000000056', type=<OrderType.LIMIT: '限价'>, direction=<Direction.SHORT: '空'>, offset=<Offset.OPEN: '开'>, price=3215.0, volume=1, traded=0, status=<Status.NOTTRADED: '未成交'>, datetime=datetime.datetime(2021, 3, 11, 22, 58, 32, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), reference='')
Member
avatar
加入于:
帖子: 2038
声望: 134

vt_orderid是vnpy的本地订单id

Member
avatar
加入于:
帖子: 29
声望: 3

收到其他软件下单后,用order.vtorderid,它也能实现本地订单号,譬如生成:CTP.21-647510950_000000000056,请问为什么这个其他软件下单生成的本地订单号对应的事件在事件引擎中,不会被get取出来呢(如果能提取,就不会阻塞队列了)?

Member
avatar
加入于:
帖子: 95
声望: 7

easibay wrote:

收到其他软件下单后,用order.vtorderid,它也能实现本地订单号,譬如生成:CTP.21-647510950_000000000056,请问为什么这个其他软件下单生成的本地订单号对应的事件在事件引擎中,不会被get取出来呢(如果能提取,就不会阻塞队列了)?

不是这样的,
vnpy的事件引擎,任何事件扔进来,都会被处理。

区别在于:
如果事件A的类别有注册的处理程序,进入处理程序进行处理。
如果没有,则这个事件就被扔掉了。

Member
avatar
加入于:
帖子: 29
声望: 3

找到原因了,正常的情况,其他软件下的单,也都能进入到cta_engine,收到第一个单就卡住了,是因为自己写了一个处理函数,函数有点问题,但是vnstudio里面没有报错,也没死机。删除自己写的这个函数之后,推送恢复正常

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

沪公网安备 31011502017034号