22#include "fastmm/sim/md_source.hpp"
34using sim::kMaxSourceEventBytes;
38concept DataSourceLike =
requires(S& s) {
39 { s.next() } -> std::same_as<const EventHeader*>;
52 std::uint64_t
seq = 0;
60 template <
class Prov
ider>
62 if (!have_row_ && !fetch(provide))
return nullptr;
64 const Row first = row_;
70 t.price = first.
price;
72 t.trade_id = first.
seq;
73 t.aggressor = first.
side;
81 t.bid_px = t.ask_px =
Price{};
82 t.bid_qty = t.ask_qty =
Qty{};
102 if (nb < kMaxLevels) bids_[nb++] =
Level{cur.
price, cur.
qty};
104 if (na < kMaxLevels) asks_[na++] =
Level{cur.
price, cur.
qty};
106 if (!fetch(provide))
break;
111 if (!snap && (row_.
side ==
Side::Buy ? nb : na) == kMaxLevels)
break;
125 d.first_update_id = d.last_update_id = first.
seq;
126 d.prev_update_id = 0;
127 if (nb > 0) std::memcpy(d.levels(), bids_, nb *
sizeof(
Level));
128 if (na > 0) std::memcpy(d.levels() + nb, asks_, na *
sizeof(
Level));
134 void reset() noexcept { have_row_ =
false; }
137 static constexpr std::uint32_t kMaxLevels = 256;
138 template <
class Prov
ider>
139 bool fetch(Provider& provide) {
140 have_row_ = provide(row_);
144 (r.side ==
Side::Buy ? t.bid_px : t.ask_px) = r.price;
145 (r.side ==
Side::Buy ? t.bid_qty : t.ask_qty) = r.qty;
147 static void stamp(
EventHeader& h,
const Row& r)
noexcept {
149 h.t0_cycles = Cycles{
static_cast<std::uint64_t
>(r.ts)};
155 bool have_row_ =
false;
157 Level bids_[kMaxLevels];
158 Level asks_[kMaxLevels];
164class ChainSource final :
public MdSource {
166 explicit ChainSource(std::vector<MdSource*> sources) : sources_(std::move(sources)) {}
168 while (at_ < sources_.size()) {
174 void reset()
override {
175 for (MdSource* s : sources_) s->reset();
179 return sources_.empty() ?
Timestamp{} : sources_.front()->start_ts();
182 return !sources_.empty() && sources_.front()->order_by_order();
186 std::vector<MdSource*> sources_;
197class TimeSliceSource final :
public MdSource {
206 [[nodiscard]]
bool order_by_order()
const override {
return inner_.order_by_order(); }
213 std::unique_ptr<Primer> primer_;
215 bool started_ =
false;
225 explicit MergedSource(std::vector<MdSource*> sources) : sources_(std::move(sources)) {
226 heads_.assign(sources_.size(),
nullptr);
227 for (std::size_t i = 0; i < sources_.size(); ++i) heads_[i] = sources_[i]->
next();
230 if (last_ != kNone) {
231 heads_[last_] = sources_[last_]->next();
234 std::size_t best = kNone;
236 for (std::size_t i = 0; i < heads_.size(); ++i) {
237 if (heads_[i] ==
nullptr)
continue;
239 if (best == kNone || ts < best_ts) {
244 if (best == kNone)
return nullptr;
248 void reset()
override {
249 for (std::size_t i = 0; i < sources_.size(); ++i) {
250 sources_[i]->reset();
251 heads_[i] = sources_[i]->next();
258 for (
const MdSource* s : sources_) {
260 if (st.
valid() && (!t.valid() || st < t)) t = st;
265 return h.exch_ts.
valid() ? h.exch_ts : h.recv_ts;
269 for (
const MdSource* s : sources_)
270 if (s->order_by_order())
return true;
275 static constexpr std::size_t kNone =
static_cast<std::size_t
>(-1);
276 std::vector<MdSource*> sources_;
277 std::vector<const EventHeader*> heads_;
278 std::size_t last_ = kNone;
282class OwnedMergedSource final :
public MdSource {
285 : owned_(std::move(sources)), merged_(raw(owned_)) {}
292 static std::vector<MdSource*> raw(
const std::vector<std::unique_ptr<MdSource>>& v) {
293 std::vector<MdSource*> out;
294 out.reserve(v.size());
295 for (
const auto& s : v) out.push_back(s.get());
298 std::vector<std::unique_ptr<MdSource>> owned_;
Concatenation of sources that cover consecutive spans of time (one file per day).
Definition data_source.hpp:166
bool order_by_order() const override
Definition data_source.hpp:183
const EventHeader * next() override
Definition data_source.hpp:169
Timestamp start_ts() const override
Definition data_source.hpp:180
void reset() override
Definition data_source.hpp:176
k-way merge of several sources by event time (stable: lower source index first on ties).
Definition data_source.hpp:225
bool order_by_order() const override
Order by order when any input is: each venue's books come from their own input.
Definition data_source.hpp:270
Timestamp start_ts() const override
Earliest start among the inputs (invalid if none reports one).
Definition data_source.hpp:258
static Timestamp time_of(const EventHeader &h) noexcept
Definition data_source.hpp:266
const EventHeader * next() override
Definition data_source.hpp:231
void reset() override
Definition data_source.hpp:250
MergedSource over sources it owns: open_data() of several specs ("a; b"), one per venue.
Definition data_source.hpp:284
void reset() override
Definition data_source.hpp:289
const EventHeader * next() override
Definition data_source.hpp:288
Timestamp start_ts() const override
Definition data_source.hpp:290
bool order_by_order() const override
Definition data_source.hpp:291
Builds messages from a row stream. Provider is bool(Row&) returning false at end.
Definition data_source.hpp:58
void reset() noexcept
Definition data_source.hpp:136
const EventHeader * next(Provider &&provide)
Definition data_source.hpp:63
The events of inner in [t0, t1) of event time (exch_ts, recv_ts when unset), for walk-forward folds.
Definition data_source.hpp:199
TimeSliceSource(MdSource &inner, Timestamp t0, Timestamp t1)
Timestamp start_ts() const override
Definition data_source.hpp:207
const EventHeader * next() override
TimeSliceSource & operator=(const TimeSliceSource &)=delete
bool order_by_order() const override
Definition data_source.hpp:208
~TimeSliceSource() override
Event bus message structs (5.5).
Definition backtest_config.hpp:44
RowType
Definition data_source.hpp:45
constexpr void init_header(M &m, EventType type, InstrumentId inst={}, VenueId venue={}, std::uint32_t len=static_cast< std::uint32_t >(sizeof(M))) noexcept
For fixed-size messages len defaults to sizeof(M); BookDeltaMsg passes size_for().
Definition messages.hpp:76
Side
Definition enums.hpp:12
StrongId< struct VenueTag, std::uint8_t > VenueId
Definition strong_id.hpp:39
Bids then asks follow the fixed part as a flexible array of Level (bid_count + ask_count).
Definition messages.hpp:100
EventHeader hdr
Definition messages.hpp:101
static constexpr std::uint32_t size_for(std::uint32_t bids, std::uint32_t asks) noexcept
Definition messages.hpp:108
Definition messages.hpp:229
One price level. Shared by book messages and the L2 book.
Definition messages.hpp:31
static constexpr Timestamp max() noexcept
Definition time.hpp:56
constexpr bool valid() const noexcept
Definition time.hpp:55
A trade whose venue names the buyer and the seller carries them in the 64 bytes after the message: hd...
Definition messages.hpp:200
Definition data_source.hpp:47
std::uint32_t inst
Definition data_source.hpp:50
Price price
Definition data_source.hpp:52
RowType type
Definition data_source.hpp:49
Qty qty
Definition data_source.hpp:53
Side side
Definition data_source.hpp:51
std::int64_t ts
Definition data_source.hpp:48
std::uint64_t seq
Definition data_source.hpp:54