# Real-time Order Book Callback

# OrderBookHandlerBase

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

    Parameter 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 Futu server receives order book of bid from the exchange.
      svr_recv_time_ask str The time when Futu 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 futu 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(['HK.00700'], [SubType.ORDER_BOOK]) # Subscribe to the order type, Futu 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 Futu OpenD push duration to 15 seconds
quote_ctx.close() # Close the current link, Futu 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': '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, {}), (332.2, 18900, 20, {}), (332.0, 52300, 71, {}), (331.8, 188100, 30, {}), (331.6, 64600, 37, {}), (331.4, 36200, 13, {}), (331.2, 29500, 20, {}), (331.0, 282200, 82, {})], 'Ask': [(333.0, 2393100, 98, {}), (333.2, 90300, 27, {}), (333.4, 107100, 16, {}), (333.6, 122000, 26, {}), (333.8, 62500, 39, {}), (334.0, 62600, 48, {}), (334.2, 109200, 12, {}), (334.4, 103100, 18, {}), (334.6, 67900, 13, {}), (334.8, 142200, 48, {})]}
1

Tips