# 過去ローソク足データの取得
- Python
- Proto
- C#
- Java
- C++
- JavaScript
request_history_kline(code, start=None, end=None, ktype=KLType.K_DAY, autype=AuType.QFQ, fields=[KL_FIELD.ALL], max_count=1000, page_req_key=None, extended_time=False, session=Session.NONE)
概要
過去ローソク足データの取得
パラメータ
パラメータ 型 説明 code str 銘柄コード start str 開始時刻 形式:yyyy-MM-dd
例如:“2017-06-20”end str 結束時刻 形式:yyyy-MM-dd
例如:“2017-07-20”ktype KLType ローソク足タイプ autype AuType 復権タイプ fields KLFields 返すフィールドリスト max_count int 今回のリクエストで返すローソク足の最大本数 - Noneを指定すると、startとendの間のすべてのデータを返します
- 注意:OpenDはすべてのデータを受信してからスクリプトに送信します。
取得するローソク足本数が1000本を超える場合は、タイムアウトを防ぐためにページングの使用をお勧めします
page_req_key bytes ページングリクエストキー startとendの間のローソク足本数がmax_countを超える場合:
1. 最初のページリクエスト時はNoneを指定
2. 次ページ以降のリクエスト時は前回のレスポンスで返されたpage_req_keyを指定extended_time bool 是否許可米国株プレ/アフターマーケットデータ False:不許可
True:許可session Session 米国株の時間帯別過去ローソク足データの取得 - 米国株の時間帯別過去ローソク足データの取得にのみ使用
- 米国株の過去ローソク足データ取得ではOVERNIGHTパラメータは非対応
- 最低OpenDバージョン要件:9.2.4207
- startとendの組み合わせは以下の通り
Start タイプ End タイプ 説明 str str start と end がそれぞれ指定された日付 None str start 為 end 往前 365 天 str None end 為 start 往后 365 天 None None end 為現在の日付,start 往前 365 天
戻り値
パラメータ 型 説明 ret RET_CODE API呼び出し結果 data pd.DataFrame 当 ret == RET_OK,返す過去ローソク足データデータ str ret != RET_OK の場合、エラーの説明を返す page_req_key bytes 次ページリクエスト用のkey - 過去ローソク足データのフォーマットは以下の通り:
フィールド タイプ 説明 code str 銘柄コード name str 銘柄名 time_key str ローソク足時刻 形式:yyyy-MM-dd HH:mm:ss
香港株和 A株市場デフォルト是北京時刻,米国株市場デフォルト是美东時刻open float 始値 close float 終値 high float 高値 low float 安値 pe_ratio float PER このフィールドは比率フィールドで、デフォルトでは % を表示しませんturnover_rate float 売買回転率 volume int 出来高 turnover float 売買代金 change_rate float 騰落率 last_close float 前日終値
- 過去ローソク足データのフォーマットは以下の通り:
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data, page_req_key = quote_ctx.request_history_kline('US.AAPL', start='2019-09-11', end='2019-09-18', max_count=5, session=Session.ALL) # 1ページ5件、最初のページをリクエスト
if ret == RET_OK:
print(data)
print(data['code'][0]) # 最初のレコードの銘柄コードを取得
print(data['close'].values.tolist()) # 最初のページの終値をlistに変換
else:
print('error:', data)
while page_req_key != None: # 残りの全結果をリクエスト
print('*************************************')
ret, data, page_req_key = quote_ctx.request_history_kline('US.AAPL', start='2019-09-11', end='2019-09-18', max_count=5, page_req_key=page_req_key, session=Session.ALL) # ページネーション後のデータをリクエスト
if ret == RET_OK:
print(data)
else:
print('error:', data)
print('All pages are finished!')
quote_ctx.close() # 使用後は接続をクローズしてください。接続数の枯渇を防止します。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- Output
code name time_key open close high low pe_ratio turnover_rate volume turnover change_rate last_close
0 US.AAPL 苹果 2019-09-11 00:00:00 52.631194 53.963447 53.992409 52.549135 18.773 0.01039 177158584 9.808562e+09 3.179511 52.300545
.. ... ... ... ... ... ... ... ... ... ... ... ... ...
4 US.AAPL 苹果 2019-09-17 00:00:00 53.087346 53.265945 53.294907 52.884612 18.530 0.00432 73545872 4.046314e+09 0.363802 53.072865
[5 rows x 13 columns]
US.AAPL
[53.9634465, 53.84156475, 52.7953125, 53.072865, 53.265945]
*************************************
code name time_key open close high low pe_ratio turnover_rate volume turnover change_rate last_close
0 US.AAPL 苹果 2019-09-18 00:00:00 53.352831 53.76554 53.784847 52.961844 18.704 0.00602 102572372 5.682068e+09 0.937925 53.265945
All pages are finished!
2
3
4
5
6
7
8
9
10
11
12
# Qot_RequestHistoryKL.proto
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は1分足のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
プロトコル ID
3103
uint RequestHistoryKL(QotRequestHistoryKL.Request req);
virtual void OnReply_RequestHistoryKL(FTAPI_Conn client, uint nSerialNo, QotRequestHistoryKL.Response rsp);
- 概要
過去ローソク足データの取得
- パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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();
QotRequestHistoryKL.C2S c2s = QotRequestHistoryKL.C2S.CreateBuilder()
.SetRehabType((int)QotCommon.RehabType.RehabType_Forward)
.SetKlType((int)QotCommon.KLType.KLType_1Min)
.SetSecurity(sec)
.SetBeginTime("2020-09-01")
.SetEndTime("2020-09-10")
.Build();
QotRequestHistoryKL.Request req = QotRequestHistoryKL.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.RequestHistoryKL(req);
Console.Write("Send QotRequestHistoryKL: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_RequestHistoryKL(FTAPI_Conn client, uint nSerialNo, QotRequestHistoryKL.Response rsp) {
Console.Write("Reply: QotRequestHistoryKL: {0} {1}\n", nSerialNo, rsp.ToString());
Console.Write("Code: {0}, lastClosePrice: {1} \n",
rsp.S2C.Security.Code,
rsp.S2C.KlListList[0].LastClosePrice);
}
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
- Output
Qot onInitConnect: ret=0 desc= connID=6819076272648767075
Send QotRequestHistoryKL: 3
Reply: QotRequestHistoryKL: 3 retType: 0
retMsg: ""
errCode: 0
s2c {
security {
market: 1
code: "00700"
}
klList {
time: "2020-09-01 09:30:00"
isBlank: false
highPrice: 535.9
openPrice: 535.9
lowPrice: 535.9
closePrice: 535.9
lastClosePrice: 528.9
volume: 665000
turnover: 357437500
turnoverRate: 0
pe: 0
changeRate: 1.3235016071090944
timestamp: 1598923800
}
...
klList {
time: "2020-09-09 16:00:00"
isBlank: false
highPrice: 503.9
openPrice: 502.4
lowPrice: 501.9
closePrice: 502.4
lastClosePrice: 502.9
volume: 1504800
turnover: 758472600
turnoverRate: 0
pe: 0
changeRate: -0.0994233446013124
timestamp: 1599638400
}
}
Code: 00700, lastClosePrice: 528.9
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
int requestHistoryKL(QotRequestHistoryKL.Request req);
void onReply_RequestHistoryKL(FTAPI_Conn client, int nSerialNo, QotRequestHistoryKL.Response rsp);
- 概要
過去ローソク足データの取得
- パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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();
QotRequestHistoryKL.C2S c2s = QotRequestHistoryKL.C2S.newBuilder()
.setRehabType(QotCommon.RehabType.RehabType_Forward_VALUE)
.setKlType(QotCommon.KLType.KLType_1Min_VALUE)
.setSecurity(sec)
.setBeginTime("2020-09-01")
.setEndTime("2020-09-02")
.build();
QotRequestHistoryKL.Request req = QotRequestHistoryKL.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.requestHistoryKL(req);
System.out.printf("Send QotRequestHistoryKL: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_RequestHistoryKL(FTAPI_Conn client, int nSerialNo, QotRequestHistoryKL.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotRequestHistoryKL failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotRequestHistoryKL: %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
- Output
Send QotRequestHistoryKL: 2
Receive QotRequestHistoryKL: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"klList": [{
"time": "2020-09-01 09:30:00",
"isBlank": false,
"highPrice": 535.9,
"openPrice": 535.9,
"lowPrice": 535.9,
"closePrice": 535.9,
"lastClosePrice": 528.9,
"volume": "665000",
"turnover": 3.574375E8,
"turnoverRate": 0.0,
"pe": 0.0,
"changeRate": 1.3235016071090944,
"timestamp": 1.5989238E9
}, ... {
"time": "2020-09-01 16:00:00",
"isBlank": false,
"highPrice": 542.4,
"openPrice": 542.4,
"lowPrice": 537.4,
"closePrice": 537.4,
"lastClosePrice": 542.4,
"volume": "1303900",
"turnover": 7.0309225E8,
"turnoverRate": 0.0,
"pe": 0.0,
"changeRate": -0.9218289085545722,
"timestamp": 1.5989472E9
}]
}
}
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
Futu::u32_t RequestHistoryKL(const Qot_RequestHistoryKL::Request &stReq);
virtual void OnReply_RequestHistoryKL(Futu::u32_t nSerialNo, const Qot_RequestHistoryKL::Response &stRsp) = 0;
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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_RequestHistoryKL::Request req;
Qot_RequestHistoryKL::C2S *c2s = req.mutable_c2s();
c2s->set_rehabtype(1);
c2s->set_kltype(Qot_Common::KLType::KLType_1Min);
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
c2s->set_begintime("2021-06-07");
c2s->set_endtime("2021-07-01");
m_RequestHistoryKLSerialNo = m_pQotApi->RequestHistoryKL(req);
cout << "Request RequestHistoryKL SerialNo: " << m_RequestHistoryKLSerialNo << endl;
}
virtual void OnReply_RequestHistoryKL(Futu::u32_t nSerialNo, const Qot_RequestHistoryKL::Response &stRsp){
if(nSerialNo == m_RequestHistoryKLSerialNo)
{
cout << "OnReply_RequestHistoryKL 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_RequestHistoryKLSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
FTAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
FTAPI::UnInit();
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
- Output
connect
Request RequestHistoryKL SerialNo: 3
OnReply_RequestHistoryKL SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"klList": [
{
"time": "2021-06-07 09:30:00",
"isBlank": false,
"highPrice": 611,
"openPrice": 611,
"lowPrice": 611,
"closePrice": 611,
"lastClosePrice": 611.5,
"volume": "250100",
"turnover": 152811100,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.081766148814390843,
"timestamp": 1623029400
},
{
"time": "2021-06-07 09:31:00",
"isBlank": false,
"highPrice": 611.5,
"openPrice": 611,
"lowPrice": 609,
"closePrice": 609.5,
"lastClosePrice": 611,
"volume": "230700",
"turnover": 140792100,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.24549918166939444,
"timestamp": 1623029460
},
...
{
"time": "2021-06-09 11:04:00",
"isBlank": false,
"highPrice": 604.5,
"openPrice": 604.5,
"lowPrice": 604,
"closePrice": 604,
"lastClosePrice": 604.5,
"volume": "17300",
"turnover": 10449350,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.0827129859387924,
"timestamp": 1623207840
}
]
}
}
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
RequestHistoryKL(req);
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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 QotRequestHistoryKL(){
const { RetType } = Common
const { RehabType, KLType, 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) { // ログイン成功
const req = {
c2s: {
rehabType: RehabType.RehabType_None,
klType: KLType.KLType_Day,
security:{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
beginTime: "2021-08-05",
endTime: "2021-08-10",
},
};
websocket.RequestHistoryKL(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("HistoryKL: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//相場接続をクローズ。使用後は接続をクローズしてください。不要なリソース占有を防ぎます
//OpenD は最大 128 接続に制限されています
//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
- Output
OwnerPlate: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"klList": [{
"time": "2021-08-05 00:00:00",
"isBlank": false,
"highPrice": 464.4,
"openPrice": 452.6,
"lowPrice": 432.6,
"closePrice": 439,
"lastClosePrice": 456.8,
"volume": "58625939",
"turnover": 26193545519,
"turnoverRate": 0.00611,
"pe": 22.137,
"changeRate": -3.896672504378286,
"timestamp": 1628092800
}, {
"time": "2021-08-06 00:00:00",
"isBlank": false,
"highPrice": 460.4,
"openPrice": 444.8,
"lowPrice": 440,
"closePrice": 453.6,
"lastClosePrice": 439,
"volume": "42946505",
"turnover": 19468783995,
"turnoverRate": 0.00447,
"pe": 22.873,
"changeRate": 3.3257403189066106,
"timestamp": 1628179200
}, {
"time": "2021-08-09 00:00:00",
"isBlank": false,
"highPrice": 472.4,
"openPrice": 447.8,
"lowPrice": 442.6,
"closePrice": 461.6,
"lastClosePrice": 453.6,
"volume": "40956433",
"turnover": 18847164570,
"turnoverRate": 0.00427,
"pe": 23.276,
"changeRate": 1.763668430335097,
"timestamp": 1628438400
}, {
"time": "2021-08-10 00:00:00",
"isBlank": false,
"highPrice": 493,
"openPrice": 478,
"lowPrice": 473.2,
"closePrice": 486.2,
"lastClosePrice": 461.6,
"volume": "40881986",
"turnover": 19749306230,
"turnoverRate": 0.00426,
"pe": 24.517,
"changeRate": 5.329289428076249,
"timestamp": 1628524800
}]
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
APIレート制限
- 分足は直近8年分のデータを提供、日足は直近20年分のデータを提供、日足以上は制限なし。
- お客様の口座の資産と取引状況に基づき、過去ローソク足データ枠が付与されます。そのため、30日以内に取得できる銘柄の過去ローソク足データは限られています。詳細なルールは登録枠 & 過去ローソク足データ枠をご参照ください。当日消費した過去ローソク足データ枠は、30日後に自動的に解放されます。
- 30秒以内に過去ローソク足データAPIを最大60回リクエストできます。注意:ページングでデータを取得する場合、このレート制限ルールは各銘柄の最初のページにのみ適用され、後続ページのリクエストはレート制限の対象外です。
- 売買回転率は日足以上のみ提供。
- オプション,日足、1分足、5分足、15分足、60分足のみ提供しています。
- 米国株のプレマーケット、アフターマーケット、夜間取引ローソク足は60分足以下のみ対応。米国株のプレ/アフターマーケットおよび夜間取引は非通常の取引時間帯のため、当該時間帯のローソク足データは2年分に満たない場合があります。
- 米国株の売買代金は2015-10-12以降のデータのみ提供。
- Python
- Proto
- C#
- Java
- C++
- JavaScript
request_history_kline(code, start=None, end=None, ktype=KLType.K_DAY, autype=AuType.QFQ, fields=[KL_FIELD.ALL], max_count=1000, page_req_key=None, extended_time=False)
概要
過去ローソク足データの取得
パラメータ
パラメータ 型 説明 code str 銘柄コード start str 開始時刻 形式:yyyy-MM-dd
例如:“2017-06-20”end str 結束時刻 形式:yyyy-MM-dd
例如:“2017-07-20”ktype KLType ローソク足タイプ autype AuType 復権タイプ fields KLFields 返すフィールドリスト max_count int 今回のリクエストで返すローソク足の最大本数 - Noneを指定すると、startとendの間のすべてのデータを返します
- 注意:OpenDはすべてのデータを受信してからスクリプトに送信します。
取得するローソク足本数が1000本を超える場合は、タイムアウトを防ぐためにページングの使用をお勧めします
page_req_key bytes ページングリクエストキー startとendの間のローソク足本数がmax_countを超える場合:
1. 最初のページリクエスト時はNoneを指定
2. 次ページ以降のリクエスト時は前回のレスポンスで返されたpage_req_keyを指定extended_time bool 是否許可米国株プレ/アフターマーケットデータ False:不許可
True:許可- startとendの組み合わせは以下の通り
Start タイプ End タイプ 説明 str str start と end がそれぞれ指定された日付 None str start 為 end 往前 365 天 str None end 為 start 往后 365 天 None None end 為現在の日付,start 往前 365 天
戻り値
パラメータ 型 説明 ret RET_CODE API呼び出し結果 data pd.DataFrame 当 ret == RET_OK,返す過去ローソク足データデータ str ret != RET_OK の場合、エラーの説明を返す page_req_key bytes 次ページリクエスト用のkey - 過去ローソク足データのフォーマットは以下の通り:
フィールド タイプ 説明 code str 銘柄コード name str 銘柄名 time_key str ローソク足時刻 形式:yyyy-MM-dd HH:mm:ss
香港株和 A株市場デフォルト是北京時刻,米国株市場デフォルト是美东時刻open float 始値 close float 終値 high float 高値 low float 安値 pe_ratio float PER このフィールドは比率フィールドで、デフォルトでは % を表示しませんturnover_rate float 売買回転率 volume int 出来高 turnover float 売買代金 change_rate float 騰落率 last_close float 前日終値
- 過去ローソク足データのフォーマットは以下の通り:
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data, page_req_key = quote_ctx.request_history_kline('US.AAPL', start='2019-09-11', end='2019-09-18', max_count=5, session=Session.ALL) # 1ページ5件、最初のページをリクエスト
if ret == RET_OK:
print(data)
print(data['code'][0]) # 最初のレコードの銘柄コードを取得
print(data['close'].values.tolist()) # 最初のページの終値をlistに変換
else:
print('error:', data)
while page_req_key != None: # 残りの全結果をリクエスト
print('*************************************')
ret, data, page_req_key = quote_ctx.request_history_kline('US.AAPL', start='2019-09-11', end='2019-09-18', max_count=5, page_req_key=page_req_key, session=Session.ALL) # ページネーション後のデータをリクエスト
if ret == RET_OK:
print(data)
else:
print('error:', data)
print('All pages are finished!')
quote_ctx.close() # 使用後は接続をクローズしてください。接続数の枯渇を防止します。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- Output
code name time_key open close high low pe_ratio turnover_rate volume turnover change_rate last_close
0 US.AAPL 苹果 2019-09-11 00:00:00 52.631194 53.963447 53.992409 52.549135 18.773 0.01039 177158584 9.808562e+09 3.179511 52.300545
.. ... ... ... ... ... ... ... ... ... ... ... ... ...
4 US.AAPL 苹果 2019-09-17 00:00:00 53.087346 53.265945 53.294907 52.884612 18.530 0.00432 73545872 4.046314e+09 0.363802 53.072865
[5 rows x 13 columns]
US.AAPL
[53.9634465, 53.84156475, 52.7953125, 53.072865, 53.265945]
*************************************
code name time_key open close high low pe_ratio turnover_rate volume turnover change_rate last_close
0 US.AAPL 苹果 2019-09-18 00:00:00 53.352831 53.76554 53.784847 52.961844 18.704 0.00602 102572372 5.682068e+09 0.937925 53.265945
All pages are finished!
2
3
4
5
6
7
8
9
10
11
12
# Qot_RequestHistoryKL.proto
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
プロトコル ID
3103
uint RequestHistoryKL(QotRequestHistoryKL.Request req);
virtual void OnReply_RequestHistoryKL(MMAPI_Conn client, uint nSerialNo, QotRequestHistoryKL.Response rsp);
- 概要
過去ローソク足データの取得
- パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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();
QotRequestHistoryKL.C2S c2s = QotRequestHistoryKL.C2S.CreateBuilder()
.SetRehabType((int)QotCommon.RehabType.RehabType_Forward)
.SetKlType((int)QotCommon.KLType.KLType_1Min)
.SetSecurity(sec)
.SetBeginTime("2020-09-01")
.SetEndTime("2020-09-10")
.Build();
QotRequestHistoryKL.Request req = QotRequestHistoryKL.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.RequestHistoryKL(req);
Console.Write("Send QotRequestHistoryKL: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode) {
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_RequestHistoryKL(MMAPI_Conn client, uint nSerialNo, QotRequestHistoryKL.Response rsp) {
Console.Write("Reply: QotRequestHistoryKL: {0} {1}\n", nSerialNo, rsp.ToString());
Console.Write("Code: {0}, lastClosePrice: {1} \n",
rsp.S2C.Security.Code,
rsp.S2C.KlListList[0].LastClosePrice);
}
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
- Output
Qot onInitConnect: ret=0 desc= connID=6819076272648767075
Send QotRequestHistoryKL: 3
Reply: QotRequestHistoryKL: 3 retType: 0
retMsg: ""
errCode: 0
s2c {
security {
market: 1
code: "00700"
}
klList {
time: "2020-09-01 09:30:00"
isBlank: false
highPrice: 535.9
openPrice: 535.9
lowPrice: 535.9
closePrice: 535.9
lastClosePrice: 528.9
volume: 665000
turnover: 357437500
turnoverRate: 0
pe: 0
changeRate: 1.3235016071090944
timestamp: 1598923800
}
...
klList {
time: "2020-09-09 16:00:00"
isBlank: false
highPrice: 503.9
openPrice: 502.4
lowPrice: 501.9
closePrice: 502.4
lastClosePrice: 502.9
volume: 1504800
turnover: 758472600
turnoverRate: 0
pe: 0
changeRate: -0.0994233446013124
timestamp: 1599638400
}
}
Code: 00700, lastClosePrice: 528.9
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
int requestHistoryKL(QotRequestHistoryKL.Request req);
void onReply_RequestHistoryKL(MMAPI_Conn client, int nSerialNo, QotRequestHistoryKL.Response rsp);
- 概要
過去ローソク足データの取得
- パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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();
QotRequestHistoryKL.C2S c2s = QotRequestHistoryKL.C2S.newBuilder()
.setRehabType(QotCommon.RehabType.RehabType_Forward_VALUE)
.setKlType(QotCommon.KLType.KLType_1Min_VALUE)
.setSecurity(sec)
.setBeginTime("2020-09-01")
.setEndTime("2020-09-02")
.build();
QotRequestHistoryKL.Request req = QotRequestHistoryKL.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.requestHistoryKL(req);
System.out.printf("Send QotRequestHistoryKL: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_RequestHistoryKL(MMAPI_Conn client, int nSerialNo, QotRequestHistoryKL.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotRequestHistoryKL failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotRequestHistoryKL: %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
- Output
Send QotRequestHistoryKL: 2
Receive QotRequestHistoryKL: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"klList": [{
"time": "2020-09-01 09:30:00",
"isBlank": false,
"highPrice": 535.9,
"openPrice": 535.9,
"lowPrice": 535.9,
"closePrice": 535.9,
"lastClosePrice": 528.9,
"volume": "665000",
"turnover": 3.574375E8,
"turnoverRate": 0.0,
"pe": 0.0,
"changeRate": 1.3235016071090944,
"timestamp": 1.5989238E9
}, ... {
"time": "2020-09-01 16:00:00",
"isBlank": false,
"highPrice": 542.4,
"openPrice": 542.4,
"lowPrice": 537.4,
"closePrice": 537.4,
"lastClosePrice": 542.4,
"volume": "1303900",
"turnover": 7.0309225E8,
"turnoverRate": 0.0,
"pe": 0.0,
"changeRate": -0.9218289085545722,
"timestamp": 1.5989472E9
}]
}
}
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
moomoo::u32_t RequestHistoryKL(const Qot_RequestHistoryKL::Request &stReq);
virtual void OnReply_RequestHistoryKL(moomoo::u32_t nSerialNo, const Qot_RequestHistoryKL::Response &stRsp) = 0;
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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;
// パケット生成
Qot_RequestHistoryKL::Request req;
Qot_RequestHistoryKL::C2S *c2s = req.mutable_c2s();
c2s->set_rehabtype(1);
c2s->set_kltype(Qot_Common::KLType::KLType_1Min);
Qot_Common::Security *sec = c2s->mutable_security();
sec->set_code("00700");
sec->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
c2s->set_begintime("2021-06-07");
c2s->set_endtime("2021-07-01");
m_RequestHistoryKLSerialNo = m_pQotApi->RequestHistoryKL(req);
cout << "Request RequestHistoryKL SerialNo: " << m_RequestHistoryKLSerialNo << endl;
}
virtual void OnReply_RequestHistoryKL(Futu::u32_t nSerialNo, const Qot_RequestHistoryKL::Response &stRsp){
if(nSerialNo == m_RequestHistoryKLSerialNo)
{
cout << "OnReply_RequestHistoryKL 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_RequestHistoryKLSerialNo;
};
int32_t main(int32_t argc, char** argv)
{
MMAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
MMAPI::UnInit();
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
- Output
connect
Request RequestHistoryKL SerialNo: 3
OnReply_RequestHistoryKL SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"security": {
"market": 1,
"code": "00700"
},
"klList": [
{
"time": "2021-06-07 09:30:00",
"isBlank": false,
"highPrice": 611,
"openPrice": 611,
"lowPrice": 611,
"closePrice": 611,
"lastClosePrice": 611.5,
"volume": "250100",
"turnover": 152811100,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.081766148814390843,
"timestamp": 1623029400
},
{
"time": "2021-06-07 09:31:00",
"isBlank": false,
"highPrice": 611.5,
"openPrice": 611,
"lowPrice": 609,
"closePrice": 609.5,
"lastClosePrice": 611,
"volume": "230700",
"turnover": 140792100,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.24549918166939444,
"timestamp": 1623029460
},
...
{
"time": "2021-06-09 11:04:00",
"isBlank": false,
"highPrice": 604.5,
"openPrice": 604.5,
"lowPrice": 604,
"closePrice": 604,
"lastClosePrice": 604.5,
"volume": "17300",
"turnover": 10449350,
"turnoverRate": 0,
"pe": 0,
"changeRate": -0.0827129859387924,
"timestamp": 1623207840
}
]
}
}
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
RequestHistoryKL(req);
概要
過去ローソク足データの取得
パラメータ
message C2S
{
required int32 rehabType = 1; //Qot_Common.RehabType, 復権タイプ
required int32 klType = 2; //Qot_Common.KLType, ローソク足の時間周期
required Qot_Common.Security security = 3; //株式市場および銘柄コード
required string beginTime = 4; //開始時刻文字列(形式:yyyy-MM-dd)
required string endTime = 5; //終了時刻文字列(形式:yyyy-MM-dd)
optional int32 maxAckKLNum = 6; //最大返却ローソク足本数。未指定の場合は制限なし
optional int64 needKLFieldsFlag = 7; //ローソク足構造体の特定フィールドを指定して返す。KLFields列挙値またはその組み合わせ。未指定の場合はすべてのフィールドを返す
optional bytes nextReqKey = 8; //ページネーションリクエスト key
optional bool extendedTime = 9; //米国株プレ/アフターマーケットデータの取得可否。現在は60分足以下のみ対応。
optional int32 session = 10; //米国株のプレ/アフターマーケットおよび夜間取引データの取得可否。現在は60分足以下のみ対応。このパラメータ指定時はextendedTimeを無視
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 戻り値
message S2C
{
required Qot_Common.Security security = 1; //銘柄
optional string name = 4; // 銘柄名
repeated Qot_Common.KLine klList = 2; //ローソク足データ
optional bytes nextReqKey = 3; //ページネーションリクエスト key。1回のリクエストで全データが返されなかった場合、次回リクエスト時にこの key を付与するとデータの続きを取得
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType、戻り値
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 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 QotRequestHistoryKL(){
const { RetType } = Common
const { RehabType, KLType, 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) { // ログイン成功
const req = {
c2s: {
rehabType: RehabType.RehabType_None,
klType: KLType.KLType_Day,
security:{
market: QotMarket.QotMarket_HK_Security,
code: "00700",
},
beginTime: "2021-08-05",
endTime: "2021-08-10",
},
};
websocket.RequestHistoryKL(req)
.then((res) => {
let { errCode, retMsg, retType,s2c } = res
console.log("HistoryKL: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log(data);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//相場接続をクローズ。使用後は接続をクローズしてください。不要なリソース占有を防ぎます
//OpenD は最大 128 接続に制限されています
//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
- Output
OwnerPlate: errCode 0, retMsg , retType 0
{
"security": {
"market": 1,
"code": "00700"
},
"klList": [{
"time": "2021-08-05 00:00:00",
"isBlank": false,
"highPrice": 464.4,
"openPrice": 452.6,
"lowPrice": 432.6,
"closePrice": 439,
"lastClosePrice": 456.8,
"volume": "58625939",
"turnover": 26193545519,
"turnoverRate": 0.00611,
"pe": 22.137,
"changeRate": -3.896672504378286,
"timestamp": 1628092800
}, {
"time": "2021-08-06 00:00:00",
"isBlank": false,
"highPrice": 460.4,
"openPrice": 444.8,
"lowPrice": 440,
"closePrice": 453.6,
"lastClosePrice": 439,
"volume": "42946505",
"turnover": 19468783995,
"turnoverRate": 0.00447,
"pe": 22.873,
"changeRate": 3.3257403189066106,
"timestamp": 1628179200
}, {
"time": "2021-08-09 00:00:00",
"isBlank": false,
"highPrice": 472.4,
"openPrice": 447.8,
"lowPrice": 442.6,
"closePrice": 461.6,
"lastClosePrice": 453.6,
"volume": "40956433",
"turnover": 18847164570,
"turnoverRate": 0.00427,
"pe": 23.276,
"changeRate": 1.763668430335097,
"timestamp": 1628438400
}, {
"time": "2021-08-10 00:00:00",
"isBlank": false,
"highPrice": 493,
"openPrice": 478,
"lowPrice": 473.2,
"closePrice": 486.2,
"lastClosePrice": 461.6,
"volume": "40881986",
"turnover": 19749306230,
"turnoverRate": 0.00426,
"pe": 24.517,
"changeRate": 5.329289428076249,
"timestamp": 1628524800
}]
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
APIレート制限
- 分足は直近8年分のデータを提供、日足は直近20年分のデータを提供、日足以上は制限なし。
- お客様の口座の資産と取引状況に基づき、過去ローソク足データ枠が付与されます。そのため、30日以内に取得できる銘柄の過去ローソク足データは限られています。詳細なルールは登録枠 & 過去ローソク足データ枠をご参照ください。当日消費した過去ローソク足データ枠は、30日後に自動的に解放されます。
- 30秒以内に過去ローソク足データAPIを最大60回リクエストできます。注意:ページングでデータを取得する場合、このレート制限ルールは各銘柄の最初のページにのみ適用され、後続ページのリクエストはレート制限の対象外です。
- 売買回転率は日足以上のみ提供。
- オプション,日足、1分足、5分足、15分足、60分足のみ提供しています。
- 米国株のプレマーケット、アフターマーケット、夜間取引ローソク足は60分足以下のみ対応。米国株のプレ/アフターマーケットおよび夜間取引は非通常の取引時間帯のため、当該時間帯のローソク足データは2年分に満たない場合があります。
- 米国株の売買代金は2015-10-12以降のデータのみ提供。
← 取得株式所属セクター 取得復権因子 →