# Event Contract Order Book Push

on_recv_rsp(self, rsp_pb)

  • Description

    Event contract order book push callback, which asynchronously processes real-time order book data of subscribed event contracts. After subscribing to event contracts of the SubType.ORDER_BOOK type, the server will actively push order book data. Upon receiving the push, this function will be called back, and you need to override on_recv_rsp in the derived class.

  • Parameters

    Parameter Type Description
    rsp_pb Qot_UpdateEventContractOrderBook_pb2.Response This parameter does not need to be processed directly in the derived class.
  • Return

    Parameter Type Description
    ret RET_CODE Interface call result.
    data list If ret == RET_OK, order book data is returned (one dict per contract).
    str If ret != RET_OK, error description is returned.
    • The order book data format is as follows (each dict contains the following fields):
      Field Type Description
      code str Event contract code.
      yes_bids list[tuple] YES bid side [(price, size), ...]
      yes_asks list[tuple] YES ask side [(price, size), ...]
      no_bids list[tuple] NO bid side [(price, size), ...]
      no_asks list[tuple] NO ask side [(price, size), ...]
  • Example

import time
from moomoo import *

class EventContractOrderBookHandler(EventContractOrderBookHandlerBase):
    def on_recv_rsp(self, rsp_pb):
        ret_code, content = super(EventContractOrderBookHandler, self).on_recv_rsp(rsp_pb)
        if ret_code != RET_OK:
            print("EventContractOrderBook error:", content)
            return RET_ERROR, content
        for ob in content:
            print("Received order book push:", ob['code'])
            print("  YES bids:", ob['yes_bids'])
            print("  YES asks:", ob['yes_asks'])
            print("  NO bids:", ob['no_bids'])
            print("  NO asks:", ob['no_asks'])
        return RET_OK, content

quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.set_handler(EventContractOrderBookHandler())  # Register the event contract order book push callback
ret, data = quote_ctx.subscribe_event_contract(
    code_list=['EC.KXODIMATCH-26JUL140600INDENG-IND'],
    subtype_list=[SubType.ORDER_BOOK])  # Subscribe to the event contract order book type, OpenD starts to receive continuous push from the server
if ret == RET_OK:
    print(data)
else:
    print('error:', data)
time.sleep(15)  # Set the script to receive OpenD push duration to 15 seconds
quote_ctx.close()   # Close the current connection, OpenD will automatically cancel the corresponding type of subscription for the corresponding contract after 1 minute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  • Output
Received order book push: EC.KXODIMATCH-26JUL140600INDENG-IND
  YES bids: [(0.5, 1030.0), (0.48, 0.01), (0.45, 5.0), (0.43, 1.0), (0.42, 175.0)]
  YES asks: [(0.51, 2633.6), (0.52, 15500.0), (0.53, 3000.0), (0.54, 1000.0), (0.55, 2124.0)]
  NO bids: [(0.49, 2633.6), (0.48, 15500.0), (0.47, 3000.0), (0.46, 1000.0), (0.45, 2124.0)]
  NO asks: [(0.5, 1030.0), (0.52, 0.01), (0.55, 5.0), (0.57, 1.0), (0.58, 175.0)]
1
2
3
4
5