# Get Real-time Order Book

get_order_book(code, num=10)

  • Description

    To get the real-time order book of subscribed stocks, you must subscribe first.

  • Parameters

    Parameter Type Description
    code str Stock code.
    name str Stock name.
    num int The requested number of price levels.
  • 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

from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub = quote_ctx.subscribe(['HK.00700'], [SubType.ORDER_BOOK], subscribe_push=False)[0]
# First subscribe to the order type. After the subscription is successful, OpenD will continue to receive pushes from the server, False means that there is no need to push to the script temporarily
if ret_sub == RET_OK:  # Successfully subscribed
    ret, data = quote_ctx.get_order_book('HK.00700', num=3)  # Get 3 files of real-time panning data once
    if ret == RET_OK:
        print(data)
    else:
        print('error:', data)
else:
    print('subscription failed')
quote_ctx.close()  # Close the current connection, OpenD will automatically cancel the subscription of the corresponding stock in 1 minute
1
2
3
4
5
6
7
8
9
10
11
12
13
  • Output
{'code': 'HK.00700', 'name': 'TENCENT', 'svr_recv_time_bid': '', 'svr_recv_time_ask': '', 'Bid': [(332.8, 4700, 4, {}), (332.6, 158300, 11, {}), (332.4, 172700, 19, {})], 'Ask': [(333.0, 2393100, 98, {}), (333.2, 90300, 27, {}), (333.4, 107100, 16, {})]}
1