algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kis-orders.cpp
1#include "kis_credentials.hpp"
2
3#include <algotradelib/kis>
4
5#include <boost/asio/ssl/context.hpp>
6
7#include <chrono>
8#include <cstdlib>
9#include <ctime>
10#include <exception>
11#include <iostream>
12#include <string>
13#include <string_view>
14#include <utility>
15
16namespace ssl = boost::asio::ssl;
17namespace atl = algotradelib;
18
19namespace {
20
22void print_error(std::string_view operation, const atl::BrokerError& error) {
23 std::cerr << operation << " failed";
24 if (!error.broker_code_.empty()) {
25 std::cerr << " [" << error.broker_code_ << ']';
26 }
27 if (error.http_status_ != 0) {
28 std::cerr << " HTTP " << error.http_status_;
29 }
30 if (!error.message_.empty()) {
31 std::cerr << ": " << error.message_;
32 }
33 std::cerr << '\n';
35 std::cerr << " the outcome is indeterminate; reconcile through the order inquiries before resubmitting\n";
36 }
37}
38
40std::string_view status_name(atl::OrderStatus status) {
41 switch (status) {
42 case atl::OrderStatus::PENDING:
43 return "PENDING";
44 case atl::OrderStatus::PENDING_CANCEL:
45 return "PENDING_CANCEL";
46 case atl::OrderStatus::PENDING_REPLACE:
47 return "PENDING_REPLACE";
48 case atl::OrderStatus::PARTIAL_FILLED:
49 return "PARTIAL_FILLED";
50 case atl::OrderStatus::FILLED:
51 return "FILLED";
52 case atl::OrderStatus::CANCELED:
53 return "CANCELED";
54 case atl::OrderStatus::REJECTED:
55 return "REJECTED";
56 case atl::OrderStatus::CANCEL_REJECTED:
57 return "CANCEL_REJECTED";
58 case atl::OrderStatus::REPLACE_REJECTED:
59 return "REPLACE_REJECTED";
60 case atl::OrderStatus::REPLACED:
61 return "REPLACED";
62 default:
63 return "UNKNOWN";
64 }
65}
66
68void print_order(const atl::OrderSnapshot& order) {
69 std::cout << order.order_id_ << " " << order.instrument_.symbol_ << " " << order.name_ << '\n';
70 std::cout
71 << " " << (order.side_ == atl::OrderSide::BUY ? "BUY" : "SELL") << " " << status_name(order.status_)
72 << " quantity " << order.quantity_.str();
73 if (order.price_) {
74 std::cout << " @ " << order.price_->str();
75 }
76 std::cout << '\n';
77 std::cout << " filled: " << order.execution_.filled_quantity_.str();
79 std::cout << " @ average " << order.execution_.average_filled_price_->str();
80 }
81 std::cout << '\n';
82}
83
85atl::Date today() {
86 const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
87 std::tm local{};
88#ifdef _WIN32
89 localtime_s(&local, &now);
90#else
91 localtime_r(&now, &local);
92#endif
93 return atl::Date{
94 local.tm_year + 1900,
95 static_cast<unsigned>(local.tm_mon + 1),
96 static_cast<unsigned>(local.tm_mday),
97 };
98}
99
100} // namespace
101
103int main(int argc, char* argv[]) {
104 try {
105 atl::kis::KisConfig config;
106 atl::kis::KisAccount account;
107 kis_credentials::load(kis_credentials::take_directory(argc, argv), config, &account, std::cout);
108 const bool demo = config.is_demo_;
109
110 std::cout << "environment: " << (demo ? "demo" : "live") << '\n';
111
112 ssl::context tls(ssl::context::tls_client);
113 tls.set_default_verify_paths();
114 tls.set_verify_mode(ssl::verify_peer);
115
116 atl::kis::KisBlockingClient client(tls, std::move(config), kis_credentials::paced_options());
117
118 // On demo KIS has no modifiable-order inquiry, and the client answers this read from today's
119 // order history instead.
120 {
121 auto open_result = client.open_orders(account);
122 if (!open_result) {
123 print_error("modifiable-order inquiry", open_result.error());
124 return 1;
125 }
126 std::cout
127 << "open orders" << (demo ? " (from today's history)" : "") << ": " << open_result.value().orders_.size()
128 << '\n';
129 for (const auto& open_order : open_result.value().orders_) {
130 print_order(open_order.order_);
131 std::cout
132 << " amendable quantity: " << open_order.amendable_quantity_.str() << " branch "
133 << open_order.order_ref_.branch_number_ << '\n';
134 }
135 }
136
137 const auto day = today();
138 auto history_result = client.order_history(account, atl::kis::KisHistoryRange{day, day});
139 if (!history_result) {
140 print_error("order-history inquiry", history_result.error());
141 return 1;
142 }
143 std::cout << "orders today: " << history_result.value().orders_.size() << '\n';
144 for (const auto& order : history_result.value().orders_) {
145 print_order(order);
146 }
147 return 0;
148 } catch (const std::exception& exception) {
149 std::cerr << "example failed: " << exception.what() << '\n';
150 return 1;
151 }
152}
std::string str() const
Formats the value as a decimal string.
Definition types.hpp:557
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.
The library: broker-neutral types (core), and the modules below.
OrderStatus
Where an order stands in its lifecycle.
Definition types.hpp:2170
@ TRANSPORT
The connection failed (DNS, connect, TLS, write or read).
Definition types.hpp:952
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
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:1022
std::string symbol_
The symbol as the venue spells it, such as "005930".
Definition types.hpp:95
Decimal64 filled_quantity_
The quantity filled so far; zero if nothing has filled. Always present.
Definition types.hpp:2274
std::optional< Decimal64 > average_filled_price_
The average fill price, only where the broker supplies it.
Definition types.hpp:2281
OrderSide side_
Buy or sell.
Definition types.hpp:2307
OrderStatus status_
The status; UNKNOWN where the broker publishes none.
Definition types.hpp:2329
OrderExecution execution_
What has filled and what it cost; see OrderExecution before testing a fill.
Definition types.hpp:2365
std::string name_
The instrument's name as the broker spells it.
Definition types.hpp:2305
Decimal64 quantity_
The quantity ordered; the remainder is this minus execution_.filled_quantity_.
Definition types.hpp:2341
std::string order_id_
The broker's order number.
Definition types.hpp:2301
InstrumentId instrument_
The instrument (identity venue; the routing venue is venue_).
Definition types.hpp:2303
std::optional< Decimal64 > price_
The limit price, if the order has one.
Definition types.hpp:2331
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
The date range of a KIS order-history inquiry, both ends included (Korean dates).
Definition client.hpp:202