algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-tossinvest-account.cpp
1#include <algotradelib/tossinvest>
2
3#include <boost/asio/ssl/context.hpp>
4
5#include <algorithm>
6#include <cstdint>
7#include <exception>
8#include <iostream>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <utility>
13
14namespace ssl = boost::asio::ssl;
15
16namespace {
17
18void print_error(std::string_view operation, const algotradelib::BrokerError& error) {
19 std::cerr << operation << " failed";
20 if (!error.broker_code_.empty()) {
21 std::cerr << " [" << error.broker_code_ << ']';
22 }
23 if (!error.request_id_.empty()) {
24 std::cerr << " request=" << error.request_id_;
25 }
26 if (error.http_status_ != 0) {
27 std::cerr << " HTTP " << error.http_status_;
28 }
29 if (!error.message_.empty()) {
30 std::cerr << ": " << error.message_;
31 }
32 std::cerr << '\n';
33}
34
35std::string mask_account_number(std::string_view account_number) {
36 if (account_number.size() <= 4) {
37 return std::string(account_number.size(), '*');
38 }
39 return std::string(account_number.size() - 4, '*') + std::string(account_number.substr(account_number.size() - 4));
40}
41
42void print_holdings(const algotradelib::HoldingsSnapshot& snapshot) {
43 std::cout << "positions: " << snapshot.positions_.size() << '\n';
44 for (const auto& position : snapshot.positions_) {
45 std::cout << position.instrument_.symbol_ << " " << position.name_ << '\n';
46 std::cout << " quantity: " << position.quantity_.str() << '\n';
47 std::cout
48 << " last price: " << position.last_price_.str() << ' ' << position.market_value_.currency_.str() << '\n';
49 std::cout
50 << " market value: " << position.market_value_.amount_.str() << ' ' << position.market_value_.currency_.str()
51 << '\n';
52 std::cout
53 << " unrealized P/L: " << position.unrealized_profit_loss_.amount_.str() << ' '
54 << position.unrealized_profit_loss_.currency_.str() << '\n';
55 }
56
57 std::cout << "totals: " << snapshot.totals_.size() << '\n';
58 for (const auto& total : snapshot.totals_) {
59 std::cout
60 << total.currency_.str() << " purchase=" << total.purchase_value_.str()
61 << " market=" << total.market_value_.str() << " unrealized=" << total.unrealized_profit_loss_.str() << '\n';
62 }
63}
64
65std::int64_t parse_account_sequence(const char* value) {
66 const std::string text(value);
67 std::size_t parsed = 0;
68 const auto sequence = std::stoll(text, &parsed);
69 if (parsed != text.size() || sequence <= 0) {
70 throw std::invalid_argument("account sequence must be a positive integer");
71 }
72 return sequence;
73}
74
75} // namespace
76
77int main(int argc, char* argv[]) {
78 try {
80 config.from_env();
81
82 ssl::context tls(ssl::context::tls_client);
83 tls.set_default_verify_paths();
84 tls.set_verify_mode(ssl::verify_peer);
85
86 algotradelib::tossinvest::TossBlockingClient client(tls, std::move(config));
87 auto accounts_result = client.accounts();
88 if (!accounts_result) {
89 print_error("account discovery", accounts_result.error());
90 return 1;
91 }
92
93 const auto& accounts = accounts_result.value();
94 std::cout << "accounts: " << accounts.size() << '\n';
95 for (const auto& account : accounts) {
96 std::cout
97 << account.sequence_ << " " << mask_account_number(account.account_number_) << " " << account.account_type_
98 << '\n';
99 }
100 if (accounts.empty()) {
101 return 0;
102 }
103
104 const std::int64_t selected_sequence = argc > 1 ? parse_account_sequence(argv[1]) : accounts.front().sequence_;
105 const auto selected = std::find_if(
106 accounts.begin(), accounts.end(), [selected_sequence](const algotradelib::tossinvest::TossAccount& account) {
107 return account.sequence_ == selected_sequence;
108 }
109 );
110 if (selected == accounts.end()) {
111 throw std::invalid_argument("selected account sequence was not returned by discovery");
112 }
113
114 std::cout << "selected account: " << selected_sequence << '\n';
115 auto holdings_result = client.holdings(selected_sequence);
116 if (!holdings_result) {
117 print_error("holdings request", holdings_result.error());
118 return 1;
119 }
120
121 print_holdings(holdings_result.value());
122 return 0;
123 } catch (const std::exception& exception) {
124 std::cerr << "example failed: " << exception.what() << '\n';
125 return 1;
126 }
127}
void from_env()
Loads the credentials from the broker's environment variables.
Definition oauth.hpp:47
Blocking facade that drives TossClient on a private event loop.
Definition client.hpp:3689
std::string request_id_
The broker's request id for this call, if it gave one.
Definition types.hpp:1024
int http_status_
The HTTP status of the reply: set for an HTTP error and for a reply whose body is not a JSON object....
Definition types.hpp:1020
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:1033
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:1022
std::vector< HoldingsTotals > totals_
One totals row per currency held.
Definition types.hpp:1349
std::vector< HoldingPosition > positions_
Every position row; one instrument may appear in more than one.
Definition types.hpp:1347
A Toss account, as account discovery (async_accounts) returns it.
Definition client.hpp:30
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition config.hpp:27