Skip to content

Commit b51a827

Browse files
committed
mod demo
1 parent 5adc3ec commit b51a827

File tree

4 files changed

+88
-72
lines changed

4 files changed

+88
-72
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,26 @@ void signal_handler(int signal)
455455

456456
class TestPushClient {
457457
private:
458-
// 保存 push_client 实例作为成员变量
459458
std::shared_ptr<IPushClient> push_client;
459+
std::vector<std::string> symbols;
460460

461461
public:
462-
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {}
462+
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {
463+
std::vector<std::string> hk_option_symbols = {"TCH.HK 20241230 410.00 CALL"};
464+
std::vector<std::string> future_symbols = {"CL2412"};
465+
symbols = future_symbols;
466+
}
463467

464-
// 将回调方法改为非静态成员函数
465468
void connected_callback() {
466469
ucout << "Connected to push server" << std::endl;
470+
push_client->subscribe_position(push_client->get_client_config().account);
471+
push_client->subscribe_order(push_client->get_client_config().account);
472+
push_client->subscribe_asset(push_client->get_client_config().account);
473+
// push_client->query_subscribed_symbols();
474+
push_client->subscribe_quote(symbols);
475+
// push_client->subscribe_kline(symbols);
476+
// push_client->subscribe_quote_depth(symbols);
477+
push_client->subscribe_tick(symbols);
467478
}
468479

469480
void position_changed_callback(const tigeropen::push::pb::PositionData& data) {
@@ -473,27 +484,61 @@ class TestPushClient {
473484
}
474485

475486
void order_changed_callback(const tigeropen::push::pb::OrderStatusData& data) {
487+
// example: {"dataType":"OrderStatus","orderStatusData":{"id":"37129891133115200","account":"123123123","symbol":"PDD","identifier":"PDD","multiplier":1,
488+
// "action":"BUY","market":"US","currency":"USD","segType":"S","secType":"STK","orderType":"LMT","isLong":true,"totalQuantity":"1","limitPrice":50,
489+
// "status":"PendingSubmit","replaceStatus":"NONE","cancelStatus":"NONE","outsideRth":true,"canModify":true,"canCancel":true,"name":"PDD Holdings","source":"openapi","openTime":"1732177851000","timestamp":"1732177851874"}}
476490
ucout << "Order changed:" << std::endl;
477491
ucout << "- id: " << data.id() << std::endl;
492+
ucout << "- status: " << data.status() << std::endl;
493+
ucout << "- avgfillprice: " << data.avgfillprice() << std::endl;
478494
}
479495

480496
void asset_changed_callback(const tigeropen::push::pb::AssetData& data) {
497+
// example: {"dataType":"Asset","assetData":{"account":"111111111111","segType":"S","availableFunds":797832.55572773679,
498+
// "excessLiquidity":826227.79308567208,"netLiquidation":944515.55984458746,"equityWithLoan":944494.85984458751,"buyingPower":3191330.2229109472,
499+
// "cashBalance":656046.6059349241,"grossPositionValue":288448.25390966347,"initMarginReq":146662.30411685078,"maintMarginReq":118287.76675891539,"timestamp":"1732177851891"}}
481500
ucout << "Asset changed:" << std::endl;
482501
ucout << "- cashbalance: " << data.cashbalance() << std::endl;
502+
ucout << "- netliquidation: " << data.netliquidation() << std::endl;
483503
}
484504

485505
void tick_changed_callback(const TradeTick& data) {
486506
ucout << "TradeTick changed: " << std::endl;
487507
ucout << "- data: " << data.to_string() << std::endl;
488508
}
489509

510+
void full_tick_changed_callback(const tigeropen::push::pb::TickData& data) {
511+
ucout << "Full TickData changed: " << std::endl;
512+
ucout << "- symbol: " << data.symbol() << std::endl;
513+
ucout << "- tick size: " << data.ticks_size() << std::endl;
514+
}
515+
516+
void query_subscribed_symbols_changed_callback(const tigeropen::push::pb::Response& data) {
517+
ucout << "QuerySubscribedSymbols changed: " << std::endl;
518+
ucout << "- data: " << data.msg() << std::endl;
519+
}
520+
490521
void quote_changed_callback(const tigeropen::push::pb::QuoteBasicData& data) {
491522
ucout << "BasicQuote changed: " << std::endl;
492523
ucout << "- symbol: " << data.symbol() << std::endl;
493524
ucout << "- latestPrice: " << data.latestprice() << std::endl;
494525
ucout << "- volume: " << data.volume() << std::endl;
495526
}
496527

528+
void quote_bbo_changed_callback(const tigeropen::push::pb::QuoteBBOData& data) {
529+
ucout << "BBOQuote changed: " << std::endl;
530+
ucout << "- symbol: " << data.symbol() << std::endl;
531+
ucout << "- bidPrice: " << data.bidprice() << std::endl;
532+
ucout << "- askPrice: " << data.askprice() << std::endl;
533+
}
534+
535+
void quote_depth_changed_callback(const tigeropen::push::pb::QuoteDepthData& data) {
536+
ucout << "QuoteDepth changed: " << std::endl;
537+
ucout << "- symbol: " << data.symbol() << std::endl;
538+
ucout << "- ask price size: " << data.ask().price_size() << std::endl;
539+
ucout << "- bid price size: " << data.bid().price_size() << std::endl;
540+
}
541+
497542
void kline_changed_callback(const tigeropen::push::pb::KlineData& data) {
498543
ucout << "Kline changed: " << std::endl;
499544
ucout << "- symbol: " << data.symbol() << std::endl;
@@ -503,27 +548,21 @@ class TestPushClient {
503548
ucout << "- close: " << data.close() << std::endl;
504549
}
505550

506-
void start_test() {
507-
push_client->connect();
508-
509-
// sleep 10 seconds
510-
std::this_thread::sleep_for(std::chrono::seconds(10));
511-
512-
// 使用 std::bind 绑定成员函数
551+
void start_test(ClientConfig config) {
513552
push_client->set_connected_callback(std::bind(&TestPushClient::connected_callback, this));
514553
push_client->set_position_changed_callback(std::bind(&TestPushClient::position_changed_callback, this, std::placeholders::_1));
515554
push_client->set_order_changed_callback(std::bind(&TestPushClient::order_changed_callback, this, std::placeholders::_1));
516555
push_client->set_asset_changed_callback(std::bind(&TestPushClient::asset_changed_callback, this, std::placeholders::_1));
517556
push_client->set_tick_changed_callback(std::bind(&TestPushClient::tick_changed_callback, this, std::placeholders::_1));
557+
push_client->set_full_tick_changed_callback(std::bind(&TestPushClient::full_tick_changed_callback, this, std::placeholders::_1));
558+
push_client->set_query_subscribed_symbols_changed_callback(std::bind(&TestPushClient::query_subscribed_symbols_changed_callback, this, std::placeholders::_1));
518559
push_client->set_quote_changed_callback(std::bind(&TestPushClient::quote_changed_callback, this, std::placeholders::_1));
560+
push_client->set_quote_bbo_changed_callback(std::bind(&TestPushClient::quote_bbo_changed_callback, this, std::placeholders::_1));
561+
push_client->set_quote_depth_changed_callback(std::bind(&TestPushClient::quote_depth_changed_callback, this, std::placeholders::_1));
519562
push_client->set_kline_changed_callback(std::bind(&TestPushClient::kline_changed_callback, this, std::placeholders::_1));
520-
push_client->subscribe_position("");
521-
push_client->subscribe_order("");
522-
push_client->subscribe_asset("");
523563

524-
std::vector<std::string> symbols = {"NVDA", "00700"};
525-
push_client->subscribe_tick(symbols);
526-
push_client->subscribe_quote(symbols);
564+
565+
push_client->connect();
527566

528567
std::signal(SIGINT, signal_handler); //Ctrl+C
529568
std::signal(SIGTERM, signal_handler); //kill
@@ -532,12 +571,19 @@ class TestPushClient {
532571
std::this_thread::sleep_for(std::chrono::seconds(1));
533572
}
534573

574+
push_client->unsubscribe_quote(symbols);
575+
push_client->unsubscribe_kline(symbols);
576+
push_client->unsubscribe_quote_depth(symbols);
577+
push_client->unsubscribe_tick(symbols);
578+
push_client->unsubscribe_asset(config.account);
579+
push_client->unsubscribe_position(config.account);
580+
push_client->unsubscribe_order(config.account);
535581
push_client->disconnect();
536582
}
537583

538-
static void test_push_client(std::shared_ptr<IPushClient> push_client) {
584+
static void test_push_client(std::shared_ptr<IPushClient> push_client, ClientConfig config) {
539585
TestPushClient test(push_client);
540-
test.start_test();
586+
test.start_test(config);
541587
}
542588
};
543589

@@ -551,35 +597,18 @@ int main()
551597
// config.tiger_id = U("");
552598
// config.account = U("");
553599

554-
600+
config.use_full_tick = true;
555601

556602
#else
557603
config.private_key = U("");
558604
config.tiger_id = U("");
559605
config.account = U("");
560606
#endif
561607

562-
563-
564-
// std::string input;
565-
// while (true)
566-
// {
567-
// std::cout << "Enter command (type 'exit' to quit): ";
568-
// std::getline(std::cin, input);
569-
//
570-
// if (input == "exit") {
571-
// std::cout << "Exiting loop." << std::endl;
572-
// // push_client->disconnect();
573-
// break;
574-
// }
575-
// // Process other commands or input here
576-
// std::cout << "You entered: " << input << std::endl;
577-
// }
578-
579608
//config.lang = U("en_US");
580609

581610
auto push_client = IPushClient::create_push_client(config);
582-
TestPushClient::test_push_client(push_client);
611+
TestPushClient::test_push_client(push_client, config);
583612
/**
584613
* QuoteClient
585614
*/

include/tigerapi/push_client.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace TIGER_API
2525
{
2626
public:
2727
static std::shared_ptr<IPushClient> create_push_client(const TIGER_API::ClientConfig& client_config);
28+
virtual const ClientConfig& get_client_config() const = 0;
29+
2830
virtual void connect() = 0;
2931
virtual void disconnect() = 0;
3032

@@ -56,15 +58,12 @@ namespace TIGER_API
5658
virtual void query_subscribed_symbols() = 0;
5759

5860
virtual void set_quote_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBasicData&)>& cb) = 0;
61+
virtual void set_quote_bbo_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBBOData&)>& cb) = 0;
5962
virtual bool subscribe_quote(const std::vector<std::string>& symbols) = 0;
6063
virtual bool subscribe_future_quote(const std::vector<std::string>& symbols) = 0;
6164
virtual bool subscribe_option_quote(const std::vector<std::string>& symbols) = 0;
6265
virtual bool unsubscribe_quote(const std::vector<std::string>& symbols) = 0;
6366

64-
virtual void set_quote_bbo_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBBOData&)>& cb) = 0;
65-
virtual bool subscribe_quote_bbo(const std::vector<std::string>& symbols) = 0;
66-
virtual bool unsubscribe_quote_bbo(const std::vector<std::string>& symbols) = 0;
67-
6867
virtual void set_quote_depth_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteDepthData&)>& cb) = 0;
6968
virtual bool subscribe_quote_depth(const std::vector<std::string>& symbols) = 0;
7069
virtual bool unsubscribe_quote_depth(const std::vector<std::string>& symbols) = 0;
@@ -74,9 +73,6 @@ namespace TIGER_API
7473
virtual bool unsubscribe_kline(const std::vector<std::string>& symbols) = 0;
7574

