3#include "../runner/execution.hpp"
6#include <algotradelib/broker>
24template <
typename Session>
34 using Caps =
typename Session::Caps;
40 "SessionExecutor sends orders, and this session's client is a market-data source (DataOnlyTraits) "
41 "that takes none: bind a broker's session here, and give the data source to SessionSource as its "
81 return asked == Venue::UNSPECIFIED ? default_route_ : asked;
110 if constexpr (!Caps::has_client_order_id) {
113 last_error_->reached_broker_ =
false;
114 last_error_->message_ =
115 "this broker accepts no idempotency key; the intent asked for one";
138 if constexpr (Caps::has_client_order_id) {
142 auto result = session_->place(std::move(request));
144 last_ref_ = result.value().ref_;
148 last_error_ = result.error();
161 if (working.
ref_.primary_.empty()) {
175 last_error_->reached_broker_ =
false;
176 last_error_->message_ =
177 "no broker reference for this order; it cannot be withdrawn until a listing supplies one";
180 auto result = session_->cancel(working.
ref_,
cancel_all(working));
185 last_error_ = result.error();
201 auto result = session_->sellable_quantity(instrument);
203 last_error_ = result.error();
207 return result.value();
209 static_cast<void>(instrument);
224 return caps_detail::inquiry_basis<Caps>();
231 const std::optional<OrderRef<BrokerTag>>&
last_ref() const noexcept {
return last_ref_; }
237 const std::optional<BrokerError>&
last_error() const noexcept {
return last_error_; }
241 template <
typename S>
242 static constexpr auto traits_route(
Venue requested,
int)
noexcept
243 ->
decltype(S::Traits::effective_route(requested)) {
244 return S::Traits::effective_route(requested);
248 template <
typename S>
249 static constexpr Venue traits_route(
Venue requested,
long)
noexcept {
return requested; }
252 Venue default_route_;
253 std::optional<OrderRef<BrokerTag>> last_ref_;
254 std::optional<BrokerError> last_error_;
std::optional< Decimal64 > sellable_quantity(const InstrumentId &instrument)
Asks the broker how much of an instrument may be sold, where the broker answers that by a separate in...
const std::optional< OrderRef< BrokerTag > > & last_ref() const noexcept
Returns the reference the broker issued for the last accepted order.
typename Session::BrokerTag BrokerTag
The broker tag, from the session.
Venue requested_route(Venue asked) const noexcept
Returns the venue this executor requests for an order.
runner::SendOutcome place(const strategy::PlannedOrder &order)
Places one order.
static constexpr bool inquires_sellable
true if the sellable quantity is a separate inquiry (one request per instrument sold).
Venue default_route() const noexcept
Returns the venue an order that names none is sent to.
runner::SendOutcome cancel(const WorkingOrder< BrokerTag > &working)
Cancels the whole remaining quantity of a working order.
const std::optional< BrokerError > & last_error() const noexcept
Returns why the last instruction failed.
static constexpr SellableBasis sellable_inquiry_basis()
Returns what the broker's sellable-quantity inquiry establishes, recorded with each figure.
Venue effective_route(Venue asked) const noexcept
Returns the route an order is sent with, for the journal to record before sending (for SOR,...
static constexpr bool reaches_a_broker
true: a runner built on this executor requires holdings to have been read before it places,...
typename Session::Caps Caps
The broker's capabilities, from the session.
SessionExecutor(Session &session, Venue default_route=Venue::UNSPECIFIED)
Constructs an executor for a session.
Connects a runner to a broker session for live trading.
SendOutcome
What became of one instruction (a placement or a cancellation) an executor was asked to send.
@ ACCEPTED
The broker accepted the instruction (for a placement, the order exists).
@ REFUSED_LOCALLY
The binding refused the instruction before sending it (such as an idempotency key at a broker without...
SendOutcome outcome_of(const BrokerError &error)
Classifies an error from an order request: did the order come to exist?
The library: broker-neutral types (core), and the modules below.
@ 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.
AmendInstruction cancel_all(const WorkingOrder< Broker > &order)
Makes an instruction cancelling all that remains of an order.
Venue
A trading venue: an exchange, or the combined view across exchanges.
Reconciliation: turning a strategy's intents and targets into the orders and cancellations to send.
The error a broker or transport operation returns.
ErrorCategory category_
The kind of failure; the field to branch on.
Identifies an instrument: its country, venue and symbol.
An order to place, in broker-neutral form.
std::optional< std::string > client_order_id_
An idempotency key, if the broker offers one; never generated by the library.
Decimal64 quantity_
The quantity, in the instrument's units (shares).
OrderType type_
The price basis: limit, market, best-limit, and so on.
InstrumentId instrument_
The instrument; its venue_ is identity, and routing is this struct's venue_.
Venue venue_
Where to route the order, if the broker offers a choice.
std::optional< Decimal64 > price_
The price; required when carries_caller_price(type_), refused otherwise.
OrderSide side_
Buy or sell.
OrderSession session_
The trading session.
TimeInForce time_in_force_
How long the order stays valid.
std::optional< Decimal64 > trigger_price_
The trigger price; required for STOP_LIMIT, refused otherwise.
What a sellable quantity covers, and how RiskGuard must treat resting sells against it.
An order still open to modification or cancellation.
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
One order the runner is to place.
TimeInForce time_in_force_
How long the order stays valid.
OrderSession session_
The trading session.
std::optional< std::string > client_order_id_
The idempotency key, if the intent carried one.
Venue venue_
Where to route the order; see OrderRequest::venue_.
std::optional< Decimal64 > trigger_price_
The trigger price of a STOP_LIMIT order, exactly as stated; empty otherwise.
OrderType type_
The order type.
InstrumentId instrument_
The instrument to trade.
std::optional< Decimal64 > price_
The limit price, after clamping and rounding; empty for a market order.
Decimal64 quantity_
The quantity, after rounding onto the lot.