8#define ATL_BROKER_ADAPTER_PRESENT 1
10#include "./client.hpp"
12#include "../../core/broker_session.hpp"
13#include "../../core/broker_traits.hpp"
30 static constexpr std::string_view
name =
"tossinvest";
33namespace traits_detail {
41 static constexpr bool has_depth =
true;
44 static constexpr bool has_price_limits =
true;
47 static constexpr bool has_trade_prints =
true;
50 static constexpr bool has_batch_prices =
true;
53 static constexpr bool has_buying_power =
true;
56 static constexpr bool has_buying_power_quantity =
false;
79 static constexpr bool has_commission_schedule =
true;
82 static constexpr bool has_reference_data =
true;
85 static constexpr bool has_batch_reference_data =
true;
88 static constexpr bool has_calendar =
true;
91 static constexpr bool has_calendar_range =
false;
94 static constexpr bool has_calendar_sessions =
true;
101 static std::optional<BrokerError> validate_calendar(
Country, std::optional<Date> from) {
109 error.
message_ =
"calendar request requires a valid Gregorian date";
118 static constexpr bool supports_interval(
BarInterval interval) {
119 static_cast<void>(interval);
120 return interval == BarInterval::ONE_MINUTE || interval == BarInterval::ONE_DAY;
128 static std::optional<BrokerError> validate_bars(
const BarQuery& query) {
137 static Ref make_ref(std::string order_id) {
149 static std::optional<BrokerError> validate_place(
const OrderRequest&) {
return std::nullopt; }
161 placed.
ref_ = make_ref(raw.
value().order_id_);
181 for (
auto& order : raw.
value().orders_) {
183 working.
ref_ = make_ref(order.order_id_);
184 working.
order_ = std::move(order);
185 orders.
orders_.push_back(std::move(working));
225 "Toss cannot change an order's validity condition or session by amending it, and this "
226 "instruction does not say what the order's are, so neither can be verified; build the "
227 "instruction with reprice() or replace_with() from the working order";
238 "Toss cannot change an order's validity condition by amending it; cancel the order and "
239 "place a new one instead";
249 "Toss cannot move an order to another trading session by amending it; cancel the order "
250 "and place a new one instead";
282 "Toss cancels an order in full; use cancel_all() or modify the order to a smaller "
283 "quantity instead of cancelling part of it";
299 std::int64_t account_sequence,
307 converted.
to_ = query.
to_;
356 static constexpr bool has_account_discovery =
true;
359 static constexpr bool has_client_order_id =
true;
365 static constexpr bool has_trigger_price =
false;
374 static constexpr bool has_working_orders =
true;
377 static constexpr bool has_amendable_quantity =
false;
383 static constexpr bool working_orders_state_terminal =
false;
386 static constexpr bool has_order_by_id =
true;
389 static constexpr bool has_partial_cancel =
false;
392 static constexpr bool history_requires_range =
false;
395 static constexpr bool has_history_cursor =
true;
398 static constexpr bool has_history_limit =
true;
401 static constexpr bool has_history_symbol_filter =
true;
410: tossinvest::traits_detail::Normalizers, tossinvest::traits_detail::Capabilities {
471 template <
typename Token>
473 return client.
async_instrument(std::move(subject), std::forward<Token>(token));
485 template <
typename Token>
487 return client.
async_calendar(country, from, std::forward<Token>(token));
498 template <
typename Token>
512 template <
typename Token>
525 template <
typename Token>
526 static auto prices(
Client& client, std::vector<InstrumentId> instruments, Token&& token) {
527 return client.
async_prices(std::move(instruments), std::forward<Token>(token));
538 template <
typename Token>
551 template <
typename Token>
553 return client.
async_bars(std::move(query), std::forward<Token>(token));
564 template <
typename Token>
576 template <
typename Token>
590 template <
typename Token>
604 template <
typename Token>
622 template <
typename Token>
635 template <
typename Token>
637 return client.
async_holdings(account, std::forward<Token>(token));
649 template <
typename Token>
651 return client.
async_place_order(account, std::move(order), std::forward<Token>(token));
663 template <
typename Token>
670 return client.
async_replace_order(account, std::move(amendment), std::forward<Token>(token));
682 template <
typename Token>
690 account, std::move(amendment.
order_id_), std::forward<Token>(token)
702 template <
typename Token>
704 return client.
async_orders(open_query(account), std::forward<Token>(token));
716 template <
typename Token>
723 static_cast<void>(account);
724 return client.
async_orders(std::move(query), std::forward<Token>(token));
736 template <
typename Token>
740 std::string order_id,
743 return client.
async_order(account, std::move(order_id), std::forward<Token>(token));
750: tossinvest::traits_detail::Normalizers, tossinvest::traits_detail::Capabilities {
821 return client.
calendar(country, from);
857 std::vector<InstrumentId> instruments
859 return client.
prices(std::move(instruments));
879 return client.
bars(std::move(query));
959 return client.
place_order(account, std::move(order));
999 return client.
orders(open_query(account));
1014 static_cast<void>(account);
1015 return client.
orders(std::move(query));
1029 std::string order_id
1031 return client.
order(account, std::move(order_id));
A three-letter currency code, such as KRW or USD (ISO 4217 style).
Either a value or a BrokerError.
BrokerError & error()
Returns the error.
T & value()
Returns the value.
Blocking facade that drives TossClient on a private event loop.
Result< OrderAck > replace_order(std::int64_t account_sequence, TossAmendment amendment)
Amends an open order; the outcome can be unknown after an error. The acknowledgement carries a new id...
Result< BuyingPower > buying_power(Currency currency, std::int64_t account_sequence)
Returns what the account may spend in currency.
const network::RateLimitStats * rate_limit_stats() const
Returns the rate limiter's statistics (safe to read between calls).
Result< Decimal64 > sellable_quantity(InstrumentId instrument, std::int64_t account_sequence)
Returns how much of instrument the account may sell.
Result< OrderPage > orders(TossOrderQuery query)
Returns one page of orders; see TossClient::async_orders for which orders are listed.
Result< Instrument > instrument(InstrumentId subject)
Returns static reference data for one instrument.
Result< OrderAck > place_order(std::int64_t account_sequence, OrderRequest order)
Places an order; see TossClient::async_place_order.
Result< DepthSnapshot > depth(InstrumentId instrument)
Returns the resting orders on both sides of the book.
Result< HoldingsSnapshot > holdings(std::int64_t account_sequence)
Returns normalized positions and per-currency totals for one account.
Result< std::vector< QuoteSnapshot > > prices(std::vector< InstrumentId > instruments)
Returns a quote for every instrument in one request.
Result< QuoteSnapshot > quote(InstrumentId instrument)
Returns a quote for a Korean instrument (a US instrument is refused; use prices).
Result< CommissionSchedule > commissions(std::int64_t account_sequence)
Returns the commission rates in force for the account.
Result< OrderAck > cancel_order(std::int64_t account_sequence, std::string order_id)
Cancels an open order; the outcome can be unknown after an error. The acknowledgement carries a new i...
Result< PriceLimits > price_limits(InstrumentId instrument)
Returns today's upper and lower price bound.
Result< std::vector< TradePrint > > trades(InstrumentId instrument, int count)
Returns recent executions for one instrument.
Result< std::vector< TossAccount > > accounts()
Discovers the brokerage accounts available to the current credentials.
Result< OrderSnapshot > order(std::int64_t account_sequence, std::string order_id)
Returns one order by id; see TossClient::async_order for which orders can be read.
Result< BarSeries > bars(BarQuery query)
Returns up to count_ bars; see TossClient::async_bars.
Result< TradingCalendar > calendar(Country country, std::optional< Date > from=std::nullopt)
Returns the trading calendar around from for one market.
The asynchronous Toss Securities REST client: market data, accounts and orders.
auto async_commissions(std::int64_t account_sequence, CompletionToken &&token)
Requests the commission rates in force for the account.
auto async_instrument(InstrumentId instrument, CompletionToken &&token)
Requests static reference data for one instrument.
auto async_orders(TossOrderQuery query, CompletionToken &&token)
Requests a page of orders.
auto async_trades(InstrumentId instrument, int count, CompletionToken &&token)
Requests recent executions for one instrument, newest first.
auto async_quote(InstrumentId instrument, CompletionToken &&token)
Requests a quote for a Korean instrument (a US instrument is refused before sending; use async_prices...
auto async_bars(BarQuery query, CompletionToken &&token)
Requests up to count_ bars, oldest first.
auto async_holdings(std::int64_t account_sequence, CompletionToken &&token)
Requests and normalizes holdings for one discovered account sequence.
auto async_accounts(CompletionToken &&token)
Discovers the brokerage accounts available to the current credentials.
auto async_cancel_order(std::int64_t account_sequence, std::string order_id, CompletionToken &&token)
Cancels an open order.
auto async_place_order(std::int64_t account_sequence, OrderRequest order, CompletionToken &&token)
Places an order.
auto async_depth(InstrumentId instrument, CompletionToken &&token)
Requests and normalizes the resting orders on both sides of the book.
auto async_buying_power(Currency currency, std::int64_t account_sequence, CompletionToken &&token)
Requests the account's buying power in a currency.
const network::RateLimitStats * rate_limit_stats() const
Returns the rate limiter's statistics.
auto async_calendar(Country country, std::optional< Date > from, CompletionToken &&token)
Requests the trading calendar around a date: that date and the business days either side of it (three...
auto async_prices(std::vector< InstrumentId > instruments, CompletionToken &&token)
Quotes up to 200 instruments in one request.
auto async_price_limits(InstrumentId instrument, CompletionToken &&token)
Requests today's upper and lower price bound for one instrument.
auto async_order(std::int64_t account_sequence, std::string order_id, CompletionToken &&token)
Requests one order by id, in any state.
auto async_sellable_quantity(InstrumentId instrument, std::int64_t account_sequence, CompletionToken &&token)
Requests how much of one instrument the account may sell.
auto async_replace_order(std::int64_t account_sequence, TossAmendment amendment, CompletionToken &&token)
Amends an open order's price, and for a Korean order its quantity.
Placeholder types and the refusal used by DataOnlyTraits.
The Toss Securities Open API client.
std::optional< BrokerError > unsupported_bar_interval(BarInterval interval)
Refuses a bar interval Toss does not publish (it publishes one-minute and one-day bars only; nothing ...
@ 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.
SellableSource
Where the library gets a broker's sellable quantity.
@ INQUIRY
From a per-instrument inquiry, made only where the snapshot has no figure.
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Country
A country (market) the library supports.
Venue
A trading venue: an exchange, or the combined view across exchanges.
BarInterval
The period one bar covers.
bool is_valid_date(const Date &date)
Tests whether a date exists in the Gregorian calendar, in years 1 to 9999.
A modification or cancellation of a working order.
bool confirm_high_value_
Confirms a high-value amendment, where the broker asks for that confirmation.
std::optional< OrderShape > resting_
The shape the order has now, or unset if unknown.
std::optional< Decimal64 > quantity_
The quantity affected; never substituted if a broker needs one and it is missing.
OrderShape shape_
The shape the order should have afterwards.
bool all_remaining_
Whether the whole remaining quantity is affected, rather than quantity_.
BarInterval interval_
The period of each bar; an interval the broker does not publish is refused.
The error a broker or transport operation returns.
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).
bool reached_broker_
Whether a request was sent before this error.
static Result< OrderPage > order_history(Client &client, const Account &account, HistoryQuery query)
Returns the CLOSED listing for the bound account.
static Result< PriceLimits > price_limits(Client &client, InstrumentId instrument)
Returns today's price bounds.
static Result< TradingCalendar > calendar(Client &client, Country country, std::optional< Date > from)
Returns the calendar for one market.
static Result< CommissionSchedule > commissions(Client &client, const Account &account)
Returns the commission rates in force for the bound account.
tossinvest::TossBlockingClient Client
The client type.
static Result< HoldingsSnapshot > holdings(Client &client, const Account &account)
Returns holdings for the bound account.
static Result< DepthSnapshot > depth(Client &client, InstrumentId instrument)
Returns the order book.
static Result< BarSeries > bars(Client &client, BarQuery query)
Returns bars, paging inside the client.
static constexpr bool blocking
true: requests return their result directly.
static Result< PlaceAck > cancel_order(Client &client, const Account &account, Amendment amendment)
Cancels an order for the bound account, transmitting the identifier only.
tossinvest::TossOrderQuery HistoryQuery
The order-history query type.
OrderPage WorkingOrdersResult
The working-order result type.
OrderAck PlaceAck
The order acknowledgement type.
static constexpr Venue effective_route(Venue) noexcept
Returns the venue a Toss order reaches: none in particular (Toss does not route by venue,...
static const network::RateLimitStats * rate_limit_stats(const Client &client)
Returns this client's pacing statistics, or null when it is unpaced.
static Result< PlaceAck > replace_order(Client &client, const Account &account, Amendment amendment)
Modifies an order for the bound account.
tossinvest::TossConfig Config
The configuration type.
tossinvest::TossAmendment Amendment
The amendment type.
static Result< std::vector< AccountListItem > > accounts(Client &client)
Returns the broker's own account listing.
static Result< std::vector< TradePrint > > trades(Client &client, InstrumentId instrument, int count)
Returns recent executions.
static Result< Instrument > instrument(Client &client, InstrumentId subject)
Returns static reference data for one instrument.
tossinvest::Broker BrokerTag
The broker tag.
static Result< PlaceAck > place_order(Client &client, const Account &account, OrderRequest order)
Submits an order for the bound account.
static Result< OrderSnapshot > order_by_id(Client &client, const Account &account, std::string order_id)
Returns one order by id for the bound account; see TossClient::async_order for which orders can be re...
static Result< std::vector< QuoteSnapshot > > prices(Client &client, std::vector< InstrumentId > instruments)
Returns a quote for every instrument in one request.
std::int64_t Account
The account type: the account sequence.
static constexpr std::string_view name
The broker's name.
static Result< BuyingPower > buying_power(Client &client, const Account &account, Currency currency)
Returns what the bound account may spend in currency.
tossinvest::TossAccount AccountListItem
The account-listing item type.
static Result< WorkingOrdersResult > working_orders(Client &client, const Account &account)
Returns the OPEN listing for the bound account.
static Result< Decimal64 > sellable_quantity(Client &client, const Account &account, InstrumentId instrument)
Returns how much of instrument the bound account may sell.
static Result< QuoteSnapshot > quote(Client &client, InstrumentId instrument)
Returns a quote.
static auto trades(Client &client, InstrumentId instrument, int count, Token &&token)
Forwards a recent-execution request.
static auto depth(Client &client, InstrumentId instrument, Token &&token)
Forwards an order book request.
static const network::RateLimitStats * rate_limit_stats(const Client &client)
Returns this client's pacing statistics, or null when it is unpaced.
tossinvest::TossOrderQuery HistoryQuery
The order-history query type.
tossinvest::TossClient Client
The client type.
static auto holdings(Client &client, const Account &account, Token &&token)
Forwards a holdings request for the bound account.
static constexpr Venue effective_route(Venue) noexcept
Returns the venue a Toss order reaches: none in particular (Toss does not route by venue,...
tossinvest::TossAmendment Amendment
The amendment type.
OrderPage WorkingOrdersResult
The working-order result type.
static auto accounts(Client &client, Token &&token)
Forwards an account listing request.
static auto bars(Client &client, BarQuery query, Token &&token)
Forwards a bar request.
tossinvest::Broker BrokerTag
The broker tag.
tossinvest::TossAccount AccountListItem
The account-listing item type.
static auto cancel_order(Client &client, const Account &account, Amendment amendment, Token &&token)
Forwards an order cancellation, which transmits the identifier only.
static auto quote(Client &client, InstrumentId instrument, Token &&token)
Forwards a quote request.
static auto order_history(Client &client, const Account &account, HistoryQuery query, Token &&token)
Forwards a CLOSED listing for the bound account.
static auto order_by_id(Client &client, const Account &account, std::string order_id, Token &&token)
Forwards a single-order inquiry for the bound account.
std::int64_t Account
The account type: the account sequence.
static auto price_limits(Client &client, InstrumentId instrument, Token &&token)
Forwards a price-bound request.
static auto place_order(Client &client, const Account &account, OrderRequest order, Token &&token)
Forwards an order submission for the bound account.
static auto calendar(Client &client, Country country, std::optional< Date > from, Token &&token)
Forwards a calendar request.
static auto sellable_quantity(Client &client, const Account &account, InstrumentId instrument, Token &&token)
Forwards a sellable-quantity request for one instrument.
static auto prices(Client &client, std::vector< InstrumentId > instruments, Token &&token)
Forwards a batch quote request.
static constexpr bool blocking
false: requests complete through a completion token.
tossinvest::TossConfig Config
The configuration type.
static auto buying_power(Client &client, const Account &account, Currency currency, Token &&token)
Forwards a buying-power request for the bound account.
static constexpr std::string_view name
The broker's name.
static auto instrument(Client &client, InstrumentId subject, Token &&token)
Forwards a reference-data request.
static auto working_orders(Client &client, const Account &account, Token &&token)
Forwards an OPEN listing for the bound account.
static auto replace_order(Client &client, const Account &account, Amendment amendment, Token &&token)
Forwards an order modification for the bound account.
static auto commissions(Client &client, const Account &account, Token &&token)
Forwards a commission-schedule request for the bound account.
OrderAck PlaceAck
The order acknowledgement type.
Describes one broker client to the library: its types, capabilities, call thunks and normalizers.
Identifies an instrument: its country, venue and symbol.
The broker's acknowledgement that it accepted an instruction.
The filter for an order-history inquiry.
std::optional< std::string > symbol_
Only this symbol, where the broker supports the filter.
std::optional< int > limit_
The most rows to return, where the broker supports a limit.
std::optional< std::string > cursor_
A previous page's next_cursor_, passed back unchanged.
std::optional< Date > from_
The first date to include; required where BrokerCaps::history_requires_range.
std::optional< Date > to_
The last date to include.
One page of an order listing.
A broker's reference to one order, used to amend or cancel it.
std::string primary_
The order number or id.
An order to place, in broker-neutral form.
std::optional< TimeInForce > time_in_force_
The time in force; unset means "keep the resting order's".
std::optional< Decimal64 > price_
The price, for a basis that takes one.
std::optional< OrderSession > session_
The session; unset means "keep the resting order's".
OrderType type_
The price basis.
An accepted order: the acknowledgement and the reference to address it with.
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
OrderAck ack_
The acknowledgement.
What a sellable quantity covers, and how RiskGuard must treat resting sells against it.
@ WHOLE_HOLDING
Every share held of the instrument, whatever venue or lot.
@ DEDUCT
Subtract every resting sell from the figure.
An order still open to modification or cancellation.
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
OrderSnapshot order_
The order's state.
The orders open to modification or cancellation at one point in time.
std::vector< WorkingOrder< Broker > > orders_
The open orders.
std::chrono::system_clock::time_point received_at_
When the listing was received, on this machine's clock.
A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from Rate...
The Toss Securities broker tag: order references are typed by it, so a Toss reference works with both...
static constexpr std::string_view name
The broker's name, "tossinvest".
A Toss account, as account discovery (async_accounts) returns it.
std::int64_t sequence_
Toss's account sequence, which every account request names.
std::string account_type_
The account type as Toss reports it, for display.
std::string account_number_
The account number as Toss reports it (accountNo): the full number, not masked.
An amendment of a Toss order: a new price, and for a Korean order a new quantity.
std::string order_id_
Toss's order id.
bool confirm_high_value_
Confirms a high-value order, which Toss otherwise refuses.
std::optional< Decimal64 > price_
The new price.
std::optional< Decimal64 > quantity_
The new quantity: required for a Korean order, empty for a US one.
OrderType type_
The order type it should have.
Toss Securities credentials (client id and secret). Toss has no demo environment.
A Toss order-listing query.
std::optional< Date > to_
The last order date to include; before from_ is refused.
std::optional< Date > from_
The first order date to include (Korean date; any scope).
std::optional< std::string > symbol_
Only this symbol, if set.
std::int64_t account_sequence_
The account (TossAccount::sequence_).
std::optional< std::string > cursor_
The cursor from a previous page (OrderPage::next_cursor_); ignored for OPEN.
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.