Skip to content

Commit 740c050

Browse files
committed
add api
1 parent c8205cf commit 740c050

File tree

4 files changed

+285
-10
lines changed

4 files changed

+285
-10
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,36 @@ class TestTradeClient {
5353
ucout << U("active orders: ") << res << endl;
5454
}
5555

56+
static void test_get_transactions(const std::shared_ptr<TradeClient>& trade_client) {
57+
value res = trade_client->get_transactions(trade_client->client_config.account, U("AAPL"));
58+
ucout << U("transactions: ") << res << endl;
59+
}
60+
5661
static void test_get_contract(const std::shared_ptr<TradeClient>& trade_client) {
5762
value res = trade_client->get_contract(U("AAPL"));
5863
ucout << U("contract: ") << res << endl;
5964
}
6065

66+
static void test_get_contracts(const std::shared_ptr<TradeClient>& trade_client) {
67+
value symbols = value::array();
68+
symbols[0] = value::string(U("AAPL"));
69+
symbols[1] = value::string(U("JD"));
70+
value res = trade_client->get_contracts(symbols, U("STK"));
71+
ucout << U("contracts: ") << res << endl;
72+
}
73+
74+
static void test_get_quote_contract(const std::shared_ptr<TradeClient>& trade_client) {
75+
value res = trade_client->get_quote_contract(U("00700"), U("IOPT"), U("20230728"));
76+
ucout << U("quote contract: ") << res << endl;
77+
}
78+
79+
static void test_get_estimate_tradable_quantity(const std::shared_ptr<TradeClient>& trade_client) {
80+
Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));
81+
Order order = OrderUtil::limit_order(contract, U("BUY"), 1, 100.0);
82+
value res = trade_client->get_estimate_tradable_quantity(order);
83+
ucout << U("estimate tradable quantity: ") << res << endl;
84+
}
85+
6186
static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
6287
Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));
6388
Order order = OrderUtil::limit_order(contract, U("BUY"), 1, 100.0);
@@ -115,6 +140,32 @@ class TestTradeClient {
115140
ucout << U("modified order: ") << mod_order.to_string() << endl;
116141
}
117142

143+
static void test_get_analytics_asset(const std::shared_ptr<TradeClient>& trade_client) {
144+
value res = trade_client->get_analytics_asset(trade_client->client_config.account, U("2023-11-01"),
145+
U("2023-12-31"));
146+
ucout << U("analytics asset: ") << res << endl;
147+
}
148+
149+
static void test_get_segment_fund_history(const std::shared_ptr<TradeClient>& trade_client) {
150+
value res = trade_client->get_segment_fund_history();
151+
ucout << U("segment fund history: ") << res << endl;
152+
}
153+
154+
static void test_get_segment_fund_available(const std::shared_ptr<TradeClient>& trade_client) {
155+
value res = trade_client->get_segment_fund_available(U("SEC"));
156+
ucout << U("segment fund available: ") << res << endl;
157+
}
158+
159+
static void test_transfer_segment_fund(const std::shared_ptr<TradeClient>& trade_client) {
160+
value res = trade_client->transfer_segment_fund(U("SEC"), U("FUT"), 10.0);
161+
ucout << U("transfer segment fund: ") << res << endl;
162+
}
163+
164+
static void test_place_forex_order(const std::shared_ptr<TradeClient>& trade_client) {
165+
value res = trade_client->place_forex_order(U("SEC"), U("USD"), U("HKD"), 1.0);
166+
ucout << U("place forex order: ") << res << endl;
167+
}
168+
118169

119170
static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
120171
TestTradeClient::test_get_orders(trade_client);
@@ -368,20 +419,21 @@ int main()
368419

369420

370421

422+
371423
//config.lang = U("en_US");
372424

373425

374426
/**
375427
* ʹ�÷�װ�������ӿ� QuoteClient
376428
*/
377-
std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
378-
TestQuoteClient::test_quote(quote_client);
429+
//std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
430+
//TestQuoteClient::test_quote(quote_client);
379431

380432
/**
381433
* ʹ�÷�װ��Ľ��׽ӿ� TradeClient
382434
*/
383-
//std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
384-
//TestTradeClient::test_trade(trade_client);
435+
std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
436+
TestTradeClient::test_trade(trade_client);
385437

