# リアルタイムティックの取得
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_rt_ticker(code, num=500)
概要
登録済み銘柄のリアルタイムティックデータを取得します。事前に登録が必要です。
パラメータ
パラメータ 型 説明 code str 銘柄コード num int 直近のティック件数
戻り値
パラメータ 型 説明 ret RET_CODE API呼び出し結果 data pd.DataFrame ret == RET_OK の場合、ティックデータを返します str ret != RET_OK の場合、エラーの説明を返す - ティックデータのフォーマット:
フィールド タイプ 説明 code str 銘柄コード name str 銘柄名 sequence int ティック番号 time str 約定時間 フォーマット:yyyy-MM-dd HH:mm:ss:xxx
香港株およびA株市場はデフォルトで北京時間、米国株市場はデフォルトで米国東部時間price float 約定価格 volume int 約定数量 株数turnover float 売買代金 ticker_direction TickerDirect ティック方向 type TickerType ティックタイプ
- ティックデータのフォーマット:
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['US.AAPL'], [SubType.TICKER], subscribe_push=False, session=Session.ALL)
# 先にティックタイプを登録。登録成功後、moomoo OpenDはサーバーからのプッシュを継続受信。Falseはスクリプトへのプッシュが一時不要であることを示す
if ret_sub == RET_OK: # 登録成功
ret, data = quote_ctx.get_rt_ticker('US.AAPL', 2) # 米国株AAPLの直近2件のティックを取得
if ret == RET_OK:
print(data)
print(data['turnover'][0]) # 1件目の約定金額を取得
print(data['turnover'].values.tolist()) # list に変換
else:
print('error:', data)
else:
print('subscription failed', err_message)
quote_ctx.close() # 接続をクローズすると、OpenD は1分後に対応銘柄の登録を自動解除
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
code name time price volume turnover ticker_direction sequence type
0 US.AAPL 苹果 2025-04-07 05:50:23.745 181.70 2 363.40 NEUTRAL 7490506385373790208 ODD_LOT
1 US.AAPL 苹果 2025-04-07 05:50:24.170 181.73 1 181.73 NEUTRAL 7490506389668757504 ODD_LOT
363.4
[363.4, 181.73]
2
3
4
5
# Qot_GetTicker.proto
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
プロトコル ID
3010
uint GetTicker(QotGetTicker.Request req);
virtual void OnReply_GetTicker(FTAPI_Conn client, uint nSerialNo, QotGetTicker.Response rsp);
- 概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
- パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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_Ticker)
.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();
QotGetTicker.C2S c2s = QotGetTicker.C2S.CreateBuilder()
.SetSecurity(sec)
.SetMaxRetNum(10)
.Build();
QotGetTicker.Request req = QotGetTicker.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetTicker(req);
Console.Write("Send QotGetTicker: {0}\n", seqNo);
}
public void OnReply_GetTicker(FTAPI_Conn client, uint nSerialNo, QotGetTicker.Response rsp)
{
Console.Write("Reply: QotGetTicker: {0}\n", nSerialNo);
Console.Write("price: {0}\n", rsp.S2C.TickerListList[0].Price);
}
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
- Output
Qot onInitConnect: ret=0 desc= connID=6825674961091539442
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetTicker: 4
Reply: QotGetTicker: 4
price: 456
2
3
4
5
6
7
8
9
int getTicker(QotGetTicker.Request req);
void onReply_GetTicker(FTAPI_Conn client, int nSerialNo, QotGetTicker.Response rsp);
- 概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
- パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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_Ticker_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();
QotGetTicker.C2S c2s = QotGetTicker.C2S.newBuilder()
.setSecurity(sec)
.setMaxRetNum(10)
.build();
QotGetTicker.Request req = QotGetTicker.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getTicker(req);
System.out.printf("Send QotGetTicker: %d\n", seqNo);
}
@Override
public void onReply_GetTicker(FTAPI_Conn client, int nSerialNo, QotGetTicker.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetTicker failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetTicker: %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
88
- Output
Send QotGetTicker: 3
Receive QotGetTicker: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [{
"time": "2021-06-24 15:59:55",
"sequence": "6977267122170775380",
"dir": 1,
"price": 583.5,
"volume": "100",
"turnover": 58350.0,
"recvTime": 0.0,
"type": 1,
"typeSign": 32,
"timestamp": 1.624521595E9
}, ... {
"time": "2021-06-24 16:08:10",
"sequence": "6977269248179586909",
"dir": 3,
"price": 583.0,
"volume": "1131400",
"turnover": 6.596062E8,
"recvTime": 0.0,
"type": 7,
"typeSign": 85,
"timestamp": 1.62452209E9
}]
}
}
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
Futu::u32_t GetTicker(const Qot_GetTicker::Request &stReq);
virtual void OnReply_GetTicker(Futu::u32_t nSerialNo, const Qot_GetTicker::Response &stRsp) = 0;
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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;
// この API は事前に登録が必要
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_Ticker);
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_GetTicker::Request req;
Qot_GetTicker::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);
c2s->set_maxretnum(10);
m_GetTickerSerialNo = m_pQotApi->GetTicker(req);
cout << "Request GetTicker SerialNo: " << m_GetTickerSerialNo << endl;
}
}
virtual void OnReply_GetTicker(Futu::u32_t nSerialNo, const Qot_GetTicker::Response &stRsp){
if(nSerialNo == m_GetTickerSerialNo)
{
cout << "OnReply_GetTicker 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_GetTickerSerialNo;
};
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 GetTicker SerialNo: 4
OnReply_GetTicker SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [
{
"time": "2021-06-09 11:02:09",
"sequence": "6971624110669434300",
"dir": 1,
"price": 604.5,
"volume": "100",
"turnover": 60450,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1623207729
},
...
{
"time": "2021-06-09 11:02:30",
"sequence": "6971624200863747525",
"dir": 1,
"price": 604.5,
"volume": "100",
"turnover": 60450,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1623207750
}
]
}
}
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
GetTicker(req);
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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 QotGetTicker(){
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_Ticker ], // リアルタイムティックタイプを登録
isSubOrUnSub: true, // 登録 true、登録解除 false
isRegOrUnRegPush: true, // プッシュ登録 true、プッシュ解除 false
},
})
.then((res) => {
const req = {
c2s: {
security: {
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
maxRetNum: 3,
},
};
websocket.GetTicker(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("Ticker: 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 接続に制限されています
//1 ページまたは 1 プロジェクトで 1 接続を維持することも可能。ここではサンプルとしてリクエストごとに 1 接続を作成
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
75
- Output
Ticker: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [{
"time": "2021-09-09 15:59:59",
"sequence": "7005840697777510727",
"dir": 2,
"price": 481.2,
"volume": "100",
"turnover": 48120,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1631174399
}, {
"time": "2021-09-09 16:05:24",
"sequence": "7005842093641881928",
"dir": 3,
"price": 476,
"volume": "42",
"turnover": 19992,
"recvTime": 0,
"type": 6,
"typeSign": 68,
"timestamp": 1631174724
}, {
"time": "2021-09-09 16:08:12",
"sequence": "7005842815196387657",
"dir": 3,
"price": 480,
"volume": "4561200",
"turnover": 2189376000,
"recvTime": 0,
"type": 7,
"typeSign": 85,
"timestamp": 1631174892
}]
}
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
APIレート制限
- 直近最大1000件のティックデータを取得可能です。それ以上の過去ティックデータは現在提供されていません
- 香港株オプション・先物はLV1権限ではティック取得に対応していません
ご注意
- このAPIは一括でリアルタイムデータを取得する機能を提供します。継続的なプッシュデータが必要な場合は、リアルタイムティックコールバック APIを参照してください
- リアルタイムデータの取得とリアルタイムデータコールバックの違いについては、 如何から登録 API で取得してくださいリアルタイム相場情報?
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_rt_ticker(code, num=500)
概要
登録済み銘柄のリアルタイムティックデータを取得します。事前に登録が必要です。
パラメータ
パラメータ 型 説明 code str 銘柄コード num int 直近のティック件数
戻り値
パラメータ 型 説明 ret RET_CODE API呼び出し結果 data pd.DataFrame ret == RET_OK の場合、ティックデータを返します str ret != RET_OK の場合、エラーの説明を返す - ティックデータのフォーマット:
フィールド タイプ 説明 code str 銘柄コード name str 銘柄名 sequence int ティック番号 time str 約定時間 フォーマット:yyyy-MM-dd HH:mm:ss
香港株およびA株市場はデフォルトで北京時間、米国株市場はデフォルトで米国東部時間price float 約定価格 volume int 約定数量 株数turnover float 売買代金 ticker_direction TickerDirect ティック方向 type TickerType ティックタイプ
- ティックデータのフォーマット:
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret_sub, err_message = quote_ctx.subscribe(['US.AAPL'], [SubType.TICKER], subscribe_push=False, session=Session.ALL)
# 先にティックタイプを登録。登録成功後、moomoo OpenDはサーバーからのプッシュを継続受信。Falseはスクリプトへのプッシュが一時不要であることを示す
if ret_sub == RET_OK: # 登録成功
ret, data = quote_ctx.get_rt_ticker('US.AAPL', 2) # 米国株AAPLの直近2件のティックを取得
if ret == RET_OK:
print(data)
print(data['turnover'][0]) # 1件目の約定金額を取得
print(data['turnover'].values.tolist()) # list に変換
else:
print('error:', data)
else:
print('subscription failed', err_message)
quote_ctx.close() # 接続をクローズすると、OpenD は1分後に対応銘柄の登録を自動解除
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
code name time price volume turnover ticker_direction sequence type
0 US.AAPL 苹果 2025-04-07 05:50:23.745 181.70 2 363.40 NEUTRAL 7490506385373790208 ODD_LOT
1 US.AAPL 苹果 2025-04-07 05:50:24.170 181.73 1 181.73 NEUTRAL 7490506389668757504 ODD_LOT
363.4
[363.4, 181.73]
2
3
4
5
# Qot_GetTicker.proto
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
プロトコル ID
3010
uint GetTicker(QotGetTicker.Request req);
virtual void OnReply_GetTicker(MMAPI_Conn client, uint nSerialNo, QotGetTicker.Response rsp);
- 概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
- パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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_Ticker)
.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();
QotGetTicker.C2S c2s = QotGetTicker.C2S.CreateBuilder()
.SetSecurity(sec)
.SetMaxRetNum(10)
.Build();
QotGetTicker.Request req = QotGetTicker.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetTicker(req);
Console.Write("Send QotGetTicker: {0}\n", seqNo);
}
public void OnReply_GetTicker(MMAPI_Conn client, uint nSerialNo, QotGetTicker.Response rsp)
{
Console.Write("Reply: QotGetTicker: {0}\n", nSerialNo);
Console.Write("price: {0}\n", rsp.S2C.TickerListList[0].Price);
}
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
- Output
Qot onInitConnect: ret=0 desc= connID=6825674961091539442
Send QotSub: 3
Reply: QotSub: 3 retType: 0
retMsg: ""
errCode: 0
Send QotGetTicker: 4
Reply: QotGetTicker: 4
price: 456
2
3
4
5
6
7
8
9
int getTicker(QotGetTicker.Request req);
void onReply_GetTicker(MMAPI_Conn client, int nSerialNo, QotGetTicker.Response rsp);
- 概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
- パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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_Ticker_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();
QotGetTicker.C2S c2s = QotGetTicker.C2S.newBuilder()
.setSecurity(sec)
.setMaxRetNum(10)
.build();
QotGetTicker.Request req = QotGetTicker.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getTicker(req);
System.out.printf("Send QotGetTicker: %d\n", seqNo);
}
@Override
public void onReply_GetTicker(MMAPI_Conn client, int nSerialNo, QotGetTicker.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetTicker failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetTicker: %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
88
- Output
Send QotGetTicker: 3
Receive QotGetTicker: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [{
"time": "2021-06-24 15:59:55",
"sequence": "6977267122170775380",
"dir": 1,
"price": 583.5,
"volume": "100",
"turnover": 58350.0,
"recvTime": 0.0,
"type": 1,
"typeSign": 32,
"timestamp": 1.624521595E9
}, ... {
"time": "2021-06-24 16:08:10",
"sequence": "6977269248179586909",
"dir": 3,
"price": 583.0,
"volume": "1131400",
"turnover": 6.596062E8,
"recvTime": 0.0,
"type": 7,
"typeSign": 85,
"timestamp": 1.62452209E9
}]
}
}
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
moomoo::u32_t GetTicker(const Qot_GetTicker::Request &stReq);
virtual void OnReply_GetTicker(moomoo::u32_t nSerialNo, const Qot_GetTicker::Response &stRsp) = 0;
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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, Futu::i64_t nErrCode, const char* strDesc) {
cout << "connect" << endl;
// この API は事前に登録が必要
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_Ticker);
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_GetTicker::Request req;
Qot_GetTicker::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);
c2s->set_maxretnum(10);
m_GetTickerSerialNo = m_pQotApi->GetTicker(req);
cout << "Request GetTicker SerialNo: " << m_GetTickerSerialNo << endl;
}
}
virtual void OnReply_GetTicker(moomoo::u32_t nSerialNo, const Qot_GetTicker::Response &stRsp){
if(nSerialNo == m_GetTickerSerialNo)
{
cout << "OnReply_GetTicker 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_GetTickerSerialNo;
};
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 GetTicker SerialNo: 4
OnReply_GetTicker SerialNo: 4
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [
{
"time": "2021-06-09 11:02:09",
"sequence": "6971624110669434300",
"dir": 1,
"price": 604.5,
"volume": "100",
"turnover": 60450,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1623207729
},
...
{
"time": "2021-06-09 11:02:30",
"sequence": "6971624200863747525",
"dir": 1,
"price": 604.5,
"volume": "100",
"turnover": 60450,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1623207750
}
]
}
}
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
GetTicker(req);
概要
指定銘柄のリアルタイムティックを取得します。事前に登録が必要です。
パラメータ
message C2S
{
required Qot_Common.Security security = 1; //株式
required int32 maxRetNum = 2; //返すティックの最大件数。実際の返却件数はこれより少ない場合があり、最大1000件
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 株式構造は~を参照: Security
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //株式
optional string name = 3; // 銘柄名
repeated Qot_Common.Ticker tickerList = 2; //ティックデータ構造体
}
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
- 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 QotGetTicker(){
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_Ticker ], // リアルタイムティックタイプを登録
isSubOrUnSub: true, // 登録 true、登録解除 false
isRegOrUnRegPush: true, // プッシュ登録 true、プッシュ解除 false
},
})
.then((res) => {
const req = {
c2s: {
security: {
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
maxRetNum: 3,
},
};
websocket.GetTicker(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("Ticker: 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 接続に制限されています
//1 ページまたは 1 プロジェクトで 1 接続を維持することも可能。ここではサンプルとしてリクエストごとに 1 接続を作成
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
75
- Output
Ticker: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"tickerList": [{
"time": "2021-09-09 15:59:59",
"sequence": "7005840697777510727",
"dir": 2,
"price": 481.2,
"volume": "100",
"turnover": 48120,
"recvTime": 0,
"type": 1,
"typeSign": 32,
"timestamp": 1631174399
}, {
"time": "2021-09-09 16:05:24",
"sequence": "7005842093641881928",
"dir": 3,
"price": 476,
"volume": "42",
"turnover": 19992,
"recvTime": 0,
"type": 6,
"typeSign": 68,
"timestamp": 1631174724
}, {
"time": "2021-09-09 16:08:12",
"sequence": "7005842815196387657",
"dir": 3,
"price": 480,
"volume": "4561200",
"turnover": 2189376000,
"recvTime": 0,
"type": 7,
"typeSign": 85,
"timestamp": 1631174892
}]
}
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
APIレート制限
- 直近最大1000件のティックデータを取得可能です。それ以上の過去ティックデータは現在提供されていません
- 香港株オプション・先物はLV1権限ではティック取得に対応していません
ご注意
- このAPIは一括でリアルタイムデータを取得する機能を提供します。継続的なプッシュデータが必要な場合は、リアルタイムティックコールバック APIを参照してください
- リアルタイムデータの取得とリアルタイムデータコールバックの違いについては、 如何から登録 API で取得してくださいリアルタイム相場情報?