algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::BrokerSession< Client > Class Template Reference

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.

Detailed Description

template<typename Client>
class algotradelib::BrokerSession< Client >

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).

Template Parameters
ClientAn asynchronous broker client (BrokerTraits<Client>::blocking is false); use BlockingSession for a blocking one.

Definition at line 133 of file broker_session.hpp.

Member Typedef Documentation

◆ Account

template<typename Client>
using algotradelib::BrokerSession< Client >::Account = typename Traits::Account

The broker's account selector type.

Definition at line 142 of file broker_session.hpp.

◆ BrokerTag

template<typename Client>
using algotradelib::BrokerSession< Client >::BrokerTag = typename Traits::BrokerTag

The broker's tag type.

Definition at line 148 of file broker_session.hpp.

◆ Caps

template<typename Client>
using algotradelib::BrokerSession< Client >::Caps = BrokerCaps<Client>

The client's capabilities.

Definition at line 139 of file broker_session.hpp.

◆ Config

template<typename Client>
using algotradelib::BrokerSession< Client >::Config = typename Traits::Config

The broker's configuration type.

Definition at line 145 of file broker_session.hpp.

◆ Placed

template<typename Client>
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.

◆ Ref

template<typename Client>
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.

◆ Traits

template<typename Client>
using algotradelib::BrokerSession< Client >::Traits = BrokerTraits<Client>

The client's traits.

Definition at line 136 of file broker_session.hpp.

◆ Working

template<typename Client>
using algotradelib::BrokerSession< Client >::Working = WorkingOrder<BrokerTag>

One working order of this broker.

Definition at line 157 of file broker_session.hpp.

◆ WorkingSet

template<typename Client>
using algotradelib::BrokerSession< Client >::WorkingSet = WorkingOrders<BrokerTag>

A working-order listing of this broker.

Definition at line 160 of file broker_session.hpp.

Constructor & Destructor Documentation

◆ BrokerSession()

template<typename Client>
algotradelib::BrokerSession< Client >::BrokerSession ( std::shared_ptr< Client > client,
Account account )
inline

Binds a client and an account.

Parameters
clientThe client; must not be null.
accountThe account to act for.
Exceptions
std::invalid_argumentif client is null.

Definition at line 173 of file broker_session.hpp.

Member Function Documentation

◆ account()

template<typename Client>
const Account & algotradelib::BrokerSession< Client >::account ( ) const
inlinenoexcept

◆ async_bars()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_bars ( BarQuery query,
CompletionToken && token )
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_.

Template Parameters
CompletionTokenA completion token for void(Result<BarSeries>), such as a callback or boost::asio::use_future.
Parameters
queryWhat to fetch; an interval the broker does not publish is refused before any I/O (see Caps::supports_interval).
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 566 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_buying_power()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_buying_power ( Currency currency,
CompletionToken && token )
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.

Template Parameters
CompletionTokenA completion token for void(Result<BuyingPower>), such as a callback or boost::asio::use_future.
Parameters
currencyThe currency; one the broker does not price is refused with INVALID_ARGUMENT before any I/O.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 287 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_calendar()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_calendar ( Country country,
std::optional< Date > from,
CompletionToken && token )
inline

Requests the trading calendar.

Requires Caps::has_calendar. How many dates one answer covers differs by broker (Caps::has_calendar_range).

Template Parameters
CompletionTokenA completion token for void(Result<TradingCalendar>), such as a callback or boost::asio::use_future.
Parameters
countryThe market.
fromThe date the answer is anchored to; empty means today.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 626 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_cancel()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_cancel ( Ref ref,
AmendInstruction instruction,
CompletionToken && token )
inline

Cancels a working order, in full or in part.

Warning
An error for which runner::outcome_of returns INDETERMINATE (reached_broker_ set and a category of TRANSPORT, CANCELLED or PROTOCOL, or HTTP 500 or more) leaves the outcome unknown: the request may have taken effect. The request is never resent automatically. Check working orders or order history before sending it again.
Template Parameters
CompletionTokenA completion token for void(Result<Placed>), such as a callback or boost::asio::use_future.
Parameters
refThe order. A reference from another broker does not compile.
instructionBuild 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).
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 466 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_commissions()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_commissions ( CompletionToken && token)
inline

Requests the account's commission rates.

Requires Caps::has_commission_schedule. An empty schedule means the account has no stated rates.

Template Parameters
CompletionTokenA completion token for void(Result<CommissionSchedule>), such as a callback or boost::asio::use_future.
Parameters
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 353 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_depth()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_depth ( InstrumentId instrument,
CompletionToken && token )
inline

Requests an instrument's order book, best levels first.

Requires Caps::has_depth.

