# 拉取事件合约历史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 线接口