# 事件合约K线推送
- Python
- Proto
- C#
- Java
- C++
- JavaScript
on_recv_rsp(self, rsp_pb)
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
在收到事件合约 K 线数据推送后会回调到该函数,您需要在派生类中覆盖 on_recv_rsp。参数
参数 类型 说明 rsp_pb Qot_UpdateEventContractKline_pb2.Response 派生类中不需要直接处理该参数 返回
参数 类型 说明 ret RET_CODE 接口调用结果 data pd.DataFrame 当 ret == RET_OK,返回事件合约 K 线数据 str 当 ret != RET_OK,返回错误描述 - K 线数据格式如下:
字段 类型 说明 code str 事件合约代码 pre_side PredSide 合约方向(YES/NO),合约 K 线时有值,事件 K 线以服务端返回为准 name str 合约名称 time_key str K 线时间 格式:yyyy-MM-dd HH:mm:ssopen float 开盘价 high float 最高价 low float 最低价 close float 收盘价 volume float 成交量
- K 线数据格式如下:
Example
import time
from futu import *
class EventContractKlineTest(EventContractKlineHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, data = super(EventContractKlineTest, self).on_recv_rsp(rsp_pb)
if ret_code != RET_OK:
print("EventContractKlineTest: error, msg: %s" % data)
return RET_ERROR, data
print("EventContractKlineTest ", data) # EventContractKlineTest 自己的处理逻辑
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = EventContractKlineTest()
quote_ctx.set_handler(handler) # 设置事件合约 K 线推送回调
ret, data = quote_ctx.subscribe_event_contract(
code_list=['EC.KXODIMATCH-26JUL140600INDENG-IND'],
subtype_list=[SubType.K_DAY],
kline_source_list=[ECKlineSource.NONE],
subscribe_push=True) # 订阅 K 线类型,OpenD 开始持续收到服务器的推送
if ret == RET_OK:
print(data)
else:
print('error:', data)
time.sleep(15) # 设置脚本接收 OpenD 的推送持续时间为15秒
quote_ctx.close() # 关闭当条连接,OpenD 会在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
- Output
EventContractKlineTest code pre_side name time_key open high low close volume
0 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-13 00:00:00 0.51 0.52 0.48 0.5 7831.0
2
# Qot_UpdateEventContractKline.proto
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
// 单只合约 K 线
message KlineItem
{
required Qot_Common.Security code = 1; // 合约代码
optional Common.PredSide pre_side = 2; // 合约方向(合约K线时有值,事件K线以服务端返回为准)
optional string name = 3; // 合约名称
repeated ECKLine klineList = 4; // K线数据
}
// K线数据点
message ECKLine
{
required string time_key = 1; // K线时间(yyyy-MM-dd HH:mm:ss)
optional double open = 2; // 开盘价
optional double high = 3; // 最高价
optional double low = 4; // 最低价
optional double close = 5; // 收盘价
optional double volume = 6; // 成交量
}
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
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
29
30
31
32
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
协议 ID
3451
virtual void OnPush_UpdateEventContractKline(FTAPI_Conn client, QotUpdateEventContractKline.Response rsp);
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
public class Program : FTSPI_Qot, FTSPI_Conn {
FTAPI_Qot qot = new FTAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //设置客户端信息
qot.SetConnCallback(this); //设置连接回调
qot.SetQotCallback(this); //设置行情回调
}
public void Start() {
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(FTAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_EventContract)
.SetCode("EC.KXODIMATCH-26JUL140600INDENG-IND")
.Build();
QotSubEventContract.C2S c2s = QotSubEventContract.C2S.CreateBuilder()
.AddSecurity(sec)
.AddSubType((int)QotCommon.SubType.SubType_KLDay)
.SetIsSubOrUnSub(true)
.SetIsRegOrUnRegPush(true)
.Build();
QotSubEventContract.Request req = QotSubEventContract.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.SubEventContract(req);
Console.Write("Send SubEventContract: {0}\n", seqNo);
}
public void OnPush_UpdateEventContractKline(FTAPI_Conn client, QotUpdateEventContractKline.Response rsp)
{
Console.Write("Push: UpdateEventContractKline: {0}\n", rsp.ToString());
if (rsp.S2C != null && rsp.S2C.KlineListList.Count > 0)
{
var item = rsp.S2C.KlineListList[0];
if (item.KlineListList.Count > 0)
{
var k = item.KlineListList[0];
Console.Write("code: {0} pre_side: {1} time_key: {2} open: {3} close: {4}\n",
item.Code.Code, item.PreSide, k.TimeKey, k.Open, k.Close);
}
}
}
public static void Main(String[] args) {
FTAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
- Output
Qot onInitConnect: ret=0 desc= connID=6825408105210218283
Send SubEventContract: 1
Push: UpdateEventContractKline: retType: 0
code: EC.KXODIMATCH-26JUL140600INDENG-IND pre_side: 1 time_key: 2026-07-13 00:00:00 open: 0.51 close: 0.5
...
2
3
4
5
void onPush_UpdateEventContractKline(FTAPI_Conn client, QotUpdateEventContractKline.Response rsp);
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
public class QotDemo implements FTSPI_Qot, FTSPI_Conn {
FTAPI_Conn_Qot qot = new FTAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //设置客户端信息
qot.setConnSpi(this); //设置连接回调
qot.setQotSpi(this); //设置行情回调
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(FTAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_EventContract_VALUE)
.setCode("EC.KXODIMATCH-26JUL140600INDENG-IND")
.build();
QotSubEventContract.C2S c2s = QotSubEventContract.C2S.newBuilder()
.addSecurity(sec)
.addSubType(QotCommon.SubType.SubType_KLDay_VALUE)
.setIsSubOrUnSub(true)
.setIsRegOrUnRegPush(true)
.build();
QotSubEventContract.Request req = QotSubEventContract.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.subEventContract(req);
System.out.printf("Send SubEventContract: %d\n", seqNo);
}
@Override
public void onPush_UpdateEventContractKline(FTAPI_Conn client, QotUpdateEventContractKline.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotUpdateEventContractKline failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotUpdateEventContractKline: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FTAPI.init();
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
- Output
Receive QotUpdateEventContractKline: {
"retType": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXODIMATCH-26JUL140600INDENG-IND"},
"pre_side": 1,
"name": "England vs India Winner?",
"klineList": [
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831.0
}
]
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
virtual void OnPush_UpdateEventContractKline(const Qot_UpdateEventContractKline::Response &stRsp) = 0;
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
// 单只合约 K 线
message KlineItem
{
required Qot_Common.Security code = 1; // 合约代码
optional Common.PredSide pre_side = 2; // 合约方向(合约K线时有值,事件K线以服务端返回为准)
optional string name = 3; // 合约名称
repeated ECKLine klineList = 4; // K线数据
}
// K线数据点
message ECKLine
{
required string time_key = 1; // K线时间(yyyy-MM-dd HH:mm:ss)
optional double open = 2; // 开盘价
optional double high = 3; // 最高价
optional double low = 4; // 最低价
optional double close = 5; // 收盘价
optional double volume = 6; // 成交量
}
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
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
29
30
31
32
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
class Program : public FTSPI_Qot, public FTSPI_Trd, public FTSPI_Conn
{
public:
Program() {
m_pQotApi = FTAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this);
m_pQotApi->RegisterConnSpi(this);
}
~Program() {
if (m_pQotApi != nullptr)
{
m_pQotApi->UnregisterQotSpi();
m_pQotApi->UnregisterConnSpi();
FTAPI::ReleaseQotApi(m_pQotApi);
m_pQotApi = nullptr;
}
}
void Start() {
m_pQotApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(FTAPI_Conn* pConn, Futu::i64_t nErrCode, const char* strDesc) {
cout << "connect" << endl;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
Qot_SubEventContract::Request req;
Qot_SubEventContract::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->add_securitylist();
sec->set_code("EC.KXODIMATCH-26JUL140600INDENG-IND");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->add_subtypelist(Qot_Common::SubType::SubType_KLDay);
c2s->add_klinesource(Qot_Common::EC_KlineSource::EC_KlineSource_OrderBookYes);
c2s->set_isregorunregpush(true);
c2s->set_issuborunsub(true);
m_SubEventContractSerialNo = m_pQotApi->SubEventContract(req);
cout << "Request SubEventContract SerialNo: " << m_SubEventContractSerialNo << endl;
}
virtual void OnReply_SubEventContract(Futu::u32_t nSerialNo, const Qot_SubEventContract::Response &stRsp) {
if (nSerialNo == m_SubEventContractSerialNo)
{
cout << "OnReply_SubEventContract SerialNo: " << nSerialNo << endl;
}
}
virtual void OnPush_UpdateEventContractKline(const Qot_UpdateEventContractKline::Response &stRsp) {
cout << "OnPush_UpdateEventContractKline" << endl;
// 解析内部结构打印出来
// ProtoBufToBodyData和UTF8ToLocal函数的定义参见Sample中的tool.h文件
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
FTAPI_Qot *m_pQotApi;
Futu::u32_t m_SubEventContractSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
FTAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
FTAPI::UnInit();
return 0;
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
- Output
connect
Request SubEventContract SerialNo: 1
OnReply_SubEventContract SerialNo: 1
OnPush_UpdateEventContractKline
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXODIMATCH-26JUL140600INDENG-IND"},
"pre_side": 1,
"name": "England vs India Winner?",
"klineList": [
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831.0
}
]
}
]
}
}
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
onPush(cmd, res)
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
import ftWebsocket from "futu-api";
import { ftCmdID } from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
function QotUpdateEventContractKline(){
const { RetType } = Common
const { SubType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登录成功
const req = {
c2s: {
securityList: [
{
market: QotMarket.QotMarket_EventContract,
code: "EC.KXODIMATCH-26JUL140600INDENG-IND",
},
],
subTypeList: [ SubType.SubType_KLDay ], // 订阅 K 线类型
isSubOrUnSub: true, // 订阅 true, 反订阅 false
isRegOrUnRegPush: true, // 注册推送 true, 反注册推送 false
},
};
websocket.SubEventContract(req) // 订阅, OpenD 开始持续收到服务器的推送
.then((res) => { })
.catch((error) => {
if ("retMsg" in error) {
console.log("error:", error.retMsg);
}
});
} else {
console.log("error", msg);
}
};
websocket.onPush = (cmd, res)=>{
if(ftCmdID.QotUpdateEventContractKline.cmd == cmd){ // 事件合约 K 线推送的处理逻辑
let { retType, s2c } = res
if(retType == RetType.RetType_Succeed){
console.log("EventContractKlineTest", JSON.stringify(s2c));
} else {
console.log("EventContractKlineTest: error")
}
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 接收 OpenD 的推送持续时间为5秒,5秒后断开
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- Output
EventContractKlineTest {"klineList":[{"code":{"market":101,"code":"EC.KXODIMATCH-26JUL140600INDENG-IND"},"pre_side":1,"name":"England vs India Winner?","klineList":[{"time_key":"2026-07-13 00:00:00","open":0.51,"high":0.52,"low":0.48,"close":0.5,"volume":7831.0}]}]}
stop
2
- Python
- Proto
- C#
- Java
- C++
- JavaScript
on_recv_rsp(self, rsp_pb)
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
在收到事件合约 K 线数据推送后会回调到该函数,您需要在派生类中覆盖 on_recv_rsp。参数
参数 类型 说明 rsp_pb Qot_UpdateEventContractKline_pb2.Response 派生类中不需要直接处理该参数 返回
参数 类型 说明 ret RET_CODE 接口调用结果 data pd.DataFrame 当 ret == RET_OK,返回事件合约 K 线数据 str 当 ret != RET_OK,返回错误描述 - K 线数据格式如下:
字段 类型 说明 code str 事件合约代码 pre_side PredSide 合约方向(YES/NO),合约 K 线时有值,事件 K 线以服务端返回为准 name str 合约名称 time_key str K 线时间 格式:yyyy-MM-dd HH:mm:ssopen float 开盘价 high float 最高价 low float 最低价 close float 收盘价 volume float 成交量
- K 线数据格式如下:
Example
import time
from moomoo import *
class EventContractKlineTest(EventContractKlineHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, data = super(EventContractKlineTest, self).on_recv_rsp(rsp_pb)
if ret_code != RET_OK:
print("EventContractKlineTest: error, msg: %s" % data)
return RET_ERROR, data
print("EventContractKlineTest ", data) # EventContractKlineTest 自己的处理逻辑
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = EventContractKlineTest()
quote_ctx.set_handler(handler) # 设置事件合约 K 线推送回调
ret, data = quote_ctx.subscribe_event_contract(
code_list=['EC.KXODIMATCH-26JUL140600INDENG-IND'],
subtype_list=[SubType.K_DAY],
kline_source_list=[ECKlineSource.NONE],
subscribe_push=True) # 订阅 K 线类型,OpenD 开始持续收到服务器的推送
if ret == RET_OK:
print(data)
else:
print('error:', data)
time.sleep(15) # 设置脚本接收 OpenD 的推送持续时间为15秒
quote_ctx.close() # 关闭当条连接,OpenD 会在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
- Output
EventContractKlineTest code pre_side name time_key open high low close volume
0 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-13 00:00:00 0.51 0.52 0.48 0.5 7831.0
2
# Qot_UpdateEventContractKline.proto
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
// 单只合约 K 线
message KlineItem
{
required Qot_Common.Security code = 1; // 合约代码
optional Common.PredSide pre_side = 2; // 合约方向(合约K线时有值,事件K线以服务端返回为准)
optional string name = 3; // 合约名称
repeated ECKLine klineList = 4; // K线数据
}
// K线数据点
message ECKLine
{
required string time_key = 1; // K线时间(yyyy-MM-dd HH:mm:ss)
optional double open = 2; // 开盘价
optional double high = 3; // 最高价
optional double low = 4; // 最低价
optional double close = 5; // 收盘价
optional double volume = 6; // 成交量
}
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
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
29
30
31
32
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
协议 ID
3451
virtual void OnPush_UpdateEventContractKline(MMAPI_Conn client, QotUpdateEventContractKline.Response rsp);
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
public class Program : MMSPI_Qot, MMSPI_Conn {
MMAPI_Qot qot = new MMAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //设置客户端信息
qot.SetConnCallback(this); //设置连接回调
qot.SetQotCallback(this); //设置行情回调
}
public void Start() {
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(MMAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_EventContract)
.SetCode("EC.KXODIMATCH-26JUL140600INDENG-IND")
.Build();
QotSubEventContract.C2S c2s = QotSubEventContract.C2S.CreateBuilder()
.AddSecurity(sec)
.AddSubType((int)QotCommon.SubType.SubType_KLDay)
.SetIsSubOrUnSub(true)
.SetIsRegOrUnRegPush(true)
.Build();
QotSubEventContract.Request req = QotSubEventContract.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.SubEventContract(req);
Console.Write("Send SubEventContract: {0}\n", seqNo);
}
public void OnPush_UpdateEventContractKline(MMAPI_Conn client, QotUpdateEventContractKline.Response rsp)
{
Console.Write("Push: UpdateEventContractKline: {0}\n", rsp.ToString());
if (rsp.S2C != null && rsp.S2C.KlineListList.Count > 0)
{
var item = rsp.S2C.KlineListList[0];
if (item.KlineListList.Count > 0)
{
var k = item.KlineListList[0];
Console.Write("code: {0} pre_side: {1} time_key: {2} open: {3} close: {4}\n",
item.Code.Code, item.PreSide, k.TimeKey, k.Open, k.Close);
}
}
}
public static void Main(String[] args) {
MMAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
- Output
Qot onInitConnect: ret=0 desc= connID=6825408105210218283
Send SubEventContract: 1
Push: UpdateEventContractKline: retType: 0
code: EC.KXODIMATCH-26JUL140600INDENG-IND pre_side: 1 time_key: 2026-07-13 00:00:00 open: 0.51 close: 0.5
...
2
3
4
5
void onPush_UpdateEventContractKline(MMAPI_Conn client, QotUpdateEventContractKline.Response rsp);
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
public class QotDemo implements MMSPI_Qot, MMSPI_Conn {
MMAPI_Conn_Qot qot = new MMAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //设置客户端信息
qot.setConnSpi(this); //设置连接回调
qot.setQotSpi(this); //设置行情回调
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(MMAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_EventContract_VALUE)
.setCode("EC.KXODIMATCH-26JUL140600INDENG-IND")
.build();
QotSubEventContract.C2S c2s = QotSubEventContract.C2S.newBuilder()
.addSecurity(sec)
.addSubType(QotCommon.SubType.SubType_KLDay_VALUE)
.setIsSubOrUnSub(true)
.setIsRegOrUnRegPush(true)
.build();
QotSubEventContract.Request req = QotSubEventContract.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.subEventContract(req);
System.out.printf("Send SubEventContract: %d\n", seqNo);
}
@Override
public void onPush_UpdateEventContractKline(MMAPI_Conn client, QotUpdateEventContractKline.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotUpdateEventContractKline failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotUpdateEventContractKline: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MMAPI.init();
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
- Output
Receive QotUpdateEventContractKline: {
"retType": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXODIMATCH-26JUL140600INDENG-IND"},
"pre_side": 1,
"name": "England vs India Winner?",
"klineList": [
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831.0
}
]
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
virtual void OnPush_UpdateEventContractKline(const Qot_UpdateEventContractKline::Response &stRsp) = 0;
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
// 单只合约 K 线
message KlineItem
{
required Qot_Common.Security code = 1; // 合约代码
optional Common.PredSide pre_side = 2; // 合约方向(合约K线时有值,事件K线以服务端返回为准)
optional string name = 3; // 合约名称
repeated ECKLine klineList = 4; // K线数据
}
// K线数据点
message ECKLine
{
required string time_key = 1; // K线时间(yyyy-MM-dd HH:mm:ss)
optional double open = 2; // 开盘价
optional double high = 3; // 最高价
optional double low = 4; // 最低价
optional double close = 5; // 收盘价
optional double volume = 6; // 成交量
}
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
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
29
30
31
32
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
class Program : public MMSPI_Qot, public MMSPI_Trd, public MMSPI_Conn
{
public:
Program() {
m_pQotApi = MMAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this);
m_pQotApi->RegisterConnSpi(this);
}
~Program() {
if (m_pQotApi != nullptr)
{
m_pQotApi->UnregisterQotSpi();
m_pQotApi->UnregisterConnSpi();
MMAPI::ReleaseQotApi(m_pQotApi);
m_pQotApi = nullptr;
}
}
void Start() {
m_pQotApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(MMAPI_Conn* pConn, moomoo::i64_t nErrCode, const char* strDesc) {
cout << "connect" << endl;
// 先订阅事件合约 K 线类型,订阅成功后开始持续收到推送
Qot_SubEventContract::Request req;
Qot_SubEventContract::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->add_securitylist();
sec->set_code("EC.KXODIMATCH-26JUL140600INDENG-IND");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->add_subtypelist(Qot_Common::SubType::SubType_KLDay);
c2s->add_klinesource(Qot_Common::EC_KlineSource::EC_KlineSource_OrderBookYes);
c2s->set_isregorunregpush(true);
c2s->set_issuborunsub(true);
m_SubEventContractSerialNo = m_pQotApi->SubEventContract(req);
cout << "Request SubEventContract SerialNo: " << m_SubEventContractSerialNo << endl;
}
virtual void OnReply_SubEventContract(moomoo::u32_t nSerialNo, const Qot_SubEventContract::Response &stRsp) {
if (nSerialNo == m_SubEventContractSerialNo)
{
cout << "OnReply_SubEventContract SerialNo: " << nSerialNo << endl;
}
}
virtual void OnPush_UpdateEventContractKline(const Qot_UpdateEventContractKline::Response &stRsp) {
cout << "OnPush_UpdateEventContractKline" << endl;
// 解析内部结构打印出来
// ProtoBufToBodyData和UTF8ToLocal函数的定义参见Sample中的tool.h文件
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
MMAPI_Qot *m_pQotApi;
moomoo::u32_t m_SubEventContractSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
MMAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
MMAPI::UnInit();
return 0;
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
- Output
connect
Request SubEventContract SerialNo: 1
OnReply_SubEventContract SerialNo: 1
OnPush_UpdateEventContractKline
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXODIMATCH-26JUL140600INDENG-IND"},
"pre_side": 1,
"name": "England vs India Winner?",
"klineList": [
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831.0
}
]
}
]
}
}
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
onPush(cmd, res)
介绍
事件合约 K 线推送回调,异步处理已订阅事件合约的实时 K 线推送。
参数
message S2C
{
repeated KlineItem klineList = 1; // K线列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,返回结果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 合约结构参见 Security
- 合约方向参见 PredSide
- K 线来源参见 EC_KlineSource
- 接口调用结果,结构参见 RetType
- Example
import mmWebsocket from "moomoo-api";
import { mmCmdID } from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
function QotUpdateEventContractKline(){
const { RetType } = Common
const { SubType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new mmWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登录成功
const req = {
c2s: {
securityList: [
{
market: QotMarket.QotMarket_EventContract,
code: "EC.KXODIMATCH-26JUL140600INDENG-IND",
},
],
subTypeList: [ SubType.SubType_KLDay ], // 订阅 K 线类型
isSubOrUnSub: true, // 订阅 true, 反订阅 false
isRegOrUnRegPush: true, // 注册推送 true, 反注册推送 false
},
};
websocket.SubEventContract(req) // 订阅, OpenD 开始持续收到服务器的推送
.then((res) => { })
.catch((error) => {
if ("retMsg" in error) {
console.log("error:", error.retMsg);
}
});
} else {
console.log("error", msg);
}
};
websocket.onPush = (cmd, res)=>{
if(mmCmdID.QotUpdateEventContractKline.cmd == cmd){ // 事件合约 K 线推送的处理逻辑
let { retType, s2c } = res
if(retType == RetType.RetType_Succeed){
console.log("EventContractKlineTest", JSON.stringify(s2c));
} else {
console.log("EventContractKlineTest: error")
}
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 接收 OpenD 的推送持续时间为5秒,5秒后断开
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- Output
EventContractKlineTest {"klineList":[{"code":{"market":101,"code":"EC.KXODIMATCH-26JUL140600INDENG-IND"},"pre_side":1,"name":"England vs India Winner?","klineList":[{"time_key":"2026-07-13 00:00:00","open":0.51,"high":0.52,"low":0.48,"close":0.5,"volume":7831.0}]}]}
stop
2