algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kis-account.cpp
1#include "kis_credentials.hpp"
2
3#include <algotradelib/kis>
4
5#include <boost/asio/ssl/context.hpp>
6
7#include <exception>
8#include <iostream>
9#include <string_view>
10#include <utility>
11
12namespace ssl = boost::asio::ssl;
13
14namespace {
15
16void print_error(std::string_view operation, const algotradelib::BrokerError& error) {
17 std::cerr << operation << " failed";
18 if (!error.broker_code_.empty()) {
19 std::cerr << " [" << error.broker_code_ << ']';
20 }
21 if (error.http_status_ != 0) {
22 std::cerr << " HTTP " << error.http_status_;
23 }
24 if (!error.message_.empty()) {
25 std::cerr << ": " << error.message_;
26 }
27 std::cerr << '\n';
28}
29
30void print_holdings(const algotradelib::HoldingsSnapshot& snapshot) {
31 std::cout << "positions: " << snapshot.positions_.size() << '\n';
32 for (const auto& position : snapshot.positions_) {
33 std::cout << position.instrument_.symbol_ << " " << position.name_ << '\n';
34 std::cout << " quantity: " << position.quantity_.str();
35 if (position.sellable_quantity_) {
36 std::cout << " (sellable " << position.sellable_quantity_->str() << ')';
37 }
38 std::cout << '\n';
39 std::cout
40 << " last price: " << position.last_price_.str() << ' ' << position.market_value_.currency_.str() << '\n';
41 std::cout
42 << " market value: " << position.market_value_.amount_.str() << ' ' << position.market_value_.currency_.str()
43 << '\n';
44 std::cout
45 << " unrealized P/L: " << position.unrealized_profit_loss_.amount_.str() << ' '
46 << position.unrealized_profit_loss_.currency_.str() << '\n';
47 }
48
49 std::cout << "totals: " << snapshot.totals_.size() << '\n';
50 for (const auto& total : snapshot.totals_) {
51 std::cout
52 << total.currency_.str() << " purchase=" << total.purchase_value_.str()
53 << " market=" << total.market_value_.str() << " unrealized=" << total.unrealized_profit_loss_.str() << '\n';
54 }
55}
56
57} // namespace
58
59int main(int argc, char* argv[]) {
60 try {
63 kis_credentials::load(kis_credentials::take_directory(argc, argv), config, &account, std::cout);
64
65 std::cout << "environment: " << (config.is_demo_ ? "demo" : "live") << '\n';
66
67 ssl::context tls(ssl::context::tls_client);
68 tls.set_default_verify_paths();
69 tls.set_verify_mode(ssl::verify_peer);
70
72 auto result = client.holdings(std::move(account));
73 if (!result) {
74 print_error("holdings request", result.error());
75 return 1;
76 }
77
78 print_holdings(result.value());
79 return 0;
80 } catch (const std::exception& exception) {
81 std::cerr << "example failed: " << exception.what() << '\n';
82 return 1;
83 }
84}
A blocking KIS client: each call runs a KisClient request on a private event loop and returns its res...
Definition client.hpp:4270
The examples' KIS credentials: one JSON file per account, or the environment.
algotradelib::network::HttpClientOptions paced_options()
Returns transport options that pace a KIS client at one request a second.
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...
std::optional< std::filesystem::path > take_directory(int &argc, char **argv)
Removes --credentials DIR from the command line and returns the directory, else ATL_CREDENTIALS_DIR.
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 KIS account: the account number and product code every account request names.
Definition client.hpp:31
KIS credentials (app key and secret), demo or real account, and the market-data tape.
Definition config.hpp:83
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:94