7675
virtual void set_full_tick_changed_callback(const std::function<void(const tigeropen::push::pb::TickData&)>& cb) = 0;
77-
virtual bool subscribe_full_tick(const std::vector<std::string>& symbols) = 0;
78-
virtual bool unsubscribe_full_tick(const std::vector<std::string>& symbols) = 0;
79-
8076
virtual void set_tick_changed_callback(const std::function<void(const TradeTick&)>& cb) = 0;
8177
virtual bool subscribe_tick(const std::vector<std::string>& symbols) = 0;
8278
virtual bool unsubscribe_tick(const std::vector<std::string>& symbols) = 0;
@@ -91,7 +87,9 @@ namespace TIGER_API
9187
virtual void set_option_top_changed_callback(const std::function<void(const tigeropen::push::pb::OptionTopData&)>& cb) = 0;
9288
virtual bool subscribe_option_top(const std::string& market) = 0;
9389
virtual bool unsubscribe_option_top(const std::string& market) = 0;
90+
9491
};
9592
}
9693

9794
#endif // PUSH_CLIENT_H
95+

include/tigerapi/push_socket/push_client_impl.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,12 @@ namespace TIGER_API
6666
virtual void set_query_subscribed_symbols_changed_callback(const std::function<void(const tigeropen::push::pb::Response& query_subscribed_symbols_response)>& cb) override;
6767
virtual void query_subscribed_symbols() override;
6868
virtual void set_quote_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBasicData&)>& cb) override;
69+
virtual void set_quote_bbo_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBBOData&)>& cb) override;
6970
virtual bool subscribe_quote(const std::vector<std::string>& symbols) override;
7071
virtual bool subscribe_future_quote(const std::vector<std::string>& symbols) override;
7172
virtual bool subscribe_option_quote(const std::vector<std::string>& symbols) override;
7273
virtual bool unsubscribe_quote(const std::vector<std::string>& symbols) override;
7374

