# 實時 K 線回呼

on_recv_rsp(self, rsp_pb)

  • 介紹

    實時 K 線回呼,非同步處理已訂閲股票的實時 K 線推送。

    在收到實時 K 線數據推送後會回呼到該函數,您需要在衍生類別中覆寫 on_recv_rsp。

  • 參數

    參數 類型 説明
    rsp_pb Qot_UpdateKL_pb2.Response 衍生類別中不需要直接處理該參數
  • 返回

    參數 類型 説明
    ret RET_CODE 介面呼叫結果
    data pd.DataFrame 當 ret == RET_OK,返回 K 線數據數據
    str 當 ret != RET_OK,返回錯誤描述
    • K 線數據格式如下:
      欄位 類型 説明
      code str 股票代碼
      name str 股票名稱
      time_key str 時間
      open float 開盤價
      close float 收盤價
      high float 最高價
      low float 最低價
      volume int 成交量
      turnover float 成交額
      pe_ratio float 市盈率
      turnover_rate float 換手率
      last_close float 昨收價
      k_type KLType K 線類型
  • Example

import time
from moomoo import *
class CurKlineTest(CurKlineHandlerBase):
    def on_recv_rsp(self, rsp_pb):
        ret_code, data = super(CurKlineTest,self).on_recv_rsp(rsp_pb)
        if ret_code != RET_OK:
            print("CurKlineTest: error, msg: %s" % data)
            return RET_ERROR, data
        print("CurKlineTest ", data) # CurKlineTest 自己的處理邏輯
        return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = CurKlineTest()
quote_ctx.set_handler(handler)  # 設定實時K線回呼
ret, data = quote_ctx.subscribe(['US.AAPL'], [SubType.K_1M], session=Session.ALL)   # 訂閲 K 線數據類型,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
  • Output
CurKlineTest        code name             time_key    open   close    high    low  volume   turnover k_type  last_close
0  US.AAPL   蘋果  2025-04-07 05:15:00  180.39  180.26  180.46  180.2    1322  238340.48   K_1M         0.0
1
2

提示