algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
session_executor.hpp
1#pragma once
2
3#include "../runner/execution.hpp"
5
6#include <algotradelib/broker>
7
8#include <optional>
9#include <string>
10#include <utility>
11
12namespace algotradelib {
13namespace live {
14
24template <typename Session>
26public:
31 static constexpr bool reaches_a_broker = true;
33 using Caps = typename Session::Caps;
35 using BrokerTag = typename Session::BrokerTag;
36
37 static_assert(
38 !Caps::data_only,
39 "SessionExecutor sends orders, and this session's client is a market-data source (DataOnlyTraits) "
40 "that takes none: bind a broker's session here, and give the data source to SessionSource as its "
41 "market-data session"
42 );
43
49 explicit SessionExecutor(Session& session, Venue default_route = Venue::UNSPECIFIED)
50 : session_(&session), default_route_(default_route) {}
51
72 Venue default_route() const noexcept {
73 return default_route_;
74 }
75
81 Venue requested_route(Venue asked) const noexcept {
82 return asked == Venue::UNSPECIFIED ? default_route_ : asked;
83 }
84
95 Venue effective_route(Venue asked) const noexcept {
96 return traits_route<Session>(requested_route(asked), 0);
97 }
98
110 if (order.client_order_id_) {
111 if constexpr (!Caps::has_client_order_id) {
112 last_error_ = BrokerError{};
114 last_error_->reached_broker_ = false;
115 last_error_->message_ = "this broker accepts no idempotency key; the intent asked for one";
116 // Refused here, not by the broker: nothing was sent, so nothing may be counted against the
117 // rate limit. Reporting this as a broker rejection spent budget for a request nobody made.
119 }
120 }
121
122 OrderRequest request;
123 request.instrument_ = order.instrument_;
124 request.side_ = order.side_;
125 request.type_ = order.type_;
126 request.time_in_force_ = order.time_in_force_;
127 // All three, every time. A fact not copied here never reaches the wire, so
128 // the order placed is not the order the caller planned -- and it fails
129 // silently, because a default is a valid value rather than an empty one.
130 request.session_ = order.session_;
131 // The runner has already written the route in; applied again for an executor driven by hand.
132 request.venue_ = requested_route(order.venue_);
133 request.quantity_ = order.quantity_;
134 request.price_ = order.price_;
135 // A broker whose `has_trigger_price` is false is refused it by the session before its adapter runs,
136 // which `outcome_of` reports as a local refusal: nothing was sent.
137 request.trigger_price_ = order.trigger_price_;
138 if constexpr (Caps::has_client_order_id) {
139 request.client_order_id_ = order.client_order_id_;
140 }
141
142 auto result = session_->place(std::move(request));
143 if (result) {
144 last_ref_ = result.value().ref_;
145 last_error_.reset();
147 }
148 last_error_ = result.error();
149 return runner::outcome_of(*last_error_);
150 }
151
161 if (working.ref_.primary_.empty()) {
162 // **A reference naming nothing addresses nothing at a broker.** A row can reach here with an
163 // empty one: a runner reserves an order it has just sent so that later limits count it, and
164 // an executor that could not report what the broker issued leaves the reference blank.
165 // Sending that is a request which can only fail while looking like an attempt, and a
166 // cancellation failure is the one this library must not manufacture noise around. Refused as
167 // an answer rather than as an outage, because nothing is in doubt: the order was never
168 // addressed, so there is nothing to reconcile.
169 //
170 // The check is here and not in the runner because a reference is only meaningful to a
171 // broker. A replay's engine matches a cancellation on instrument and side, and refusing one
172 // there would leave a queued order to fill that the caller asked to withdraw.
173 last_error_ = BrokerError{};
175 last_error_->reached_broker_ = false;
176 last_error_->message_ = "no broker reference for this order; it cannot be withdrawn until a listing supplies one";
178 }
179 auto result = session_->cancel(working.ref_, cancel_all(working));
180 if (result) {
181 last_error_.reset();
183 }
184 last_error_ = result.error();
185 return runner::outcome_of(*last_error_);
186 }
187
198 std::optional<Decimal64> sellable_quantity(const InstrumentId& instrument) {
199 if constexpr (Caps::sellable_source == SellableSource::INQUIRY) {
200 auto result = session_->sellable_quantity(instrument);
201 if (!result) {
202 last_error_ = result.error();
203 return std::nullopt;
204 }
205 last_error_.reset();
206 return result.value();
207 } else {
208 static_cast<void>(instrument);
209 return std::nullopt;
210 }
211 }
212
214 static constexpr bool inquires_sellable = Caps::sellable_source == SellableSource::INQUIRY;
215
221 // Read through the same gate as `BrokerCaps`, because `Caps` is a caller's type parameter and need
222 // not be `BrokerCaps`: a hand-written capability set that makes the inquiry states its basis.
223 return caps_detail::inquiry_basis<Caps>();
224 }
225
230 const std::optional<OrderRef<BrokerTag>>& last_ref() const noexcept {
231 return last_ref_;
232 }
233
238 const std::optional<BrokerError>& last_error() const noexcept {
239 return last_error_;
240 }
241
242private:
243 // Asks the session's traits where `requested` goes, where they say.
244 template <typename S>
245 static constexpr auto traits_route(Venue requested, int) noexcept -> decltype(S::Traits::effective_route(requested)) {
246 return S::Traits::effective_route(requested);
247 }
248
249 // The fallback, for traits that do not say: the requested route is the most that is known.
250 template <typename S>
251 static constexpr Venue traits_route(Venue requested, long) noexcept {
252 return requested;
253 }
254
255 Session* session_;
256 Venue default_route_;
257 std::optional<OrderRef<BrokerTag>> last_ref_;
258 std::optional<BrokerError> last_error_;
259};
260
261} // namespace live
262} // namespace algotradelib
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.
Definition bind.hpp:12
SendOutcome
What became of one instruction (a placement or a cancellation) an executor was asked to send.
Definition execution.hpp:30
@ ACCEPTED
The broker accepted the instruction (for a placement, the order exists).
Definition execution.hpp:32
@ REFUSED_LOCALLY
The binding refused the instruction before sending it (such as an idempotency key at a broker without...
Definition execution.hpp:63
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.
Definition types.hpp:1366
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
AmendInstruction cancel_all(const WorkingOrder< Broker > &order)
Makes an instruction cancelling all that remains of an order.
Definition types.hpp:2654
Venue
A trading venue: an exchange, or the combined view across exchanges.
Definition types.hpp:45
Reconciliation: turning a strategy's intents and targets into the orders and cancellations to send.
The error a broker or transport operation returns.
Definition types.hpp:1003
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85
An order to place, in broker-neutral form.
Definition types.hpp:2216
std::optional< std::string > client_order_id_
An idempotency key, if the broker offers one; never generated by the library.
Definition types.hpp:2257
Decimal64 quantity_
The quantity, in the instrument's units (shares).
Definition types.hpp:2241
OrderType type_
The price basis: limit, market, best-limit, and so on.
Definition types.hpp:2222
InstrumentId instrument_
The instrument; its venue_ is identity, and routing is this struct's venue_.
Definition types.hpp:2218
Venue venue_
Where to route the order, if the broker offers a choice.
Definition types.hpp:2239
std::optional< Decimal64 > price_
The price; required when carries_caller_price(type_), refused otherwise.
Definition types.hpp:2243
OrderSide side_
Buy or sell.
Definition types.hpp:2220
OrderSession session_
The trading session.
Definition types.hpp:2226
TimeInForce time_in_force_
How long the order stays valid.
Definition types.hpp:2224
std::optional< Decimal64 > trigger_price_
The trigger price; required for STOP_LIMIT, refused otherwise.
Definition types.hpp:2250
What a sellable quantity covers, and how RiskGuard must treat resting sells against it.
Definition types.hpp:1191
An order still open to modification or cancellation.
Definition types.hpp:2582
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
Definition types.hpp:2586
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.