74-
virtual void set_quote_bbo_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteBBOData&)>& cb) override;
75-
virtual bool subscribe_quote_bbo(const std::vector<std::string>& symbols) override;
76-
virtual bool unsubscribe_quote_bbo(const std::vector<std::string>& symbols) override;
77-
7875
virtual void set_quote_depth_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteDepthData&)>& cb) override;
7976
virtual bool subscribe_quote_depth(const std::vector<std::string>& symbols) override;
8077
virtual bool unsubscribe_quote_depth(const std::vector<std::string>& symbols) override;
@@ -84,9 +81,6 @@ namespace TIGER_API
8481
virtual bool unsubscribe_kline(const std::vector<std::string>& symbols) override;
8582

8683
virtual void set_full_tick_changed_callback(const std::function<void(const tigeropen::push::pb::TickData&)>& cb) override;
87-
virtual bool subscribe_full_tick(const std::vector<std::string>& symbols) override;
88-
virtual bool unsubscribe_full_tick(const std::vector<std::string>& symbols) override;
89-
9084
virtual void set_tick_changed_callback(const std::function<void(const TradeTick&)>& cb) override;
9185
virtual bool subscribe_tick(const std::vector<std::string>& symbols) override;
9286
virtual bool unsubscribe_tick(const std::vector<std::string>& symbols) override;
@@ -102,6 +96,8 @@ namespace TIGER_API
10296
virtual bool subscribe_option_top(const std::string& market) override;
10397
virtual bool unsubscribe_option_top(const std::string& market) override;
10498

