# 事件合约摆盘推送

on_recv_rsp(self, rsp_pb)

  • 介绍

    事件合约摆盘推送回调,异步处理已订阅事件合约的实时摆盘数据。 在订阅了 SubType.ORDER_BOOK 类型的事件合约后,服务端会主动推送摆盘数据,收到推送后会回调到该函数,您需要在派生类中覆盖 on_recv_rsp。

  • 参数

    参数 类型 说明
    rsp_pb Qot_UpdateEventContractOrderBook_pb2.Response 派生类中不需要直接处理该参数
  • 返回

    参数 类型 说明
    ret RET_CODE 接口调用结果
    data list 当 ret == RET_OK,返回摆盘数据(每只合约一个 dict)
    str 当 ret != RET_OK,返回错误描述
    • 摆盘数据格式如下(每个 dict 含以下字段):
      字段 类型 说明
      code str 事件合约代码
      yes_bids list[tuple] YES 买盘 [(price, size), ...]
      yes_asks list[tuple] YES 卖盘 [(price, size), ...]
      no_bids list[tuple] NO 买盘 [(price, size), ...]
      no_asks list[tuple] NO 卖盘 [(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("收到摆盘推送:", ob['code'])
            print("  YES 买盘:", ob['yes_bids'])
            print("  YES 卖盘:", ob['yes_asks'])
            print("  NO 买盘:", ob['no_bids'])
            print("  NO 卖盘:", ob['no_asks'])
        return RET_OK, content

quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.set_handler(EventContractOrderBookHandler())  # 注册事件合约摆盘推送回调
ret, data = quote_ctx.subscribe_event_contract(
    code_list=['EC.KXODIMATCH-26JUL140600INDENG-IND'],
    subtype_list=[SubType.ORDER_BOOK])  # 订阅事件合约摆盘类型,OpenD 开始持续收到服务器的推送
if ret == RET_OK:
    print(data)
else:
    print('error:', data)
time.sleep(15)  # 设置脚本接收 OpenD 的推送持续时间为15秒
quote_ctx.close()   # 关闭当条连接,OpenD 会在1分钟后自动取消相应合约相应类型的订阅
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
收到摆盘推送: EC.KXODIMATCH-26JUL140600INDENG-IND
  YES 买盘: [(0.5, 1030.0), (0.48, 0.01), (0.45, 5.0), (0.43, 1.0), (0.42, 175.0)]
  YES 卖盘: [(0.51, 2633.6), (0.52, 15500.0), (0.53, 3000.0), (0.54, 1000.0), (0.55, 2124.0)]
  NO 买盘: [(0.49, 2633.6), (0.48, 15500.0), (0.47, 3000.0), (0.46, 1000.0), (0.45, 2124.0)]
  NO 卖盘: [(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