21#include <algotradelib/kis>
22#include <algotradelib/tossinvest>
24#include <boost/asio/ssl/context.hpp>
34namespace ssl = boost::asio::ssl;
38using algotradelib::BlockingSession;
39using algotradelib::BrokerCaps;
40using algotradelib::BrokerError;
41using algotradelib::HoldingsSnapshot;
44std::string mask_account_number(std::string_view account_number) {
45 if (account_number.size() <= 4) {
46 return std::string(account_number.size(),
'*');
48 return std::string(account_number.size() - 4,
'*') + std::string(account_number.substr(account_number.size() - 4));
52void print_error(std::string_view operation,
const BrokerError& error) {
53 std::cerr << operation <<
" failed";
54 if (!error.broker_code_.empty()) {
55 std::cerr <<
" [" << error.broker_code_ <<
']';
57 if (error.http_status_ != 0) {
58 std::cerr <<
" HTTP " << error.http_status_;
60 if (!error.message_.empty()) {
61 std::cerr <<
": " << error.message_;
63 if (error.retry_after_) {
64 std::cerr <<
" (retry after " << error.retry_after_->count() <<
"s)";
70void print_holdings(
const HoldingsSnapshot& snapshot) {
71 std::cout <<
"positions: " << snapshot.positions_.size() <<
'\n';
72 for (
const auto& position : snapshot.positions_) {
74 <<
" " << position.instrument_.symbol_ <<
" " << position.name_ <<
" quantity=" << position.quantity_.str()
75 <<
" market=" << position.market_value_.amount_.str() <<
' ' << position.market_value_.currency_.str() <<
'\n';
77 for (
const auto& total : snapshot.totals_) {
79 <<
" total " << total.currency_.str() <<
" purchase=" << total.purchase_value_.str()
80 <<
" market=" << total.market_value_.str() <<
" unrealized=" << total.unrealized_profit_loss_.str() <<
'\n';
85template <
typename Broker>
86void print_working_orders(
const algotradelib::WorkingOrders<Broker>& orders) {
87 std::cout <<
"working orders: " << orders.
orders_.size() <<
'\n';
88 for (
const auto& working : orders.
orders_) {
90 <<
" " << working.order_.instrument_.symbol_ <<
" reference=" << working.ref_.str() <<
" ordered="
91 << working.order_.quantity_.str() <<
" filled=" << working.order_.execution_.filled_quantity_.str();
92 if (working.amendable_quantity_) {
93 std::cout <<
" amendable=" << working.amendable_quantity_->str();
95 std::cout <<
" amendable=not reported";
108template <
typename Client>
109int report(BlockingSession<Client>& session) {
110 using Caps = BrokerCaps<Client>;
112 std::cout <<
"broker: " << session.broker_name() <<
'\n';
113 std::cout <<
"reports an amendable quantity: " << (Caps::has_amendable_quantity ?
"yes" :
"no") <<
'\n';
114 std::cout <<
"accepts an idempotency key: " << (Caps::has_client_order_id ?
"yes" :
"no") <<
'\n';
116 auto holdings = session.holdings();
118 print_error(
"holdings request", holdings.error());
121 print_holdings(holdings.value());
123 if constexpr (Caps::has_working_orders) {
124 auto working = session.working_orders();
129 print_error(
"working-order inquiry", working.error());
131 print_working_orders(working.value());
139int report_kis(ssl::context& tls) {
140 namespace kis = algotradelib::kis;
142 kis::KisConfig config;
143 kis::KisAccount account;
146 std::cout <<
"environment: " << (config.is_demo_ ?
"demo" :
"live") <<
'\n';
148 auto client = std::make_shared<kis::KisBlockingClient>(tls, std::move(config), kis_credentials::paced_options());
149 BlockingSession<kis::KisBlockingClient> session(std::move(client), std::move(account));
150 return report(session);
154int report_tossinvest(ssl::context& tls,
const char* requested_sequence) {
155 namespace toss = algotradelib::tossinvest;
157 toss::TossConfig config;
160 auto client = std::make_shared<toss::TossBlockingClient>(tls, std::move(config));
162 std::int64_t sequence = 0;
163 if (requested_sequence) {
164 sequence = std::strtoll(requested_sequence,
nullptr, 10);
168 print_error(
"account discovery", accounts.error());
171 if (accounts.value().empty()) {
172 std::cerr <<
"no Toss accounts are available for these credentials\n";
175 for (
const auto& account : accounts.value()) {
177 <<
"account " << mask_account_number(account.display_number_) <<
" type=" << account.type_
178 <<
" sequence=" << account.selector_ <<
'\n';
180 sequence = accounts.value().front().selector_;
183 BlockingSession<toss::TossBlockingClient> session(std::move(client), sequence);
184 return report(session);
189int main(
int argc,
char** argv) {
190 const std::string broker = argc > 1 ? argv[1] :
"";
191 if (broker !=
"kis" && broker !=
"tossinvest") {
192 std::cerr <<
"usage: example-portable-holdings <kis|tossinvest> [account-sequence]\n";
197 ssl::context tls(ssl::context::tls_client);
198 tls.set_default_verify_paths();
199 tls.set_verify_mode(ssl::verify_peer);
201 if (broker ==
"kis") {
202 return report_kis(tls);
204 return report_tossinvest(tls, argc > 2 ? argv[2] :
nullptr);
205 }
catch (
const std::exception& exception) {
206 std::cerr <<
"example failed: " << exception.what() <<
'\n';
The examples' KIS credentials: one JSON file per account, or the environment.
void load(const std::optional< std::filesystem::path > &directory, algotradelib::kis::KisConfig &config, algotradelib::kis::KisAccount *account, std::ostream &out)
Loads the credentials (and account) KIS_DEMO chooses, and prints where from (a path or "environment v...
Result< std::vector< AccountInfo< Client > > > list_accounts(Client &client)
Lists a broker's accounts with a blocking client, before a session exists; requires BrokerCaps<Client...
std::vector< WorkingOrder< Broker > > orders_
The open orders.