![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
An asynchronous broker client bound to one account, with broker-neutral operations. More...
#include <algotradelib/src/core/broker_session.hpp>
Public Types | |
| using | Traits = BrokerTraits<Client> |
| The client's traits. | |
| using | Caps = BrokerCaps<Client> |
| The client's capabilities. | |
| using | Account = typename Traits::Account |
| The broker's account selector type. | |
| using | Config = typename Traits::Config |
| The broker's configuration type. | |
| using | BrokerTag = typename Traits::BrokerTag |
| The broker's tag type. | |
| using | Ref = OrderRef<BrokerTag> |
| The order reference type this session issues and accepts. | |
| using | Placed = PlacedOrder<BrokerTag> |
| What an order-writing call returns: acknowledgement and reference. | |
| using | Working = WorkingOrder<BrokerTag> |
| One working order of this broker. | |
| using | WorkingSet = WorkingOrders<BrokerTag> |
| A working-order listing of this broker. | |
Public Member Functions | |
| BrokerSession (std::shared_ptr< Client > client, Account account) | |
| Binds a client and an account. | |
| const Account & | account () const noexcept |
| Returns the bound account. | |
| Client & | client () noexcept |
| Returns the broker client, for broker-specific operations the session does not offer. | |
| const Client & | client () const noexcept |
| Returns the broker client. | |
| const std::shared_ptr< Client > & | shared_client () const noexcept |
| Returns the shared client pointer, for binding a second session. | |
| const network::RateLimitStats * | rate_limit_stats () const |
| Returns the client's request-pacing counters. | |
| void | close () |
| Cancels the client's transport work and queued operations. | |
| template<typename CompletionToken> | |
| auto | async_quote (InstrumentId instrument, CompletionToken &&token) |
| Requests the current quote for an instrument. | |
| template<typename CompletionToken> | |
| auto | async_holdings (CompletionToken &&token) |
| Requests the account's holdings. | |
| template<typename CompletionToken> | |
| auto | async_buying_power (Currency currency, CompletionToken &&token) |
| Requests what the account may spend in a currency. | |
| template<typename CompletionToken> | |
| auto | async_sellable_quantity (InstrumentId instrument, CompletionToken &&token) |
| Requests how much of an instrument the account may sell. | |
| template<typename CompletionToken> | |
| auto | async_commissions (CompletionToken &&token) |
| Requests the account's commission rates. | |
| template<typename CompletionToken> | |
| auto | async_place (OrderRequest order, CompletionToken &&token) |
| Places a new order. | |
| template<typename CompletionToken> | |
| auto | async_replace (Ref ref, AmendInstruction instruction, CompletionToken &&token) |
| Modifies a working order. | |
| template<typename CompletionToken> | |
| auto | async_cancel (Ref ref, AmendInstruction instruction, CompletionToken &&token) |
| Cancels a working order, in full or in part. | |
| template<typename CompletionToken> | |
| auto | async_working_orders (CompletionToken &&token) |
| Requests the orders still open to modification or cancellation. | |
| template<typename CompletionToken> | |
| auto | async_order_history (OrderHistoryQuery query, CompletionToken &&token) |
| Requests order history: placed orders and their executions. | |
| template<typename CompletionToken> | |
| auto | async_bars (BarQuery query, CompletionToken &&token) |
| Requests bars for an instrument, paging until count_ bars are read. | |
| template<typename CompletionToken> | |
| auto | async_instrument (InstrumentId instrument, CompletionToken &&token) |
| Requests an instrument's reference data. | |
| template<typename CompletionToken> | |
| auto | async_calendar (Country country, std::optional< Date > from, CompletionToken &&token) |
| Requests the trading calendar. | |
| template<typename CompletionToken> | |
| auto | async_price_limits (InstrumentId instrument, CompletionToken &&token) |
| Requests an instrument's price limits for today. | |
| template<typename CompletionToken> | |
| auto | async_trades (InstrumentId instrument, int count, CompletionToken &&token) |
| Requests recent trades for an instrument, newest first. | |
| template<typename CompletionToken> | |
| auto | async_prices (std::vector< InstrumentId > instruments, CompletionToken &&token) |
| Requests quotes for several instruments in one request. | |
| template<typename CompletionToken> | |
| auto | async_depth (InstrumentId instrument, CompletionToken &&token) |
| Requests an instrument's order book, best levels first. | |
| template<typename CompletionToken> | |
| auto | async_order (Ref ref, CompletionToken &&token) |
| Requests one order by its reference. | |
Static Public Member Functions | |
| static constexpr std::string_view | broker_name () noexcept |
| Returns the broker's name. | |
An asynchronous broker client bound to one account, with broker-neutral operations.
Every operation takes a completion token and completes once with Result<T>, on the handler's associated executor. An operation the broker does not offer (see Caps) does not compile; gate it with if constexpr. A request the session or adapter refuses before any I/O completes with an error that is posted, never invoked inline. For a handler with no associated executor, that refusal runs on the system executor while a broker's reply runs on the client's strand, so one callback can run on two threads; bind the handler to an executor if that matters. A session is cheap to copy (a shared client pointer and the account).
| Client | An asynchronous broker client (BrokerTraits<Client>::blocking is false); use BlockingSession for a blocking one. |
Definition at line 133 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Account = typename Traits::Account |
The broker's account selector type.
Definition at line 142 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::BrokerTag = typename Traits::BrokerTag |
The broker's tag type.
Definition at line 148 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Caps = BrokerCaps<Client> |
The client's capabilities.
Definition at line 139 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Config = typename Traits::Config |
The broker's configuration type.
Definition at line 145 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Placed = PlacedOrder<BrokerTag> |
What an order-writing call returns: acknowledgement and reference.
Definition at line 154 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Ref = OrderRef<BrokerTag> |
The order reference type this session issues and accepts.
Definition at line 151 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Traits = BrokerTraits<Client> |
The client's traits.
Definition at line 136 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::Working = WorkingOrder<BrokerTag> |
One working order of this broker.
Definition at line 157 of file broker_session.hpp.
| using algotradelib::BrokerSession< Client >::WorkingSet = WorkingOrders<BrokerTag> |
A working-order listing of this broker.
Definition at line 160 of file broker_session.hpp.
|
inline |
Binds a client and an account.
| client | The client; must not be null. |
| account | The account to act for. |
| std::invalid_argument | if client is null. |
Definition at line 173 of file broker_session.hpp.
|
inlinenoexcept |
Returns the bound account.
Definition at line 190 of file broker_session.hpp.
Referenced by async_buying_power(), async_cancel(), async_commissions(), async_holdings(), async_order(), async_order_history(), async_place(), async_replace(), async_sellable_quantity(), async_working_orders(), and BrokerSession().
|
inline |
Requests bars for an instrument, paging until count_ bars are read.
Bars arrive oldest first, with the broker's stamp convention in BarSeries::stamp_.
| CompletionToken | A completion token for void(Result<BarSeries>), such as a callback or boost::asio::use_future. |
| query | What to fetch; an interval the broker does not publish is refused before any I/O (see Caps::supports_interval). |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 566 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests what the account may spend in a currency.
The figure was true when the broker answered; a fill since can change it. Requires Caps::has_buying_power.
| CompletionToken | A completion token for void(Result<BuyingPower>), such as a callback or boost::asio::use_future. |
| currency | The currency; one the broker does not price is refused with INVALID_ARGUMENT before any I/O. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 287 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests the trading calendar.
Requires Caps::has_calendar. How many dates one answer covers differs by broker (Caps::has_calendar_range).
| CompletionToken | A completion token for void(Result<TradingCalendar>), such as a callback or boost::asio::use_future. |
| country | The market. |
| from | The date the answer is anchored to; empty means today. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 626 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Cancels a working order, in full or in part.
| CompletionToken | A completion token for void(Result<Placed>), such as a callback or boost::asio::use_future. |
| ref | The order. A reference from another broker does not compile. |
| instruction | Build it with cancel_all or cancel_quantity; a partial cancellation at a broker that cancels only in full is refused before any I/O. The session's has_trigger_price check does not apply to cancellations, so restating a stop's trigger (as cancel_all does) does not block its withdrawal; the adapter still validates the instruction (Kiwoom refuses a trigger that differs from the resting one). |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 466 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests the account's commission rates.
Requires Caps::has_commission_schedule. An empty schedule means the account has no stated rates.
| CompletionToken | A completion token for void(Result<CommissionSchedule>), such as a callback or boost::asio::use_future. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 353 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests an instrument's order book, best levels first.
Requires Caps::has_depth.
| CompletionToken | A completion token for void(Result<DepthSnapshot>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 756 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests the account's holdings.
| CompletionToken | A completion token for void(Result<HoldingsSnapshot>), such as a callback or boost::asio::use_future. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 263 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests an instrument's reference data.
Requires Caps::has_reference_data. One instrument per call; for many, see Caps::has_batch_reference_data and client(). price_rule_ is the library's own grid.
| CompletionToken | A completion token for void(Result<Instrument>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 594 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests one order by its reference.
Requires Caps::has_order_by_id; otherwise read it from working orders or order history.
| CompletionToken | A completion token for void(Result<OrderSnapshot>), such as a callback or boost::asio::use_future. |
| ref | The order. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 786 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests order history: placed orders and their executions.
| CompletionToken | A completion token for void(Result<OrderPage>), such as a callback or boost::asio::use_future. |
| query | The filter; one the broker cannot apply is refused with INVALID_ARGUMENT before any I/O. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 531 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Places a new order.
| CompletionToken | A completion token for void(Result<Placed>), such as a callback or boost::asio::use_future. |
| order | The order. An idempotency key at a broker without one, a trigger at a broker without Caps::has_trigger_price, or a combination the adapter does not offer is refused before any I/O. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 389 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests an instrument's price limits for today.
Requires Caps::has_price_limits.
| CompletionToken | A completion token for void(Result<PriceLimits>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 661 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests quotes for several instruments in one request.
Requires Caps::has_batch_prices; otherwise call async_quote per instrument.
| CompletionToken | A completion token for void(Result<std::vector<QuoteSnapshot>>), such as a callback or boost::asio::use_future. |
| instruments | The instruments. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 726 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests the current quote for an instrument.
| CompletionToken | A completion token for void(Result<QuoteSnapshot>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 244 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Modifies a working order.
| CompletionToken | A completion token for void(Result<Placed>), such as a callback or boost::asio::use_future. |
| ref | The order. |
| instruction | What to change; build it with replace_with or reprice. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 423 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests how much of an instrument the account may sell.
Only where Caps::sellable_source is INQUIRY; elsewhere read HoldingPosition::sellable_quantity_ from the holdings.
| CompletionToken | A completion token for void(Result<Decimal64>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 318 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests recent trades for an instrument, newest first.
Requires Caps::has_trade_prints.
| CompletionToken | A completion token for void(Result<std::vector<TradePrint>>), such as a callback or boost::asio::use_future. |
| instrument | The instrument. |
| count | How many trades to request. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 694 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Requests the orders still open to modification or cancellation.
Requires Caps::has_working_orders. KIS does not serve this inquiry to demo accounts; its adapter answers from the day's order history there.
| CompletionToken | A completion token for void(Result<WorkingSet>), such as a callback or boost::asio::use_future. |
| token | The completion token; the handler runs once, on its associated executor. |
Definition at line 500 of file broker_session.hpp.
Referenced by algotradelib::conformance::call_claimed_async_verbs().
|
inlinestaticconstexprnoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Returns the broker client, for broker-specific operations the session does not offer.
Use it inside an if constexpr on Caps so the broker-specific code compiles only where the feature exists.
Definition at line 200 of file broker_session.hpp.
Referenced by async_bars(), async_buying_power(), async_calendar(), async_cancel(), async_commissions(), async_depth(), async_holdings(), async_instrument(), async_order(), async_order_history(), async_place(), async_price_limits(), async_prices(), async_quote(), async_replace(), async_sellable_quantity(), async_trades(), async_working_orders(), BrokerSession(), and algotradelib::conformance::call_claimed_async_verbs().
|
inline |
Cancels the client's transport work and queued operations.
Definition at line 232 of file broker_session.hpp.
|
inline |
Returns the client's request-pacing counters.
A rising delayed_ on a polling group while TRADING stays undelayed means trading capacity is being kept free; a rising shed_ means the queue was full or the budget is too slow for the timeout.
Definition at line 227 of file broker_session.hpp.
|
inlinenoexcept |
Returns the shared client pointer, for binding a second session.
Definition at line 212 of file broker_session.hpp.