FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
ab_report.hpp
Go to the documentation of this file.
1#pragma once
2// The readout of an [ab] test (fastmm-data ab), from the journals of the live sessions or
3// backtests that ran it. A journal carries everything it needs: the configuration with [ab] (the
4// arms and the design, rebuilt as an AbPlan), the arm updates (ParamUpdate, Origin::Experiment),
5// the arm of each order sent (OutNewOrder, OutReplace) and the BookTicker mids the fills are
6// marked against.
7//
8// Each instrument's time is cut into cells: one arm over one block (time, crossover) or over the
9// whole test (instrument). A fill belongs to the cell of its order's arm at its time; a fill that
10// came after its instrument changed arm goes to the last block of the arm it was sent under. Per
11// cell: the time, the fills, the traded notional and, at each horizon, the markout, the fees the
12// configuration charges ([venues.<x>.fees], [[instruments]] maker_bps / taker_bps) less any extra
13// maker rebate, and their net, over the fills marked at that horizon.
14//
15// The comparison pairs the arms where the design makes them comparable: the instruments of one
16// stratum (instrument), or one instrument over one run of blocks that gives each arm one block
17// (time, crossover). A pair counts when it has time in every arm. Each arm is compared with arm
18// 0 on the net, notional and fills per instrument-hour (each arm's total over its time in the
19// pairs, so a pair cut short at the test's start or stop weighs by the time it has) and on net
20// bps (its net over its notional in the pairs); the 95% interval comes from resampling clusters of
21// pairs (the strata, or the runs of blocks), which keeps what one market moment does to every
22// instrument inside one draw. An A/A test's intervals, run the same way, are its noise floor.
24
25#include <cstddef>
26#include <cstdint>
27#include <span>
28#include <string>
29#include <vector>
30
31namespace fastmm::bt {
32
33struct AbReportOptions {
34 std::vector<std::int64_t> horizons_ns; // empty: kDefaultMarkoutHorizonsNs
35 double maker_rebate_bps = 0.0; // paid on maker notional, on top of the configured fees
36 std::size_t resamples = 2000;
37 std::uint64_t seed = 1;
38};
40struct AbHorizon {
41 MarkoutBucket all; // the fills marked at this horizon
44 std::int64_t fees_raw = 0; // configured fees less the rebate on the same fills, quote currency
45 [[nodiscard]] std::int64_t net_raw() const noexcept { return all.markout_raw - fees_raw; }
46 void add(const AbHorizon& o) noexcept;
47};
49// One instrument in one arm over one block (the whole test with assignment = "instrument").
50struct AbCell {
51 std::size_t instrument = 0; // AbReport::instruments
52 int arm = 0;
53 std::int64_t block = 0;
54 std::int64_t ns = 0; // time the instrument was in the arm
55 std::uint64_t fills = 0;
56 std::uint64_t maker_fills = 0;
57 std::int64_t notional_raw = 0; // every fill, quote currency
58 std::vector<AbHorizon> horizons;
59};
61struct AbArmSummary {
62 std::string name;
63 std::string params; // "name=value ..."; empty: the configured parameters
64 std::int64_t ns = 0; // instrument time in the arm
65 std::uint64_t fills = 0;
66 std::uint64_t maker_fills = 0;
67 std::int64_t notional_raw = 0;
68 std::vector<AbHorizon> horizons;
69};
71// One arm against arm 0 on one metric. `arm` and `control` are the two sides' values over the
72// pairs, `diff` their difference, [lo, hi] its 95% interval.
73struct AbEffect {
74 int arm = 0;
75 std::string metric; // "net/hour 60s", "net bps 60s", "notional/hour", "fills/hour"
76 double treated = 0.0;
77 double control = 0.0;
78 double diff = 0.0;
79 double lo = 0.0;
80 double hi = 0.0;
81 bool noise = false; // the A/A run's interval for this metric is known
82 double noise_half_width = 0.0;
83};
85struct AbReport {
86 std::string test;
87 std::string assignment;
88 std::uint64_t seed = 0;
89 std::int64_t block_s = 0;
90 bool aa = false; // every arm runs the same parameters
91 std::size_t journals = 0;
92 std::int64_t first_ns = 0; // the first and last event read
93 std::int64_t last_ns = 0;
94 bool mids = false; // some journal has a BookTicker stream to mark against
95 double maker_rebate_bps = 0.0;
96 std::vector<std::string> instruments; // "venue:symbol"
97 std::vector<std::int64_t> horizons_ns;
98 std::vector<AbArmSummary> arms;
99 std::vector<AbCell> cells;
100 std::uint64_t outside_fills = 0; // of orders sent outside any arm
101 std::uint64_t repeated_fills = 0; // an execution reported again: counted once
102 std::size_t pairs = 0; // with time in every arm
103 std::size_t incomplete_pairs = 0; // missing an arm (the test began, ended or stopped in it)
104 std::size_t clusters = 0;
105 std::size_t resamples = 0;
106 std::vector<AbEffect> effects;
109// Throws std::runtime_error when a journal cannot be read, has no [ab] in its configuration, or
110// the journals ran different tests.
111[[nodiscard]] AbReport ab_report(std::span<const std::string> journals, const AbReportOptions& opt);
112// Puts `aa`'s interval half-widths next to the matching effects of `r` (by metric).
114[[nodiscard]] std::string format_ab_report(const AbReport& r);
115[[nodiscard]] std::string ab_report_json(const AbReport& r);
116// One row per cell.
117[[nodiscard]] std::string ab_cells_csv(const AbReport& r);
118
119} // namespace fastmm::bt
Post-fill markouts: the metric that separates spread captured from adverse selection.
Definition backtest_config.hpp:44
std::string ab_cells_csv(const AbReport &r)
One row per cell.
std::string ab_report_json(const AbReport &r)
AbReport ab_report(std::span< const std::string > journals, const AbReportOptions &opt)
Throws std::runtime_error when a journal cannot be read, has no [ab] in its configuration,...
void add_noise_floor(AbReport &r, const AbReport &aa)
Puts aa's interval half-widths next to the matching effects of r (by metric).
std::string format_ab_report(const AbReport &r)
Definition ab_report.hpp:63
std::int64_t notional_raw
Definition ab_report.hpp:69
std::string name
Definition ab_report.hpp:64
std::uint64_t fills
Definition ab_report.hpp:67
std::string params
"name=value ..."; empty: the configured parameters
Definition ab_report.hpp:65
std::uint64_t maker_fills
Definition ab_report.hpp:68
std::vector< AbHorizon > horizons
Definition ab_report.hpp:70
std::int64_t ns
instrument time in the arm
Definition ab_report.hpp:66
One instrument in one arm over one block (the whole test with assignment = "instrument").
Definition ab_report.hpp:52
int arm
Definition ab_report.hpp:54
std::int64_t notional_raw
every fill, quote currency
Definition ab_report.hpp:59
std::int64_t block
Definition ab_report.hpp:55
std::int64_t ns
time the instrument was in the arm
Definition ab_report.hpp:56
std::uint64_t fills
Definition ab_report.hpp:57
std::vector< AbHorizon > horizons
Definition ab_report.hpp:60
std::uint64_t maker_fills
Definition ab_report.hpp:58
std::size_t instrument
AbReport::instruments.
Definition ab_report.hpp:53
One arm against arm 0 on one metric.
Definition ab_report.hpp:75
double diff
Definition ab_report.hpp:80
double hi
Definition ab_report.hpp:82
bool noise
the A/A run's interval for this metric is known
Definition ab_report.hpp:83
double treated
Definition ab_report.hpp:78
int arm
Definition ab_report.hpp:76
std::string metric
"net/hour 60s", "net bps 60s", "notional/hour", "fills/hour"
Definition ab_report.hpp:77
double control
Definition ab_report.hpp:79
double lo
Definition ab_report.hpp:81
double noise_half_width
Definition ab_report.hpp:84
Definition ab_report.hpp:42
std::int64_t fees_raw
configured fees less the rebate on the same fills, quote currency
Definition ab_report.hpp:46
std::int64_t net_raw() const noexcept
Definition ab_report.hpp:47
MarkoutBucket all
the fills marked at this horizon
Definition ab_report.hpp:43
MarkoutBucket sell
Definition ab_report.hpp:45
void add(const AbHorizon &o) noexcept
MarkoutBucket buy
Definition ab_report.hpp:44
Definition ab_report.hpp:35
std::size_t resamples
Definition ab_report.hpp:38
std::vector< std::int64_t > horizons_ns
empty: kDefaultMarkoutHorizonsNs
Definition ab_report.hpp:36
double maker_rebate_bps
paid on maker notional, on top of the configured fees
Definition ab_report.hpp:37
std::uint64_t seed
Definition ab_report.hpp:39
Definition ab_report.hpp:87
std::uint64_t outside_fills
of orders sent outside any arm
Definition ab_report.hpp:102
bool mids
some journal has a BookTicker stream to mark against
Definition ab_report.hpp:96
std::int64_t first_ns
the first and last event read
Definition ab_report.hpp:94
std::vector< AbCell > cells
Definition ab_report.hpp:101
std::string assignment
Definition ab_report.hpp:89
std::size_t pairs
with time in every arm
Definition ab_report.hpp:104
std::vector< AbArmSummary > arms
Definition ab_report.hpp:100
std::vector< std::int64_t > horizons_ns
Definition ab_report.hpp:99
std::uint64_t repeated_fills
an execution reported again: counted once
Definition ab_report.hpp:103
std::size_t resamples
Definition ab_report.hpp:107
std::int64_t block_s
Definition ab_report.hpp:91
std::int64_t last_ns
Definition ab_report.hpp:95
std::size_t incomplete_pairs
missing an arm (the test began, ended or stopped in it)
Definition ab_report.hpp:105
double maker_rebate_bps
Definition ab_report.hpp:97
std::uint64_t seed
Definition ab_report.hpp:90
bool aa
every arm runs the same parameters
Definition ab_report.hpp:92
std::vector< std::string > instruments
"venue:symbol"
Definition ab_report.hpp:98
std::vector< AbEffect > effects
Definition ab_report.hpp:108
std::size_t journals
Definition ab_report.hpp:93
std::size_t clusters
Definition ab_report.hpp:106
std::string test
Definition ab_report.hpp:88
Definition markout.hpp:33
std::int64_t markout_raw
sum of s * (mid(t0 + h) - price), quote currency
Definition markout.hpp:34