algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
execution.hpp
1#pragma once
2
4
5#include <algotradelib/core>
6
7#include <array>
8#include <chrono>
9#include <string>
10#include <string_view>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15namespace algotradelib {
16namespace runner {
17
65
74
80inline constexpr std::string_view send_outcome_name(SendOutcome outcome) noexcept {
81 switch (outcome) {
83 return "accepted";
85 return "rejected";
87 return "indeterminate";
89 return "unavailable";
91 return "refused_locally";
92 }
93 return "";
94}
95
96static_assert(
99 [](SendOutcome outcome) {
100 return !send_outcome_name(outcome).empty();
101 }
102 ),
103 "all_send_outcomes must list every SendOutcome"
104);
105
122inline SendOutcome outcome_of(const BrokerError& error) {
123 if (!error.reached_broker_) {
124 // Checked first: a request that never went out settles the question whatever the category.
126 }
127 switch (error.category_) {
138 // The broker was asked and refused (a client's own validation was caught above).
140 }
141 // An unrecognized category is treated as unknown, not refused.
143}
144
155inline constexpr bool error_retries_when_unsent(ErrorCategory category) noexcept {
156 switch (category) {
158 return false;
165 return true;
166 }
167 return false;
168}
169
181inline bool broker_unreachable(const BrokerError& error) noexcept {
182 return !error.reached_broker_ && error_retries_when_unsent(error.category_);
183}
184
195inline bool broker_rate_limited(const BrokerError& error) noexcept {
196 return error.reached_broker_ && error_category_is_rate_limit(error.category_);
197}
198
204inline constexpr bool send_outcome_reached_broker(SendOutcome outcome) noexcept {
205 switch (outcome) {
209 return true;
212 return false;
213 }
214 return true;
215}
216
223inline constexpr bool send_outcome_is_local_failure(SendOutcome outcome) noexcept {
224 switch (outcome) {
226 return true;
231 return false;
232 }
233 return true;
234}
235
236namespace runner_detail {
237
238// Whether `Executor` can put an order in front of a real broker.
239//
240// Declared by the executor, because it is the executor that knows. It decides one thing: whether a
241// runner built on it must have read the account before it places. That requirement has to hold for
242// every live path, and there are more of those than the two binding factories --
243// `doxygen/vignettes/binding-a-broker.md` shows a `Runner<..., SessionExecutor<Session>>` assembled
244// by hand, and a rule installed only by the factories would hold for their callers and nobody else.
245//
246// **The default is `true`, and an executor opts *out*.** That is the opposite of how a detection
247// trait beside it usually reads, and it is deliberate: the two directions fail differently. An unmarked
248// executor assumed harmless is a consumer's own adapter -- one whose `place` forwards to `SessionExecutor` or
249// straight to a broker client -- silently exempted from the rule, and it fails by sending a real order sized
250// against an account nobody read. An unmarked executor assumed live is a test double that refuses until it is
251// given a stamped portfolio or says `allow_absent_portfolio()`, and it fails by being noticed at once. Only
252// one of those two is discoverable, so silence means live.
253//
254// `RecordingExecutor` and `backtest::QueueingExecutor` therefore say `false` explicitly: a dry run
255// and a replay have no holdings inquiry that could have failed, and a replay stamps its own
256// portfolio. `JournalingExecutor` forwards whatever it wraps, because recording an order does not
257// change who receives it.
258template <typename Executor, typename = void>
259struct sends_to_a_broker : std::true_type {};
260
261template <typename Executor>
262struct sends_to_a_broker<Executor, std::void_t<decltype(Executor::reaches_a_broker)>>
263: std::bool_constant<Executor::reaches_a_broker> {};
264
265} // namespace runner_detail
266
282
288inline constexpr std::string_view withheld_reason_name(WithheldReason reason) noexcept {
289 switch (reason) {
291 return "reconcile_required";
293 return "local_failure";
295 return "awaiting_confirmation";
296 }
297 return "unknown";
298}
299
333
339inline constexpr std::string_view withheld_plan_reason_name(WithheldPlanReason reason) noexcept {
340 switch (reason) {
342 return "portfolio_absent";
344 return "portfolio_stale";
346 return "portfolio_superseded";
348 return "placement_held";
350 return "awaiting_confirmation";
351 }
352 return "unknown";
353}
354
360template <typename BrokerTag>
375
389
396inline constexpr int obligation_weight(ObligationState state) noexcept {
397 switch (state) {
399 return 0;
401 return 1;
403 return 2;
405 return 3;
406 }
407 return 3;
408}
409
416inline constexpr bool obligation_is_unaccounted(ObligationState state) noexcept {
417 switch (state) {
420 return true;
423 return false;
424 }
425 return true;
426}
427
434inline constexpr bool obligation_needs_operator(ObligationState state) noexcept {
435 switch (state) {
437 return true;
441 return false;
442 }
443 return true;
444}
445
451 std::string journal_id_;
453 std::string order_id_;
455 OrderSide side_{OrderSide::BUY};
459 std::chrono::system_clock::time_point sent_at_{};
462};
463
469inline constexpr std::string_view obligation_state_name(ObligationState state) noexcept {
470 switch (state) {
472 return "none";
474 return "named";
476 return "unnamed";
478 return "operator";
479 }
480 return "unknown";
481}
482
491 std::string code_;
493 std::string message_;
497 std::string tag_;
498};
499
509
524public:
530 void mark(const InstrumentId& id, std::string detail) {
531 for (auto& entry : pending_) {
532 if (entry.instrument_ == id) {
533 // Keep the first reason: the earliest unresolved instruction is the one in doubt.
534 return;
535 }
536 }
537 pending_.push_back(Entry{id, std::move(detail)});
538 }
539
545 bool requires_reconcile(const InstrumentId& id) const {
546 for (const auto& entry : pending_) {
547 if (entry.instrument_ == id) {
548 return true;
549 }
550 }
551 return false;
552 }
553
559 std::string_view detail(const InstrumentId& id) const {
560 for (const auto& entry : pending_) {
561 if (entry.instrument_ == id) {
562 return entry.detail_;
563 }
564 }
565 return {};
566 }
567
573 for (auto at = pending_.begin(); at != pending_.end(); ++at) {
574 if (at->instrument_ == id) {
575 pending_.erase(at);
576 return;
577 }
578 }
579 }
580
585 std::vector<InstrumentId> pending() const {
586 std::vector<InstrumentId> ids;
587 ids.reserve(pending_.size());
588 for (const auto& entry : pending_) {
589 ids.push_back(entry.instrument_);
590 }
591 return ids;
592 }
593
598 bool empty() const noexcept {
599 return pending_.empty();
600 }
601
602private:
603 struct Entry {
604 InstrumentId instrument_;
605 std::string detail_;
606 };
607
608 std::vector<Entry> pending_;
609};
610
618template <typename BrokerTag>
620public:
622 static constexpr bool reaches_a_broker = false;
629 placed_.push_back(order);
631 }
632
639 canceled_.push_back(working.ref_);
641 }
642
647 const std::vector<strategy::PlannedOrder>& placed() const noexcept {
648 return placed_;
649 }
650
655 const std::vector<OrderRef<BrokerTag>>& canceled() const noexcept {
656 return canceled_;
657 }
658
659private:
660 std::vector<strategy::PlannedOrder> placed_;
661 std::vector<OrderRef<BrokerTag>> canceled_;
662};
663
664} // namespace runner
665} // namespace algotradelib
An exact signed decimal: an int64 coefficient times 10^-scale.
Definition types.hpp:443
The instruments with an order of unknown outcome, where nothing may be placed until the broker's orde...
std::string_view detail(const InstrumentId &id) const
Returns why an instrument is unresolved.
bool empty() const noexcept
Tests whether nothing is unresolved.
std::vector< InstrumentId > pending() const
Returns every instrument waiting for reconciliation.
bool requires_reconcile(const InstrumentId &id) const
Tests whether an instrument is waiting for reconciliation.
void mark(const InstrumentId &id, std::string detail)
Records an instrument's order of unknown outcome; an existing entry keeps its first detail.
void resolve_after_reading_orders(const InstrumentId &id)
Clears an instrument; call it only after reading the broker's order list for it.
An executor that sends nothing and records what it was given (the dry run's default).
const std::vector< strategy::PlannedOrder > & placed() const noexcept
Returns every placement recorded.
SendOutcome place(const strategy::PlannedOrder &order)
Records a placement.
static constexpr bool reaches_a_broker
false: a dry run reaches no broker, so the runner does not require a read account.
const std::vector< OrderRef< BrokerTag > > & canceled() const noexcept
Returns every cancellation recorded.
SendOutcome cancel(const WorkingOrder< BrokerTag > &working)
Records a cancellation.
Runs a strategy: the runner, the polling loop, the order journal, and stopping.
Definition clock.hpp:7
bool broker_unreachable(const BrokerError &error) noexcept
Tests whether an error means the broker could not be reached (as opposed to the binding refusing the ...
constexpr bool send_outcome_reached_broker(SendOutcome outcome) noexcept
Tests whether an outcome means a request reached the broker.
constexpr bool obligation_is_unaccounted(ObligationState state) noexcept
Tests whether an order in this state counts toward the loop's owed-order time limit (PollingLoop::set...
constexpr std::array< SendOutcome, 5 > all_send_outcomes
Every SendOutcome, in declaration order.
Definition execution.hpp:67
constexpr std::string_view obligation_state_name(ObligationState state) noexcept
Returns a lowercase name for a state, such as "named", for logs.
SendOutcome
What became of one instruction (a placement or a cancellation) an executor was asked to send.
Definition execution.hpp:30
@ UNAVAILABLE
Local machinery failed (such as the order journal not being writable), so nothing was sent.
Definition execution.hpp:55
@ INDETERMINATE
The outcome is unknown (see outcome_of): the request failed in transit or was cancelled,...
Definition execution.hpp:48
@ REJECTED
The broker refused the instruction (for a placement, the order does not exist); nothing needs reconci...
Definition execution.hpp:34
@ 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
WithheldPlanReason
Why the runner held back a whole reconciliation plan (cancellations and placements) before sending an...
@ PLACEMENT_HELD
A hold stops placements, so the plan's cancellations are held too (a reprice must not become a bare c...
@ PORTFOLIO_ABSENT
No holdings have been read, so the plan was computed against an account that looks flat (and could ca...
@ AWAITING_CONFIRMATION
A serialized runner may not place on the instrument yet, so the plan's cancellations are held too: an...
@ PORTFOLIO_STALE
The holdings the plan was computed from are older than the configured maximum age.
@ PORTFOLIO_SUPERSEDED
The working-order listing is newer than the holdings the plan was sized against (see strategy::Portfo...
constexpr std::string_view withheld_reason_name(WithheldReason reason) noexcept
Returns a lowercase name for a reason, such as "reconcile_required", for logs.
WithheldReason
Why the runner held back a planned order without sending it.
@ LOCAL_FAILURE
Local machinery cannot send (see SendOutcome::UNAVAILABLE).
@ AWAITING_CONFIRMATION
A serialized runner already has an order on this instrument that holdings have not yet accounted for ...
@ RECONCILE_REQUIRED
An earlier instruction for this instrument has an unknown outcome; nothing is sent until the broker's...
constexpr bool obligation_needs_operator(ObligationState state) noexcept
Tests whether only an operator can settle an obligation in this state, so no read of the broker's lis...
SendOutcome outcome_of(const BrokerError &error)
Classifies an error from an order request: did the order come to exist?
constexpr std::string_view withheld_plan_reason_name(WithheldPlanReason reason) noexcept
Returns a lowercase name for a reason, such as "portfolio_stale", for logs.
constexpr bool error_retries_when_unsent(ErrorCategory category) noexcept
Tests whether a request that was never sent may be tried again on a later pass, by its error's catego...
constexpr bool send_outcome_is_local_failure(SendOutcome outcome) noexcept
Tests whether an outcome means the runner's own machinery failed (not the broker, and not a check tha...
ObligationState
What a serialized runner still owes on an instrument (Runner::obligation_state).
@ NAMED
An order the broker named, waiting for its history and holdings to account for it.
@ OPERATOR
An order only an operator may account for (Runner::release_obligations).
@ NONE
Nothing: an order may be placed.
@ UNNAMED
An order whose existence is unknown (its reply was lost and no sweep named it).
constexpr int obligation_weight(ObligationState state) noexcept
Returns how much an obligation state weighs when one instrument owes several orders: the instrument r...
constexpr std::string_view send_outcome_name(SendOutcome outcome) noexcept
Returns a lowercase name for an outcome, such as "indeterminate", for logs.
Definition execution.hpp:80
bool broker_rate_limited(const BrokerError &error) noexcept
Tests whether an error is the broker's rate-limit refusal (such as KIS EGW00201).
The library: broker-neutral types (core), and the modules below.
constexpr bool lists_every_value(const std::array< Enum, N > &values, IsEnumerator is_enumerator) noexcept
Tests whether a list holds every value of an enum exactly once, in any order.
ErrorCategory
The kind of failure a BrokerError reports.
Definition types.hpp:948
@ RATE_LIMIT
The broker refused the request for its rate limit.
Definition types.hpp:958
@ HTTP
The broker answered with an HTTP error status (http_status_).
Definition types.hpp:954
@ PROTOCOL
The reply could not be read, or the broker declared a failure this client does not classify further: ...
Definition types.hpp:965
@ CANCELLED
The operation was cancelled, such as by closing the client or a local deadline.
Definition types.hpp:950
@ AUTHENTICATION
The credentials or token were refused.
Definition types.hpp:956
@ TRANSPORT
The connection failed (DNS, connect, TLS, write or read).
Definition types.hpp:952
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
constexpr bool error_category_is_rate_limit(ErrorCategory category) noexcept
Tests whether an error of this category is a broker asking the caller to slow down,...
Definition types.hpp:983
OrderSide
The side of an order: its economic direction, buy or sell.
Definition types.hpp:1974
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
int http_status_
The HTTP status of the reply: set for an HTTP error and for a reply whose body is not a JSON object....
Definition types.hpp:1020
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
bool reached_broker_
Whether a request was sent before this error.
Definition types.hpp:1014
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85
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
The broker's own code and message for a refusal, kept for diagnosis.
std::string tag_
The tag of the instruction.
std::string message_
The broker's message.
std::string code_
The broker's error code, if it gave one.
InstrumentId instrument_
The instrument of the refused instruction.
One order a serialized runner still owes (Runner::obligations).
std::string order_id_
The broker's order id; empty while unnamed.
Decimal64 quantity_
The quantity holdings must still account for.
ObligationState state_
Its state: NAMED, UNNAMED or OPERATOR.
std::chrono::system_clock::time_point sent_at_
When it was sent, on the journal's clock.
InstrumentId instrument_
The instrument.
std::string journal_id_
The id of the order's journal lines.
One order the runner held back without sending, and why.
WithheldReason reason_
Why it was held back.
strategy::PlannedOrder order_
The order.
std::string detail_
A description of why.
One reconciliation plan the runner held back, and what it would have done.
std::vector< OrderRef< BrokerTag > > not_canceled_
The orders the plan would have cancelled.
std::size_t not_placed_
How many orders the plan would have placed.
std::string tag_
The tag of the strategy's instruction.
InstrumentId instrument_
The instrument.
WithheldPlanReason reason_
Why the plan was held back.
std::string detail_
A description of why, naming the hold if there was one.
One order the runner is to place.