FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
feature_table.hpp
Go to the documentation of this file.
1#pragma once
2// FeatureTable: book features and forward mid moves, one row per qualifying market-data event,
3// so a predictor can be judged before anyone writes a strategy around it.
4//
5// Columns are structure-of-arrays, the convention of include/fastmm/backtest/result.hpp: each
6// column is one contiguous vector, so Python borrows it as a numpy array without a copy. Prices,
7// quantities and spreads are raw 1e-8 fixed point; imbalance is a Ratio in the same scale
8// (1e8 == 1.0).
9//
10// A forward mid is the venue mid at `ts + horizon`, read AT that time and never interpolated -
11// the same rule the backtest applies to its fill markouts. A row whose horizon falls after the
12// last event of the data, or lands where the book has no two sides, has no forward mid: the
13// column holds 0 and HorizonCoverage counts it. It is never substituted with the last known mid.
14// This is the discipline of include/fastmm/backtest/markout.hpp (`excluded_past_end`), applied
15// per row instead of per fill.
16#include <cstdint>
17#include <string>
18#include <vector>
19
20namespace fastmm::research {
21
22// Horizons measured by default, in nanoseconds of event time: 100 ms, 1 s, 10 s, 1 minute.
23inline constexpr std::int64_t kDefaultHorizonsNs[] = {
24 100'000'000LL, 1'000'000'000LL, 10'000'000'000LL, 60'000'000'000LL};
26// "100ms", "1s", "1m": the shortest exact unit, the spelling MarkoutHorizon::label() uses.
27[[nodiscard]] std::string horizon_label(std::int64_t ns);
28
30 std::vector<std::int64_t> ts; // venue time of the event that produced the row
31 std::vector<std::uint32_t> instrument;
32 std::vector<std::int64_t> mid; // (best bid + best ask) / 2, raw 1e-8
33 std::vector<std::int64_t> microprice; // fastmm::microprice(book), raw 1e-8
34 std::vector<std::int64_t> best_bid;
35 std::vector<std::int64_t> best_ask;
36 std::vector<std::int64_t> bid_qty; // displayed quantity at the best bid, raw 1e-8
37 std::vector<std::int64_t> ask_qty;
38 std::vector<std::int64_t> imbalance; // fastmm::imbalance(book, levels), Ratio raw 1e-8
39 std::vector<std::int64_t> spread; // best ask - best bid, raw 1e-8
40 // Horizons in nanoseconds, and the venue mid at ts + horizon. forward_mid[h][i] is 0 when that
41 // row has no value at horizon h; it is excluded there, never marked at a substitute price.
42 std::vector<std::int64_t> horizon_ns;
43 std::vector<std::vector<std::int64_t>> forward_mid;
45 [[nodiscard]] std::size_t size() const noexcept { return ts.size(); }
46 void reserve(std::size_t n);
47 void clear() noexcept;
48};
50// Per horizon, how many rows got a forward mid and why the rest did not. resolved +
51// excluded_past_end + excluded_no_mid == FeatureRows::size().
52struct HorizonCoverage {
53 std::int64_t horizon_ns = 0;
54 std::uint64_t resolved = 0;
55 std::uint64_t excluded_past_end = 0; // ts + horizon is after the last event of the data
56 std::uint64_t excluded_no_mid = 0; // the book had no two sides at ts + horizon
58 [[nodiscard]] std::string label() const { return horizon_label(horizon_ns); }
59};
61struct FeatureTable {
62 FeatureRows rows;
63 std::vector<HorizonCoverage> coverage; // one entry per horizon, in horizon_ns order
64 std::uint64_t events = 0; // market-data events read from the source
65 std::uint64_t book_updates = 0; // BookDelta / BookSnapshot applied to a book
66 std::uint64_t skipped_one_sided = 0; // sampled events whose book had no two sides
67 std::uint64_t skipped_subsample = 0; // sampled events dropped by FeatureConfig::subsample
68 std::int64_t start_ts = 0; // first event time of the data (0 when empty)
69 std::int64_t end_ts = 0; // last event time; the horizon cut-off
71 [[nodiscard]] std::size_t size() const noexcept { return rows.size(); }
72 // One line per horizon plus the event counts.
73 [[nodiscard]] std::string summary_table() const;
74 // Every column, forward mids last; an empty field where a forward mid is unset.
75 [[nodiscard]] std::string csv() const;
76};
78} // namespace fastmm::research
Definition feature_table.hpp:22
constexpr std::int64_t kDefaultHorizonsNs[]
Horizons measured by default, in nanoseconds of event time: 100 ms, 1 s, 10 s, 1 minute.
Definition feature_table.hpp:25
std::string horizon_label(std::int64_t ns)
"100ms", "1s", "1m": the shortest exact unit, the spelling MarkoutHorizon::label() uses.
Definition feature_table.hpp:31
std::vector< std::int64_t > spread
best ask - best bid, raw 1e-8
Definition feature_table.hpp:41
std::vector< std::int64_t > bid_qty
displayed quantity at the best bid, raw 1e-8
Definition feature_table.hpp:38
std::vector< std::int64_t > microprice
fastmm::microprice(book), raw 1e-8
Definition feature_table.hpp:35
std::vector< std::int64_t > best_bid
Definition feature_table.hpp:36
std::vector< std::int64_t > ts
venue time of the event that produced the row
Definition feature_table.hpp:32
std::vector< std::vector< std::int64_t > > forward_mid
Definition feature_table.hpp:45
std::vector< std::int64_t > imbalance
fastmm::imbalance(book, levels), Ratio raw 1e-8
Definition feature_table.hpp:40
void reserve(std::size_t n)
std::vector< std::uint32_t > instrument
Definition feature_table.hpp:33
std::vector< std::int64_t > horizon_ns
Horizons in nanoseconds, and the venue mid at ts + horizon.
Definition feature_table.hpp:44
std::vector< std::int64_t > ask_qty
Definition feature_table.hpp:39
std::vector< std::int64_t > mid
(best bid + best ask) / 2, raw 1e-8
Definition feature_table.hpp:34
std::vector< std::int64_t > best_ask
Definition feature_table.hpp:37
std::size_t size() const noexcept
Definition feature_table.hpp:47
Definition feature_table.hpp:63
Per horizon, how many rows got a forward mid and why the rest did not.
Definition feature_table.hpp:54