3#include <algotradelib/kis>
5#include <boost/asio/ssl/context.hpp>
16namespace ssl = boost::asio::ssl;
22void print_error(std::string_view operation,
const atl::BrokerError& error) {
23 std::cerr << operation <<
" failed";
36 <<
" the outcome is indeterminate; reconcile through the order inquiries "
37 "before resubmitting\n";
44 case atl::OrderStatus::PENDING:
46 case atl::OrderStatus::PENDING_CANCEL:
47 return "PENDING_CANCEL";
48 case atl::OrderStatus::PENDING_REPLACE:
49 return "PENDING_REPLACE";
50 case atl::OrderStatus::PARTIAL_FILLED:
51 return "PARTIAL_FILLED";
52 case atl::OrderStatus::FILLED:
54 case atl::OrderStatus::CANCELED:
56 case atl::OrderStatus::REJECTED:
58 case atl::OrderStatus::CANCEL_REJECTED:
59 return "CANCEL_REJECTED";
60 case atl::OrderStatus::REPLACE_REJECTED:
61 return "REPLACE_REJECTED";
62 case atl::OrderStatus::REPLACED:
70void print_order(
const atl::OrderSnapshot& order) {
72 std::cout <<
" " << (order.
side_ == atl::OrderSide::BUY ?
"BUY" :
"SELL") <<
" "
75 std::cout <<
" @ " << order.
price_->str();
87 const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
90 localtime_s(&local, &now);
92 localtime_r(&now, &local);
96 static_cast<unsigned>(local.tm_mon + 1),
97 static_cast<unsigned>(local.tm_mday),
104int main(
int argc,
char* argv[]) {
111 std::cout <<
"environment: " << (demo ?
"demo" :
"live") <<
'\n';
113 ssl::context tls(ssl::context::tls_client);
114 tls.set_default_verify_paths();
115 tls.set_verify_mode(ssl::verify_peer);
122 auto open_result = client.open_orders(account);
124 print_error(
"modifiable-order inquiry", open_result.error());
127 std::cout <<
"open orders" << (demo ?
" (from today's history)" :
"") <<
": "
128 << open_result.value().orders_.size() <<
'\n';
129 for (
const auto& open_order : open_result.value().orders_) {
130 print_order(open_order.order_);
131 std::cout <<
" amendable quantity: " << open_order.amendable_quantity_.str()
132 <<
" branch " << open_order.order_ref_.branch_number_ <<
'\n';
136 const auto day = today();
138 if (!history_result) {
139 print_error(
"order-history inquiry", history_result.error());
142 std::cout <<
"orders today: " << history_result.value().orders_.size() <<
'\n';
143 for (
const auto& order : history_result.value().orders_) {
147 }
catch (
const std::exception& exception) {
148 std::cerr <<
"example failed: " << exception.what() <<
'\n';
std::string str() const
Formats the value as a decimal string.
A blocking KIS client: each call runs a KisClient request on a private event loop and returns its res...
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.
@ TRANSPORT
The connection failed (DNS, connect, TLS, write or read).
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....
std::string message_
A human-readable description, for logs; do not parse it.
ErrorCategory category_
The kind of failure; the field to branch on.
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
std::string symbol_
The symbol as the venue spells it, such as "005930".
Decimal64 filled_quantity_
The quantity filled so far; zero if nothing has filled. Always present.
std::optional< Decimal64 > average_filled_price_
The average fill price, only where the broker supplies it.
OrderSide side_
Buy or sell.
OrderStatus status_
The status; UNKNOWN where the broker publishes none.
OrderExecution execution_
What has filled and what it cost; see OrderExecution before testing a fill.
std::string name_
The instrument's name as the broker spells it.
Decimal64 quantity_
The quantity ordered; the remainder is this minus execution_.filled_quantity_.
std::string order_id_
The broker's order number.
InstrumentId instrument_
The instrument (identity venue; the routing venue is venue_).
std::optional< Decimal64 > price_
The limit price, if the order has one.
A KIS account: the account number and product code every account request names.
KIS credentials (app key and secret), demo or real account, and the market-data tape.
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
The date range of a KIS order-history inquiry, both ends included (Korean dates).