Template Parameters
CompletionTokenA completion token for void(Result<DepthSnapshot>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 756 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_holdings()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_holdings ( CompletionToken && token)
inline

Requests the account's holdings.

Template Parameters
CompletionTokenA completion token for void(Result<HoldingsSnapshot>), such as a callback or boost::asio::use_future.
Parameters
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 263 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_instrument()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_instrument ( InstrumentId instrument,
CompletionToken && token )
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.

Template Parameters
CompletionTokenA completion token for void(Result<Instrument>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 594 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_order()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_order ( Ref ref,
CompletionToken && token )
inline

Requests one order by its reference.

Requires Caps::has_order_by_id; otherwise read it from working orders or order history.

Template Parameters
CompletionTokenA completion token for void(Result<OrderSnapshot>), such as a callback or boost::asio::use_future.
Parameters
refThe order.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 786 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_order_history()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_order_history ( OrderHistoryQuery query,
CompletionToken && token )
inline

Requests order history: placed orders and their executions.

Template Parameters
CompletionTokenA completion token for void(Result<OrderPage>), such as a callback or boost::asio::use_future.
Parameters
queryThe filter; one the broker cannot apply is refused with INVALID_ARGUMENT before any I/O.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 531 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_place()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_place ( OrderRequest order,
CompletionToken && token )
inline

Places a new order.

Warning
An error for which runner::outcome_of returns INDETERMINATE (reached_broker_ set and a category of TRANSPORT, CANCELLED or PROTOCOL, or HTTP 500 or more) leaves the outcome unknown: the request may have taken effect. The request is never resent automatically. Check working orders or order history before sending it again.
Template Parameters
CompletionTokenA completion token for void(Result<Placed>), such as a callback or boost::asio::use_future.
Parameters
orderThe 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.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 389 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_price_limits()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_price_limits ( InstrumentId instrument,
CompletionToken && token )
inline

Requests an instrument's price limits for today.

Requires Caps::has_price_limits.

Template Parameters
CompletionTokenA completion token for void(Result<PriceLimits>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 661 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_prices()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_prices ( std::vector< InstrumentId > instruments,
CompletionToken && token )
inline

Requests quotes for several instruments in one request.

Requires Caps::has_batch_prices; otherwise call async_quote per instrument.

Template Parameters
CompletionTokenA completion token for void(Result<std::vector<QuoteSnapshot>>), such as a callback or boost::asio::use_future.
Parameters
instrumentsThe instruments.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 726 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_quote()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_quote ( InstrumentId instrument,
CompletionToken && token )
inline

Requests the current quote for an instrument.

Template Parameters
CompletionTokenA completion token for void(Result<QuoteSnapshot>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 244 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_replace()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_replace ( Ref ref,
AmendInstruction instruction,
CompletionToken && token )
inline

Modifies a working order.

Warning
An error for which runner::outcome_of returns INDETERMINATE (reached_broker_ set and a category of TRANSPORT, CANCELLED or PROTOCOL, or HTTP 500 or more) leaves the outcome unknown: the request may have taken effect. The request is never resent automatically. Check working orders or order history before sending it again.
Template Parameters
CompletionTokenA completion token for void(Result<Placed>), such as a callback or boost::asio::use_future.
Parameters
refThe order.
instructionWhat to change; build it with replace_with or reprice.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 423 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_sellable_quantity()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_sellable_quantity ( InstrumentId instrument,
CompletionToken && token )
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.

Template Parameters
CompletionTokenA completion token for void(Result<Decimal64>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 318 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_trades()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_trades ( InstrumentId instrument,
int count,
CompletionToken && token )
inline

Requests recent trades for an instrument, newest first.

Requires Caps::has_trade_prints.

Template Parameters
CompletionTokenA completion token for void(Result<std::vector<TradePrint>>), such as a callback or boost::asio::use_future.
Parameters
instrumentThe instrument.
countHow many trades to request.
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 694 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ async_working_orders()

template<typename Client>
template<typename CompletionToken>
auto algotradelib::BrokerSession< Client >::async_working_orders ( CompletionToken && token)
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.

Template Parameters
CompletionTokenA completion token for void(Result<WorkingSet>), such as a callback or boost::asio::use_future.
Parameters
tokenThe completion token; the handler runs once, on its associated executor.
Returns
Whatever token produces (nothing for a callback, a future for use_future).

Definition at line 500 of file broker_session.hpp.

Referenced by algotradelib::conformance::call_claimed_async_verbs().

◆ broker_name()

template<typename Client>
constexpr std::string_view algotradelib::BrokerSession< Client >::broker_name ( )
inlinestaticconstexprnoexcept

Returns the broker's name.

Returns
Traits::name.

Definition at line 184 of file broker_session.hpp.

◆ client() [1/2]

template<typename Client>
const Client & algotradelib::BrokerSession< Client >::client ( ) const
inlinenoexcept

Returns the broker client.

Returns
The client.

Definition at line 206 of file broker_session.hpp.

◆ client() [2/2]

template<typename Client>
Client & algotradelib::BrokerSession< Client >::client ( )
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.

Returns
The client.

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().

◆ close()

template<typename Client>
void algotradelib::BrokerSession< Client >::close ( )
inline

Cancels the client's transport work and queued operations.

Definition at line 232 of file broker_session.hpp.

◆ rate_limit_stats()

template<typename Client>
const network::RateLimitStats * algotradelib::BrokerSession< Client >::rate_limit_stats ( ) const
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.

Warning
The counters are plain integers the client updates on its own strand. Read them only when no request is in flight, or from code running on that strand; a completion handler bound to another executor can race with other requests.
Returns
The counters, or nullptr if the client is not paced.

Definition at line 227 of file broker_session.hpp.

◆ shared_client()

template<typename Client>
const std::shared_ptr< Client > & algotradelib::BrokerSession< Client >::shared_client ( ) const
inlinenoexcept

Returns the shared client pointer, for binding a second session.

Returns
The client pointer.

Definition at line 212 of file broker_session.hpp.


The documentation for this class was generated from the following file: