Skip to content

Commit b3f5ea1

Browse files
committed
quote model
1 parent 94089ca commit b3f5ea1

File tree

4 files changed

+295
-27
lines changed

4 files changed

+295
-27
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "tigerapi/quote_client.h"
44
#include "tigerapi/trade_client.h"
55
#include "tigerapi/contract_util.h"
6+
#include "tigerapi/model.h"
67
#include "tigerapi/order_util.h"
78
#include "tigerapi/utils.h"
89
#include <cpprest/details/basic_types.h>
@@ -150,8 +151,8 @@ class TestQuoteClient {
150151
value symbols = value::array();
151152
symbols[0] = value::string(U("AAPL"));
152153
symbols[1] = value::string(U("JD"));
153-
value result = quote_client->get_quote_real_time(symbols);
154-
ucout << U("result: ") << result << endl;
154+
auto result = quote_client->get_quote_real_time(symbols);
155+
ucout << U("result: ") << result.at(0).to_string() << endl;
155156
}
156157

157158
static void test_get_quote_delay(const std::shared_ptr<QuoteClient> quote_client) {
@@ -183,9 +184,9 @@ class TestQuoteClient {
183184
symbols[0] = value::string(U("AAPL"));
184185
symbols[1] = value::string(U("JD"));
185186
//ucout << U("symbols ") << symbols << endl;
186-
value result = quote_client->get_kline(symbols, U("day"), -1, -1, U("br"), 5);
187-
// ucout << U("result: ") << result << endl;
188-
//LOG(INFO) << U("result: ") << result.serialize() << endl;
187+
// value result = quote_client->get_kline(symbols, U("day"), -1, -1, U("br"), 5);
188+
vector<Kline> result = quote_client->get_kline(symbols, "day", -1, -1, 5);
189+
cout << result.at(0).to_string() << endl;
189190
}
190191

191192
static void test_get_quote_stock_trade(std::shared_ptr<QuoteClient> quote_client) {
@@ -200,7 +201,7 @@ class TestQuoteClient {
200201
value symbols = value::array();
201202
symbols[0] = value::string(U("AAPL"));
202203
symbols[1] = value::string(U("JD"));
203-
value result = quote_client->get_trade_tick(symbols, 0, 100);
204+
value result = quote_client->get_trade_tick(symbols, 0, 100000000);
204205
ucout << U("Result: ") << result << endl;
205206
}
206207

@@ -248,8 +249,9 @@ class TestQuoteClient {
248249
value symbols = value::array();
249250
symbols[0] = value::string(U("CL2303"));
250251
ucout << U("symbols ") << symbols << endl;
251-
value result = quote_client->get_future_kline(symbols);
252-
ucout << U("result: ") << result << endl;
252+
// value result = quote_client->get_future_kline(symbols);
253+
vector<Kline> result = quote_client->get_future_kline(symbols, BarPeriod::DAY);
254+
ucout << U("result: ") << result.at(0).to_string() << endl;
253255
}
254256

255257
static void test_get_future_tick(std::shared_ptr<QuoteClient> quote_client) {
@@ -261,8 +263,9 @@ class TestQuoteClient {
261263
value symbols = value::array();
262264
symbols[0] = value::string(U("CL2303"));
263265
ucout << U("symbols ") << symbols << endl;
264-
value result = quote_client->get_future_real_time_quote(symbols);
265-
ucout << U("result: ") << result << endl;
266+
// value result = quote_client->get_future_real_time_quote(symbols);
267+
auto result = quote_client->get_future_real_time_quote(symbols);
268+
ucout << U("result: ") << result.at(0).to_string() << endl;
266269
}
267270

268271
static void test_get_option_expiration(std::shared_ptr<QuoteClient> quote_client) {
@@ -280,26 +283,27 @@ class TestQuoteClient {
280283

281284

282285
static void test_get_option_brief(std::shared_ptr<QuoteClient> quote_client) {
283-
value result = quote_client->get_option_brief(U("AAPL 230317C000135000"));
286+
value result = quote_client->get_option_brief(U("AAPL 230224C000150000"));
284287
ucout << U("result: ") << result << endl;
285288
}
286289

287290
static void test_get_option_kline(std::shared_ptr<QuoteClient> quote_client) {
288291
value identifiers = value::array();
289-
identifiers[0] = value::string(U("AMD 220819C000165000"));
290-
value result = quote_client->get_option_kline(identifiers, 1639026000000, 1649026000000);
291-
ucout << U("result: ") << result << endl;
292+
identifiers[0] = value::string(U("AAPL 230224C000150000"));
293+
// value result = quote_client->get_option_kline(identifiers, 1639026000000, 1649026000000);
294+
vector<Kline> result = quote_client->get_option_kline(identifiers, 1639026000000, 1649026000000);
295+
ucout << U("result: ") << result.at(0).to_string() << endl;
292296
}
293297

294298
static void test_get_option_trade_tick(std::shared_ptr<QuoteClient> quote_client) {
295299
value identifiers = value::array();
296-
identifiers[0] = value::string(U("AAPL 230317C000135000"));
300+
identifiers[0] = value::string(U("AAPL 230224C000150000"));
297301
value result = quote_client->get_option_trade_tick(identifiers);
298302
ucout << U("result: ") << result << endl;
299303
}
300304

301305
static void test_quote(const std::shared_ptr<QuoteClient> quote_client) {
302-
TestQuoteClient::test_get_quote_delay(quote_client);
306+
TestQuoteClient::test_get_kline(quote_client);
303307
}
304308
};
305309

@@ -337,6 +341,8 @@ int main()
337341
config.private_key = U("");
338342
config.tiger_id = U("");
339343
config.account = U("");
344+
345+
340346
// config.lang = U("en_US");
341347

342348

@@ -349,8 +355,8 @@ int main()
349355
/**
350356
* ʹ�÷�װ��Ľ��׽ӿ� TradeClient
351357
*/
352-
std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
353-
TestTradeClient::test_trade(trade_client);
358+
// std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
359+
// TestTradeClient::test_trade(trade_client);
354360

355361
/**
356362
* ֱ��ʹ��δ��װ�� TigerApi

include/tigerapi/model.h

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,158 @@ namespace TIGER_API {
458458
return ss.str();
459459
};
460460
};
461+
462+
class OPENAPI_EXPORT KlineItem {
463+
public:
464+
double open = 0;
465+
double high = 0;
466+
double low = 0;
467+
double close = 0;
468+
time_t time;
469+
long volume = 0;
470+
time_t last_time;
471+
long open_interest = 0;
472+
double settlement = 0;
473+
474+
void from_json(const web::json::value& j) {
475+
if (j.has_field(U("open"))) {
476+
open = j.at(U("open")).as_double();
477+
}
478+
if (j.has_field(U("high"))) {
479+
high = j.at(U("high")).as_double();
480+
}
481+
if (j.has_field(U("low"))) {
482+
low = j.at(U("low")).as_double();
483+
}
484+
if (j.has_field(U("close"))) {
485+
close = j.at(U("close")).as_double();
486+
}
487+
if (j.has_field(U("time"))) {
488+
time = j.at(U("time")).as_number().to_int64();
489+
}
490+
if (j.has_field(U("lastTime"))) {
491+
last_time = j.at(U("lastTime")).as_number().to_int64();
492+
}
493+
if (j.has_field(U("volume"))) {
494+
volume = j.at(U("volume")).as_number().to_int64();
495+
}
496+
if (j.has_field(U("settlement"))) {
497+
settlement = j.at(U("settlement")).as_double();
498+
}
499+
if (j.has_field(U("openInterest"))) {
500+
open_interest = j.at(U("openInterest")).as_number().to_int64();
501+
}
502+
503+
}
504+
};
505+
506+
class OPENAPI_EXPORT Kline {
507+
public:
508+
vector<KlineItem> items;
509+
utility::string_t symbol;
510+
utility::string_t contract_code;
511+
utility::string_t period;
512+
time_t expiry;
513+
utility::string_t right;
514+
utility::string_t strike;
515+
516+
utility::string_t to_string() {
517+
return U("Kline of " + symbol + U(" ") + std::to_string(items.size()) + U(" items"));
518+
}
519+
};
520+
521+
class OPENAPI_EXPORT RealtimeQuote {
522+
public:
523+
double open = 0;
524+
double high = 0;
525+
double low = 0;
526+
double close = 0;
527+
long volume = 0;
528+
double adj_pre_close = 0;
529+
double pre_close = 0;
530+
double ask_price = 0;
531+
double ask_size = 0;
532+
double bid_price = 0;
533+
double bid_size = 0;
534+
double latest_price = 0;
535+
time_t latest_time;
536+
long latest_size = 0;
537+
utility::string_t status;
538+
utility::string_t symbol;
539+
540+
utility::string_t contract_code;
541+
long open_interest = 0;
542+
int limit_down = 0;
543+
int limit_up = 0;
544+
545+
546+
void from_json(const web::json::value &j) {
547+
if (j.has_field(U("open")))
548+
open = j.at(U("open")).as_double();
549+
550+
if (j.has_field(U("high")))
551+
high = j.at(U("high")).as_double();
552+
553+
if (j.has_field(U("low")))
554+
low = j.at(U("low")).as_double();
555+
556+
if (j.has_field(U("close")))
557+
close = j.at(U("close")).as_double();
558+
559+
if (j.has_field(U("volume")))
560+
volume = j.at(U("volume")).as_number().to_int64();
561+
562+
if (j.has_field(U("adjPreClose")))
563+
adj_pre_close = j.at(U("adjPreClose")).as_double();
564+
565+
if (j.has_field(U("preClose")))
566+
pre_close = j.at(U("preClose")).as_double();
567+
568+
if (j.has_field(U("askPrice")))
569+
ask_price = j.at(U("askPrice")).as_double();
570+
571+
if (j.has_field(U("askSize")))
572+
ask_size = j.at(U("askSize")).as_double();
573+
574+
if (j.has_field(U("bidPrice")))
575+
bid_price = j.at(U("bidPrice")).as_double();
576+
577+
if (j.has_field(U("bidSize")))
578+
bid_size = j.at(U("bidSize")).as_double();
579+
580+
if (j.has_field(U("latestPrice")))
581+
latest_price = j.at(U("latestPrice")).as_double();
582+
583+
if (j.has_field(U("latestTime")))
584+
latest_time = j.at(U("latestTime")).as_number().to_int64();
585+
586+
if (j.has_field(U("latestSize")))
587+
latest_size = j.at(U("latestSize")).as_number().to_int64();
588+
589+
if (j.has_field(U("status")))
590+
status = j.at(U("status")).as_string();
591+
592+
if (j.has_field(U("symbol")))
593+
symbol = j.at(U("symbol")).as_string();
594+
595+
if (j.has_field(U("contractCode")))
596+
contract_code = j.at(U("contractCode")).as_string();
597+
598+
if (j.has_field(U("openInterest")))
599+
open_interest = j.at(U("openInterest")).as_number().to_int64();
600+
601+
if (j.has_field(U("limitDown")))
602+
limit_down = j.at(U("limitDown")).as_integer();
603+
604+
if (j.has_field(U("limitUp")))
605+
limit_up = j.at(U("limitUp")).as_integer();
606+
}
607+
608+
utility::string_t to_string() {
609+
return U("RealtimeQuote<" + symbol + U("> ") + std::to_string(latest_price));
610+
}
611+
};
612+
461613
}
462614

463615

include/tigerapi/quote_client.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "client_config.h"
1010
#include "enums.h"
1111
#include "service_types.h"
12+
#include "model.h"
13+
1214

1315
namespace TIGER_API {
1416
class OPENAPI_EXPORT QuoteClient : public TigerClient {
@@ -35,11 +37,14 @@ namespace TIGER_API {
3537
QuoteRight right=QuoteRight::br, int limit=251, utility::string_t page_token=U(""));
3638
value get_kline(const value &symbols, utility::string_t period, time_t begin_time=-1, time_t end_time=-1,
3739
utility::string_t right=U("br"), int limit=251, utility::string_t page_token=U(""));
40+
vector<Kline> get_kline(const value &symbols, utility::string_t period, time_t begin_time=-1, time_t end_time=-1,
41+
int limit=251, utility::string_t right=U("br"), utility::string_t page_token=U(""));
3842
value get_trade_tick(const value &symbols, TradingSession trade_session=TradingSession::Regular, long begin_index=-1,
3943
long end_index=-1, int limit=100);
4044
value get_trade_tick(const value &symbols, long begin_index=-1,
4145
long end_index=-1, utility::string_t trade_session=U("Regular"), int limit=100);
42-
value get_quote_real_time(const value &symbols);
46+
value get_quote_real_time_value(const value &symbols);
47+
vector<RealtimeQuote> get_quote_real_time(const value &symbols);
4348
value get_quote_delay(const value &symbols);
4449
value get_quote_shortable_stocks(const value &symbols);
4550
value get_quote_depth(const value &symbols, Market market = Market::US);
@@ -54,7 +59,8 @@ namespace TIGER_API {
5459
value get_option_chain(const utility::string_t symbol, utility::string_t expiry, value option_filter= value::null());
5560
value get_option_brief(value identifiers);
5661
value get_option_brief(const utility::string_t identifier);
57-
value get_option_kline(value identifiers, time_t begin_time, time_t end_time=4070880000000);
62+
value get_option_kline_value(value identifiers, time_t begin_time, time_t end_time=4070880000000);
63+
vector<Kline> get_option_kline(value identifiers, time_t begin_time, time_t end_time=4070880000000);
5864
value get_option_trade_tick(value identifiers);
5965

6066
/** 期货行情 Future quote related api **/
@@ -64,9 +70,12 @@ namespace TIGER_API {
6470
value get_future_contracts(utility::string_t type);
6571
value get_future_continuous_contracts(utility::string_t type);
6672
value get_future_current_contract(utility::string_t type);
67-
value get_future_kline(value contract_codes, BarPeriod period=BarPeriod::DAY, time_t begin_time=-1, time_t end_time=-1,
73+
value get_future_kline(value contract_codes, utility::string_t period=U("day"), time_t begin_time=-1, time_t end_time=-1,
74+
int limit=251, utility::string_t page_token=U(""));
75+
vector<Kline> get_future_kline(value contract_codes, BarPeriod period=BarPeriod::DAY, time_t begin_time=-1, time_t end_time=-1,
6876
int limit=251, utility::string_t page_token=U(""));
69-
value get_future_real_time_quote(value contract_codes);
77+
value get_future_real_time_quote_value(value contract_codes);
78+
vector<RealtimeQuote> get_future_real_time_quote(value contract_codes);
7079
value get_future_tick(utility::string_t contract_code, long begin_index=0, long end_index=100, int limit=1000);
7180
value get_future_trading_date(utility::string_t contract_code, utility::string_t trading_date);
7281

0 commit comments

Comments
 (0)