algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
journal.hpp
1#pragma once
2
3#include "./clock.hpp"
4#include "./execution.hpp"
5
7
8#include <algotradelib/core>
9
10#include <algorithm>
11#include <chrono>
12#include <cstddef>
13#include <cstdint>
14#include <iterator>
15#include <memory>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <utility>
20#include <vector>
21
22namespace algotradelib {
23namespace runner {
24
31enum class JournalAction {
33 PLACE,
35 CANCEL,
42 LEDGER,
43};
44
46inline constexpr std::array<JournalAction, 3> all_journal_actions{
50};
51
57inline constexpr std::string_view journal_action_name(JournalAction action) noexcept {
58 switch (action) {
60 return "place";
62 return "cancel";
64 return "ledger";
65 }
66 return "";
67}
68
69static_assert(
72 [](JournalAction action) {
73 return !journal_action_name(action).empty();
74 }
75 ),
76 "all_journal_actions must list every JournalAction"
77);
78
86inline constexpr bool journal_action_matches_by_order_id(JournalAction action) noexcept {
87 switch (action) {
89 return true;
92 return false;
93 }
94 return false;
95}
96
102inline std::optional<JournalAction> journal_action_from(std::string_view text) noexcept {
104}
105
153
165
171inline constexpr std::string_view journal_outcome_name(JournalOutcome outcome) noexcept {
172 switch (outcome) {
174 return "pending";
176 return "accepted";
178 return "rejected";
180 return "indeterminate";
182 return "resolved";
184 return "confirmed";
186 return "held";
188 return "recorded";
189 }
190 return "";
191}
192
193static_assert(
196 [](JournalOutcome outcome) {
197 return !journal_outcome_name(outcome).empty();
198 }
199 ),
200 "all_journal_outcomes must list every JournalOutcome"
201);
202
208inline std::optional<JournalOutcome> journal_outcome_from(std::string_view text) noexcept {
210}
211
220inline constexpr JournalOutcome journal_outcome_of(SendOutcome outcome) noexcept {
221 switch (outcome) {
229 // Nothing was sent, so the order does not exist.
232 // Nothing was sent, so the order does not exist.
234 }
236}
237
243inline constexpr bool journal_outcome_is_terminal(JournalOutcome outcome) noexcept {
244 switch (outcome) {
246 return false;
254 return true;
255 }
256 return true;
257}
258
265inline constexpr bool journal_outcome_answers_send(JournalOutcome outcome) noexcept {
266 switch (outcome) {
270 return true;
276 return false;
277 }
278 return false;
279}
280
286inline constexpr bool journal_outcome_names_order(JournalOutcome outcome) noexcept {
287 switch (outcome) {
290 return true;
297 return false;
298 }
299 return false;
300}
301
307inline constexpr bool journal_outcome_refuses(JournalOutcome outcome) noexcept {
308 switch (outcome) {
310 return true;
318 return false;
319 }
320 return false;
321}
322
329inline constexpr bool journal_outcome_is_closing_account(JournalOutcome outcome) noexcept {
330 switch (outcome) {
333 return true;
340 return false;
341 }
342 return false;
343}
344
351inline constexpr bool journal_outcome_requires_operator(JournalOutcome outcome) noexcept {
352 switch (outcome) {
354 return true;
362 return false;
363 }
364 return false;
365}
366
374inline constexpr bool journal_outcome_leaves_open(JournalOutcome outcome) noexcept {
375 switch (outcome) {
378 return true;
385 return false;
386 }
387 return true;
388}
389
401
403inline constexpr std::array<RunRecordKind, 4> all_run_record_kinds{
408};
409
415inline constexpr std::string_view run_record_kind_name(RunRecordKind kind) noexcept {
416 switch (kind) {
418 return "open";
420 return "deposit";
422 return "withdrawal";
424 return "fill";
425 }
426 return "";
427}
428
429static_assert(
432 [](RunRecordKind kind) {
433 return !run_record_kind_name(kind).empty();
434 }
435 ),
436 "all_run_record_kinds must list every RunRecordKind"
437);
438
444inline std::optional<RunRecordKind> run_record_kind_from(std::string_view text) noexcept {
446}
447
456struct RunRecord {
458 std::string run_;
460 std::optional<RunRecordKind> kind_;
462 std::optional<Money> amount_;
464 std::optional<Decimal64> filled_;
466 std::optional<Decimal64> fee_;
468 bool fee_estimated_{false};
470 std::string given_;
472 std::string of_;
473};
474
488 std::string id_;
494 std::chrono::system_clock::time_point recorded_at_{};
498 OrderSide side_{OrderSide::BUY};
512 std::optional<Venue> venue_{Venue::UNSPECIFIED};
516 std::optional<Decimal64> price_;
518 std::optional<Decimal64> trigger_price_;
525 std::optional<Decimal64> baseline_;
530 std::string order_id_;
532 std::string detail_;
546 std::string scope_;
551 std::shared_ptr<const RunRecord> run_;
552};
553
559inline std::string_view journal_run_of(const JournalEntry& entry) noexcept {
560 return entry.run_ ? std::string_view(entry.run_->run_) : std::string_view();
561}
562
577
583inline constexpr std::string_view journal_read_status_name(JournalReadStatus status) noexcept {
584 switch (status) {
586 return "missing";
588 return "read";
590 return "unreadable";
591 }
592 return "unknown";
593}
594
602 std::vector<JournalEntry> entries_;
604 std::size_t unreadable_lines_{0};
606 bool truncated_tail_{false};
608 std::string detail_;
610 std::string scope_;
612 bool has_header_{false};
615
620 bool readable() const noexcept {
621 switch (status_) {
624 return true;
626 return false;
627 }
628 return false;
629 }
630
635 bool intact() const noexcept {
636 return readable() && unreadable_lines_ == 0 && !truncated_tail_;
637 }
638};
639
650 std::chrono::system_clock::time_point opened_at_{};
655 std::optional<std::chrono::system_clock::time_point> closed_at_;
657 bool answered_{false};
659 std::string detail_;
660};
661
673inline std::vector<OpenInstruction> unresolved_instructions(const JournalReplay& replay) {
674 std::vector<OpenInstruction> open;
675 for (const auto& entry : replay.entries_) {
676 // A ledger record answers no instruction, whatever its id.
677 if (entry.action_ == JournalAction::LEDGER) {
678 continue;
679 }
680 if (!journal_outcome_is_terminal(entry.outcome_)) {
681 OpenInstruction instruction;
682 instruction.entry_ = entry;
683 instruction.opened_at_ = entry.recorded_at_;
684 instruction.detail_ = entry.detail_;
685 open.push_back(std::move(instruction));
686 continue;
687 }
688
689 bool paired = false;
690 for (auto at = open.begin(); at != open.end(); ++at) {
691 if (at->entry_.id_ != entry.id_) {
692 continue;
693 }
694 paired = true;
695 if (journal_outcome_leaves_open(entry.outcome_)) {
696 at->answered_ = true;
697 at->closed_at_ = entry.recorded_at_;
698 at->detail_ = entry.detail_;
699 // The broker issued no reference, so the placement keeps the one its pending line had.
700 at->entry_.order_id_ = entry.order_id_.empty() ? at->entry_.order_id_ : entry.order_id_;
701 } else {
702 open.erase(at);
703 }
704 break;
705 }
706 if (!paired && journal_outcome_leaves_open(entry.outcome_)) {
707 OpenInstruction instruction;
708 instruction.entry_ = entry;
709 instruction.opened_at_ = entry.recorded_at_;
710 instruction.closed_at_ = entry.recorded_at_;
711 instruction.answered_ = true;
712 instruction.detail_ = entry.detail_;
713 open.push_back(std::move(instruction));
714 }
715 }
716 return open;
717}
718
732inline std::vector<OpenInstruction> placements_of(const JournalReplay& replay, const std::vector<std::string>& ids) {
733 std::vector<OpenInstruction> found;
734 for (const auto& entry : replay.entries_) {
735 if (
736 entry.action_ != JournalAction::PLACE || entry.requires_operator_ ||
737 !journal_outcome_leaves_open(entry.outcome_) || std::find(ids.begin(), ids.end(), entry.id_) == ids.end()
738 ) {
739 continue;
740 }
741 auto known = std::find_if(found.begin(), found.end(), [&](const OpenInstruction& instruction) {
742 return instruction.entry_.id_ == entry.id_;
743 });
744 if (known == found.end()) {
745 OpenInstruction instruction;
746 instruction.entry_ = entry;
747 instruction.opened_at_ = entry.recorded_at_;
748 instruction.detail_ = entry.detail_;
749 found.push_back(std::move(instruction));
750 known = std::prev(found.end());
751 }
752 if (journal_outcome_is_terminal(entry.outcome_)) {
753 known->answered_ = true;
754 known->closed_at_ = entry.recorded_at_;
755 known->detail_ = entry.detail_;
756 }
757 }
758 return found;
759}
760
765public:
767 MemoryJournal() = default;
768
773 explicit MemoryJournal(std::string scope) : scope_(std::move(scope)) {}
774
780 bool append(const JournalEntry& entry) {
781 entries_.push_back(entry);
782 if (entries_.back().scope_.empty()) {
783 entries_.back().scope_ = scope_;
784 }
785 return true;
786 }
787
794 replay.entries_ = entries_;
795 return replay;
796 }
797
802 const std::vector<JournalEntry>& records() const noexcept {
803 return entries_;
804 }
805
807 void clear() noexcept {
808 entries_.clear();
809 }
810
815 const std::string& scope() const noexcept {
816 return scope_;
817 }
818
819private:
820 std::vector<JournalEntry> entries_;
821 std::string scope_;
822};
823
841template <typename Inner, typename Journal, typename Clock = SteadyClock>
843public:
845 static constexpr bool reaches_a_broker = runner_detail::sends_to_a_broker<Inner>::value;
846
853 JournalingExecutor(Inner inner, Journal& journal, Clock clock = {})
854 : inner_(std::move(inner)), journal_(&journal), clock_(std::move(clock)) {
855 prefix_ = default_prefix();
856 }
857
868 void set_id_prefix(std::string prefix) {
869 prefix_ = std::move(prefix);
870 }
871
881 void set_run(std::string run) {
882 if (run.empty()) {
883 run_.reset();
884 return;
885 }
886 auto record = std::make_shared<RunRecord>();
887 record->run_ = std::move(run);
888 run_ = std::move(record);
889 }
890
895 std::string next_run_id() {
896 return "run-" + next_id();
897 }
898
909 template <typename E = Inner>
910 auto route_of(const strategy::PlannedOrder& order, int) const
911 -> decltype(std::declval<const E&>().effective_route(order.venue_)) {
912 return inner_.effective_route(order.venue_);
913 }
914
921 Venue route_of(const strategy::PlannedOrder& order, long) const {
922 return order.venue_;
923 }
924
931 return route_of(order, 0);
932 }
933
940 return place(order, std::nullopt);
941 }
942
950 SendOutcome place(const strategy::PlannedOrder& order, std::optional<Decimal64> baseline) {
951 JournalEntry entry;
952 entry.id_ = next_id();
955 entry.recorded_at_ = clock_.now();
956 entry.instrument_ = order.instrument_;
957 entry.side_ = order.side_;
958 entry.type_ = order.type_;
959 entry.time_in_force_ = order.time_in_force_;
960 entry.session_ = order.session_;
961 // Where the order will actually go (an unrouted KIS order reaches KRX), asked of the binding;
962 // a pure function of the order, so the line is still written before any I/O.
963 entry.venue_ = effective_route_of(order);
964 entry.quantity_ = order.quantity_;
965 entry.price_ = order.price_;
966 entry.trigger_price_ = order.trigger_price_;
967 entry.baseline_ = std::move(baseline);
968 entry.run_ = run_;
969 if (!journal_->append(entry)) {
970 return refuse_unjournaled();
971 }
972
973 const auto outcome = inner_.place(order);
974
975 entry.outcome_ = journal_outcome_of(outcome);
976 entry.recorded_at_ = clock_.now();
977 entry.order_id_ = accepted_order_id(outcome);
978 entry.detail_ = failure_detail(outcome);
979 last_entry_ = entry;
980 if (!journal_->append(entry)) {
981 // The send happened; only its answer is lost, so recovery will see it unresolved. Recorded so
982 // the runner stops.
983 note_failure("the order journal could not record an instruction's outcome");
984 }
985 return outcome;
986 }
987
993 const std::optional<JournalEntry>& last_entry() const noexcept {
994 return last_entry_;
995 }
996
1006 bool record(JournalEntry& entry) {
1007 entry.recorded_at_ = clock_.now();
1008 if (journal_->append(entry)) {
1009 return true;
1010 }
1011 note_failure("the order journal could not record that a placement was accounted for");
1012 return false;
1013 }
1014
1021 template <typename BrokerTag>
1023 JournalEntry entry;
1024 entry.id_ = next_id();
1027 entry.recorded_at_ = clock_.now();
1028 entry.instrument_ = working.order_.instrument_;
1029 entry.side_ = working.order_.side_;
1030 entry.type_ = working.order_.type_;
1031 entry.quantity_ = working.order_.quantity_;
1032 entry.price_ = working.order_.price_;
1033 entry.trigger_price_ = working.order_.trigger_price_;
1034 // Known from the start: a cancellation names an existing order.
1035 entry.order_id_ = working.ref_.primary_;
1036 if (!journal_->append(entry)) {
1037 return refuse_unjournaled();
1038 }
1039
1040 const auto outcome = inner_.cancel(working);
1041
1042 entry.outcome_ = journal_outcome_of(outcome);
1043 entry.recorded_at_ = clock_.now();
1044 entry.detail_ = failure_detail(outcome);
1045 if (!journal_->append(entry)) {
1046 note_failure("the order journal could not record an instruction's outcome");
1047 }
1048 return outcome;
1049 }
1050
1055 const Inner& inner() const noexcept {
1056 return inner_;
1057 }
1058
1062 Inner& inner() noexcept {
1063 return inner_;
1064 }
1065
1070 const Journal& journal() const noexcept {
1071 return *journal_;
1072 }
1073
1078 const std::string& last_journal_error() const noexcept {
1079 return journal_error_;
1080 }
1081
1086 std::size_t unjournaled_refusals() const noexcept {
1087 return refusals_;
1088 }
1089
1095 std::size_t journal_failures() const noexcept {
1096 return failures_;
1097 }
1098
1105 template <typename Executor = Inner>
1106 auto requested_route(Venue asked) const noexcept -> decltype(std::declval<const Executor&>().requested_route(asked)) {
1107 return inner_.requested_route(asked);
1108 }
1109
1115 template <typename Executor = Inner>
1116 auto last_ref() const noexcept -> decltype(std::declval<const Executor&>().last_ref()) {
1117 return inner_.last_ref();
1118 }
1119
1125 template <typename Executor = Inner>
1126 auto last_error() const noexcept -> decltype(std::declval<const Executor&>().last_error()) {
1127 return inner_.last_error();
1128 }
1129
1130private:
1131 // Refuses an instruction that could not be recorded, without attempting it.
1132 SendOutcome refuse_unjournaled() {
1133 ++refusals_;
1134 note_failure("the order journal could not be written, so nothing was sent");
1136 }
1137
1138 // Records why the journal failed, keeping the first reason and counting every failure.
1139 //
1140 // The two halves answer different questions. The reason is for a person reading a report, and the
1141 // first one is the useful one. The count is for the runner, which needs to know whether anything
1142 // has gone wrong *since* it last acknowledged a problem.
1143 void note_failure(std::string detail) {
1144 ++failures_;
1145 if (journal_error_.empty()) {
1146 journal_error_ = std::move(detail);
1147 }
1148 }
1149
1150 // Returns a line id unique within this process and, by its prefix, across restarts.
1151 std::string next_id() {
1152 return prefix_ + '-' + std::to_string(++sequence_);
1153 }
1154
1155 // Returns a prefix derived from the moment of construction, on the wall clock rather than `clock_`:
1156 // the prefix tells process runs apart, and an injected clock (a test's, a replay's) may start two
1157 // runs at the same instant.
1158 std::string default_prefix() const {
1159 const auto since = std::chrono::system_clock::now().time_since_epoch();
1160 const auto micros = std::chrono::duration_cast<std::chrono::microseconds>(since).count();
1161 return std::to_string(static_cast<std::int64_t>(micros));
1162 }
1163
1164 // Returns the broker's id for an accepted order, where the inner executor reports one.
1165 //
1166 // Detected rather than required, because `RecordingExecutor` has no references to report and must
1167 // still be wrappable — it is what every journal test runs against.
1168 std::string accepted_order_id(SendOutcome outcome) const {
1169 switch (outcome) {
1171 return accepted_order_id_impl(0);
1176 return {};
1177 }
1178 return {};
1179 }
1180
1181 template <typename Executor = Inner>
1182 auto accepted_order_id_impl(int) const -> decltype(std::declval<const Executor&>().last_ref(), std::string()) {
1183 const auto& ref = inner_.last_ref();
1184 return ref ? ref->primary_ : std::string();
1185 }
1186
1187 std::string accepted_order_id_impl(...) const {
1188 return {};
1189 }
1190
1191 // Returns what the inner executor said went wrong, where it says anything.
1192 std::string failure_detail(SendOutcome outcome) const {
1193 switch (outcome) {
1195 return {};
1200 return failure_detail_impl(0);
1201 }
1202 return failure_detail_impl(0);
1203 }
1204
1205 template <typename Executor = Inner>
1206 auto failure_detail_impl(int) const -> decltype(std::declval<const Executor&>().last_error(), std::string()) {
1207 const auto& error = inner_.last_error();
1208 return error ? error->message_ : std::string();
1209 }
1210
1211 std::string failure_detail_impl(...) const {
1212 return {};
1213 }
1214
1215 Inner inner_;
1216 Journal* journal_;
1217 Clock clock_;
1218 std::optional<JournalEntry> last_entry_;
1219 std::shared_ptr<const RunRecord> run_;
1220 std::string prefix_;
1221 std::string journal_error_;
1222 std::size_t refusals_{0};
1223 std::size_t failures_{0};
1224 std::uint64_t sequence_{0};
1225};
1226
1227} // namespace runner
1228} // namespace algotradelib
An exact signed decimal: an int64 coefficient times 10^-scale.
Definition types.hpp:443
auto last_ref() const noexcept -> decltype(std::declval< const Executor & >().last_ref())
Returns the reference of the last order the wrapped executor placed.
Definition journal.hpp:1116
SendOutcome cancel(const WorkingOrder< BrokerTag > &working)
Records and sends one cancellation.
Definition journal.hpp:1022
std::size_t unjournaled_refusals() const noexcept
Returns how many instructions were not sent because their PENDING line could not be written.
Definition journal.hpp:1086
Inner & inner() noexcept
Returns the wrapped executor.
Definition journal.hpp:1062
Venue route_of(const strategy::PlannedOrder &order, long) const
Returns the venue an order asked for, when the wrapped executor cannot resolve routes (the fallback o...
Definition journal.hpp:921
std::string next_run_id()
Returns a new id for a run budget's ledger line: run-, the prefix, a hyphen and the counter.
Definition journal.hpp:895
const std::optional< JournalEntry > & last_entry() const noexcept
Returns the answering line last written (or attempted) for a placement the wrapped executor was asked...
Definition journal.hpp:993
std::size_t journal_failures() const noexcept
Returns how many journal writes have failed; Runner compares it with the count it last acknowledged t...
Definition journal.hpp:1095
const std::string & last_journal_error() const noexcept
Returns the first journal failure's description.
Definition journal.hpp:1078
void set_run(std::string run)
Names the run budget's run that every later placement belongs to (RunRecord::run_).
Definition journal.hpp:881
Venue effective_route_of(const strategy::PlannedOrder &order) const
Returns the venue to record for an order: the binding's route, or the venue asked for.
Definition journal.hpp:930
auto route_of(const strategy::PlannedOrder &order, int) const -> decltype(std::declval< const E & >().effective_route(order.venue_))
Returns the venue the wrapped executor says an order will reach.
Definition journal.hpp:910
const Journal & journal() const noexcept
Returns the journal.
Definition journal.hpp:1070
void set_id_prefix(std::string prefix)
Sets the prefix of line ids.
Definition journal.hpp:868
SendOutcome place(const strategy::PlannedOrder &order)
Records and places one order.
Definition journal.hpp:939
static constexpr bool reaches_a_broker
Whether the wrapped executor reaches a broker.
Definition journal.hpp:845
SendOutcome place(const strategy::PlannedOrder &order, std::optional< Decimal64 > baseline)
Records and places one order, stating the holding it was decided against (a serialized Runner calls t...
Definition journal.hpp:950
auto requested_route(Venue asked) const noexcept -> decltype(std::declval< const Executor & >().requested_route(asked))
Returns the wrapped executor's route for a requested venue.
Definition journal.hpp:1106
auto last_error() const noexcept -> decltype(std::declval< const Executor & >().last_error())
Returns why the last instruction failed, from the wrapped executor.
Definition journal.hpp:1126
const Inner & inner() const noexcept
Returns the wrapped executor.
Definition journal.hpp:1055
bool record(JournalEntry &entry)
Appends a line the caller built, such as a serialized Runner's CONFIRMED line.
Definition journal.hpp:1006
JournalingExecutor(Inner inner, Journal &journal, Clock clock={})
Wraps an executor.
Definition journal.hpp:853
void clear() noexcept
Forgets every line.
Definition journal.hpp:807
bool append(const JournalEntry &entry)
Appends a line.
Definition journal.hpp:780
MemoryJournal()=default
Constructs a journal with an empty scope.
const std::vector< JournalEntry > & records() const noexcept
Returns the lines recorded.
Definition journal.hpp:802
JournalReplay replay() const
Returns everything recorded.
Definition journal.hpp:792
const std::string & scope() const noexcept
Returns the journal's broker and account.
Definition journal.hpp:815
MemoryJournal(std::string scope)
Constructs a journal.
Definition journal.hpp:773
Runs a strategy: the runner, the polling loop, the order journal, and stopping.
Definition clock.hpp:7
constexpr bool journal_outcome_answers_send(JournalOutcome outcome) noexcept
Tests whether a line with this outcome answers the send itself (the broker's reply,...
Definition journal.hpp:265
std::optional< RunRecordKind > run_record_kind_from(std::string_view text) noexcept
Parses a record kind's name.
Definition journal.hpp:444
std::optional< JournalOutcome > journal_outcome_from(std::string_view text) noexcept
Parses an outcome's name.
Definition journal.hpp:208
constexpr bool journal_outcome_is_closing_account(JournalOutcome outcome) noexcept
Tests whether a line with this outcome is a serialized runner's closing account of a placement (it co...
Definition journal.hpp:329
constexpr std::string_view journal_outcome_name(JournalOutcome outcome) noexcept
Returns an outcome's name as written to the journal ("pending", ...).
Definition journal.hpp:171
JournalReadStatus
Whether a journal file could be read.
Definition journal.hpp:569
@ MISSING
No such file: a first start, nothing owed.
Definition journal.hpp:571
@ READ
The file was read in full.
Definition journal.hpp:573
@ UNREADABLE
The file exists and could not be read; its contents are unknown.
Definition journal.hpp:575
constexpr JournalOutcome journal_outcome_of(SendOutcome outcome) noexcept
Maps a send outcome to the journal outcome that records it.
Definition journal.hpp:220
constexpr bool journal_action_matches_by_order_id(JournalAction action) noexcept
Tests whether recovery matches an instruction of this action by the broker's order id it names,...
Definition journal.hpp:86
constexpr bool journal_outcome_refuses(JournalOutcome outcome) noexcept
Tests whether a line with this outcome says the instruction was refused, so it does not exist.
Definition journal.hpp:307
constexpr std::string_view journal_action_name(JournalAction action) noexcept
Returns an action's name as written to the journal ("place", "cancel").
Definition journal.hpp:57
constexpr std::array< JournalOutcome, 8 > all_journal_outcomes
Every JournalOutcome, in declaration order.
Definition journal.hpp:155
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
constexpr std::string_view journal_read_status_name(JournalReadStatus status) noexcept
Returns a status's lowercase name, such as "unreadable".
Definition journal.hpp:583
RunRecordKind
What a run budget's JournalAction::LEDGER line records (RunRecord::kind_).
Definition journal.hpp:391
@ OPEN
A run begins; its amount is the budget, the run's first deposit, and its line's id is the run's id.
Definition journal.hpp:393
@ WITHDRAWAL
Cash taken from the run, under the operator's id (RunRecord::given_).
Definition journal.hpp:397
@ DEPOSIT
Cash added to the run, under the operator's id (RunRecord::given_).
Definition journal.hpp:395
@ FILL
An operator's booking of a placement released without one (runner::Runner::book_run_fill).
Definition journal.hpp:399
constexpr bool journal_outcome_leaves_open(JournalOutcome outcome) noexcept
Tests whether a line with this outcome leaves its instruction's fate unknown, so recovery must still ...
Definition journal.hpp:374
constexpr std::string_view run_record_kind_name(RunRecordKind kind) noexcept
Returns a record kind's name as written to the journal ("open", ...).
Definition journal.hpp:415
JournalOutcome
What a journal line says about an instruction.
Definition journal.hpp:113
@ CONFIRMED
A serialized runner accounted for the placement: holdings showed what it filled, its history showed i...
Definition journal.hpp:144
@ INDETERMINATE
The outcome is unknown (SendOutcome::INDETERMINATE: a failure in transit, an unreadable reply,...
Definition journal.hpp:128
@ RESOLVED
A recovery sweep read the broker's order list and closed the question.
Definition journal.hpp:136
@ REJECTED
The instruction was refused, by the broker or before it was sent, so it does not exist.
Definition journal.hpp:123
@ ACCEPTED
The broker accepted the instruction; it exists.
Definition journal.hpp:117
@ HELD
Evidence contradicted the placement (holdings moved past what it can explain), so only an operator ma...
Definition journal.hpp:149
@ PENDING
Written before the send; whether the instruction exists is not yet known.
Definition journal.hpp:115
@ RECORDED
A run budget's ledger record (JournalAction::LEDGER); it answers no instruction.
Definition journal.hpp:151
constexpr std::array< RunRecordKind, 4 > all_run_record_kinds
Every RunRecordKind, in declaration order.
Definition journal.hpp:403
constexpr bool journal_outcome_requires_operator(JournalOutcome outcome) noexcept
Tests whether a line with this outcome is written with requires_operator_ by design,...
Definition journal.hpp:351
std::vector< OpenInstruction > placements_of(const JournalReplay &replay, const std::vector< std::string > &ids)
Returns the placements with the given journal ids, each with the span in which it may have reached th...
Definition journal.hpp:732
std::vector< OpenInstruction > unresolved_instructions(const JournalReplay &replay)
Lists the instructions whose outcome is unknown, in the order they were opened.
Definition journal.hpp:673
std::string_view journal_run_of(const JournalEntry &entry) noexcept
Returns the run budget's run a journal line belongs to.
Definition journal.hpp:559
JournalAction
Which kind of instruction a journal line records.
Definition journal.hpp:31
@ CANCEL
A cancellation of a resting order.
Definition journal.hpp:35
@ LEDGER
A record of a run budget's ledger (RunRecord), not an instruction: it sends nothing and names no orde...
Definition journal.hpp:42
std::optional< JournalAction > journal_action_from(std::string_view text) noexcept
Parses an action's name.
Definition journal.hpp:102
constexpr std::array< JournalAction, 3 > all_journal_actions
Every JournalAction, in declaration order.
Definition journal.hpp:46
constexpr bool journal_outcome_names_order(JournalOutcome outcome) noexcept
Tests whether a line with this outcome may carry the broker's id for the order.
Definition journal.hpp:286
constexpr bool journal_outcome_is_terminal(JournalOutcome outcome) noexcept
Tests whether an outcome answers a PENDING line.
Definition journal.hpp:243
The library: broker-neutral types (core), and the modules below.
constexpr std::optional< Enum > value_named(const std::array< Enum, N > &values, Name name, std::string_view text) noexcept
Returns the value of a list whose name is text, so a parser reads exactly what its writer writes.
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.
OrderType
How an order's price is decided (its price basis).
Definition types.hpp:1998
@ LIMIT
지정가: fills at the stated price or better.
Definition types.hpp:2001
@ UNREADABLE
No readable date rules it out, but an unreadable date might.
OrderSession
The trading session an order is submitted into.
Definition types.hpp:2130
@ REGULAR
The continuous regular session.
Definition types.hpp:2133
Venue
A trading venue: an exchange, or the combined view across exchanges.
Definition types.hpp:45
TimeInForce
How long an order stays valid and what happens to an unfilled remainder.
Definition types.hpp:2088
@ DAY
Rests until the session ends; an unfilled remainder is cancelled then.
Definition types.hpp:2091
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.
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85
OrderSide side_
Buy or sell.
Definition types.hpp:2307
OrderType type_
The price basis; UNKNOWN if the broker's code was not recognized.
Definition types.hpp:2309
std::optional< Decimal64 > trigger_price_
The trigger price, whenever the broker reports a positive one.
Definition types.hpp:2339
Decimal64 quantity_
The quantity ordered; the remainder is this minus execution_.filled_quantity_.
Definition types.hpp:2341
InstrumentId instrument_
The instrument (identity venue; the routing venue is venue_).
Definition types.hpp:2303
std::optional< Decimal64 > price_
The limit price, if the order has one.
Definition types.hpp:2331
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
OrderSnapshot order_
The order's state.
Definition types.hpp:2584
One journal line: an instruction and what was known about it when the line was written.
Definition journal.hpp:482
std::string id_
Links an answering line to its PENDING line.
Definition journal.hpp:488
std::chrono::system_clock::time_point recorded_at_
When the line was written (for PENDING, before the request went out).
Definition journal.hpp:494
OrderType type_
The order type; recovery matches on type, time in force and session together.
Definition journal.hpp:500
Decimal64 quantity_
The quantity, which recovery matches against.
Definition journal.hpp:514
std::optional< Decimal64 > price_
The price, if the order has one.
Definition journal.hpp:516
JournalOutcome outcome_
What the line says about the instruction.
Definition journal.hpp:492
std::string detail_
Why an answering line says what it says, for the operator.
Definition journal.hpp:532
std::optional< Venue > venue_
The venue the order actually goes to, as the binding resolves it.
Definition journal.hpp:512
InstrumentId instrument_
The instrument (identity; the routing venue is venue_).
Definition journal.hpp:496
std::optional< Decimal64 > trigger_price_
The trigger price of a stop-limit order; empty otherwise (and on lines that predate it).
Definition journal.hpp:518
std::optional< Decimal64 > baseline_
On a serialized runner's placement, the instrument's net holding the order was decided against; on a ...
Definition journal.hpp:525
bool requires_operator_
Whether only an operator may resolve this instruction.
Definition journal.hpp:539
std::string scope_
The broker and account the instruction belongs to, such as "kis:live:12345678-01".
Definition journal.hpp:546
JournalAction action_
Whether the instruction is a placement or a cancellation.
Definition journal.hpp:490
OrderSession session_
The session.
Definition journal.hpp:504
TimeInForce time_in_force_
The time in force.
Definition journal.hpp:502
std::shared_ptr< const RunRecord > run_
What the line says about a run budget's ledger; null on a line of a runner without one,...
Definition journal.hpp:551
std::string order_id_
The broker's order id, if known: from the start on a CANCEL line, and only on an answering line for a...
Definition journal.hpp:530
The contents of a journal, including what could not be read.
Definition journal.hpp:600
bool readable() const noexcept
Tests whether the file could be read.
Definition journal.hpp:620
std::size_t unreadable_lines_
How many lines did not parse (on an append-only file, writes that did not finish).
Definition journal.hpp:604
bool intact() const noexcept
Tests whether everything was read.
Definition journal.hpp:635
bool has_header_
Whether a header was found; a non-empty file without one is not written to.
Definition journal.hpp:612
std::string scope_
The broker and account the file's header declares, if it has one.
Definition journal.hpp:610
std::vector< JournalEntry > entries_
Every line that parsed, in file order.
Definition journal.hpp:602
JournalReadStatus status_
Whether the file was read; READ for an in-memory journal.
Definition journal.hpp:614
std::string detail_
What was wrong, for the operator.
Definition journal.hpp:608
bool truncated_tail_
Whether the last line was incomplete, as after a kill during a write.
Definition journal.hpp:606
One instruction of unknown outcome, and the span in which it may have reached the broker.
Definition journal.hpp:646
JournalEntry entry_
The instruction, as its first line recorded it (its order_id_ may come from a later line).
Definition journal.hpp:648
std::string detail_
What the journal says went wrong.
Definition journal.hpp:659
bool answered_
Whether an INDETERMINATE line answered it.
Definition journal.hpp:657
std::optional< std::chrono::system_clock::time_point > closed_at_
When the send was answered: by an INDETERMINATE line in unresolved_instructions, by any answer in rec...
Definition journal.hpp:655
std::chrono::system_clock::time_point opened_at_
When the send began.
Definition journal.hpp:650
What a journal line says about a run budget's ledger (strategy::RiskLimits::run_budget_).
Definition journal.hpp:456
std::optional< Decimal64 > fee_
A booked fill's commission and taxes together, in amount_'s currency.
Definition journal.hpp:466
std::optional< Money > amount_
A transfer's amount, an opening's budget, or what a booked fill traded for.
Definition journal.hpp:462
bool fee_estimated_
Whether part of fee_ was estimated from strategy::RiskLimits::run_fees_.
Definition journal.hpp:468
std::optional< Decimal64 > filled_
A booked fill's quantity.
Definition journal.hpp:464
std::string of_
The journal id of the placement a RunRecordKind::FILL books.
Definition journal.hpp:472
std::string run_
The run's id: the id of its RunRecordKind::OPEN line.
Definition journal.hpp:458
std::string given_
The operator's id of a transfer, which makes it apply once.
Definition journal.hpp:470
std::optional< RunRecordKind > kind_
What a LEDGER line records; empty on every other line.
Definition journal.hpp:460
One order the runner is to place.
TimeInForce time_in_force_
How long the order stays valid.
OrderSession session_
The trading session.
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.