algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-tossinvest-orders.cpp
1#include <algotradelib/tossinvest>
2
3#include <boost/asio/ssl/context.hpp>
4
5#include <cstdint>
6#include <exception>
7#include <iostream>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <utility>
12
13namespace ssl = boost::asio::ssl;
14namespace atl = algotradelib;
15
16namespace {
17
19void print_error(std::string_view operation, const atl::BrokerError& error) {
20 std::cerr << operation << " failed";
21 if (!error.broker_code_.empty()) {
22 std::cerr << " [" << error.broker_code_ << ']';
23 }
24 if (!error.request_id_.empty()) {
25 std::cerr << " request=" << error.request_id_;
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';
34 if (error.retry_after_) {
35 std::cerr << " retry after " << error.retry_after_->count() << " seconds\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.instrument_.symbol_ << ' '
70 << (order.side_ == atl::OrderSide::BUY ? "BUY" : "SELL") << ' '
71 << status_name(order.status_) << " qty=" << order.quantity_.str();
72 if (order.price_) {
73 std::cout << " price=" << order.price_->str();
74 }
75 std::cout << " filled=" << order.execution_.filled_quantity_.str();
77 std::cout << " avg=" << order.execution_.average_filled_price_->str();
78 }
79 std::cout << " " << order.currency_.str() << " id=" << order.order_id_ << '\n';
80}
81
83std::int64_t parse_account_sequence(const char* value) {
84 const std::string text(value);
85 std::size_t parsed = 0;
86 const auto sequence = std::stoll(text, &parsed);
87 if (parsed != text.size() || sequence <= 0) {
88 throw std::invalid_argument("account sequence must be a positive integer");
89 }
90 return sequence;
91}
92
93} // namespace
94
96int main(int argc, char* argv[]) {
97 try {
99 config.from_env();
100
101 ssl::context tls(ssl::context::tls_client);
102 tls.set_default_verify_paths();
103 tls.set_verify_mode(ssl::verify_peer);
104
105 atl::tossinvest::TossBlockingClient client(tls, std::move(config));
106
107 std::int64_t sequence = 0;
108 if (argc > 1) {
109 sequence = parse_account_sequence(argv[1]);
110 } else {
111 auto accounts_result = client.accounts();
112 if (!accounts_result) {
113 print_error("account discovery", accounts_result.error());
114 return 1;
115 }
116 if (accounts_result.value().empty()) {
117 std::cout << "no accounts available\n";
118 return 0;
119 }
120 sequence = accounts_result.value().front().sequence_;
121 }
122 std::cout << "selected account: " << sequence << '\n';
123
125 open_query.account_sequence_ = sequence;
127 auto open_result = client.orders(open_query);
128 if (!open_result) {
129 print_error("open order listing", open_result.error());
130 return 1;
131 }
132 std::cout << "open orders: " << open_result.value().orders_.size() << '\n';
133 for (const auto& order : open_result.value().orders_) {
134 print_order(order);
135 }
136
137 // Fetch only the first closed page. Following every cursor automatically
138 // can consume the ORDER_HISTORY request budget; callers decide explicitly
139 // whether and when to request another page.
141 closed_query.account_sequence_ = sequence;
143 closed_query.limit_ = 20;
144 auto closed_result = client.orders(closed_query);
145 if (!closed_result) {
146 print_error("closed order listing", closed_result.error());
147 return 1;
148 }
149 std::cout << "closed orders on first page: " << closed_result.value().orders_.size() << '\n';
150 std::cout << "more closed orders: "
151 << (closed_result.value().next_cursor_ ? "yes" : "no") << '\n';
152 return 0;
153 } catch (const std::exception& exception) {
154 std::cerr << "example failed: " << exception.what() << '\n';
155 return 1;
156 }
157}
std::string str() const
Returns the code.
Definition types.hpp:274
std::string str() const
Formats the value as a decimal string.
Definition types.hpp:411
void from_env()
Loads the credentials from the broker's environment variables.
Definition oauth.hpp:46
Blocking facade that drives TossClient on a private event loop.
Definition client.hpp:3994
@ CLOSED
Ended orders (including a partly filled order that ended).
Definition client.hpp:45
@ OPEN
Open orders: pending, partly filled, or with an amendment or cancellation pending.
Definition client.hpp:43
The library: broker-neutral types (core), and the modules below.
OrderStatus
Where an order stands in its lifecycle.
Definition types.hpp:1879
std::string request_id_
The broker's request id for this call, if it gave one.
Definition types.hpp:763
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::optional< std::chrono::seconds > retry_after_
How long the broker asked the caller to wait (Retry-After), for a rate-limit error.
Definition types.hpp:779
std::string symbol_
The symbol as the venue spells it, such as "005930".
Definition types.hpp:88
Decimal64 filled_quantity_
The quantity filled so far; zero if nothing has filled. Always present.
Definition types.hpp:1983
std::optional< Decimal64 > average_filled_price_
The average fill price, only where the broker supplies it.
Definition types.hpp:1990
OrderSide side_
Buy or sell.
Definition types.hpp:2016
OrderStatus status_
The status; UNKNOWN where the broker publishes none.
Definition types.hpp:2038
OrderExecution execution_
What has filled and what it cost; see OrderExecution before testing a fill.
Definition types.hpp:2074
Currency currency_
The currency of the row's prices and amounts.
Definition types.hpp:2060
Decimal64 quantity_
The quantity ordered; the remainder is this minus execution_.filled_quantity_.
Definition types.hpp:2050
std::string order_id_
The broker's order number.
Definition types.hpp:2010
InstrumentId instrument_
The instrument (identity venue; the routing venue is venue_).
Definition types.hpp:2012
std::optional< Decimal64 > price_
The limit price, if the order has one.
Definition types.hpp:2040
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition config.hpp:24
A Toss order-listing query.
Definition client.hpp:55
std::int64_t account_sequence_
The account (TossAccount::sequence_).
Definition client.hpp:57
TossOrderScope scope_
Open or closed orders.
Definition client.hpp:59
std::optional< int > limit_
The most rows to return, 1 to 100 (outside that range is refused, even for OPEN); not sent for OPEN.
Definition client.hpp:69