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 << " last price: " << position.last_price_.str() << ' '
40 << position.market_value_.currency_.str() << '\n';
41 std::cout << " market value: " << position.market_value_.amount_.str() << ' '
42 << position.market_value_.currency_.str() << '\n';
43 std::cout << " unrealized P/L: " << position.unrealized_profit_loss_.amount_.str() << ' '
44 << position.unrealized_profit_loss_.currency_.str() << '\n';
45 }
46
47 std::cout << "totals: " << snapshot.totals_.size() << '\n';
48 for (const auto& total : snapshot.totals_) {
49 std::cout << total.currency_.str() << " purchase=" << total.purchase_value_.str()
50 << " market=" << total.market_value_.str()
51 << " unrealized=" << total.unrealized_profit_loss_.str() << '\n';
52 }
53}
54
55} // namespace
56
57int main(int argc, char* argv[]) {
58 try {
61 kis_credentials::load(kis_credentials::take_directory(argc, argv), config, &account, std::cout);
62
63 std::cout << "environment: " << (config.is_demo_ ? "demo" : "live") << '\n';
64
65 ssl::context tls(ssl::context::tls_client);
66 tls.set_default_verify_paths();
67 tls.set_verify_mode(ssl::verify_peer);
68
70 auto result = client.holdings(std::move(account));
71 if (!result) {
72 print_error("holdings request", result.error());
73 return 1;
74 }
75
76 print_holdings(result.value());
77 return 0;
78 } catch (const std::exception& exception) {
79 std::cerr << "example failed: " << exception.what() << '\n';
80 return 1;
81 }
82}
A blocking KIS client: each call runs a KisClient request on a private event loop and returns its res...
Definition client.hpp:4540
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:759
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:772
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:761
std::vector< HoldingsTotals > totals_
One totals row per currency held.
Definition types.hpp:1079
std::vector< HoldingPosition > positions_
Every position row; one instrument may appear in more than one.
Definition types.hpp:1077
A KIS account: the account number and product code every account request names.
Definition client.hpp:29
KIS credentials (app key and secret), demo or real account, and the market-data tape.
Definition config.hpp:90
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:101