1#include <algotradelib/tossinvest>
3#include <boost/asio/ssl/context.hpp>
13namespace ssl = boost::asio::ssl;
19void print_error(std::string_view operation,
const atl::BrokerError& error) {
20 std::cerr << operation <<
" failed";
35 std::cerr <<
" retry after " << error.
retry_after_->count() <<
" seconds\n";
42 case atl::OrderStatus::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:
52 case atl::OrderStatus::CANCELED:
54 case atl::OrderStatus::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:
68void print_order(
const atl::OrderSnapshot& order) {
70 << (order.
side_ == atl::OrderSide::BUY ?
"BUY" :
"SELL") <<
' '
73 std::cout <<
" price=" << order.
price_->str();
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");
96int main(
int argc,
char* argv[]) {
101 ssl::context tls(ssl::context::tls_client);
102 tls.set_default_verify_paths();
103 tls.set_verify_mode(ssl::verify_peer);
107 std::int64_t sequence = 0;
109 sequence = parse_account_sequence(argv[1]);
111 auto accounts_result = client.accounts();
112 if (!accounts_result) {
113 print_error(
"account discovery", accounts_result.error());
116 if (accounts_result.value().empty()) {
117 std::cout <<
"no accounts available\n";
120 sequence = accounts_result.value().front().sequence_;
122 std::cout <<
"selected account: " << sequence <<
'\n';
127 auto open_result = client.orders(open_query);
129 print_error(
"open order listing", open_result.error());
132 std::cout <<
"open orders: " << open_result.value().orders_.size() <<
'\n';
133 for (
const auto& order : open_result.value().orders_) {
144 auto closed_result = client.orders(closed_query);
145 if (!closed_result) {
146 print_error(
"closed order listing", closed_result.error());
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';
153 }
catch (
const std::exception& exception) {
154 std::cerr <<
"example failed: " << exception.what() <<
'\n';
std::string str() const
Returns the code.
std::string str() const
Formats the value as a decimal string.
void from_env()
Loads the credentials from the broker's environment variables.
Blocking facade that drives TossClient on a private event loop.
@ CLOSED
Ended orders (including a partly filled order that ended).
@ OPEN
Open orders: pending, partly filled, or with an amendment or cancellation pending.
The library: broker-neutral types (core), and the modules below.
OrderStatus
Where an order stands in its lifecycle.
std::string request_id_
The broker's request id for this call, if it gave one.
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.
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
std::optional< std::chrono::seconds > retry_after_
How long the broker asked the caller to wait (Retry-After), for a rate-limit error.
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.
Currency currency_
The currency of the row's prices and amounts.
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.
Toss Securities credentials (client id and secret). Toss has no demo environment.
A Toss order-listing query.
std::int64_t account_sequence_
The account (TossAccount::sequence_).
TossOrderScope scope_
Open or closed orders.
std::optional< int > limit_
The most rows to return, 1 to 100 (outside that range is refused, even for OPEN); not sent for OPEN.