FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
synthetic_source.hpp
Go to the documentation of this file.
1#pragma once
2// SyntheticSource: a MarketGenerator run against its own MatchingEngine, published through
3// MdAggregator as a plain market-data stream (snapshot, 100 ms depth batches, trades, book
4// tickers). Same seed => byte-identical stream. Use it to record fixtures / CSVs or to
5// backtest with the L2 queue model; the coupled mode (strategy orders in the same book)
6// is BacktestRunner's default for `source = "synthetic"` with the matching fill model.
8#include "fastmm/core/msg_ring.hpp"
9#include "fastmm/sim/market_generator.hpp"
10#include "fastmm/sim/matching_engine.hpp"
11#include "fastmm/sim/md_aggregator.hpp"
12
13#include <cstdint>
14#include <memory>
15
16namespace fastmm::bt {
17
18struct SyntheticSourceConfig {
19 sim::MarketGeneratorParams generator{};
20 sim::MdAggregatorConfig md{};
21 std::uint64_t seed = 42;
24 Timestamp start{seconds(1'700'000'000).ns};
26 int seed_levels = 20;
27};
29class SyntheticSource final : public MdSource, private sim::MatchingSink {
30 public:
32 const EventHeader* next() override;
33 void reset() override;
34 [[nodiscard]] Timestamp start_ts() const override { return cfg_.start; }
35 [[nodiscard]] const sim::MarketGeneratorStats& generator_stats() const noexcept {
36 return gen_->stats();
37 }
38 [[nodiscard]] std::uint64_t events() const noexcept { return events_; }
39 [[nodiscard]] std::uint64_t dropped() const noexcept { return dropped_; } // should be 0
41 private:
42 void on_book_change(InstrumentId, Side, Price, Qty, std::uint64_t) override;
43 void on_trade(InstrumentId, Price, Qty, Side, std::uint64_t, Timestamp) override;
44 static void emit(void* ctx, EventHeader& h, Timestamp ts) noexcept;
45 void push(EventHeader& h, Timestamp ts) noexcept;
46 void rebuild();
47 bool produce(); // advances the world until at least one message is queued
48
50 std::unique_ptr<sim::MatchingEngine> me_;
51 std::unique_ptr<sim::MdAggregator> agg_;
52 std::unique_ptr<sim::MarketGenerator> gen_;
53 MsgRing queue_;
54 EventBuf buf_{};
55 Timestamp end_{};
56 std::uint64_t events_ = 0;
57 std::uint64_t dropped_ = 0;
58 bool done_ = false;
59};
60
61} // namespace fastmm::bt
Definition synthetic_source.hpp:31
Timestamp start_ts() const override
Definition synthetic_source.hpp:36
std::uint64_t events() const noexcept
Definition synthetic_source.hpp:40
const EventHeader * next() override
const sim::MarketGeneratorStats & generator_stats() const noexcept
Definition synthetic_source.hpp:37
std::uint64_t dropped() const noexcept
should be 0
Definition synthetic_source.hpp:41
DataSource (8.4): a timestamp-ordered stream of normalized market-data events consumed by the simulat...
Definition backtest_config.hpp:39
Side
Definition enums.hpp:12
constexpr Duration seconds(std::int64_t v) noexcept
Definition time.hpp:41
Definition time.hpp:22
std::int64_t ns
Definition time.hpp:23
Definition messages.hpp:37
Definition time.hpp:45
Definition synthetic_source.hpp:20
int seed_levels
Definition synthetic_source.hpp:28
Timestamp start
Definition synthetic_source.hpp:26
InstrumentId instrument
Definition synthetic_source.hpp:24
VenueId venue
Definition synthetic_source.hpp:25
std::uint64_t seed
Definition synthetic_source.hpp:23
sim::MarketGeneratorParams generator
Definition synthetic_source.hpp:21
Duration duration
Definition synthetic_source.hpp:27
sim::MdAggregatorConfig md
Definition synthetic_source.hpp:22