Skip to content

Commit a93d455

Browse files
committed
mod demo
1 parent 31c2c58 commit a93d455

File tree

4 files changed

+131
-58
lines changed

4 files changed

+131
-58
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ install(DIRECTORY include/
133133
DESTINATION "include"
134134
FILES_MATCHING
135135
PATTERN "*.h"
136-
PATTERN "*.cc"
136+
# PATTERN "*.cc"
137137
)
138138

139139
install(TARGETS ${PROJECT_NAME}

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 126 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include "tigerapi/utils.h"
1111
#include "cpprest/details/basic_types.h"
1212
#include "tigerapi/price_util.h"
13-
1413
#include "tigerapi/easylogging++.h"
14+
#include <chrono>
15+
#include <thread>
1516

16-
//INITIALIZE_EASYLOGGINGPP
1717

1818
using namespace std;
1919
using namespace web;
@@ -440,67 +440,146 @@ class TestTigerApi {
440440
tigerapi->post(POSITIONS, obj);
441441
}
442442
};
443-
//
444-
void position_changed_callback(const tigeropen::push::pb::PositionData& data) {
445-
ucout << "Position changed:" << std::endl;
446-
ucout << "- symbol: " << utility::conversions::to_string_t(data.symbol()) << std::endl;
447-
ucout << "- positionqty: " << data.positionqty() << std::endl;
448-
ucout << "- salableqty: " << data.salableqty() << std::endl;
449-
ucout << "- marketvalue: " << data.marketvalue() << std::endl;
450-
ucout << "- averagecost: " << data.averagecost() << std::endl;
451-
}
452-
453-
void order_changed_callback(const tigeropen::push::pb::OrderStatusData& data) {
454-
ucout << "Order changed:" << std::endl;
455-
ucout << "- id: " << data.id() << std::endl;
456-
}
457443

458-
void asset_changed_callback(const tigeropen::push::pb::AssetData& data) {
459-
ucout << "Asset changed:" << std::endl;
460-
ucout << "- cashbalance: " << data.cashbalance() << std::endl;
461-
}
462444

463445
std::atomic<bool> keep_running(true);
446+
464447
void signal_handler(int signal)
465448
{
466-
if (signal == SIGINT || signal == SIGTERM)
449+
if (signal == SIGINT || signal == SIGTERM)
467450
{
468-
keep_running = false;
469-
}
451+
keep_running = false;
452+
}
470453
}
471454

