# 拉取事件合約歷史K線
- Python
- Proto
- C#
- Java
- C++
- JavaScript
request_history_event_contract_kline(code, start=None, end=None, pre_side=None, ktype=KLType.K_DAY, kline_source=None, max_count=1000, page_req_key=None)
介紹
拉取事件合約的歷史 K 線數據,不需要先下載歷史數據。支持按時間區間、方向、K 線來源查詢,自動處理分頁(
max_count大於單包上限時循環拉取)。參數
參數 類型 說明 code str 必填,事件合約代碼,例如 'EC.KXNFLAFCCHAMP-27-CIN'start str 選填,開始時間,例如 '2026-07-05'end str 選填,結束時間,例如 '2026-07-09'pre_side PredSide 選填,合約方向 ktype KLType 選填,K 線類型,僅支持 K_1M/K_5M/K_60M/K_DAY,預設K_DAYkline_source ECKlineSource 選填,K 線來源 max_count int 選填,本次請求最大返回數據點個數, None表示返回區間內全部page_req_key str 選填,分頁請求 key,初始請求傳 None,後續傳上次返回值返回
參數 類型 說明 ret RET_CODE 介面調用結果 data pd.DataFrame 當 ret == RET_OK,返回歷史 K 線數據 str 當 ret != RET_OK,返回錯誤描述 page_req_key str 分頁請求 key,無更多數據時為 None 返回的 DataFrame 欄位如下:
欄位 類型 說明 code str 合約代碼 pre_side str 合約方向(YES/NO/N/A) name str 合約名稱 time_key str K 線時間 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.KXNFLAFCCHAMP-27-CIN'], [SubType.K_DAY]
)
if ret != RET_OK:
print('訂閱失敗:', err)
else:
ret, data, page_req_key = quote_ctx.request_history_event_contract_kline(
'EC.KXNFLAFCCHAMP-27-CIN',
start='2026-07-05', end='2026-07-09',
pre_side=PredSide.YES, ktype=KLType.K_DAY, max_count=10
)
if ret == RET_OK:
print(data)
print('page_req_key:', page_req_key)
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
23
- Output
code pre_side name time_key open high low close volume
0 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-05 00:00:00 0.92 0.92 0.92 0.92 201.0
1 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-06 00:00:00 0.93 0.93 0.92 0.92 659.0
2 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-07 00:00:00 0.93 0.93 0.92 0.92 490.0
3 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-08 00:00:00 0.93 0.93 0.92 0.92 669.0
4 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-09 00:00:00 0.92 0.92 0.92 0.92 343.0
page_req_key: None
2
3
4
5
6
7
# Qot_RequestHistoryEventContractKL.proto
介紹
拉取事件合約的歷史 K 線數據。
參數
// 事件合約歷史K線拉取
// 與通用 Qot_RequestHistoryKL 的差異:入參多 klineSource/preSide 方向欄位,回包結構為 ECKLine(6欄位) + KlineItem.pre_side
// KLType 僅支持 1Min/5Min/60Min/Day
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes);不填當 None(合約級K線),優先級高於 preSide,與實時EC對齊
optional Common.PredSide preSide = 3; // 方向(Yes/No);klineSource=None 且入參為合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,如果未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem,含 ECKLine 數據點與 pre_side)
optional bytes nextReqKey = 2; // 分頁請求key。一次請求沒有返回所有數據時,下次請求帶上這個key,會接著請求
}
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
- 介面調用結果,結構參見 RetType
協議 ID
3456
數據類型
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
uint RequestHistoryEventContractKL(Qot_RequestHistoryEventContractKL.Request req);
virtual void OnReply_RequestHistoryEventContractKL(FTAPI_Conn client, uint nSerialNo, Qot_RequestHistoryEventContractKL.Response rsp);
介紹
拉取事件合約的歷史 K 線數據。
參數
message C2S
{
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
required Qot_Common.KLType klType = 4;
required string beginTime = 5;
required string endTime = 6;
optional int32 maxAckKLNum = 7;
optional bytes nextReqKey = 8;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 股票結構參見 Security
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1;
optional bytes nextReqKey = 2;
}
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
11
12
13
- 介面調用結果,結構參見 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.KXNFLAFCCHAMP-27-CIN").Build();
var c2s = Qot_RequestHistoryEventContractKL.C2S.CreateBuilder()
.SetSecurity(sec)
.SetPreSide(Common.PredSide.PredSide_Yes)
.SetKlType((int)QotCommon.KLType.KLType_Day)
.SetBeginTime("2026-07-05")
.SetEndTime("2026-07-09")
.SetMaxAckKLNum(10).Build();
var req = Qot_RequestHistoryEventContractKL.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.RequestHistoryEventContractKL(req);
Console.Write("Send RequestHistoryEventContractKL: {0}\n", seqNo);
}
public void OnReply_RequestHistoryEventContractKL(FTAPI_Conn client, uint nSerialNo, Qot_RequestHistoryEventContractKL.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
40
41
int requestHistoryEventContractKL(QotRequestHistoryEventContractKL.Request req);
void onReply_RequestHistoryEventContractKL(FTAPI_Conn client, int nSerialNo, QotRequestHistoryEventContractKL.Response rsp);
介紹
拉取事件合約的歷史 K 線數據。
參數
message C2S
{
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
required Qot_Common.KLType klType = 4;
required string beginTime = 5;
required string endTime = 6;
optional int32 maxAckKLNum = 7;
optional bytes nextReqKey = 8;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 股票結構參見 Security
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1;
optional bytes nextReqKey = 2;
}
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
11
12
13
- 介面調用結果,結構參見 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.KXNFLAFCCHAMP-27-CIN").build();
QotRequestHistoryEventContractKL.C2S c2s = QotRequestHistoryEventContractKL.C2S.newBuilder()
.setSecurity(sec)
.setPreSide(Common.PredSide.PredSide_Yes)
.setKlType(QotCommon.KLType.KLType_Day_VALUE)
.setBeginTime("2026-07-05")
.setEndTime("2026-07-09")
.setMaxAckKLNum(10).build();
QotRequestHistoryEventContractKL.Request req = QotRequestHistoryEventContractKL.Request.newBuilder()
.setC2S(c2s).build();
int seqNo = qot.requestHistoryEventContractKL(req);
System.out.println("Send RequestHistoryEventContractKL: " + seqNo);
}
@Override
public void onReply_RequestHistoryEventContractKL(FTAPI_Conn client, int nSerialNo, QotRequestHistoryEventContractKL.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
45
46
Futu::u32_t RequestHistoryEventContractKL(const Qot_RequestHistoryEventContractKL::Request &stReq);
virtual void OnReply_RequestHistoryEventContractKL(Futu::u32_t nSerialNo, const Qot_RequestHistoryEventContractKL::Response &stRsp) = 0;
介紹
拉取事件合約的歷史 K 線數據。
參數
// 事件合約歷史K線拉取
// 與通用 Qot_RequestHistoryKL 的差異:入參多 klineSource/preSide 方向欄位,回包結構為 ECKLine(6欄位) + KlineItem.pre_side
// KLType 僅支持 1Min/5Min/60Min/Day
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes);不填當 None(合約級K線),優先級高於 preSide,與實時EC對齊
optional Common.PredSide preSide = 3; // 方向(Yes/No);klineSource=None 且入參為合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,如果未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem,含 ECKLine 數據點與 pre_side)
optional bytes nextReqKey = 2; // 分頁請求key。一次請求沒有返回所有數據時,下次請求帶上這個key,會接著請求
}
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
- 介面調用結果,結構參見 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_RequestHistoryEventContractKL::Request req;
Qot_RequestHistoryEventContractKL::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("EC.KXNFLAFCCHAMP-27-CIN");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->set_preside(Common::PredSide::PredSide_Yes);
c2s->set_kltype(Qot_Common::KLType::KLType_Day);
c2s->set_begintime("2026-07-05");
c2s->set_endtime("2026-07-09");
c2s->set_maxackklnum(10);
m_RequestHistoryEventContractKLSerialNo = m_pQotApi->RequestHistoryEventContractKL(req);
cout << "Request RequestHistoryEventContractKL SerialNo: " << m_RequestHistoryEventContractKLSerialNo << endl;
}
virtual void OnReply_RequestHistoryEventContractKL(Futu::u32_t nSerialNo, const Qot_RequestHistoryEventContractKL::Response &stRsp) {
if (nSerialNo == m_RequestHistoryEventContractKLSerialNo)
{
cout << "OnReply_RequestHistoryEventContractKL 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_RequestHistoryEventContractKLSerialNo;
};
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
- Output
connect
Request RequestHistoryEventContractKL SerialNo: 1
OnReply_RequestHistoryEventContractKL SerialNo: 1
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXNFLAFCCHAMP-27-CIN"},
"pre_side": 1,
"name": "Will Cincinnati win the Pro Football AFC Championship?",
"klineList": [
{
"time_key": "2026-07-05 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 201.0
},
{
"time_key": "2026-07-06 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 659.0
},
{
"time_key": "2026-07-07 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 490.0
},
{
"time_key": "2026-07-08 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 669.0
},
{
"time_key": "2026-07-09 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 343.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
RequestHistoryEventContractKL(req);
介紹
拉取事件合約的歷史 K 線數據,支持 K 線來源、合約方向與時間區間。
參數
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),不填當 None
optional Common.PredSide preSide = 3; // 方向(Yes/No),klineSource=None 且合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem)
optional bytes nextReqKey = 2; // 分頁請求key
}
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
- 介面調用結果,結構參見 RetType
- Example
import ftWebsocket from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
import beautify from "js-beautify";
function RequestHistoryEventContractKL() {
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 {
const req = { c2s: { security: { market: QotMarket.QotMarket_EventContract, code: "EC.KXNFLAFCCHAMP-27-CIN" }, preSide: PredSide.PredSide_Yes, klType: KLType.KLType_Day, beginTime: "2026-07-05", endTime: "2026-07-09", maxAckKLNum: 10 } };
let { errCode, retMsg, retType, s2c } = await websocket.RequestHistoryEventContractKL(req);
console.log("RequestHistoryEventContractKL: 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
- Output
RequestHistoryEventContractKL: errCode 0, retMsg , retType 0
{
"klineList": [{
"code": {
"market": 101,
"code": "EC.KXNFLAFCCHAMP-27-CIN"
},
"preSide": 1,
"name": "Will Cincinnati win the Pro Football AFC Championship?",
"klineList": [
{
"timeKey": "2026-07-05 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 201
},
{
"timeKey": "2026-07-06 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 659
},
{
"timeKey": "2026-07-07 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 490
},
{
"timeKey": "2026-07-08 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 669
},
{
"timeKey": "2026-07-09 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 343
}
]
}]
}
stop
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
介面限制
- 每 30 秒內最多請求 60 次拉取事件合約歷史 K 線介面
- Python
- Proto
- C#
- Java
- C++
- JavaScript
request_history_event_contract_kline(code, start=None, end=None, pre_side=None, ktype=KLType.K_DAY, kline_source=None, max_count=1000, page_req_key=None)
介紹
拉取事件合約的歷史 K 線數據,不需要先下載歷史數據。支持按時間區間、方向、K 線來源查詢,自動處理分頁(
max_count大於單包上限時循環拉取)。參數
參數 類型 說明 code str 必填,事件合約代碼,例如 'EC.KXNFLAFCCHAMP-27-CIN'start str 選填,開始時間,例如 '2026-07-05'end str 選填,結束時間,例如 '2026-07-09'pre_side PredSide 選填,合約方向 ktype KLType 選填,K 線類型,僅支持 K_1M/K_5M/K_60M/K_DAY,預設K_DAYkline_source ECKlineSource 選填,K 線來源 max_count int 選填,本次請求最大返回數據點個數, None表示返回區間內全部page_req_key str 選填,分頁請求 key,初始請求傳 None,後續傳上次返回值返回
參數 類型 說明 ret RET_CODE 介面調用結果 data pd.DataFrame 當 ret == RET_OK,返回歷史 K 線數據 str 當 ret != RET_OK,返回錯誤描述 page_req_key str 分頁請求 key,無更多數據時為 None 返回的 DataFrame 欄位如下:
欄位 類型 說明 code str 合約代碼 pre_side str 合約方向(YES/NO/N/A) name str 合約名稱 time_key str K 線時間 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.KXNFLAFCCHAMP-27-CIN'], [SubType.K_DAY]
)
if ret != RET_OK:
print('訂閱失敗:', err)
else:
ret, data, page_req_key = quote_ctx.request_history_event_contract_kline(
'EC.KXNFLAFCCHAMP-27-CIN',
start='2026-07-05', end='2026-07-09',
pre_side=PredSide.YES, ktype=KLType.K_DAY, max_count=10
)
if ret == RET_OK:
print(data)
print('page_req_key:', page_req_key)
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
23
- Output
code pre_side name time_key open high low close volume
0 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-05 00:00:00 0.92 0.92 0.92 0.92 201.0
1 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-06 00:00:00 0.93 0.93 0.92 0.92 659.0
2 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-07 00:00:00 0.93 0.93 0.92 0.92 490.0
3 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-08 00:00:00 0.93 0.93 0.92 0.92 669.0
4 EC.KXNFLAFCCHAMP-27-CIN YES Will Cincinnati win the Pro Football AFC Championship? 2026-07-09 00:00:00 0.92 0.92 0.92 0.92 343.0
page_req_key: None
2
3
4
5
6
7
# Qot_RequestHistoryEventContractKL.proto
介紹
拉取事件合約的歷史 K 線數據。
參數
// 事件合約歷史K線拉取
// 與通用 Qot_RequestHistoryKL 的差異:入參多 klineSource/preSide 方向欄位,回包結構為 ECKLine(6欄位) + KlineItem.pre_side
// KLType 僅支持 1Min/5Min/60Min/Day
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes);不填當 None(合約級K線),優先級高於 preSide,與實時EC對齊
optional Common.PredSide preSide = 3; // 方向(Yes/No);klineSource=None 且入參為合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,如果未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem,含 ECKLine 數據點與 pre_side)
optional bytes nextReqKey = 2; // 分頁請求key。一次請求沒有返回所有數據時,下次請求帶上這個key,會接著請求
}
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
- 介面調用結果,結構參見 RetType
協議 ID
3456
數據類型
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
uint RequestHistoryEventContractKL(Qot_RequestHistoryEventContractKL.Request req);
virtual void OnReply_RequestHistoryEventContractKL(MMAPI_Conn client, uint nSerialNo, Qot_RequestHistoryEventContractKL.Response rsp);
介紹
拉取事件合約的歷史 K 線數據。
參數
message C2S
{
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
required Qot_Common.KLType klType = 4;
required string beginTime = 5;
required string endTime = 6;
optional int32 maxAckKLNum = 7;
optional bytes nextReqKey = 8;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 股票結構參見 Security
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1;
optional bytes nextReqKey = 2;
}
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
11
12
13
- 介面調用結果,結構參見 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.KXNFLAFCCHAMP-27-CIN").Build();
var c2s = Qot_RequestHistoryEventContractKL.C2S.CreateBuilder()
.SetSecurity(sec)
.SetPreSide(Common.PredSide.PredSide_Yes)
.SetKlType((int)QotCommon.KLType.KLType_Day)
.SetBeginTime("2026-07-05")
.SetEndTime("2026-07-09")
.SetMaxAckKLNum(10).Build();
var req = Qot_RequestHistoryEventContractKL.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.RequestHistoryEventContractKL(req);
Console.Write("Send RequestHistoryEventContractKL: {0}\n", seqNo);
}
public void OnReply_RequestHistoryEventContractKL(MMAPI_Conn client, uint nSerialNo, Qot_RequestHistoryEventContractKL.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
40
41
int requestHistoryEventContractKL(QotRequestHistoryEventContractKL.Request req);
void onReply_RequestHistoryEventContractKL(MMAPI_Conn client, int nSerialNo, QotRequestHistoryEventContractKL.Response rsp);
介紹
拉取事件合約的歷史 K 線數據。
參數
message C2S
{
required Qot_Common.Security security = 1;
optional Qot_Common.EC_KlineSource klineSource = 2;
optional Common.PredSide preSide = 3;
required Qot_Common.KLType klType = 4;
required string beginTime = 5;
required string endTime = 6;
optional int32 maxAckKLNum = 7;
optional bytes nextReqKey = 8;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 股票結構參見 Security
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1;
optional bytes nextReqKey = 2;
}
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
11
12
13
- 介面調用結果,結構參見 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.KXNFLAFCCHAMP-27-CIN").build();
QotRequestHistoryEventContractKL.C2S c2s = QotRequestHistoryEventContractKL.C2S.newBuilder()
.setSecurity(sec)
.setPreSide(Common.PredSide.PredSide_Yes)
.setKlType(QotCommon.KLType.KLType_Day_VALUE)
.setBeginTime("2026-07-05")
.setEndTime("2026-07-09")
.setMaxAckKLNum(10).build();
QotRequestHistoryEventContractKL.Request req = QotRequestHistoryEventContractKL.Request.newBuilder()
.setC2S(c2s).build();
int seqNo = qot.requestHistoryEventContractKL(req);
System.out.println("Send RequestHistoryEventContractKL: " + seqNo);
}
@Override
public void onReply_RequestHistoryEventContractKL(MMAPI_Conn client, int nSerialNo, QotRequestHistoryEventContractKL.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
45
46
moomoo::u32_t RequestHistoryEventContractKL(const Qot_RequestHistoryEventContractKL::Request &stReq);
virtual void OnReply_RequestHistoryEventContractKL(moomoo::u32_t nSerialNo, const Qot_RequestHistoryEventContractKL::Response &stRsp) = 0;
介紹
拉取事件合約的歷史 K 線數據。
參數
// 事件合約歷史K線拉取
// 與通用 Qot_RequestHistoryKL 的差異:入參多 klineSource/preSide 方向欄位,回包結構為 ECKLine(6欄位) + KlineItem.pre_side
// KLType 僅支持 1Min/5Min/60Min/Day
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes);不填當 None(合約級K線),優先級高於 preSide,與實時EC對齊
optional Common.PredSide preSide = 3; // 方向(Yes/No);klineSource=None 且入參為合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,如果未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 股票結構參見 Security
- 事件合約預測方向參見 PredSide
- K 線來源參見 ECKlineSource
- K 線類型參見 KLType
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem,含 ECKLine 數據點與 pre_side)
optional bytes nextReqKey = 2; // 分頁請求key。一次請求沒有返回所有數據時,下次請求帶上這個key,會接著請求
}
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
- 介面調用結果,結構參見 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_RequestHistoryEventContractKL::Request req;
Qot_RequestHistoryEventContractKL::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("EC.KXNFLAFCCHAMP-27-CIN");
sec->set_market(Qot_Common::QotMarket::QotMarket_EventContract);
c2s->set_preside(Common::PredSide::PredSide_Yes);
c2s->set_kltype(Qot_Common::KLType::KLType_Day);
c2s->set_begintime("2026-07-05");
c2s->set_endtime("2026-07-09");
c2s->set_maxackklnum(10);
m_RequestHistoryEventContractKLSerialNo = m_pQotApi->RequestHistoryEventContractKL(req);
cout << "Request RequestHistoryEventContractKL SerialNo: " << m_RequestHistoryEventContractKLSerialNo << endl;
}
virtual void OnReply_RequestHistoryEventContractKL(moomoo::u32_t nSerialNo, const Qot_RequestHistoryEventContractKL::Response &stRsp) {
if (nSerialNo == m_RequestHistoryEventContractKLSerialNo)
{
cout << "OnReply_RequestHistoryEventContractKL 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_RequestHistoryEventContractKLSerialNo;
};
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
- Output
connect
Request RequestHistoryEventContractKL SerialNo: 1
OnReply_RequestHistoryEventContractKL SerialNo: 1
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"klineList": [
{
"code": {"market": 101, "code": "EC.KXNFLAFCCHAMP-27-CIN"},
"pre_side": 1,
"name": "Will Cincinnati win the Pro Football AFC Championship?",
"klineList": [
{
"time_key": "2026-07-05 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 201.0
},
{
"time_key": "2026-07-06 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 659.0
},
{
"time_key": "2026-07-07 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 490.0
},
{
"time_key": "2026-07-08 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 669.0
},
{
"time_key": "2026-07-09 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 343.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
RequestHistoryEventContractKL(req);
介紹
拉取事件合約的歷史 K 線數據,支持 K 線來源、合約方向與時間區間。
參數
message C2S
{
required Qot_Common.Security security = 1; // 合約標的(contract_id 或 sub_contract_id)
optional Qot_Common.EC_KlineSource klineSource = 2; // K線來源(None/OrderBookYes),不填當 None
optional Common.PredSide preSide = 3; // 方向(Yes/No),klineSource=None 且合約級標的時生效,預設 Yes
required Qot_Common.KLType klType = 4; // 通用K線類型,EC僅支持 1Min/5Min/60Min/Day
required string beginTime = 5; // 開始時間字串
required string endTime = 6; // 結束時間字串
optional int32 maxAckKLNum = 7; // 最多返回K線根數,未指定表示不限制
optional bytes nextReqKey = 8; // 分頁請求key
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 返回
message S2C
{
repeated Qot_GetEventContractKline.KlineItem klineList = 1; // K線列表(複用共享 KlineItem)
optional bytes nextReqKey = 2; // 分頁請求key
}
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
- 介面調用結果,結構參見 RetType
- Example
import mmWebsocket from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function RequestHistoryEventContractKL() {
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 {
const req = { c2s: { security: { market: QotMarket.QotMarket_EventContract, code: "EC.KXNFLAFCCHAMP-27-CIN" }, preSide: PredSide.PredSide_Yes, klType: KLType.KLType_Day, beginTime: "2026-07-05", endTime: "2026-07-09", maxAckKLNum: 10 } };
let { errCode, retMsg, retType, s2c } = await websocket.RequestHistoryEventContractKL(req);
console.log("RequestHistoryEventContractKL: 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
- Output
RequestHistoryEventContractKL: errCode 0, retMsg , retType 0
{
"klineList": [{
"code": {
"market": 101,
"code": "EC.KXNFLAFCCHAMP-27-CIN"
},
"preSide": 1,
"name": "Will Cincinnati win the Pro Football AFC Championship?",
"klineList": [
{
"timeKey": "2026-07-05 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 201
},
{
"timeKey": "2026-07-06 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 659
},
{
"timeKey": "2026-07-07 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 490
},
{
"timeKey": "2026-07-08 00:00:00",
"open": 0.93,
"high": 0.93,
"low": 0.92,
"close": 0.92,
"volume": 669
},
{
"timeKey": "2026-07-09 00:00:00",
"open": 0.92,
"high": 0.92,
"low": 0.92,
"close": 0.92,
"volume": 343
}
]
}]
}
stop
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
介面限制
- 每 30 秒內最多請求 60 次拉取事件合約歷史 K 線介面