# 獲取實時經紀佇列
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_broker_queue(code)
介紹
獲取已訂閱股票的實時經紀佇列數據,必須要先訂閱。
參數
參數 類型 說明 code str 股票代號
傳回
參數 類型 說明 ret RET_CODE 介面呼叫結果 bid_frame_table pd.DataFrame 當 ret == RET_OK,bid_frame_table 傳回買盤經紀佇列數據 str 當 ret != RET_OK,bid_frame_table 傳回錯誤描述 ask_frame_table pd.DataFrame 當 ret == RET_OK,ask_frame_table 傳回賣盤經紀佇列數據 str 當 ret != RET_OK,ask_frame_table 傳回錯誤描述 - 買盤經紀佇列格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 bid_broker_id int 經紀買盤 ID bid_broker_name str 經紀買盤名稱 bid_broker_pos int 經紀檔位 order_id int 交易所訂單 ID - 不是下單介面傳回的訂單 ID
- 只有港股 SF 行情權限支援傳回該欄位
order_volume int 單筆委託數量 只有港股 SF 行情權限支援傳回該欄位 - 賣盤經紀佇列格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 ask_broker_id int 經紀賣盤 ID ask_broker_name str 經紀賣盤名稱 ask_broker_pos int 經紀檔位 order_id int 交易所訂單 ID - 不是下單介面傳回的訂單 ID
- 只有港股 SF 行情權限支援傳回該欄位
order_volume int 單筆委託數量 只有港股 SF 行情權限支援傳回該欄位
- 買盤經紀佇列格式如下:
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['HK.00700'], [SubType.BROKER], subscribe_push=False)
# 先訂閱經紀佇列類型。訂閱成功後 OpenD 將持續收到伺服器的推送,False 代表暫時不需要推送給腳本
if ret_sub == RET_OK: # 訂閱成功
ret, bid_frame_table, ask_frame_table = quote_ctx.get_broker_queue('HK.00700') # 獲取一次經紀佇列數據
if ret == RET_OK:
print(bid_frame_table)
else:
print('error:', bid_frame_table)
else:
print(err_message)
quote_ctx.close() # 關閉當條連線,OpenD 會在1分鐘後自動取消相應股票相應類型的訂閱
2
3
4
5
6
7
8
9
10
11
12
13
- Output
code name bid_broker_id bid_broker_name bid_broker_pos order_id order_volume
0 HK.00700 騰訊控股 5338 J.P.摩根 1 N/A N/A
.. ... ... ... ... ... ... ...
36 HK.00700 騰訊控股 8305 富途證券國際(香港)有限公司 4 N/A N/A
[37 rows x 7 columns]
2
3
4
5
6
# Qot_GetBroker.proto
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
協議 ID
3014
uint GetBroker(QotGetBroker.Request req);
virtual void OnReply_GetBroker(FTAPI_Conn client, uint nSerialNo, QotGetBroker.Response rsp);
- 介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
- 參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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("00700")
.Build();
QotSub.C2S c2s = QotSub.C2S.CreateBuilder()
.AddSecurityList(sec)
.AddSubTypeList((int)QotCommon.SubType.SubType_Broker)
.SetIsSubOrUnSub(true)
.Build();
QotSub.Request req = QotSub.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.Sub(req);
Console.Write("Send QotSub: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_Sub(FTAPI_Conn client, uint nSerialNo, QotSub.Response rsp) {
Console.Write("Reply: QotSub: {0} {1}\n", nSerialNo, rsp.ToString());
if (rsp.RetType != (int)Common.RetType.RetType_Succeed)
return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("00700")
.Build();
QotGetBroker.C2S c2s = QotGetBroker.C2S.CreateBuilder()
.SetSecurity(sec)
.Build();
QotGetBroker.Request req = QotGetBroker.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetBroker(req);
Console.Write("Send QotGetBroker: {0}\n", seqNo);
}
public void OnReply_GetBroker(FTAPI_Conn client, uint nSerialNo, QotGetBroker.Response rsp)
{
Console.Write("Reply: QotGetBroker: {0}\n", nSerialNo);
Console.Write("id: {0}, name: {1}\n", rsp.S2C.BrokerAskListList[0].Id, rsp.S2C.BrokerAskListList[0].Name);
}
public static void Main(String[] args) {
FTAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
- Output
Qot onInitConnect: ret=0 desc= connID=6825676366648053042
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetBroker: 4
Reply: QotGetBroker: 4
id: 5465, name: 富途證券國際(香港)有限公司
2
3
4
5
6
7
8
9
int getBroker(QotGetBroker.Request req);
void onReply_GetBroker(FTAPI_Conn client, int nSerialNo, QotGetBroker.Response rsp);
- 介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
- 參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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("00700")
.build();
QotSub.C2S c2s = QotSub.C2S.newBuilder()
.addSecurityList(sec)
.addSubTypeList(QotCommon.SubType.SubType_Broker_VALUE)
.setIsSubOrUnSub(true)
.build();
QotSub.Request req = QotSub.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.sub(req);
System.out.printf("Send QotSub: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_Sub(FTAPI_Conn client, int nSerialNo, QotSub.Response rsp) {
System.out.printf("Reply: QotSub: %d %s\n", nSerialNo, rsp.toString());
if (rsp.getRetType() != Common.RetType.RetType_Succeed_VALUE)
return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("00700")
.build();
QotGetBroker.C2S c2s = QotGetBroker.C2S.newBuilder()
.setSecurity(sec)
.build();
QotGetBroker.Request req = QotGetBroker.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getBroker(req);
System.out.printf("Send QotGetBroker: %d\n", seqNo);
}
@Override
public void onReply_GetBroker(FTAPI_Conn client, int nSerialNo, QotGetBroker.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetBroker failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetBroker: %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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
- Output
Send QotGetBroker: 3
Receive QotGetBroker: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [{
"id": "8908",
"name": "中銀國際證券有限公司",
"pos": 1
}, ... {
"id": "8305",
"name": "富途證券國際(香港)有限公司",
"pos": 2
}],
"brokerBidList": [{
"id": "4108",
"name": "法國巴黎證券(亞洲)有限公司",
"pos": 1
}, ... {
"id": "9033",
"name": "招商證券(香港)有限公司",
"pos": 3
}]
}
}
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
Futu::u32_t GetBroker(const Qot_GetBroker::Request &stReq);
virtual void OnReply_GetBroker(Futu::u32_t nSerialNo, const Qot_GetBroker::Response &stRsp) = 0;
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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_Sub::Request req;
Qot_Sub::C2S *c2s = req.mutable_c2s();
auto secList = c2s->mutable_securitylist();
Qot_Common::Security *sec = secList->Add();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
c2s->add_subtypelist(Qot_Common::SubType::SubType_Broker);
c2s->set_isregorunregpush(true);
c2s->set_issuborunsub(true);
m_SubSerialNo = m_pQotApi->Sub(req);
cout << "Request Sub SerialNo: " << m_SubSerialNo << endl;
}
virtual void OnReply_Sub(Futu::u32_t nSerialNo, const Qot_Sub::Response &stRsp)
{
if(nSerialNo == m_SubSerialNo)
{
cout << "OnReply_Sub SerialNo: " << nSerialNo << endl;
if(stRsp.rettype() != Common::RetType::RetType_Succeed)
{
cout << "Sub Failed" << endl;
return;
}
// 組包
Qot_GetBroker::Request req;
Qot_GetBroker::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
m_GetBrokerSerialNo = m_pQotApi->GetBroker(req);
cout << "Request GetBroker SerialNo: " << m_GetBrokerSerialNo << endl;
}
}
virtual void OnReply_GetBroker(Futu::u32_t nSerialNo, const Qot_GetBroker::Response &stRsp){
if(nSerialNo == m_GetBrokerSerialNo)
{
cout << "OnReply_GetBroker 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_SubSerialNo;
Futu::u32_t m_GetBrokerSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
FTAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
FTAPI::UnInit();
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
- Output
connect
Request Sub SerialNo: 3
OnReply_Sub SerialNo: 3
Request GetBroker SerialNo: 4
OnReply_GetBroker SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [
{
"id": "6996",
"name": "中國投資信息有限公司",
"pos": 1
},
{
"id": "8037",
"name": "中信里昂證券有限公司",
"pos": 1
},
...
{
"id": "4978",
"name": "法國興業證券(香港)有限公司",
"pos": 3
}
]
}
}
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
GetBroker(req);
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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 QotGetBroker(){
const { RetType } = Common
const { SubType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
websocket.Sub({
c2s: {
securityList: [
{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
],
subTypeList: [ SubType.SubType_Broker ], // 訂閱實時經紀佇列類型
isSubOrUnSub: true, // 訂閱 true, 反訂閱 false
isRegOrUnRegPush: true, // 註冊推送 true, 反註冊推送 false
},
})
.then((res) => {
const req = {
c2s: {
security: {
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
},
};
websocket.GetBroker(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("Broker: 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);
});
})
.catch((error) => {
if ("retMsg" in error) {
console.log("sub error:", error.retMsg);
}
});
} else {
console.log("start 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- Output
Broker: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [{
"id": "6429",
"name": "中信証券經紀(香港)有限公司",
"pos": 1
}, {
"id": "4973",
"name": "法國興業證券(香港)有限公司",
"pos": 1
}, ..., {
"id": "2310",
"name": "恒生證券有限公司",
"pos": 5
}],
"brokerBidList": [{
"id": "2846",
"name": "麥格理資本股份有限公司",
"pos": 1
}, {
"id": "8578",
"name": "滙豐證券經紀(亞洲)有限公司",
"pos": 1
}, ..., {
"id": "2245",
"name": "富途證券國際(香港)有限公司",
"pos": 1
}]
}
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
提示
- 此介面提供了一次性獲取實時數據的功能,如需持續獲取推送數據,請參考 實時經紀佇列回呼 介面
- 獲取實時數據 和 實時數據回呼 的差別,請參考 如何通過訂閱介面獲取實時行情?
- 港股 BMP及LV1 權限下,不支援獲取經紀佇列數據
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_broker_queue(code)
介紹
獲取已訂閱股票的實時經紀佇列數據,必須要先訂閱。
參數
參數 類型 說明 code str 股票代號
傳回
參數 類型 說明 ret RET_CODE 介面呼叫結果 bid_frame_table pd.DataFrame 當 ret == RET_OK,bid_frame_table 傳回買盤經紀佇列數據 str 當 ret != RET_OK,bid_frame_table 傳回錯誤描述 ask_frame_table pd.DataFrame 當 ret == RET_OK,ask_frame_table 傳回賣盤經紀佇列數據 str 當 ret != RET_OK,ask_frame_table 傳回錯誤描述 - 買盤經紀佇列格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 bid_broker_id int 經紀買盤 ID bid_broker_name str 經紀買盤名稱 bid_broker_pos int 經紀檔位 order_id int 交易所訂單 ID - 不是下單介面傳回的訂單 ID
- 只有港股 SF 行情權限支援傳回該欄位
order_volume int 單筆委託數量 只有港股 SF 行情權限支援傳回該欄位 - 賣盤經紀佇列格式如下:
欄位 類型 說明 code str 股票代號 name str 股票名稱 ask_broker_id int 經紀賣盤 ID ask_broker_name str 經紀賣盤名稱 ask_broker_pos int 經紀檔位 order_id int 交易所訂單 ID - 不是下單介面傳回的訂單 ID
- 只有港股 SF 行情權限支援傳回該欄位
order_volume int 單筆委託數量 只有港股 SF 行情權限支援傳回該欄位
- 買盤經紀佇列格式如下:
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['HK.00700'], [SubType.BROKER], subscribe_push=False)
# 先訂閱經紀佇列類型。訂閱成功後 OpenD 將持續收到伺服器的推送,False 代表暫時不需要推送給腳本
if ret_sub == RET_OK: # 訂閱成功
ret, bid_frame_table, ask_frame_table = quote_ctx.get_broker_queue('HK.00700') # 獲取一次經紀佇列數據
if ret == RET_OK:
print(bid_frame_table)
else:
print('error:', bid_frame_table)
else:
print(err_message)
quote_ctx.close() # 關閉當條連線,OpenD 會在1分鐘後自動取消相應股票相應類型的訂閱
2
3
4
5
6
7
8
9
10
11
12
13
- Output
code name bid_broker_id bid_broker_name bid_broker_pos order_id order_volume
0 HK.00700 騰訊控股 5338 J.P.摩根 1 N/A N/A
.. ... ... ... ... ... ... ...
36 HK.00700 騰訊控股 8305 富途證券國際(香港)有限公司 4 N/A N/A
[37 rows x 7 columns]
2
3
4
5
6
# Qot_GetBroker.proto
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
協議 ID
3014
uint GetBroker(QotGetBroker.Request req);
virtual void OnReply_GetBroker(MMAPI_Conn client, uint nSerialNo, QotGetBroker.Response rsp);
- 介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
- 參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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("00700")
.Build();
QotSub.C2S c2s = QotSub.C2S.CreateBuilder()
.AddSecurityList(sec)
.AddSubTypeList((int)QotCommon.SubType.SubType_Broker)
.SetIsSubOrUnSub(true)
.Build();
QotSub.Request req = QotSub.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.Sub(req);
Console.Write("Send QotSub: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_Sub(MMAPI_Conn client, uint nSerialNo, QotSub.Response rsp) {
Console.Write("Reply: QotSub: {0} {1}\n", nSerialNo, rsp.ToString());
if (rsp.RetType != (int)Common.RetType.RetType_Succeed)
return;
QotCommon.Security sec = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("00700")
.Build();
QotGetBroker.C2S c2s = QotGetBroker.C2S.CreateBuilder()
.SetSecurity(sec)
.Build();
QotGetBroker.Request req = QotGetBroker.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetBroker(req);
Console.Write("Send QotGetBroker: {0}\n", seqNo);
}
public void OnReply_GetBroker(MMAPI_Conn client, uint nSerialNo, QotGetBroker.Response rsp)
{
Console.Write("Reply: QotGetBroker: {0}\n", nSerialNo);
Console.Write("id: {0}, name: {1}\n", rsp.S2C.BrokerAskListList[0].Id, rsp.S2C.BrokerAskListList[0].Name);
}
public static void Main(String[] args) {
MMAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
- Output
Qot onInitConnect: ret=0 desc= connID=6825676366648053042
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetBroker: 4
Reply: QotGetBroker: 4
id: 5465, name: 富途證券國際(香港)有限公司
2
3
4
5
6
7
8
9
int getBroker(QotGetBroker.Request req);
void onReply_GetBroker(MMAPI_Conn client, int nSerialNo, QotGetBroker.Response rsp);
- 介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
- 參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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("00700")
.build();
QotSub.C2S c2s = QotSub.C2S.newBuilder()
.addSecurityList(sec)
.addSubTypeList(QotCommon.SubType.SubType_Broker_VALUE)
.setIsSubOrUnSub(true)
.build();
QotSub.Request req = QotSub.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.sub(req);
System.out.printf("Send QotSub: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_Sub(MMAPI_Conn client, int nSerialNo, QotSub.Response rsp) {
System.out.printf("Reply: QotSub: %d %s\n", nSerialNo, rsp.toString());
if (rsp.getRetType() != Common.RetType.RetType_Succeed_VALUE)
return;
QotCommon.Security sec = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("00700")
.build();
QotGetBroker.C2S c2s = QotGetBroker.C2S.newBuilder()
.setSecurity(sec)
.build();
QotGetBroker.Request req = QotGetBroker.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getBroker(req);
System.out.printf("Send QotGetBroker: %d\n", seqNo);
}
@Override
public void onReply_GetBroker(MMAPI_Conn client, int nSerialNo, QotGetBroker.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetBroker failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetBroker: %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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
- Output
Send QotGetBroker: 3
Receive QotGetBroker: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [{
"id": "8908",
"name": "中銀國際證券有限公司",
"pos": 1
}, ... {
"id": "8305",
"name": "富途證券國際(香港)有限公司",
"pos": 2
}],
"brokerBidList": [{
"id": "4108",
"name": "法國巴黎證券(亞洲)有限公司",
"pos": 1
}, ... {
"id": "9033",
"name": "招商證券(香港)有限公司",
"pos": 3
}]
}
}
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
moomoo::u32_t GetBroker(const Qot_GetBroker::Request &stReq);
virtual void OnReply_GetBroker(moomoo::u32_t nSerialNo, const Qot_GetBroker::Response &stRsp) = 0;
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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_Sub::Request req;
Qot_Sub::C2S *c2s = req.mutable_c2s();
auto secList = c2s->mutable_securitylist();
Qot_Common::Security *sec = secList->Add();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
c2s->add_subtypelist(Qot_Common::SubType::SubType_Broker);
c2s->set_isregorunregpush(true);
c2s->set_issuborunsub(true);
m_SubSerialNo = m_pQotApi->Sub(req);
cout << "Request Sub SerialNo: " << m_SubSerialNo << endl;
}
virtual void OnReply_Sub(moomoo::u32_t nSerialNo, const Qot_Sub::Response &stRsp)
{
if(nSerialNo == m_SubSerialNo)
{
cout << "OnReply_Sub SerialNo: " << nSerialNo << endl;
if(stRsp.rettype() != Common::RetType::RetType_Succeed)
{
cout << "Sub Failed" << endl;
return;
}
// 組包
Qot_GetBroker::Request req;
Qot_GetBroker::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
m_GetBrokerSerialNo = m_pQotApi->GetBroker(req);
cout << "Request GetBroker SerialNo: " << m_GetBrokerSerialNo << endl;
}
}
virtual void OnReply_GetBroker(moomoo::u32_t nSerialNo, const Qot_GetBroker::Response &stRsp){
if(nSerialNo == m_GetBrokerSerialNo)
{
cout << "OnReply_GetBroker 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_SubSerialNo;
moomoo::u32_t m_GetBrokerSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
MMAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
MMAPI::UnInit();
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
- Output
connect
Request Sub SerialNo: 3
OnReply_Sub SerialNo: 3
Request GetBroker SerialNo: 4
OnReply_GetBroker SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [
{
"id": "6996",
"name": "中國投資信息有限公司",
"pos": 1
},
{
"id": "8037",
"name": "中信里昂證券有限公司",
"pos": 1
},
...
{
"id": "4978",
"name": "法國興業證券(香港)有限公司",
"pos": 3
}
]
}
}
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
GetBroker(req);
介紹
獲取已訂閱股票的實時報價,必須要先訂閱。
參數
message C2S
{
required Qot_Common.Security security = 1; //股票
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 股票結構參見 Security
- 傳回
message S2C
{
required Qot_Common.Security security = 1; //股票
optional string name = 4; // 股票名稱
repeated Qot_Common.Broker brokerAskList = 2; //經紀 Ask(賣)盤
repeated Qot_Common.Broker brokerBidList = 3; //經紀 Bid(買)盤
}
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
- 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 QotGetBroker(){
const { RetType } = Common
const { SubType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登錄成功
websocket.Sub({
c2s: {
securityList: [
{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
],
subTypeList: [ SubType.SubType_Broker ], // 訂閱實時經紀佇列類型
isSubOrUnSub: true, // 訂閱 true, 反訂閱 false
isRegOrUnRegPush: true, // 註冊推送 true, 反註冊推送 false
},
})
.then((res) => {
const req = {
c2s: {
security: {
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
},
};
websocket.GetBroker(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("Broker: 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);
});
})
.catch((error) => {
if ("retMsg" in error) {
console.log("sub error:", error.retMsg);
}
});
} else {
console.log("start 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- Output
Broker: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"brokerAskList": [{
"id": "6429",
"name": "中信証券經紀(香港)有限公司",
"pos": 1
}, {
"id": "4973",
"name": "法國興業證券(香港)有限公司",
"pos": 1
}, ..., {
"id": "2310",
"name": "恒生證券有限公司",
"pos": 5
}],
"brokerBidList": [{
"id": "2846",
"name": "麥格理資本股份有限公司",
"pos": 1
}, {
"id": "8578",
"name": "滙豐證券經紀(亞洲)有限公司",
"pos": 1
}, ..., {
"id": "2245",
"name": "富途證券國際(香港)有限公司",
"pos": 1
}]
}
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
提示
- 此介面提供了一次性獲取實時數據的功能,如需持續獲取推送數據,請參考 實時經紀佇列回呼 介面
- 獲取實時數據 和 實時數據回呼 的差別,請參考 如何通過訂閱介面獲取實時行情?
- 港股 LV1 權限下,不支援獲取經紀佇列數據