455+
456+
class TestPushClient {
457+
private:
458+
// 保存 push_client 实例作为成员变量
459+
std::shared_ptr<IPushClient> push_client;
460+
461+
public:
462+
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {}
463+
464+
// 将回调方法改为非静态成员函数
465+
void connected_callback() {
466+
ucout << "Connected to push server" << std::endl;
467+
}
468+
469+
void position_changed_callback(const tigeropen::push::pb::PositionData& data) {
470+
ucout << "Position changed:" << std::endl;
471+
ucout << "- symbol: " << data.symbol() << std::endl;
472+
ucout << "- positionqty: " << data.positionqty() << std::endl;
473+
}
474+
475+
void order_changed_callback(const tigeropen::push::pb::OrderStatusData& data) {
476+
ucout << "Order changed:" << std::endl;
477+
ucout << "- id: " << data.id() << std::endl;
478+
}
479+
480+
void asset_changed_callback(const tigeropen::push::pb::AssetData& data) {
481+
ucout << "Asset changed:" << std::endl;
482+
ucout << "- cashbalance: " << data.cashbalance() << std::endl;
483+
}
484+
485+
void tick_changed_callback(const TradeTick& data) {
486+
ucout << "TradeTick changed: " << std::endl;
487+
ucout << "- data: " << data.to_string() << std::endl;
488+
}
489+
490+
void quote_changed_callback(const tigeropen::push::pb::QuoteBasicData& data) {
491+
ucout << "BasicQuote changed: " << std::endl;
492+
ucout << "- symbol: " << data.symbol() << std::endl;
493+
ucout << "- latestPrice: " << data.latestprice() << std::endl;
494+
ucout << "- volume: " << data.volume() << std::endl;
495+
}
496+
497+
void kline_changed_callback(const tigeropen::push::pb::KlineData& data) {
498+
ucout << "Kline changed: " << std::endl;
499+
ucout << "- symbol: " << data.symbol() << std::endl;
500+
ucout << "- open: " << data.open() << std::endl;
501+
ucout << "- high: " << data.high() << std::endl;
502+
ucout << "- low: " << data.low() << std::endl;
503+
ucout << "- close: " << data.close() << std::endl;
504+
}
505+
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 绑定成员函数
513+
push_client->set_connected_callback(std::bind(&TestPushClient::connected_callback, this));
514+
push_client->set_position_changed_callback(std::bind(&TestPushClient::position_changed_callback, this, std::placeholders::_1));
515+
push_client->set_order_changed_callback(std::bind(&TestPushClient::order_changed_callback, this, std::placeholders::_1));
516+
push_client->set_asset_changed_callback(std::bind(&TestPushClient::asset_changed_callback, this, std::placeholders::_1));
517+
push_client->set_tick_changed_callback(std::bind(&TestPushClient::tick_changed_callback, this, std::placeholders::_1));
518+
push_client->set_quote_changed_callback(std::bind(&TestPushClient::quote_changed_callback, this, std::placeholders::_1));
519+
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("");
523+
524+
std::vector<std::string> symbols = {"NVDA", "00700"};
525+
push_client->subscribe_tick(symbols);
526+
push_client->subscribe_quote(symbols);
527+
528+
std::signal(SIGINT, signal_handler); //Ctrl+C
529+
std::signal(SIGTERM, signal_handler); //kill
530+
while (keep_running)
531+
{
532+
std::this_thread::sleep_for(std::chrono::seconds(1));
533+
}
534+
535+
push_client->disconnect();
536+
}
537+
538+
static void test_push_client(std::shared_ptr<IPushClient> push_client) {
539+
TestPushClient test(push_client);
540+
test.start_test();
541+
}
542+
};
543+
544+
472545
int main()
473546
{
474547
/************************** set config **********************/
475-
bool sandbox_debug = false;
476-
ClientConfig config = ClientConfig(sandbox_debug);
477-
if (sandbox_debug)
478-
{
479-
config.private_key = U("");
480-
config.tiger_id = U("");
481-
config.account = U("");
482-
}
483-
else
484-
{
485-
config.private_key = U("");
486-
config.tiger_id = U("");
487-
config.account = U("");
488-
}
548+
ClientConfig config = ClientConfig();
549+
#if 1
550+
// config.private_key = U("");
551+
// config.tiger_id = U("");
552+
// config.account = U("");
489553

490-
auto push_client = IPushClient::create_push_client(config);
491-
push_client->set_position_changed_callback(std::function<void(const tigeropen::push::pb::PositionData&)>(position_changed_callback));
492-
push_client->set_order_changed_callback(std::function<void(const tigeropen::push::pb::OrderStatusData&)>(order_changed_callback));
493-
push_client->set_asset_changed_callback(std::function<void(const tigeropen::push::pb::AssetData&)>(asset_changed_callback));
494554

495-
push_client->subscribe_position("");
496-
push_client->subscribe_order("");
497-
push_client->subscribe_asset("");
498-
499-
push_client->connect();
500555

501-
//config.lang = U("en_US");
556+
#else
557+
config.private_key = U("");
558+
config.tiger_id = U("");
559+
config.account = U("");
560+
#endif
502561

503562

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+
579+
//config.lang = U("en_US");
580+
581+
auto push_client = IPushClient::create_push_client(config);
582+
TestPushClient::test_push_client(push_client);
504583
/**
505584
* QuoteClient
506585
*/
@@ -519,14 +598,7 @@ int main()
519598
// std::shared_ptr<TigerClient> tigerapi = std::make_shared<TigerClient>(config);
520599
// TestTigerApi::test_grab_quote_permission(tigerapi);
521600

522-
std::signal(SIGINT, signal_handler); //Ctrl+C
523-
std::signal(SIGTERM, signal_handler); //kill
524-
while (keep_running)
525-
{
526-
std::this_thread::sleep_for(std::chrono::seconds(1));
527-
}
528601

529-
push_client->disconnect();
530602

531603
return 0;
532604
}

src/push_socket/push_client_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ TIGER_API::PushClientImpl::~PushClientImpl()
3131
TIGER_API::PushClientImpl::PushClientImpl(const TIGER_API::ClientConfig& client_config)
3232
{
3333
socket_ = PushSocket::create_push_socket(&io_service_, client_config);
34+
socket_->set_on_message_callback(
35+
[this](auto && message) { on_message(std::forward<decltype(message)>(message)); });
3436
client_config_ = client_config;
3537
}
3638

src/push_socket/push_socket.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ void TIGER_API::PushSocket::handle_read_head(const boost::system::error_code& er
378378
}
379379
else
380380
{
381-
#if 1
381+
#if 0
382382
for (size_t i = 0; i < bytes_transferred; ++i)
383383
{
384384
std::bitset<8> binary(head_buff_[i]);
@@ -503,8 +503,7 @@ void TIGER_API::PushSocket::read_body(size_t frame_len)
503503
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, recv_buff, page_num, frame_len));
504504
}
505505

506-
void TIGER_API::PushSocket::dispatch_connected_callback()
507-
{
506+
void TIGER_API::PushSocket::dispatch_connected_callback() {
508507
if (connected_callback_)
509508
{
510509
connected_callback_();

0 commit comments

Comments
 (0)