# Get Real-time Quote
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_stock_quote(code_list)
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
Parameter Type Description code_list list Stock list. Data type of elements in the list is str.
Return
Field Type Description ret RET_CODE Interface result. data pd.DataFrame If ret == RET_OK, quotation data is returned. str If ret != RET_OK, error description is returned. - quotation data format as follows:
Field Type Description code str Stock code. data_date str Date. data_time str Time of latest price. Format: yyyy-MM-dd HH:mm:ss
The default of HK stock market and A-share market is Beijing time, while that of US stock market is US Eastern time.last_price float Latest price. open_price float Open. high_price float High. low_price float Low. prev_close_price float Yesterday's close. volume int Volume. turnover float Turnover. turnover_rate float Turnover rate. This field is in percentage form, so 20 is equivalent to 20%.amplitude int Amplitude. This field is in percentage form, so 20 is equivalent to 20%.suspension bool Whether trading is suspended. True: suspensionlisting_date str Listing date. yyyy-MM-ddprice_spread float Spread. dark_status DarkStatus Grey market transaction status. sec_status SecurityStatus Stock status. strike_price float Strike price. contract_size float Contract size. open_interest int Number of open positions. implied_volatility float Implied volatility. This field is in percentage form, so 20 is equivalent to 20%.premium float Premium. This field is in percentage form, so 20 is equivalent to 20%.delta float Greek value Delta. gamma float Greek value Gamma. vega float Greek value Vega. theta float Greek value Theta. rho float Greek value Rho. index_option_type IndexOptionType Index option type. net_open_interest int Net open contract number. Only HK options support this field.expiry_date_distance int The number of days from the expiry date. a negative number means it has expired.contract_nominal_value float Contract nominal amount. Only HK options support this field.owner_lot_multiplier float Equal number of underlying stocks. Index options do not have this field , only HK options support this field.option_area_type OptionAreaType Option type (by exercise time). contract_multiplier float Contract multiplier. pre_price float Pre-market price. pre_high_price float Pre-market high. pre_low_price float Pre-market low. pre_volume int Pre-market volume. pre_turnover float Pre-market turnover. pre_change_val float Pre-market change. pre_change_rate float Pre-market change rate. This field is in percentage form, so 20 is equivalent to 20%.pre_amplitude float Pre-market amplitude. This field is in percentage form, so 20 is equivalent to 20%.after_price float After-hours price. after_high_price float After-hours high. after_low_price float After-hours high. after_volume int After-hours volume. The Sci-tech Innovation Board supports this data.After_turnover float After-hours turnover. The Sci-tech Innovation Board supports this data.after_change_val float After-hours change. after_change_rate float After-hours change rate. This field is in percentage form, so 20 is equivalent to 20%.after_amplitude float After-hours amplitude. This field is in percentage form, so 20 is equivalent to 20%.last_settle_price float Yesterday's close. Specific field for futures.position float Holding position. Specific field for futures.position_change float Daily position change. Specific field for futures.
- quotation data format as follows:
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['HK.00700'], [SubType.QUOTE], subscribe_push=False)
# Subscribe to the K line type first. After the subscription is successful, OpenD will continue to receive pushes from the server, False means that there is no need to push to the script temporarily
if ret_sub == RET_OK: # Subscription successful
ret, data = quote_ctx.get_stock_quote(['HK.00700']) # Get real-time data of subscription stock quotes
if ret == RET_OK:
print(data)
print(data['code'][0]) # Take the first stock code
print(data['code'].values.tolist()) # Convert to list
else:
print('error:', data)
else:
print('subscription failed', err_message)
quote_ctx.close() # Close the current connection, OpenD will automatically cancel the corresponding type of subscription for the corresponding stock after 1 minute
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
code data_date data_time last_price open_price high_price low_price prev_close_price volume turnover turnover_rate amplitude suspension listing_date price_spread dark_status sec_status strike_price contract_size open_interest implied_volatility premium delta gamma vega theta rho net_open_interest expiry_date_distance contract_nominal_value owner_lot_multiplier option_area_type contract_multiplier last_settle_price position position_change index_option_type pre_price pre_high_price pre_low_price pre_volume pre_turnover pre_change_val pre_change_rate pre_amplitude after_price after_high_price after_low_price after_volume after_turnover after_change_val after_change_rate after_amplitude
0 HK.00700 2021-01-04 16:08:49 572.5 558.0 583.0 557.5 564.0 20784101 1.196867e+10 0.217 4.521 False 2004-06-16 0.5 N/A NORMAL N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
HK.00700
['HK.00700']
2
3
4
# Qot_GetBasicQot.proto
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
Protocol ID
3004
uint GetBasicQot(QotGetBasicQot.Request req);
virtual void OnReply_GetBasicQot(FTAPI_Conn client, uint nSerialNo, QotGetBasicQot.Response rsp);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Example
public class Program: FTSPI_Qot, FTSPI_Conn {
FTAPI_Qot qot = new FTAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //Set client information
qot.SetConnCallback(this); //Set connection callback
qot.SetQotCallback(this); //Set transaction callback
}
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_Basic)
.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();
QotGetBasicQot.C2S c2s = QotGetBasicQot.C2S.CreateBuilder()
.AddSecurityList(sec)
.Build();
QotGetBasicQot.Request req = QotGetBasicQot.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetBasicQot(req);
Console.Write("Send QotGetBasicQot: {0}\n", seqNo);
}
public void OnReply_GetBasicQot(FTAPI_Conn client, uint nSerialNo, QotGetBasicQot.Response rsp)
{
Console.Write("Reply: QotGetBasicQot: {0}\n", nSerialNo);
Console.Write("curPrice: {0}\n", rsp.S2C.BasicQotListList[0].CurPrice);
}
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
74
75
- Output
Qot onInitConnect: ret=0 desc= connID=6825668646977718703
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetBasicQot: 4
Reply: QotGetBasicQot: 4
curPrice: 459
2
3
4
5
6
7
8
9
int getBasicQot(QotGetBasicQot.Request req);
void onReply_GetBasicQot(FTAPI_Conn client, int nSerialNo, QotGetBasicQot.Response rsp);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Example
public class QotDemo implements FTSPI_Qot, FTSPI_Conn {
FTAPI_Conn_Qot qot = new FTAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //Set client information
qot.setConnSpi(this); //Set connection callback
qot.setQotSpi(this); //Set transaction callback
}
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_Basic_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();
QotGetBasicQot.C2S c2s = QotGetBasicQot.C2S.newBuilder()
.addSecurityList(sec)
.build();
QotGetBasicQot.Request req = QotGetBasicQot.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getBasicQot(req);
System.out.printf("Send QotGetBasicQot: %d\n", seqNo);
}
@Override
public void onReply_GetBasicQot(FTAPI_Conn client, int nSerialNo, QotGetBasicQot.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetBasicQot failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetBasicQot: %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 QotGetBasicQot: 3
Receive QotGetBasicQot: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"basicQotList": [{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.5,
"updateTime": "2021-06-24 16:08:14",
"highPrice": 587.5,
"openPrice": 584.0,
"lowPrice": 580.0,
"curPrice": 583.0,
"lastClosePrice": 582.5,
"volume": "10947302",
"turnover": 6.387238277E9,
"turnoverRate": 0.114,
"amplitude": 1.288,
"darkStatus": 0,
"listTimestamp": 1.0873152E9,
"updateTimestamp": 1.624522094E9,
"secStatus": 1
}]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Futu::u32_t GetBasicQot(const Qot_GetBasicQot::Request &stReq);
virtual void OnReply_GetBasicQot(Futu::u32_t nSerialNo, const Qot_GetBasicQot::Response &stRsp) = 0;
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- 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;
// subscribe first
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_Basic);
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;
}
// construct request message
Qot_GetBasicQot::Request req;
Qot_GetBasicQot::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);
m_GetBasicQotSerialNo = m_pQotApi->GetBasicQot(req);
cout << "Request GetBasicQot SerialNo: " << m_GetBasicQotSerialNo << endl;
}
}
virtual void OnReply_GetBasicQot(Futu::u32_t nSerialNo, const Qot_GetBasicQot::Response &stRsp){
if(nSerialNo == m_GetBasicQotSerialNo)
{
cout << "OnReply_GetBasicQot SerialNo: " << nSerialNo << endl;
// print response
// ProtoBufToBodyData and UTF8ToLocal refer to tool.h in Samples
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_GetBasicQotSerialNo;
};
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
100
- Output
connect
Request Sub SerialNo: 3
OnReply_Sub SerialNo: 3
Request GetBasicQot SerialNo: 4
OnReply_GetBasicQot SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"basicQotList": [
{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.5,
"updateTime": "2021-06-09 14:08:24",
"highPrice": 606,
"openPrice": 600,
"lowPrice": 597.5,
"curPrice": 601.5,
"lastClosePrice": 601,
"volume": "4283211",
"turnover": 2579419163,
"turnoverRate": 0.045,
"amplitude": 1.414,
"darkStatus": 0,
"listTimestamp": 1087315200,
"updateTimestamp": 1623218904,
"secStatus": 1
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
GetBasicQot(req);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- 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 QotGetBasicQot(){
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_Basic ],
isSubOrUnSub: true,
isRegOrUnRegPush: true,
},
})
.then((res) => {
const req = {
c2s: {
securityList: [
{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
],
},
};
websocket.GetBasicQot(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("BasicQot: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let s2c = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(s2c);
}
})
.catch((error) => {
console.log("error:", error);
});
})
.catch((error) => {
if ("retMsg" in error) {
console.log("error:", error.retMsg);
}
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
// After using the connection, remember to close it to prevent the number of connections from running out
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // Set the script to receive OpenD push duration to 5 seconds
}
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
BasicQot: errCode 0, retMsg , retType 0
{
"basicQotList": [{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.2,
"updateTime": "2021-09-09 16:08:17",
"highPrice": 511.5,
"openPrice": 509,
"lowPrice": 479,
"curPrice": 480,
"lastClosePrice": 524.5,
"volume": "54090872",
"turnover": 26716845932,
"turnoverRate": 0.563,
"amplitude": 6.196,
"darkStatus": 0,
"listTimestamp": 1087315200,
"updateTimestamp": 1631174897,
"secStatus": 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
Tips
- This API provides the function to obtain real-time data at one time. If you need to obtain pushed data continuously, please refer to the Real-time Quote Callback API.
- For the difference between get real-time data and real-time data callback, please refer to How to Get Real-time Quotes Through Subscription Interface.
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_stock_quote(code_list)
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
Parameter Type Description code_list list Stock list. Data type of elements in the list is str.
Return
Field Type Description ret RET_CODE Interface result. data pd.DataFrame If ret == RET_OK, quotation data is returned. str If ret != RET_OK, error description is returned. - quotation data format as follows:
Field Type Description code str Stock code. data_date str Date. data_time str Time of latest price. Format: yyyy-MM-dd HH:mm:ss
The default of HK stock market and A-share market is Beijing time, while that of US stock market is US Eastern time.last_price float Latest price. open_price float Open. high_price float High. low_price float Low. prev_close_price float Yesterday's close. volume int Volume. turnover float Turnover. turnover_rate float Turnover rate. This field is in percentage form, so 20 is equivalent to 20%.amplitude int Amplitude. This field is in percentage form, so 20 is equivalent to 20%.suspension bool Whether trading is suspended. True: suspensionlisting_date str Listing date. yyyy-MM-ddprice_spread float Spread. dark_status DarkStatus Grey market transaction status. sec_status SecurityStatus Stock status. strike_price float Strike price. contract_size float Contract size. open_interest int Number of open positions. implied_volatility float Implied volatility. This field is in percentage form, so 20 is equivalent to 20%.premium float Premium. This field is in percentage form, so 20 is equivalent to 20%.delta float Greek value Delta. gamma float Greek value Gamma. vega float Greek value Vega. theta float Greek value Theta. rho float Greek value Rho. index_option_type IndexOptionType Index option type. net_open_interest int Net open contract number. Only HK options support this field.expiry_date_distance int The number of days from the expiry date. a negative number means it has expired.contract_nominal_value float Contract nominal amount. Only HK options support this field.owner_lot_multiplier float Equal number of underlying stocks. Index options do not have this field , only HK options support this field.option_area_type OptionAreaType Option type (by exercise time). contract_multiplier float Contract multiplier. pre_price float Pre-market price. pre_high_price float Pre-market high. pre_low_price float Pre-market low. pre_volume int Pre-market volume. pre_turnover float Pre-market turnover. pre_change_val float Pre-market change. pre_change_rate float Pre-market change rate. This field is in percentage form, so 20 is equivalent to 20%.pre_amplitude float Pre-market amplitude. This field is in percentage form, so 20 is equivalent to 20%.after_price float After-hours price. after_high_price float After-hours high. after_low_price float After-hours high. after_volume int After-hours volume. The Sci-tech Innovation Board supports this data.After_turnover float After-hours turnover. The Sci-tech Innovation Board supports this data.after_change_val float After-hours change. after_change_rate float After-hours change rate. This field is in percentage form, so 20 is equivalent to 20%.after_amplitude float After-hours amplitude. This field is in percentage form, so 20 is equivalent to 20%.last_settle_price float Yesterday's close. Specific field for futures.position float Holding position. Specific field for futures.position_change float Daily position change. Specific field for futures.
- quotation data format as follows:
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['HK.00700'], [SubType.QUOTE], subscribe_push=False)
# Subscribe to the K line type first. After the subscription is successful, OpenD will continue to receive pushes from the server, False means that there is no need to push to the script temporarily
if ret_sub == RET_OK: # Subscription successful
ret, data = quote_ctx.get_stock_quote(['HK.00700']) # Get real-time data of subscription stock quotes
if ret == RET_OK:
print(data)
print(data['code'][0]) # Take the first stock code
print(data['code'].values.tolist()) # Convert to list
else:
print('error:', data)
else:
print('subscription failed', err_message)
quote_ctx.close() # Close the current connection, OpenD will automatically cancel the corresponding type of subscription for the corresponding stock after 1 minute
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
code data_date data_time last_price open_price high_price low_price prev_close_price volume turnover turnover_rate amplitude suspension listing_date price_spread dark_status sec_status strike_price contract_size open_interest implied_volatility premium delta gamma vega theta rho net_open_interest expiry_date_distance contract_nominal_value owner_lot_multiplier option_area_type contract_multiplier last_settle_price position position_change index_option_type pre_price pre_high_price pre_low_price pre_volume pre_turnover pre_change_val pre_change_rate pre_amplitude after_price after_high_price after_low_price after_volume after_turnover after_change_val after_change_rate after_amplitude
0 HK.00700 2021-01-04 16:08:49 572.5 558.0 583.0 557.5 564.0 20784101 1.196867e+10 0.217 4.521 False 2004-06-16 0.5 N/A NORMAL N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
HK.00700
['HK.00700']
2
3
4
# Qot_GetBasicQot.proto
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
Protocol ID
3004
uint GetBasicQot(QotGetBasicQot.Request req);
virtual void OnReply_GetBasicQot(MMAPI_Conn client, uint nSerialNo, QotGetBasicQot.Response rsp);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Example
public class Program: MMSPI_Qot, MMSPI_Conn {
MMAPI_Qot qot = new MMAPI_Qot();
public Program() {
qot.SetClientInfo("csharp", 1); //Set client information
qot.SetConnCallback(this); //Set connection callback
qot.SetQotCallback(this); //Set transaction callback
}
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_Basic)
.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();
QotGetBasicQot.C2S c2s = QotGetBasicQot.C2S.CreateBuilder()
.AddSecurityList(sec)
.Build();
QotGetBasicQot.Request req = QotGetBasicQot.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetBasicQot(req);
Console.Write("Send QotGetBasicQot: {0}\n", seqNo);
}
public void OnReply_GetBasicQot(MMAPI_Conn client, uint nSerialNo, QotGetBasicQot.Response rsp)
{
Console.Write("Reply: QotGetBasicQot: {0}\n", nSerialNo);
Console.Write("curPrice: {0}\n", rsp.S2C.BasicQotListList[0].CurPrice);
}
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
74
75
- Output
Qot onInitConnect: ret=0 desc= connID=6825668646977718703
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetBasicQot: 4
Reply: QotGetBasicQot: 4
curPrice: 459
2
3
4
5
6
7
8
9
int getBasicQot(QotGetBasicQot.Request req);
void onReply_GetBasicQot(MMAPI_Conn client, int nSerialNo, QotGetBasicQot.Response rsp);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Example
public class QotDemo implements MMSPI_Qot, MMSPI_Conn {
MMAPI_Conn_Qot qot = new MMAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1); //Set client information
qot.setConnSpi(this); //Set connection callback
qot.setQotSpi(this); //Set transaction callback
}
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_Basic_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();
QotGetBasicQot.C2S c2s = QotGetBasicQot.C2S.newBuilder()
.addSecurityList(sec)
.build();
QotGetBasicQot.Request req = QotGetBasicQot.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getBasicQot(req);
System.out.printf("Send QotGetBasicQot: %d\n", seqNo);
}
@Override
public void onReply_GetBasicQot(MMAPI_Conn client, int nSerialNo, QotGetBasicQot.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetBasicQot failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetBasicQot: %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 QotGetBasicQot: 3
Receive QotGetBasicQot: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"basicQotList": [{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.5,
"updateTime": "2021-06-24 16:08:14",
"highPrice": 587.5,
"openPrice": 584.0,
"lowPrice": 580.0,
"curPrice": 583.0,
"lastClosePrice": 582.5,
"volume": "10947302",
"turnover": 6.387238277E9,
"turnoverRate": 0.114,
"amplitude": 1.288,
"darkStatus": 0,
"listTimestamp": 1.0873152E9,
"updateTimestamp": 1.624522094E9,
"secStatus": 1
}]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
moomoo::u32_t GetBasicQot(const Qot_GetBasicQot::Request &stReq);
virtual void OnReply_GetBasicQot(moomoo::u32_t nSerialNo, const Qot_GetBasicQot::Response &stRsp) = 0;
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- 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;
// subscribe first
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_Basic);
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;
}
// construct request message
Qot_GetBasicQot::Request req;
Qot_GetBasicQot::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);
m_GetBasicQotSerialNo = m_pQotApi->GetBasicQot(req);
cout << "Request GetBasicQot SerialNo: " << m_GetBasicQotSerialNo << endl;
}
}
virtual void OnReply_GetBasicQot(moomoo::u32_t nSerialNo, const Qot_GetBasicQot::Response &stRsp){
if(nSerialNo == m_GetBasicQotSerialNo)
{
cout << "OnReply_GetBasicQot SerialNo: " << nSerialNo << endl;
// print response
// ProtoBufToBodyData and UTF8ToLocal refer to tool.h in Samples
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_GetBasicQotSerialNo;
};
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
100
- Output
connect
Request Sub SerialNo: 3
OnReply_Sub SerialNo: 3
Request GetBasicQot SerialNo: 4
OnReply_GetBasicQot SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"basicQotList": [
{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.5,
"updateTime": "2021-06-09 14:08:24",
"highPrice": 606,
"openPrice": 600,
"lowPrice": 597.5,
"curPrice": 601.5,
"lastClosePrice": 601,
"volume": "4283211",
"turnover": 2579419163,
"turnoverRate": 0.045,
"amplitude": 1.414,
"darkStatus": 0,
"listTimestamp": 1087315200,
"updateTimestamp": 1623218904,
"secStatus": 1
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
GetBasicQot(req);
Description
To get real-time quotes of subscribed securities, you must subscribe first.
Parameters
message C2S
{
repeated Qot_Common.Security securityList = 1; //Security
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- For stock structure, refer to Security
- Return
message S2C
{
repeated Qot_Common.BasicQot basicQotList = 1; //Basic stock market
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType,returned value
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
- 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 QotGetBasicQot(){
const { RetType } = Common
const { SubType, 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) {
websocket.Sub({
c2s: {
securityList: [
{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
],
subTypeList: [ SubType.SubType_Basic ],
isSubOrUnSub: true,
isRegOrUnRegPush: true,
},
})
.then((res) => {
const req = {
c2s: {
securityList: [
{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
],
},
};
websocket.GetBasicQot(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("BasicQot: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let s2c = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(s2c);
}
})
.catch((error) => {
console.log("error:", error);
});
})
.catch((error) => {
if ("retMsg" in error) {
console.log("error:", error.retMsg);
}
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
// After using the connection, remember to close it to prevent the number of connections from running out
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // Set the script to receive OpenD push duration to 5 seconds
}
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
BasicQot: errCode 0, retMsg , retType 0
{
"basicQotList": [{
"security": {
"market": 1,
"code": "00700"
},
"isSuspended": false,
"listTime": "2004-06-16",
"priceSpread": 0.2,
"updateTime": "2021-09-09 16:08:17",
"highPrice": 511.5,
"openPrice": 509,
"lowPrice": 479,
"curPrice": 480,
"lastClosePrice": 524.5,
"volume": "54090872",
"turnover": 26716845932,
"turnoverRate": 0.563,
"amplitude": 6.196,
"darkStatus": 0,
"listTimestamp": 1087315200,
"updateTimestamp": 1631174897,
"secStatus": 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
Tips
- This API provides the function to obtain real-time data at one time. If you need to obtain pushed data continuously, please refer to the Real-time Quote Callback API.
- For the difference between get real-time data and real-time data callback, please refer to How to Get Real-time Quotes Through Subscription Interface.