386438
/**
387439
* ֱ��ʹ��δ��װ�� TigerApi

include/tigerapi/service_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ static utility::string_t FILLED_ORDERS = U("filled_orders"); // 已成交订
2424
static utility::string_t ORDER_TRANSACTIONS = U("order_transactions"); // 订单成交记录
2525
static utility::string_t ANALYTICS_ASSET = U("analytics_asset");
2626
static utility::string_t USER_LICENSE = U("user_license");
27+
static utility::string_t ESTIMATE_TRADABLE_QUANTITY = U("estimate_tradable_quantity");
28+
static utility::string_t SEGMENT_FUND_HISTORY = U("segment_fund_history");
29+
static utility::string_t SEGMENT_FUND_AVAILABLE = U("segment_fund_available");
30+
static utility::string_t TRANSFER_SEGMENT_FUND = U("transfer_segment_fund");
31+
static utility::string_t PLACE_FOREX_ORDER = U("place_forex_order");
2732

2833
//合约
2934
static utility::string_t CONTRACT = U("contract");

include/tigerapi/trade_client.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,12 @@ namespace TIGER_API {
135135
SegmentType seg_type = SegmentType::SEC);
136136

137137
Order get_order(unsigned long long id, bool is_brief=false);
138-
Order get_transactions(utility::string_t account = U(""), long order_id = 0, utility::string_t sec_type = U(""),
139-
utility::string_t symbol = U(""), long start_time = -1, time_t end_time = -1,
140-
int limit = 100, utility::string_t expiry = U(""), double strike = 0, utility::string_t right = U(""));
138+
value get_transactions(utility::string_t account, long order_id);
139+
value get_transactions(utility::string_t account, utility::string_t symbol, utility::string_t sec_type = U(""),
140+
long start_time = -1, time_t end_time = -1,
141+
int limit = 100, utility::string_t expiry = U(""), utility::string_t strike = U(""), utility::string_t right = U(""),
142+
long order_id = 0);
143+
141144
value cancel_order(unsigned long long id);
142145
value place_order(value &order);
143146
value place_order(Order &order);
@@ -152,8 +155,19 @@ namespace TIGER_API {
152155
value
153156
get_contract(utility::string_t symbol, SecType sec_type = SecType::STK, Currency currency = Currency::ALL, utility::string_t exchange = U(""), time_t expiry = -1,
154157
utility::string_t strike = U(""), Right right = Right::ALL);
155-
156-
158+
value get_contracts(const value &symbols, utility::string_t sec_type, utility::string_t currency = U(""), utility::string_t exchange = U(""), time_t expiry = -1,
159+
utility::string_t strike = U(""), utility::string_t right = U(""));
160+
value get_quote_contract(utility::string_t symbol, utility::string_t sec_type, utility::string_t expiry);
161+
162+
value get_estimate_tradable_quantity(Order &order, utility::string_t seg_type = U("SEC"));
163+
value get_analytics_asset(utility::string_t account, utility::string_t start_date, utility::string_t end_date,
164+
utility::string_t seg_type = U("SEC"), utility::string_t currency = U("USD"),
165+
utility::string_t sub_account = U(""));
166+
167+
value get_segment_fund_history(int limit = 0);
168+
value get_segment_fund_available(utility::string_t from_segment, utility::string_t currency = U("USD"));
169+
value transfer_segment_fund(utility::string_t from_segment, utility::string_t to_segment, double amount, utility::string_t currency = U("USD"));
170+
value place_forex_order(utility::string_t seg_type, utility::string_t source_currency, utility::string_t target_currency, double source_amount);
157171

158172

159173
private:

src/trade_client.cpp

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,48 @@ namespace TIGER_API {
278278
parent_id, enum_to_str(sort_by), enum_to_str(seg_type));
279279
}
280280

281+
value TradeClient::get_transactions(utility::string_t account, long order_id) {
282+
value obj = value::object(true);
283+
obj[P_ACCOUNT] = get_account_param(account);
284+
obj[U("order_id")] = (long long) order_id;
285+
return post(ORDER_TRANSACTIONS, obj)[P_ITEMS];
286+
}
287+
288+
value TradeClient::get_transactions(utility::string_t account, utility::string_t symbol, utility::string_t sec_type,
289+
long start_time, time_t end_time,
290+
int limit, utility::string_t expiry, utility::string_t strike, utility::string_t right,
291+
long order_id) {
292+
value obj = value::object(true);
293+
obj[P_ACCOUNT] = get_account_param(account);
294+
if (order_id != 0) {
295+
obj[U("order_id")] = (long long) order_id;
296+
}
297+
if (!sec_type.empty()) {
298+
obj[U("sec_type")] = value::string(sec_type);
299+
}
300+
if (!symbol.empty()) {
301+
obj[U("symbol")] = value::string(symbol);
302+
}
303+
if (!expiry.empty()) {
304+
obj[U("expiry")] = value::string(expiry);
305+
}
306+
if (!strike.empty()) {
307+
obj[U("strike")] = value::string(strike);
308+
}
309+
if (!right.empty()) {
310+
obj[U("right")] = value::string(right);
311+
}
312+
if (start_time != 0) {
313+
obj[U("start_time")] = (long long) start_time;
314+
}
315+
if (end_time != 0) {
316+
obj[U("end_time")] = (long long) end_time;
317+
}
318+
if (limit != 0) {
319+
obj[U("limit")] = (long long) limit;
320+
}
321+
return post(ORDER_TRANSACTIONS, obj)[P_ITEMS];
322+
}
281323

282324
value TradeClient::get_contract(utility::string_t symbol, utility::string_t sec_type, utility::string_t currency, utility::string_t exchange, time_t expiry,
283325
utility::string_t strike, utility::string_t right) {
@@ -313,8 +355,50 @@ namespace TIGER_API {
313355
enum_to_str(right));
314356
}
315357

316-
value TradeClient::place_order(value &order) {
358+
value TradeClient::get_contracts(const value &symbols, utility::string_t sec_type, utility::string_t currency,
359+
utility::string_t exchange, time_t expiry, utility::string_t strike,
360+
utility::string_t right) {
361+
value obj = value::object(true);
362+
obj[P_ACCOUNT] = get_account_param();
363+
obj[P_SYMBOLS] = symbols;
364+
if (!sec_type.empty()) {
365+
obj[P_SEC_TYPE] = value::string(sec_type);
366+
}
367+
if (!currency.empty()) {
368+
obj[P_CURRENCY] = value::string(currency);
369+
}
370+
if (!exchange.empty()) {
371+
obj[P_EXCHANGE] = value::string(exchange);
372+
}
373+
if (expiry != -1 && expiry != 0) {
374+
obj[P_EXPIRY] = (long long) expiry;
375+
}
376+
if (!strike.empty()) {
377+
obj[P_STRIKE] = value::string(strike);
378+
}
379+
if (!right.empty()) {
380+
obj[P_RIGHT] = value::string(right);
381+
}
382+
return post(CONTRACTS, obj)[P_ITEMS];
383+
}
317384

385+
value
386+
TradeClient::get_quote_contract(utility::string_t symbol, utility::string_t sec_type, utility::string_t expiry) {
387+
value obj = value::object(true);
388+
obj[P_ACCOUNT] = get_account_param();
389+
if (!symbol.empty()) {
390+
obj[P_SYMBOL] = value::string(symbol);
391+
}
392+
if (!sec_type.empty()) {
393+
obj[P_SEC_TYPE] = value::string(sec_type);
394+
}
395+
if (!expiry.empty()) {
396+
obj[P_EXPIRY] = value::string(expiry);
397+
}
398+
return post(QUOTE_CONTRACT, obj)[P_ITEMS];
399+
}
400+
401+
value TradeClient::place_order(value &order) {
318402
value obj = value::object(true);
319403
for (const auto &kvp: order.as_object()) {
320404
obj[kvp.first] = kvp.second;
@@ -535,5 +619,125 @@ namespace TIGER_API {
535619
return res;
536620
}
537621

622+
value TradeClient::get_estimate_tradable_quantity(Order &order, utility::string_t seg_type) {
623+
value obj = value::object(true);
624+
obj[P_ACCOUNT] = get_account_param(order.account);
625+
obj[U("seg_type")] = value::string(seg_type);
626+
Contract contract = order.contract;
627+
if (!contract.symbol.empty()) {
628+
obj[U("symbol")] = value::string(contract.symbol);
629+
}
630+
if (!contract.sec_type.empty()) {
631+
obj[U("sec_type")] = value::string(contract.sec_type);
632+
}
633+
if (!contract.expiry.empty()) {
634+
obj[U("expiry")] = value::string(contract.expiry);
635+
}
636+
if (!contract.strike.empty()) {
637+
obj[U("strike")] = value::string(contract.strike);
638+
}
639+
if (!contract.right.empty()) {
640+
obj[U("right")] = value::string(contract.right);
641+
}
642+
if (!order.order_type.empty()) {
643+
obj[U("order_type")] = value::string(order.order_type);
644+
}
645+
if (!order.action.empty()) {
646+
obj[U("action")] = value::string(order.action);
647+
}
648+
if (order.total_quantity) {
649+
obj[U("total_quantity")] = order.total_quantity;
650+
}
651+
if (order.limit_price != 0) {
652+
obj[U("limit_price")] = order.limit_price;
653+
}
654+
if (order.aux_price != 0) {
655+
obj[U("stop_price")] = order.aux_price;
656+
}
657+
return post(ESTIMATE_TRADABLE_QUANTITY, obj);
658+
}
659+
660+
value TradeClient::get_analytics_asset(utility::string_t account, utility::string_t start_date,
661+
utility::string_t end_date, utility::string_t seg_type,
662+
utility::string_t currency, utility::string_t sub_account) {
663+
value obj = value::object(true);
664+
obj[P_ACCOUNT] = get_account_param(account);
665+
if (!start_date.empty()) {
666+
obj[U("start_date")] = value::string(start_date);
667+
}
668+
if (!end_date.empty()) {
669+
obj[U("end_date")] = value::string(end_date);
670+
}
671+
if (!seg_type.empty()) {
672+
obj[U("seg_type")] = value::string(seg_type);
673+
}
674+
if (!currency.empty()) {
675+
obj[U("currency")] = value::string(currency);
676+
}
677+
if (!sub_account.empty()) {
678+
obj[U("sub_account")] = value::string(sub_account);
679+
}
680+
return post(ANALYTICS_ASSET, obj);
681+
}
682+
683+
value TradeClient::get_segment_fund_history(int limit) {
684+
value obj = value::object(true);
685+
obj[P_ACCOUNT] = get_account_param();
686+
if (limit > 0) {
687+
obj[U("limit")] = limit;
688+
}
689+
return post(SEGMENT_FUND_HISTORY, obj);
690+
}
691+
692+
value TradeClient::get_segment_fund_available(utility::string_t from_segment, utility::string_t currency) {
693+
value obj = value::object(true);
694+
obj[P_ACCOUNT] = get_account_param();
695+
if (!from_segment.empty()) {
696+
obj[U("from_segment")] = value::string(from_segment);
697+
}
698+
if (!currency.empty()) {
699+
obj[U("currency")] = value::string(currency);
700+
}
701+
return post(SEGMENT_FUND_AVAILABLE, obj);
702+
}
703+
704+
value
705+
TradeClient::transfer_segment_fund(utility::string_t from_segment, utility::string_t to_segment, double amount,
706+
utility::string_t currency) {
707+
value obj = value::object(true);
708+
obj[P_ACCOUNT] = get_account_param();
709+
if (!from_segment.empty()) {
710+
obj[U("from_segment")] = value::string(from_segment);
711+
}
712+
if (!to_segment.empty()) {
713+
obj[U("to_segment")] = value::string(to_segment);
714+
}
715+
if (!currency.empty()) {
716+
obj[U("currency")] = value::string(currency);
717+
}
718+
if (amount != 0) {
719+
obj[U("amount")] = amount;
720+
}
721+
return post(TRANSFER_SEGMENT_FUND, obj);
722+
}
723+
724+
value TradeClient::place_forex_order(utility::string_t seg_type, utility::string_t source_currency,
725+
utility::string_t target_currency, double source_amount) {
726+
value obj = value::object(true);
727+
obj[P_ACCOUNT] = get_account_param();
728+
if (!seg_type.empty()) {
729+
obj[U("seg_type")] = value::string(seg_type);
730+
}
731+
if (!source_currency.empty()) {
732+
obj[U("source_currency")] = value::string(source_currency);
733+
}
734+
if (!target_currency.empty()) {
735+
obj[U("target_currency")] = value::string(target_currency);
736+
}
737+
if (source_amount != 0) {
738+
obj[U("source_amount")] = source_amount;
739+
}
740+
return post(PLACE_FOREX_ORDER, obj);
741+
}
538742

539743
}

0 commit comments

Comments
 (0)