# 取得事件合約 K 線
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_event_contract_kline(code, pre_side=None, ktype=KLType.K_DAY, kline_source=None, max_count=1000)
介紹
取得事件合約即時 K 線,支援合約級成交價 K 線與 YES 子合約擺盤 K 線兩種來源,支援按方向(YES/NO)與 K 線類型查詢。查詢 K 線前需先訂閱對應的 K 線類型(如
SubType.K_DAY),否則返回錯誤。參數
參數 類型 說明 code str 事件合約代碼,例如 'EC.KXODIMATCH-26JUL140600INDENG-IND'pre_side PredSide 合約方向 ktype KLType K 線類型,預設 KLType.K_DAYkline_source ECKlineSource K 線來源 max_count int 最大返回條數,預設 1000 返回
參數 類型 說明 ret RET_CODE 介面調用結果 data pd.DataFrame 當 ret == RET_OK,返回 K 線列表 str 當 ret != RET_OK,返回錯誤描述 返回的 DataFrame 欄位如下:
欄位 類型 說明 code str 合約代碼 pre_side str 合約方向( YES/NO/N/A),取值參見 PredSidename str 合約名稱 time_key str K 線時間(yyyy-MM-dd HH:mm:ss) open float 開盤價 high float 最高價 low float 最低價 close float 收盤價 volume float 成交量 Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
# 查詢 K 線前需先訂閱對應 K 線類型(否則返回錯誤)
ret, err = quote_ctx.subscribe_event_contract(
['EC.KXODIMATCH-26JUL140600INDENG-IND'], [SubType.K_DAY]
)
if ret != RET_OK:
print('訂閱失敗:', err)
else:
# 合約級成交價 K 線(YES 方向)
ret, data = quote_ctx.get_event_contract_kline(
'EC.KXODIMATCH-26JUL140600INDENG-IND',
pre_side=PredSide.YES, ktype=KLType.K_DAY, max_count=5
)
if ret == RET_OK:
print(data)
else:
print('error:', data)
quote_ctx.close()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- Output
code pre_side name time_key open high low close volume
0 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-11 00:00:00 0.30 0.38 0.30 0.35 433.0
1 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-12 00:00:00 0.64 0.73 0.35 0.52 10482.0
2 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-13 00:00:00 0.51 0.52 0.46 0.49 193327.0
3 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-14 00:00:00 0.49 0.99 0.37 0.99 3860182.0
2
3
4
5
# Qot_GetEventContractKline.proto
介紹
拉取單隻事件合約的 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; // 成交量
}
// 單隻合約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線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填,contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型,沿用通用 KLType(僅支援 KLType_1Min/KLType_5Min/KLType_60Min/KLType_Day,其餘報錯)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
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
- 介面調用結果,結構參見 RetType
協議 ID
3447
數據類型
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
uint GetEventContractKline(GetEventContractKline.Request req);
virtual void OnReply_GetEventContractKline(FTAPI_Conn client, uint nSerialNo, GetEventContractKline.Response rsp);
介紹
拉取單隻事件合約的 K 線資料。
參數
message ECKLine {
required string time_key = 1;
optional double open = 2;
optional double high = 3;
optional double low = 4;
optional double close = 5;
optional double volume = 6;
}
message KlineItem {
required Qot_Common.Security code = 1;
optional Common.PredSide pre_side = 2;
optional string name = 3;
repeated ECKLine klineList = 4;
}
message C2S {
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
optional Qot_Common.KLType ktype = 4;
optional int32 maxCount = 5;
}
message Request {
required C2S c2s = 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
- 股票結構參見 Security
- 返回
message S2C {
repeated KlineItem klineList = 1;
}
message Response {
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
- 介面調用結果,結構參見 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)
{
if (errCode != 0) return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_EventContract)
.SetCode("EC.KXODIMATCH-26JUL140600INDENG-IND").Build();
var c2s = GetEventContractKline.C2S.CreateBuilder()
.SetSecurity(sec)
.SetPreSide(Common.PredSide.PredSide_Yes)
.SetKtype((int)QotCommon.KLType.KLType_Day)
.SetMaxCount(10).Build();
var req = GetEventContractKline.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetEventContractKline(req);
Console.Write("Send GetEventContractKline: {0}\n", seqNo);
}
public void OnReply_GetEventContractKline(FTAPI_Conn client, uint nSerialNo, GetEventContractKline.Response rsp)
{
Console.Write("Reply: {0} {1}\n", nSerialNo, rsp.ToString());
}
public static void Main(String[] args)
{
FTAPI.Init();
new Program().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
int getEventContractKline(QotGetEventContractKline.Request req);
void onReply_GetEventContractKline(FTAPI_Conn client, int nSerialNo, QotGetEventContractKline.Response rsp);
介紹
拉取單隻事件合約的 K 線資料。
參數
message C2S {
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
optional Qot_Common.KLType ktype = 4;
optional int32 maxCount = 5;
}
message Request {
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 股票結構參見 Security
- 返回
message S2C {
repeated KlineItem klineList = 1;
}
message Response {
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
- 介面調用結果,結構參見 RetType
- Example
public class Program implements FTSPI_Qot, FTSPI_Conn {
private FTAPI_Conn_Qot qot = new FTAPI_Conn_Qot();
public Program() {
qot.setClientInfo("java", 1);
qot.setConnCallback(this);
qot.setQotCallback(this);
}
public void start() {
qot.initConnect("127.0.0.1", (short) 11111, false);
}
@Override
public void onInitConnect(FTAPI_Conn client, long errCode, String desc) {
if (errCode != 0) return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_EventContract_VALUE)
.setCode("EC.KXODIMATCH-26JUL140600INDENG-IND").build();
QotGetEventContractKline.C2S c2s = QotGetEventContractKline.C2S.newBuilder()
.setSecurity(sec)
.setPreSide(Common.PredSide.PredSide_Yes)
.setKtype(QotCommon.KLType.KLType_Day_VALUE)
.setMaxCount(10).build();
QotGetEventContractKline.Request req = QotGetEventContractKline.Request.newBuilder()
.setC2S(c2s).build();
int seqNo = qot.getEventContractKline(req);
System.out.println("Send GetEventContractKline: " + seqNo);
}
@Override
public void onReply_GetEventContractKline(FTAPI_Conn client, int nSerialNo, QotGetEventContractKline.Response rsp) {
System.out.println("Reply: " + nSerialNo + " " + rsp.toString());
}
public static void main(String[] args) {
FTAPI.init();
new Program().start();
while (true) {
try { Thread.sleep(1000 * 600); } catch (InterruptedException e) {}
}
}
}
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
Futu::u32_t GetEventContractKline(const Qot_GetEventContractKline::Request &stReq);
virtual void OnReply_GetEventContractKline(Futu::u32_t nSerialNo, const Qot_GetEventContractKline::Response &stRsp) = 0;
介紹
拉取單隻事件合約的 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; // 成交量
}
// 單隻合約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線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填,contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型,沿用通用 KLType(僅支援 KLType_1Min/KLType_5Min/KLType_60Min/KLType_Day,其餘報錯)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
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
- 介面調用結果,結構參見 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;
// 組包
Qot_GetEventContractKline::Request req;
Qot_GetEventContractKline::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("EC.KXODIMATCH-26JUL140600INDENG-IND");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->set_preside(Common::PredSide::PredSide_Yes);
c2s->set_ktype(Qot_Common::KLType::KLType_Day);
c2s->set_maxcount(10);
m_GetEventContractKlineSerialNo = m_pQotApi->GetEventContractKline(req);
cout << "Request GetEventContractKline SerialNo: " << m_GetEventContractKlineSerialNo << endl;
}
virtual void OnReply_GetEventContractKline(Futu::u32_t nSerialNo, const Qot_GetEventContractKline::Response &stRsp) {
if (nSerialNo == m_GetEventContractKlineSerialNo)
{
cout << "OnReply_GetEventContractKline SerialNo: " << nSerialNo << 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_GetEventContractKlineSerialNo;
};
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
- Output
connect
Request GetEventContractKline SerialNo: 1
OnReply_GetEventContractKline SerialNo: 1
{
"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-11 00:00:00",
"open": 0.30,
"high": 0.38,
"low": 0.30,
"close": 0.35,
"volume": 433.0
},
{
"time_key": "2026-07-12 00:00:00",
"open": 0.64,
"high": 0.73,
"low": 0.35,
"close": 0.52,
"volume": 10482.0
},
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.46,
"close": 0.49,
"volume": 193327.0
},
{
"time_key": "2026-07-14 00:00:00",
"open": 0.49,
"high": 0.99,
"low": 0.37,
"close": 0.99,
"volume": 3860182.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
GetEventContractKline(req);
介紹
取得事件合約即時 K 線,支援 K 線來源、合約方向與 K 線類型組合查詢。查詢 K 線前需先訂閱對應的 K 線類型(如
SubType.K_DAY),否則返回錯誤。參數
// 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; // 成交量
}
// 單隻合約K線
message KlineItem {
required Qot_Common.Security code = 1; // 合約代碼
optional Common.PredSide pre_side = 2; // 合約方向(合約K線時有值)
optional string name = 3; // 合約名稱
repeated ECKLine klineList = 4; // K線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型(僅支援 1Min/5Min/60Min/Day)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 返回
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
- 介面調用結果,結構參見 RetType
- Example
import ftWebsocket from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
import beautify from "js-beautify";
function GetEventContractKline() {
const { RetType } = Common;
const { PredSide } = Common;
const { QotMarket, KLType } = Qot_Common;
let [addr, port, enable_ssl, key] = ["127.0.0.1", 11111, false, ""];
let websocket = new ftWebsocket();
websocket.onlogin = async (ret, msg) => {
if (!ret) { console.log("start error", msg); return; }
try {
// 查詢 K 線前需先訂閱對應 K 線類型(否則返回錯誤)
const subReq = { c2s: { codeList: ["EC.KXODIMATCH-26JUL140600INDENG-IND"], subtypeList: [Qot_Common.SubType.SubType_K_DAY] } };
await websocket.SubscribeEventContract(subReq);
const req = { c2s: { security: { market: QotMarket.QotMarket_EventContract, code: "EC.KXODIMATCH-26JUL140600INDENG-IND" }, preSide: PredSide.PredSide_Yes, ktype: KLType.KLType_Day, maxCount: 10 } };
let { errCode, retMsg, retType, s2c } = await websocket.GetEventContractKline(req);
console.log("GetEventContractKline: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if (retType == RetType.RetType_Succeed) {
console.log(beautify(JSON.stringify(s2c), { indent_size: 2, space_in_empty_paren: true }));
}
} catch (error) {
console.log("error:", error);
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(() => { websocket.stop(); console.log("stop"); }, 5000);
}
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
- Output
GetEventContractKline: errCode 0, retMsg , retType 0
{
"klineList": [{
"code": {
"market": 101,
"code": "EC.KXODIMATCH-26JUL140600INDENG-IND"
},
"preSide": 1,
"name": "England vs India Winner?",
"klineList": [{
"timeKey": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831
}]
}]
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_event_contract_kline(code, pre_side=None, ktype=KLType.K_DAY, kline_source=None, max_count=1000)
介紹
取得事件合約即時 K 線,支援合約級成交價 K 線與 YES 子合約擺盤 K 線兩種來源,支援按方向(YES/NO)與 K 線類型查詢。查詢 K 線前需先訂閱對應的 K 線類型(如
SubType.K_DAY),否則返回錯誤。參數
參數 類型 說明 code str 事件合約代碼,例如 'EC.KXODIMATCH-26JUL140600INDENG-IND'pre_side PredSide 合約方向 ktype KLType K 線類型,預設 KLType.K_DAYkline_source ECKlineSource K 線來源 max_count int 最大返回條數,預設 1000 返回
參數 類型 說明 ret RET_CODE 介面調用結果 data pd.DataFrame 當 ret == RET_OK,返回 K 線列表 str 當 ret != RET_OK,返回錯誤描述 返回的 DataFrame 欄位如下:
欄位 類型 說明 code str 合約代碼 pre_side str 合約方向( YES/NO/N/A),取值參見 PredSidename str 合約名稱 time_key str K 線時間(yyyy-MM-dd HH:mm:ss) open float 開盤價 high float 最高價 low float 最低價 close float 收盤價 volume float 成交量 Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
# 查詢 K 線前需先訂閱對應 K 線類型(否則返回錯誤)
ret, err = quote_ctx.subscribe_event_contract(
['EC.KXODIMATCH-26JUL140600INDENG-IND'], [SubType.K_DAY]
)
if ret != RET_OK:
print('訂閱失敗:', err)
else:
# 合約級成交價 K 線(YES 方向)
ret, data = quote_ctx.get_event_contract_kline(
'EC.KXODIMATCH-26JUL140600INDENG-IND',
pre_side=PredSide.YES, ktype=KLType.K_DAY, max_count=5
)
if ret == RET_OK:
print(data)
else:
print('error:', data)
quote_ctx.close()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- Output
code pre_side name time_key open high low close volume
0 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-11 00:00:00 0.30 0.38 0.30 0.35 433.0
1 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-12 00:00:00 0.64 0.73 0.35 0.52 10482.0
2 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-13 00:00:00 0.51 0.52 0.46 0.49 193327.0
3 EC.KXODIMATCH-26JUL140600INDENG-IND YES England vs India Winner? 2026-07-14 00:00:00 0.49 0.99 0.37 0.99 3860182.0
2
3
4
5
# Qot_GetEventContractKline.proto
介紹
拉取單隻事件合約的 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; // 成交量
}
// 單隻合約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線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填,contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型,沿用通用 KLType(僅支援 KLType_1Min/KLType_5Min/KLType_60Min/KLType_Day,其餘報錯)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
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
- 介面調用結果,結構參見 RetType
協議 ID
3447
數據類型
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
uint GetEventContractKline(GetEventContractKline.Request req);
virtual void OnReply_GetEventContractKline(MMAPI_Conn client, uint nSerialNo, GetEventContractKline.Response rsp);
介紹
拉取單隻事件合約的 K 線資料。
參數
message ECKLine {
required string time_key = 1;
optional double open = 2;
optional double high = 3;
optional double low = 4;
optional double close = 5;
optional double volume = 6;
}
message KlineItem {
required Qot_Common.Security code = 1;
optional Common.PredSide pre_side = 2;
optional string name = 3;
repeated ECKLine klineList = 4;
}
message C2S {
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
optional Qot_Common.KLType ktype = 4;
optional int32 maxCount = 5;
}
message Request {
required C2S c2s = 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
- 股票結構參見 Security
- 返回
message S2C {
repeated KlineItem klineList = 1;
}
message Response {
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
- 介面調用結果,結構參見 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)
{
if (errCode != 0) return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_EventContract)
.SetCode("EC.KXODIMATCH-26JUL140600INDENG-IND").Build();
var c2s = GetEventContractKline.C2S.CreateBuilder()
.SetSecurity(sec)
.SetPreSide(Common.PredSide.PredSide_Yes)
.SetKtype((int)QotCommon.KLType.KLType_Day)
.SetMaxCount(10).Build();
var req = GetEventContractKline.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetEventContractKline(req);
Console.Write("Send GetEventContractKline: {0}\n", seqNo);
}
public void OnReply_GetEventContractKline(MMAPI_Conn client, uint nSerialNo, GetEventContractKline.Response rsp)
{
Console.Write("Reply: {0} {1}\n", nSerialNo, rsp.ToString());
}
public static void Main(String[] args)
{
MMAPI.Init();
new Program().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
int getEventContractKline(QotGetEventContractKline.Request req);
void onReply_GetEventContractKline(MMAPI_Conn client, int nSerialNo, QotGetEventContractKline.Response rsp);
介紹
拉取單隻事件合約的 K 線資料。
參數
message C2S {
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
optional Qot_Common.KLType ktype = 4;
optional int32 maxCount = 5;
}
message Request {
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
- 股票結構參見 Security
- 返回
message S2C {
repeated KlineItem klineList = 1;
}
message Response {
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
- 介面調用結果,結構參見 RetType
- Example
public class Program implements MMSPI_Qot, MMSPI_Conn {
private MMAPI_Conn_Qot qot = new MMAPI_Conn_Qot();
public Program() {
qot.setClientInfo("java", 1);
qot.setConnCallback(this);
qot.setQotCallback(this);
}
public void start() {
qot.initConnect("127.0.0.1", (short) 11111, false);
}
@Override
public void onInitConnect(MMAPI_Conn client, long errCode, String desc) {
if (errCode != 0) return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_EventContract_VALUE)
.setCode("EC.KXODIMATCH-26JUL140600INDENG-IND").build();
QotGetEventContractKline.C2S c2s = QotGetEventContractKline.C2S.newBuilder()
.setSecurity(sec)
.setPreSide(Common.PredSide.PredSide_Yes)
.setKtype(QotCommon.KLType.KLType_Day_VALUE)
.setMaxCount(10).build();
QotGetEventContractKline.Request req = QotGetEventContractKline.Request.newBuilder()
.setC2S(c2s).build();
int seqNo = qot.getEventContractKline(req);
System.out.println("Send GetEventContractKline: " + seqNo);
}
@Override
public void onReply_GetEventContractKline(MMAPI_Conn client, int nSerialNo, QotGetEventContractKline.Response rsp) {
System.out.println("Reply: " + nSerialNo + " " + rsp.toString());
}
public static void main(String[] args) {
MMAPI.init();
new Program().start();
while (true) {
try { Thread.sleep(1000 * 600); } catch (InterruptedException e) {}
}
}
}
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
moomoo::u32_t GetEventContractKline(const Qot_GetEventContractKline::Request &stReq);
virtual void OnReply_GetEventContractKline(moomoo::u32_t nSerialNo, const Qot_GetEventContractKline::Response &stRsp) = 0;
介紹
拉取單隻事件合約的 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; // 成交量
}
// 單隻合約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線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填,contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型,沿用通用 KLType(僅支援 KLType_1Min/KLType_5Min/KLType_60Min/KLType_Day,其餘報錯)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- 事件合約 K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
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
- 介面調用結果,結構參見 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;
// 組包
Qot_GetEventContractKline::Request req;
Qot_GetEventContractKline::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("EC.KXODIMATCH-26JUL140600INDENG-IND");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->set_preside(Common::PredSide::PredSide_Yes);
c2s->set_ktype(Qot_Common::KLType::KLType_Day);
c2s->set_maxcount(10);
m_GetEventContractKlineSerialNo = m_pQotApi->GetEventContractKline(req);
cout << "Request GetEventContractKline SerialNo: " << m_GetEventContractKlineSerialNo << endl;
}
virtual void OnReply_GetEventContractKline(moomoo::u32_t nSerialNo, const Qot_GetEventContractKline::Response &stRsp) {
if (nSerialNo == m_GetEventContractKlineSerialNo)
{
cout << "OnReply_GetEventContractKline SerialNo: " << nSerialNo << 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_GetEventContractKlineSerialNo;
};
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
- Output
connect
Request GetEventContractKline SerialNo: 1
OnReply_GetEventContractKline SerialNo: 1
{
"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-11 00:00:00",
"open": 0.30,
"high": 0.38,
"low": 0.30,
"close": 0.35,
"volume": 433.0
},
{
"time_key": "2026-07-12 00:00:00",
"open": 0.64,
"high": 0.73,
"low": 0.35,
"close": 0.52,
"volume": 10482.0
},
{
"time_key": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.46,
"close": 0.49,
"volume": 193327.0
},
{
"time_key": "2026-07-14 00:00:00",
"open": 0.49,
"high": 0.99,
"low": 0.37,
"close": 0.99,
"volume": 3860182.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
GetEventContractKline(req);
介紹
取得事件合約即時 K 線,支援 K 線來源、合約方向與 K 線類型組合查詢。查詢 K 線前需先訂閱對應的 K 線類型(如
SubType.K_DAY),否則返回錯誤。參數
// 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; // 成交量
}
// 單隻合約K線
message KlineItem {
required Qot_Common.Security code = 1; // 合約代碼
optional Common.PredSide pre_side = 2; // 合約方向(合約K線時有值)
optional string name = 3; // 合約名稱
repeated ECKLine klineList = 4; // K線資料
}
message C2S {
required Qot_Common.Security security = 1; // 合約標的(單隻,必填)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),優先級高於 preSide
optional Common.PredSide preSide = 3; // 方向(klineSource=None 時必傳 YES/NO)
optional Qot_Common.KLType ktype = 4; // K線類型(僅支援 1Min/5Min/60Min/Day)
optional int32 maxCount = 5; // 最大返回條數(上限1000)
}
message Request {
required C2S c2s = 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
29
- 返回
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
- 介面調用結果,結構參見 RetType
- Example
import mmWebsocket from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function GetEventContractKline() {
const { RetType } = Common;
const { PredSide } = Common;
const { QotMarket, KLType } = Qot_Common;
let [addr, port, enable_ssl, key] = ["127.0.0.1", 11111, false, ""];
let websocket = new mmWebsocket();
websocket.onlogin = async (ret, msg) => {
if (!ret) { console.log("start error", msg); return; }
try {
// 查詢 K 線前需先訂閱對應 K 線類型(否則返回錯誤)
const subReq = { c2s: { codeList: ["EC.KXODIMATCH-26JUL140600INDENG-IND"], subtypeList: [Qot_Common.SubType.SubType_K_DAY] } };
await websocket.SubscribeEventContract(subReq);
const req = { c2s: { security: { market: QotMarket.QotMarket_EventContract, code: "EC.KXODIMATCH-26JUL140600INDENG-IND" }, preSide: PredSide.PredSide_Yes, ktype: KLType.KLType_Day, maxCount: 10 } };
let { errCode, retMsg, retType, s2c } = await websocket.GetEventContractKline(req);
console.log("GetEventContractKline: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if (retType == RetType.RetType_Succeed) {
console.log(beautify(JSON.stringify(s2c), { indent_size: 2, space_in_empty_paren: true }));
}
} catch (error) {
console.log("error:", error);
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(() => { websocket.stop(); console.log("stop"); }, 5000);
}
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
- Output
GetEventContractKline: errCode 0, retMsg , retType 0
{
"klineList": [{
"code": {
"market": 101,
"code": "EC.KXODIMATCH-26JUL140600INDENG-IND"
},
"preSide": 1,
"name": "England vs India Winner?",
"klineList": [{
"timeKey": "2026-07-13 00:00:00",
"open": 0.51,
"high": 0.52,
"low": 0.48,
"close": 0.5,
"volume": 7831
}]
}]
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
← 事件合約擺盤推送 事件合約 K 線推送 →