99+
virtual const ClientConfig& get_client_config() const override;
100+
105101
private:
106102
bool send_frame(const tigeropen::push::pb::Request& request);
107103
void do_write(const std::string& frame);

src/push_socket/push_client_impl.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,6 @@ void TIGER_API::PushClientImpl::set_quote_bbo_changed_callback(const std::functi
192192
quote_bbo_changed_ = cb;
193193
}
194194

195-
bool TIGER_API::PushClientImpl::subscribe_quote_bbo(const std::vector<std::string>& symbols)
196-
{
197-
return send_quote_request(tigeropen::push::pb::SocketCommon_Command_SUBSCRIBE, tigeropen::push::pb::SocketCommon_DataType::SocketCommon_DataType_Quote, symbols, "");
198-
}
199-
200-
bool TIGER_API::PushClientImpl::unsubscribe_quote_bbo(const std::vector<std::string>& symbols)
201-
{
202-
return send_quote_request(tigeropen::push::pb::SocketCommon_Command_UNSUBSCRIBE, tigeropen::push::pb::SocketCommon_DataType::SocketCommon_DataType_Quote, symbols, "");
203-
}
204-
205195
void TIGER_API::PushClientImpl::set_quote_depth_changed_callback(const std::function<void(const tigeropen::push::pb::QuoteDepthData&)>& cb)
206196
{
207197
quote_depth_changed_ = cb;
@@ -237,16 +227,6 @@ void TIGER_API::PushClientImpl::set_full_tick_changed_callback(const std::functi
237227
full_tick_changed_ = cb;
238228
}
239229

240-
bool TIGER_API::PushClientImpl::subscribe_full_tick(const std::vector<std::string>& symbols)
241-
{
242-
return send_quote_request(tigeropen::push::pb::SocketCommon_Command_SUBSCRIBE, tigeropen::push::pb::SocketCommon_DataType::SocketCommon_DataType_TradeTick, symbols, "");
243-
}
244-
245-
bool TIGER_API::PushClientImpl::unsubscribe_full_tick(const std::vector<std::string>& symbols)
246-
{
247-
return send_quote_request(tigeropen::push::pb::SocketCommon_Command_UNSUBSCRIBE, tigeropen::push::pb::SocketCommon_DataType::SocketCommon_DataType_TradeTick, symbols, "");
248-
}
249-
250230
void TIGER_API::PushClientImpl::set_tick_changed_callback(const std::function<void(const TradeTick&)>& cb)
251231
{
252232
tick_changed_ = cb;
@@ -389,7 +369,16 @@ void TIGER_API::PushClientImpl::do_disconnect()
389369
void TIGER_API::PushClientImpl::on_message(const std::shared_ptr<tigeropen::push::pb::Response>& frame)
390370
{
391371
try {
392-
if (frame->code() == static_cast<int>(ResponseType::GET_SUB_SYMBOLS_END)) {
372+
if (frame->command() == tigeropen::push::pb::SocketCommon_Command_CONNECTED) {
373+
LOG(DEBUG) << "recv connected";
374+
}
375+
else if (frame->command() == tigeropen::push::pb::SocketCommon_Command_DISCONNECT) {
376+
LOG(DEBUG) << "disconnected";
377+
}
378+
else if (frame->command() == tigeropen::push::pb::SocketCommon_Command_HEARTBEAT) {
379+
LOG(DEBUG) << "heartbeat";
380+
}
381+
else if (frame->code() == static_cast<int>(ResponseType::GET_SUB_SYMBOLS_END)) {
393382
if (query_subscribed_symbols_changed_) {
394383
query_subscribed_symbols_changed_(*frame);
395384
}
@@ -766,3 +755,7 @@ shared_ptr<TIGER_API::TradeTick> TIGER_API::PushClientImpl::convert_tick(const t
766755

767756
return result;
768757
}
758+
759+
const TIGER_API::ClientConfig& TIGER_API::PushClientImpl::get_client_config() const {
760+
return client_config_;
761+
}

0 commit comments

Comments
 (0)