# 獲取期貨合約資料
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_future_info(code_list)
介紹
獲取期貨合約資料
參數
參數 類型 說明 code_list list 股票代號列表 list 中元素類型是 str
傳回
參數 類型 說明 ret RET_CODE 介面呼叫結果 data pd.DataFrame 當 ret == RET_OK,傳回期貨合約資料數據 str 當 ret != RET_OK,傳回錯誤描述 - 期貨合約資料數據格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 owner str 標的 exchange str 交易所 type str 合約類型 size float 合約規模 size_unit str 合約規模單位 price_currency str 報價貨幣 price_unit str 報價單位 min_change float 最小變動 min_change_unit str 最小變動的單位 該欄位已廢棄trade_time str 交易時間 time_zone str 時區 last_trade_time str 最後交易時間 主連,當月,下月等期貨沒有該欄位exchange_format_url str 交易所規格連結 url origin_code str 實際合約代號
- 期貨合約資料數據格式如下:
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data = quote_ctx.get_future_info(["HK.MPImain", "HK.HAImain"])
if ret == RET_OK:
print(data)
print(data['code'][0]) # 取第一條的股票代號
print(data['code'].values.tolist()) # 轉為 list
else:
print('error:', data)
quote_ctx.close() # 結束後記得關閉當條連線,防止連線條數用盡
2
3
4
5
6
7
8
9
10
11
- Output
code name owner exchange type size size_unit price_currency price_unit min_change min_change_unit trade_time time_zone last_trade_time exchange_format_url origin_code
0 HK.MPImain 內房期貨主連 恒生中國內地地產指數 港交所 股指期貨 50.0 指數點×港元 港元 指數點 0.50 (09:15 - 12:00), (13:00 - 16:30) CCT https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/P... HK.MPI2112
1 HK.HAImain 海通證券期貨主連 HK.06837 港交所 股票期貨 10000.0 股 港元 每股/港元 0.01 (09:30 - 12:00), (13:00 - 16:00) CCT https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/P... HK.HAI2112
HK.MPImain
['HK.MPImain', 'HK.HAImain']
2
3
4
5
# Qot_GetFutureInfo.proto
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
協議 ID
3218
uint GetFutureInfo(QotGetFutureInfo.Request req);
virtual void OnReply_GetFutureInfo(FTAPI_Conn client, uint nSerialNo, QotGetFutureInfo.Response rsp);
- 介紹
獲取期貨合約資料
- 參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
public class Program : FTSPI_Qot, FTSPI_Conn {
FTAPI_Qot qot = new FTAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //設置用戶端資訊
qot.SetConnCallback(this); //設置連線回呼
qot.SetQotCallback(this); //設置交易回呼
}
public void Start() {
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(FTAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("HSImain")
.Build();
QotGetFutureInfo.C2S c2s = QotGetFutureInfo.C2S.CreateBuilder()
.AddSecurityList(sec)
.Build();
QotGetFutureInfo.Request req = QotGetFutureInfo.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetFutureInfo(req);
Console.Write("Send QotGetFutureInfo: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_GetFutureInfo(FTAPI_Conn client, uint nSerialNo, QotGetFutureInfo.Response rsp)
{
Console.Write("Reply: QotGetFutureInfo: {0}\n", nSerialNo);
Console.Write("name: {0}, exchange: {1} \n", rsp.S2C.FutureInfoListList[0].Name,
rsp.S2C.FutureInfoListList[0].Exchange);
}
public static void Main(String[] args) {
FTAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- Output
Qot onInitConnect: ret=0 desc= connID=6825719160020953581
Send QotGetFutureInfo: 3
Reply: QotGetFutureInfo: 3
name: 恒指主連(2108), exchange: 港交所
2
3
4
int getFutureInfo(QotGetFutureInfo.Request req);
void onReply_GetFutureInfo(FTAPI_Conn client, int nSerialNo, QotGetFutureInfo.Response rsp);
- 介紹
獲取期貨合約資料
- 參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
public class QotDemo implements FTSPI_Qot, FTSPI_Conn {
FTAPI_Conn_Qot qot = new FTAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //設置用戶端資訊
qot.setConnSpi(this); //設置連線回呼
qot.setQotSpi(this); //設置交易回呼
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(FTAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("HSImain")
.build();
QotGetFutureInfo.C2S c2s = QotGetFutureInfo.C2S.newBuilder()
.addSecurityList(sec)
.build();
QotGetFutureInfo.Request req = QotGetFutureInfo.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getFutureInfo(req);
System.out.printf("Send QotGetFutureInfo: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetFutureInfo(FTAPI_Conn client, int nSerialNo, QotGetFutureInfo.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetFutureInfo failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetFutureInfo: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FTAPI.init();
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
- Output
Send QotGetFutureInfo: 2
Receive QotGetFutureInfo: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"futureInfoList": [{
"name": "恒指主連(2106)",
"security": {
"market": 1,
"code": "HSImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "800000"
},
"ownerOther": "恒生指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50.0,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 1.0,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [{
"begin": 555.0,
"end": 720.0
}, {
"begin": 780.0,
"end": 990.0
}, {
"begin": 1035.0,
"end": 180.0
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Futures?sc_lang=zh-CN#&product=HSI",
"origin": {
"market": 1,
"code": "HSI2112"
}
}]
}
}
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
Futu::u32_t GetFutureInfo(const Qot_GetFutureInfo::Request &stReq);
virtual void OnReply_GetFutureInfo(Futu::u32_t nSerialNo, const Qot_GetFutureInfo::Response &stRsp) = 0;
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 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_GetFutureInfo::Request req;
Qot_GetFutureInfo::C2S *c2s = req.mutable_c2s();
auto secList = c2s->mutable_securitylist();
Qot_Common::Security *sec = secList->Add();
sec->set_code("HSImain");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
m_GetFutureInfoSerialNo = m_pQotApi->GetFutureInfo(req);
cout << "Request GetFutureInfo SerialNo: " << m_GetFutureInfoSerialNo << endl;
}
virtual void OnReply_GetFutureInfo(Futu::u32_t nSerialNo, const Qot_GetFutureInfo::Response &stRsp){
if(nSerialNo == m_GetFutureInfoSerialNo)
{
cout << "OnReply_GetFutureInfo 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_GetFutureInfoSerialNo;
};
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
- Output
connect
Request GetFutureInfo SerialNo: 4
OnReply_GetFutureInfo SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"futureInfoList": [
{
"name": "恒指主連(2106)",
"security": {
"market": 1,
"code": "HSImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "800000"
},
"ownerOther": "恒生指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 1,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [
{
"begin": 555,
"end": 720
},
{
"begin": 780,
"end": 990
},
{
"begin": 1035,
"end": 180
}
],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Futures?sc_lang=zh-CN#&product=HSI",
"security": {
"market": 1,
"code": "HSI2112"
}
}
]
}
}
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
GetFutureInfo(req);
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
import ftWebsocket from "futu-api";
import { ftCmdID } from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
import beautify from "js-beautify";
function QotGetFutureInfo(){
const { RetType } = Common
const { QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
const req = {
c2s: {
securityList:[{
market: QotMarket.QotMarket_HK_Future,
code: "MPImain",
},{
market: QotMarket.QotMarket_HK_Future,
code: "HAImain",
},],
},
};
websocket.GetFutureInfo(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("FutureInfo: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//關閉行情連線,連線不再使用之後,要關閉,否則佔用不必要資源
//同時OpenD也限制了最多128條連線
//也可以一個頁面或者一個專案維護一條連線,這裏範例請求一次建立一條連線
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒後斷開
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- Output
FutureInfo: errCode 0, retMsg , retType 0
{
"futureInfoList": [{
"name": "內房主連(2109)",
"security": {
"market": 1,
"code": "MPImain"
},
"lastTradeTime": "",
"ownerOther": "恒生中國內地地產指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 0.5,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [{
"begin": 555,
"end": 720
}, {
"begin": 780,
"end": 990
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Sector-Index/Sector-Index-Futures?sc_lang=zh-CN"
}, {
"name": "海通證券主連(2109)",
"security": {
"market": 1,
"code": "HAImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "06837"
},
"ownerOther": "海通證券",
"exchange": "港交所",
"contractType": "股票期貨",
"contractSize": 10000,
"contractSizeUnit": "股",
"quoteCurrency": "港元",
"minVar": 0.01,
"minVarUnit": "",
"quoteUnit": "每股/港元",
"tradeTime": [{
"begin": 570,
"end": 720
}, {
"begin": 780,
"end": 960
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://www.hkex.com.hk/Products/Listed-Derivatives/Single-Stock/Stock-Futures?sc_lang=zh-HK",
"security": {
"market": 1,
"code": "HAI2112"
}
}]
}
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
55
56
57
58
59
60
61
62
63
介面限制
- 每 30 秒內最多請求 30 次獲取期貨合約資料介面
- 每次請求的代號列表中,期貨個數上限為 200 個
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_future_info(code_list)
介紹
獲取期貨合約資料
參數
參數 類型 說明 code_list list 股票代號列表 list 中元素類型是 str
傳回
參數 類型 說明 ret RET_CODE 介面呼叫結果 data pd.DataFrame 當 ret == RET_OK,傳回期貨合約資料數據 str 當 ret != RET_OK,傳回錯誤描述 - 期貨合約資料數據格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 owner str 標的 exchange str 交易所 type str 合約類型 size float 合約規模 size_unit str 合約規模單位 price_currency str 報價貨幣 price_unit str 報價單位 min_change float 最小變動 min_change_unit str 最小變動的單位 該欄位已廢棄trade_time str 交易時間 time_zone str 時區 last_trade_time str 最後交易時間 主連,當月,下月等期貨沒有該欄位exchange_format_url str 交易所規格連結 url origin_code str 實際合約代號
- 期貨合約資料數據格式如下:
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data = quote_ctx.get_future_info(["HK.MPImain", "HK.HAImain"])
if ret == RET_OK:
print(data)
print(data['code'][0]) # 取第一條的股票代號
print(data['code'].values.tolist()) # 轉為 list
else:
print('error:', data)
quote_ctx.close() # 結束後記得關閉當條連線,防止連線條數用盡
2
3
4
5
6
7
8
9
10
11
- Output
code name owner exchange type size size_unit price_currency price_unit min_change min_change_unit trade_time time_zone last_trade_time exchange_format_url origin_code
0 HK.MPImain 內房期貨主連 恒生中國內地地產指數 港交所 股指期貨 50.0 指數點×港元 港元 指數點 0.50 (09:15 - 12:00), (13:00 - 16:30) CCT https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/P... HK.MPI2112
1 HK.HAImain 海通證券期貨主連 HK.06837 港交所 股票期貨 10000.0 股 港元 每股/港元 0.01 (09:30 - 12:00), (13:00 - 16:00) CCT https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/P... HK.HAI2112
HK.MPImain
['HK.MPImain', 'HK.HAImain']
2
3
4
5
# Qot_GetFutureInfo.proto
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
協議 ID
3218
uint GetFutureInfo(QotGetFutureInfo.Request req);
virtual void OnReply_GetFutureInfo(MMAPI_Conn client, uint nSerialNo, QotGetFutureInfo.Response rsp);
- 介紹
獲取期貨合約資料
- 參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
public class Program : MMSPI_Qot, MMSPI_Conn {
MMAPI_Qot qot = new MMAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //設置用戶端資訊
qot.SetConnCallback(this); //設置連線回呼
qot.SetQotCallback(this); //設置交易回呼
}
public void Start() {
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(MMAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("HSImain")
.Build();
QotGetFutureInfo.C2S c2s = QotGetFutureInfo.C2S.CreateBuilder()
.AddSecurityList(sec)
.Build();
QotGetFutureInfo.Request req = QotGetFutureInfo.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetFutureInfo(req);
Console.Write("Send QotGetFutureInfo: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_GetFutureInfo(MMAPI_Conn client, uint nSerialNo, QotGetFutureInfo.Response rsp)
{
Console.Write("Reply: QotGetFutureInfo: {0}\n", nSerialNo);
Console.Write("name: {0}, exchange: {1} \n", rsp.S2C.FutureInfoListList[0].Name,
rsp.S2C.FutureInfoListList[0].Exchange);
}
public static void Main(String[] args) {
MMAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- Output
Qot onInitConnect: ret=0 desc= connID=6825719160020953581
Send QotGetFutureInfo: 3
Reply: QotGetFutureInfo: 3
name: 恒指主連(2108), exchange: 港交所
2
3
4
int getFutureInfo(QotGetFutureInfo.Request req);
void onReply_GetFutureInfo(MMAPI_Conn client, int nSerialNo, QotGetFutureInfo.Response rsp);
- 介紹
獲取期貨合約資料
- 參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
public class QotDemo implements MMSPI_Qot, MMSPI_Conn {
MMAPI_Conn_Qot qot = new MMAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //設置用戶端資訊
qot.setConnSpi(this); //設置連線回呼
qot.setQotSpi(this); //設置交易回呼
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(MMAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("HSImain")
.build();
QotGetFutureInfo.C2S c2s = QotGetFutureInfo.C2S.newBuilder()
.addSecurityList(sec)
.build();
QotGetFutureInfo.Request req = QotGetFutureInfo.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getFutureInfo(req);
System.out.printf("Send QotGetFutureInfo: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetFutureInfo(MMAPI_Conn client, int nSerialNo, QotGetFutureInfo.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetFutureInfo failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetFutureInfo: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MMAPI.init();
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
- Output
Send QotGetFutureInfo: 2
Receive QotGetFutureInfo: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"futureInfoList": [{
"name": "恒指主連(2106)",
"security": {
"market": 1,
"code": "HSImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "800000"
},
"ownerOther": "恒生指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50.0,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 1.0,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [{
"begin": 555.0,
"end": 720.0
}, {
"begin": 780.0,
"end": 990.0
}, {
"begin": 1035.0,
"end": 180.0
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Futures?sc_lang=zh-CN#&product=HSI",
"origin": {
"market": 1,
"code": "HSI2112"
}
}]
}
}
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
moomoo::u32_t GetFutureInfo(const Qot_GetFutureInfo::Request &stReq);
virtual void OnReply_GetFutureInfo(moomoo::u32_t nSerialNo, const Qot_GetFutureInfo::Response &stRsp) = 0;
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 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_GetFutureInfo::Request req;
Qot_GetFutureInfo::C2S *c2s = req.mutable_c2s();
auto secList = c2s->mutable_securitylist();
Qot_Common::Security *sec = secList->Add();
sec->set_code("HSImain");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
m_GetFutureInfoSerialNo = m_pQotApi->GetFutureInfo(req);
cout << "Request GetFutureInfo SerialNo: " << m_GetFutureInfoSerialNo << endl;
}
virtual void OnReply_GetFutureInfo(moomoo::u32_t nSerialNo, const Qot_GetFutureInfo::Response &stRsp){
if(nSerialNo == m_GetFutureInfoSerialNo)
{
cout << "OnReply_GetFutureInfo 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;
Futu::u32_t m_GetFutureInfoSerialNo;
};
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
- Output
connect
Request GetFutureInfo SerialNo: 4
OnReply_GetFutureInfo SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"futureInfoList": [
{
"name": "恒指主連(2106)",
"security": {
"market": 1,
"code": "HSImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "800000"
},
"ownerOther": "恒生指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 1,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [
{
"begin": 555,
"end": 720
},
{
"begin": 780,
"end": 990
},
{
"begin": 1035,
"end": 180
}
],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Futures?sc_lang=zh-CN#&product=HSI",
"security": {
"market": 1,
"code": "HSI2112"
}
}
]
}
}
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
GetFutureInfo(req);
介紹
獲取期貨合約資料
參數
message C2S
{
repeated Qot_Common.Security securityList = 1; //股票列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 股票結構參見 Security
- 傳回
//交易時間
message TradeTime
{
optional double begin = 1; // 開始時間,以分鐘為單位
optional double end = 2; // 結束時間,以分鐘為單位
}
//期貨合約資料的列表
message FutureInfo
{
required string name = 1; // 合約名稱
required Qot_Common.Security security = 2; // 合約代號
required string lastTradeTime = 3; //最後交易日,只有非主連期貨合約才有該欄位
optional double lastTradeTimestamp = 4; //最後交易日時間戳,只有非主連期貨合約才有該欄位
optional Qot_Common.Security owner = 5; //標的股 股票期貨和股指期貨才有該欄位
required string ownerOther = 6; //標的
required string exchange = 7; //交易所
required string contractType = 8; //合約類型
required double contractSize = 9; //合約規模
required string contractSizeUnit = 10; //合約規模的單位
required string quoteCurrency = 11; //報價貨幣
required double minVar = 12; //最小變動單位
required string minVarUnit = 13; //最小變動單位的單位(該欄位已廢棄)
optional string quoteUnit = 14; //報價單位
repeated TradeTime tradeTime = 15; //交易時間
required string timeZone = 16; //所在時區
required string exchangeFormatUrl = 17; //交易所規格
optional Qot_Common.Security origin = 18; //實際合約代號
}
message S2C
{
repeated FutureInfo futureInfoList = 1; //期貨合約資料的列表
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,傳回結果
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- 介面呼叫結果,結構參見 RetType
- Example
import mmWebsocket from "moomoo-api";
import { mmCmdID } from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function QotGetFutureInfo(){
const { RetType } = Common
const { QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new mmWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
const req = {
c2s: {
securityList:[{
market: QotMarket.QotMarket_HK_Future,
code: "MPImain",
},{
market: QotMarket.QotMarket_HK_Future,
code: "HAImain",
},],
},
};
websocket.GetFutureInfo(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("FutureInfo: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//關閉行情連線,連線不再使用之後,要關閉,否則佔用不必要資源
//同時OpenD也限制了最多128條連線
//也可以一個頁面或者一個專案維護一條連線,這裏範例請求一次建立一條連線
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒後斷開
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- Output
FutureInfo: errCode 0, retMsg , retType 0
{
"futureInfoList": [{
"name": "內房主連(2109)",
"security": {
"market": 1,
"code": "MPImain"
},
"lastTradeTime": "",
"ownerOther": "恒生中國內地地產指數",
"exchange": "港交所",
"contractType": "股指期貨",
"contractSize": 50,
"contractSizeUnit": "指數點×港元",
"quoteCurrency": "港元",
"minVar": 0.5,
"minVarUnit": "",
"quoteUnit": "指數點",
"tradeTime": [{
"begin": 555,
"end": 720
}, {
"begin": 780,
"end": 990
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://sc.hkex.com.hk/TuniS/www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Sector-Index/Sector-Index-Futures?sc_lang=zh-CN"
}, {
"name": "海通證券主連(2109)",
"security": {
"market": 1,
"code": "HAImain"
},
"lastTradeTime": "",
"owner": {
"market": 1,
"code": "06837"
},
"ownerOther": "海通證券",
"exchange": "港交所",
"contractType": "股票期貨",
"contractSize": 10000,
"contractSizeUnit": "股",
"quoteCurrency": "港元",
"minVar": 0.01,
"minVarUnit": "",
"quoteUnit": "每股/港元",
"tradeTime": [{
"begin": 570,
"end": 720
}, {
"begin": 780,
"end": 960
}],
"timeZone": "CCT",
"exchangeFormatUrl": "https://www.hkex.com.hk/Products/Listed-Derivatives/Single-Stock/Stock-Futures?sc_lang=zh-HK",
"security": {
"market": 1,
"code": "HAI2112"
}
}]
}
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
55
56
57
58
59
60
61
62
63
介面限制
- 每 30 秒內最多請求 30 次獲取期貨合約資料介面
- 每次請求的代號列表中,期貨個數上限為 200 個