# Real-time Order Book Callback

on_recv_rsp(self, rsp_pb)

  • Description

    Real-time quotation callback, asynchronous processing of real-time quotation push of subscribed stocks. It will call back to this function after receiving the push of real-time disk data. You need to override on_recv_rsp in the derived class.

  • Parameters

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

    Field Type Description
    ret RET_CODE Interface result.
    data dict If ret == RET_OK, plate data is returned.
    str If ret != RET_OK, error description is returned.
    • Order Book format as follows:

      Field Type Description
      code str Stock code.
      name str Stock name.
      svr_recv_time_bid str The time when moomoo server receives order book of bid from the exchange.
      svr_recv_time_ask str The time when moomoo server receives order book of ask from the exchange.
      Bid list Each tuple contains the following information:Bid price, bid volume, order qty of bid, order details of bid.
      Ask list Each tuple contains the following information:Ask price, ask volume, order qty of ask, order details of ask.

      The format of Bid and Ask fields as follows:

      'Bid': [ (bid_price1, bid_volume1, order_num, {'orderid1': order_volume1, 'orderid2': order_volume2, …… }), (bid_price2, bid_volume2, order_num,  {'orderid1': order_volume1, 'orderid2': order_volume2, …… }),…]
      'Ask': [ (ask_price1, ask_volume1,order_num, {'orderid1': order_volume1, 'orderid2': order_volume2, …… }), (ask_price2, ask_volume2, order_num, {'orderid1': order_volume1, 'orderid2': order_volume2, …… }),…] 
      
  • Example

import time
from moomoo import *
class OrderBookTest(OrderBookHandlerBase):
    def on_recv_rsp(self, rsp_pb):
        ret_code, data = super(OrderBookTest,self).on_recv_rsp(rsp_pb)
        if ret_code != RET_OK:
            print("OrderBookTest: error, msg: %s" % data)
            return RET_ERROR, data
        print("OrderBookTest ", data) # OrderBookTest's own processing logic
        return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = OrderBookTest()
quote_ctx.set_handler(handler) # Set real-time swing callback
ret, data = quote_ctx.subscribe(['US.AAPL'], [SubType.ORDER_BOOK]) # Subscribe to the order 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 link, OpenD will automatically cancel the corresponding type of subscription for the corresponding stock after 1 minute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • Output
OrderBookTest  {'code': 'US.AAPL', 'name': 'Apple', 'svr_recv_time_bid': '', 'svr_recv_time_ask': '', 'Bid': [(179.77, 100, 1, {}), (179.68, 200, 1, {}), (179.65, 2, 2, {}), (179.64, 27, 1, {}), (179.6, 9, 2, {}), (179.58, 39, 2, {}), (179.5, 13, 4, {}), (179.48, 331, 2, {}), (179.4, 1002, 2, {}), (179.38, 330, 1, {}), (179.37, 2, 1, {}), (179.3, 47, 1, {}), (179.28, 330, 1, {}), (179.21, 2, 1, {}), (179.2, 1000, 1, {}), (179.18, 330, 1, {}), (179.17, 100, 1, {}), (179.16, 1, 1, {}), (179.13, 400, 1, {}), (179.1, 3000, 1, {}), (179.08, 330, 1, {}), (179.05, 125, 2, {}), (179.01, 17, 2, {}), (179.0, 81, 7, {})], 'Ask': [(179.95, 400, 2, {}), (180.0, 360, 2, {}), (180.05, 20, 1, {}), (180.1, 246, 4, {}), (180.18, 20, 1, {}), (180.2, 2030, 3, {}), (180.23, 20, 1, {}), (180.3, 23, 1, {}), (180.33, 15, 1, {}), (180.4, 2000, 2, {}), (180.49, 5, 1, {}), (180.59, 253, 1, {}), (180.6, 2000, 2, {}), (180.8, 2010, 3, {}), (181.0, 2018, 4, {}), (181.08, 1, 1, {}), (181.2, 1009, 2, {}), (181.3, 17, 3, {}), (181.4, 1, 1, {}), (181.5, 50, 1, {}), (181.79, 9, 1, {}), (181.9, 66, 2, {})]}
1

Tips

  • This interface provides the function of continuously obtaining pushed data. If you need to obtain real-time data at one time, please refer to the Get Real-time Orderbook API.
  • For the difference between get real-time data and real-time data callback, please refer to How to Get Real-time Quotes Through Subscription Interface.
  • Real-time order book callback for US stocks, will continuously update data during the current trading session, with no need to set the session parameter.