FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1#pragma once
2// Venue registry: `[venues.<name>] kind` -> the connector that serves it. An entry carries the
3// venue's capabilities, the `[venues.<name>]` keys the venue owns and one factory, so nothing
4// about a connector reaches the core: the central schema (include/fastmm/config/schema.hpp) knows
5// only the generic keys, and no `if (kind == X)` lives outside a venue's own code.
6//
7// Nothing registers itself, for the same reason the strategy and storage registries do not (a
8// static library's self-registering object is dropped by the linker):
9// fastmm::venues::register_builtin_venues() adds the connectors FastMM ships, and an out-of-tree
10// venue calls add() before the session starts (examples/external-venue).
11//
12// constexpr VenueKeySpec kEchoKeys[] = {
13// {"greeting", KeyType::String, false, "echo: text the venue sends back (default \"hi\")"},
14// };
15// VenueRegistry::instance().add({.name = "echo",
16// .summary = "an echo venue",
17// .keys = kEchoKeys,
18// .caps = {.credentials = false},
19// .make = &make_echo_venue});
20//
21// docs/how-to/venues/add-a-venue.md walks through it; tools/docs_config_ref.py reads the key
22// tables out of the registration files so a venue's keys land in the configuration reference.
25#include "fastmm/net/endpoint_selector.hpp"
26#include "fastmm/net/source_address.hpp"
28
29#include <cstdint>
30#include <memory>
31#include <span>
32#include <string>
33#include <string_view>
34#include <vector>
35
36namespace fastmm::venues {
37
38// Runtime knobs every connector honours, passed to the factory. Anything venue-specific is a
39// config key the venue owns instead.
40// [venues.<x>] source_ip / source_interface as a net::SourceAddress.
41[[nodiscard]] inline net::SourceAddress source_address(const VenueSection& s) {
42 return net::SourceAddress{s.source_ip, s.source_interface};
44
45// [engine] endpoint_select and its endpoint_ keys as the process's endpoint selection
46// (net/endpoint_selector.hpp); [venues.<x>] endpoint_select overrides `enabled` per venue.
47[[nodiscard]] inline net::EndpointSelectConfig endpoint_select_config(const EngineSection& e) {
48 net::EndpointSelectConfig c;
49 c.enabled = e.endpoint_select;
50 c.probe_interval_s = static_cast<std::uint32_t>(e.endpoint_probe_interval_s);
51 c.samples = static_cast<std::uint32_t>(e.endpoint_probe_samples);
52 c.spread_us = static_cast<std::uint32_t>(e.endpoint_spread_us);
53 c.switch_us = static_cast<std::uint32_t>(e.endpoint_switch_us);
54 return c;
55}
56
57struct VenueFactoryOptions {
58 bool dry_run = false; // public market data only: no keys, no orders
59 std::string record_raw_dir; // non-empty: append raw frames to <dir>/<venue>-<channel>.jsonl
60 bool busy_poll = false; // [engine] spin_mode = "busy": Venue::poll() must make progress
61 // A pool member ([venues.<x>] pool_of): the venue whose instruments this account takes orders
62 // for. The connector opens its order and user sessions only, no market data, and resolves
63 // symbols on that venue's id (its own id goes on the events). Invalid: a venue of its own.
65 // The venue is a pool's primary or member: its account shares the IP's request weight with the
66 // pool's other accounts (connectors that count weight per IP).
67 bool pooled = false;
68};
70// What a connector declares about itself before one exists. The session and the engine read these
71// instead of testing `kind`. `replace` and `order_entry` say what the connector can do; whether a
72// given instance does is Venue::caps() (a dry run, order_entry = "none", missing credentials).
73struct VenueCapabilities {
74 bool credentials = true; // needs api_key / api_secret outside a dry run
75 // Its market data needs the account's keys too (Alpaca): a dry run keeps api_key and api_secret.
76 bool market_data_keys = false;
77 bool order_entry = true; // can send orders at all
78 bool replace = false; // can amend a resting order in place
79 bool positions = false; // reports positions from the venue
80 // Its reconciliation reports the position of every subscribed derivative or stock, flat ones too,
81 // before the engine sends an order: a position the session restored from the store is replaced by
82 // the venue's own before it is traded on. Spot instruments are never reported this way.
83 bool position_snapshot = false;
84 bool polls = false; // Venue::poll() drives its sockets; the reactor never wakes for them
85 // Can fetch what the account executed since a point in time, so a reconciliation replays the
86 // fills the private stream missed instead of inferring them from a cum_qty jump. False: a fill
87 // that finished an order while the stream was down is not recoverable on this venue, and the
88 // engine says so (EngineStats::estimated_reconciles) rather than assuming it did not happen.
89 bool executions = false;
90 // Runs as a member of an account pool ([venues.<x>] pool_of, core/account_pool.hpp): the order
91 // path alone, for the primary's instruments, without market data of its own.
92 bool account_pools = false;
93 // Binds every outbound connection (WebSocket, REST, the blocking control requests) to the
94 // section's source address ([venues.<x>] source_ip / source_interface, net/source_address.hpp),
95 // and selects their endpoints ([venues.<x>] endpoint_select, net/endpoint_selector.hpp).
96 bool bind_source = false;
97 // Can move an asset between the accounts of a pool (Venue::transfer,
98 // VenueCaps::internal_transfer) once it is given the credentials: a pool treasury
99 // ([venues.<primary>.treasury]) needs it.
100 bool internal_transfer = false;
101};
103// One `[venues.<name>]` key a venue owns. `doc` is the Meaning column of the configuration
104// reference: say what the key does, its unit and its default, as schema.hpp does.
105struct VenueKeySpec {
106 std::string_view key;
109 std::string_view doc;
112struct VenueEntry {
113 // Throws std::invalid_argument (a bad URL, a key the connector rejects); the message names the
114 // venue. The section's keys have been validated against `keys` before this is called.
115 using Factory = std::unique_ptr<Venue> (*)(VenueId,
116 const VenueSection&,
118
119 // Every member has a default, so a registration names only the ones it needs.
120 std::string_view name; // the `kind` value; outlives the registry
121 std::string_view summary; // one line for the connector table
122 std::span<const std::string_view> aliases = {}; // other `kind` values that select this entry
123 std::span<const VenueKeySpec> keys = {}; // the keys it owns, beyond the generic ones
125 Factory make = nullptr;
128enum class AddResult : std::uint8_t { Added, AlreadyPresent, Conflict, Invalid };
129[[nodiscard]] constexpr std::string_view to_string(AddResult r) noexcept {
130 switch (r) {
132 return "added";
134 return "already present";
136 return "conflict";
138 return "invalid";
139 }
140 return "?";
141}
142
143class VenueRegistry {
144 public:
145 // The registry fastmm-live resolves `kind` through. Not thread-safe for writers: register
146 // before starting threads. Tests use a local registry.
147 static VenueRegistry& instance();
148
149 // Registering the same factory under the same name again changes nothing (AlreadyPresent);
150 // anything that would replace an entry, or a name an entry already claims as an alias, changes
151 // nothing and returns Conflict.
152 AddResult add(VenueEntry entry);
153 // By `kind`: the entry's name or one of its aliases.
154 [[nodiscard]] const VenueEntry* find(std::string_view kind) const noexcept;
155 [[nodiscard]] const std::vector<VenueEntry>& entries() const noexcept { return entries_; }
156 // Every accepted `kind`, names and aliases, comma-separated, for an error message.
157 [[nodiscard]] std::string kinds() const;
158
159 private:
160 std::vector<VenueEntry> entries_;
161};
162
163// Adds the connectors FastMM ships (binance_spot, binance_usdm, bybit, deribit, nasdaq_itch).
164// Idempotent; make_venue() and validate_venues() call it for the default registry.
166
167// Checks every `[venues.<name>]` section against the key table of the venue its `kind` names, the
168// way the central schema checks the generic keys: a key the venue does not own, a key of the wrong
169// type or a missing required key throws ConfigError with the line. An unknown `kind` throws too.
170// Call it once after loading a configuration; fastmm-live does.
171void validate_venues(const Config& cfg, const VenueRegistry& r = VenueRegistry::instance());
172
173// Builds the connector `section.kind` names. Throws std::invalid_argument for an unknown kind,
174// ConfigError for a key of the wrong type and whatever the connector throws for a bad value.
175std::unique_ptr<Venue> make_venue(VenueId id,
176 const VenueSection& section,
178std::unique_ptr<Venue> make_venue(VenueId id,
179 const VenueSection& section,
181 const VenueRegistry& r);
182
183} // namespace fastmm::venues
Definition config.hpp:502
Definition registry.hpp:145
const std::vector< VenueEntry > & entries() const noexcept
Definition registry.hpp:157
std::string kinds() const
Every accepted kind, names and aliases, comma-separated, for an error message.
static VenueRegistry & instance()
The registry fastmm-live resolves kind through.
const VenueEntry * find(std::string_view kind) const noexcept
By kind: the entry's name or one of its aliases.
AddResult add(VenueEntry entry)
Registering the same factory under the same name again changes nothing (AlreadyPresent); anything tha...
Typed configuration (8.7).
Definition venue.hpp:39
constexpr std::string_view to_string(ChannelState s) noexcept
Definition venue.hpp:55
net::SourceAddress source_address(const VenueSection &s)
Runtime knobs every connector honours, passed to the factory.
Definition registry.hpp:43
AddResult
Definition registry.hpp:130
std::unique_ptr< Venue > make_venue(VenueId id, const VenueSection &section, const VenueFactoryOptions &opts)
Builds the connector section.kind names.
net::EndpointSelectConfig endpoint_select_config(const EngineSection &e)
[engine] endpoint_select and its endpoint_ keys as the process's endpoint selection (net/endpoint_sel...
Definition registry.hpp:49
void validate_venues(const Config &cfg, const VenueRegistry &r=VenueRegistry::instance())
Checks every [venues.
void register_builtin_venues(VenueRegistry &r=VenueRegistry::instance())
Adds the connectors FastMM ships (binance_spot, binance_usdm, bybit, deribit, nasdaq_itch).
KeyType
Definition schema.hpp:22
StrongId< struct VenueTag, std::uint8_t > VenueId
Definition strong_id.hpp:39
Declarative config schema (8.7): every known key with its section, type and whether it is required.
Definition config.hpp:65
int endpoint_probe_samples
Definition config.hpp:135
int endpoint_spread_us
Definition config.hpp:136
int endpoint_probe_interval_s
Definition config.hpp:134
int endpoint_switch_us
Definition config.hpp:137
bool endpoint_select
Endpoint selection (net/endpoint_selector.hpp): connect to the fastest of a venue host's addresses.
Definition config.hpp:133
Definition config.hpp:227
What a connector declares about itself before one exists.
Definition registry.hpp:75
bool internal_transfer
Can move an asset between the accounts of a pool (Venue::transfer, VenueCaps::internal_transfer) once...
Definition registry.hpp:102
bool polls
Venue::poll() drives its sockets; the reactor never wakes for them.
Definition registry.hpp:86
bool bind_source
Binds every outbound connection (WebSocket, REST, the blocking control requests) to the section's sou...
Definition registry.hpp:98
bool order_entry
can send orders at all
Definition registry.hpp:79
bool replace
can amend a resting order in place
Definition registry.hpp:80
bool market_data_keys
Its market data needs the account's keys too (Alpaca): a dry run keeps api_key and api_secret.
Definition registry.hpp:78
bool positions
reports positions from the venue
Definition registry.hpp:81
bool executions
Can fetch what the account executed since a point in time, so a reconciliation replays the fills the ...
Definition registry.hpp:91
bool credentials
needs api_key / api_secret outside a dry run
Definition registry.hpp:76
bool account_pools
Runs as a member of an account pool ([venues.
Definition registry.hpp:94
bool position_snapshot
Its reconciliation reports the position of every subscribed derivative or stock, flat ones too,...
Definition registry.hpp:85
Definition registry.hpp:114
std::span< const VenueKeySpec > keys
the keys it owns, beyond the generic ones
Definition registry.hpp:125
std::unique_ptr< Venue >(*)(VenueId, const VenueSection &, const VenueFactoryOptions &) Factory
Throws std::invalid_argument (a bad URL, a key the connector rejects); the message names the venue.
Definition registry.hpp:119
std::span< const std::string_view > aliases
other kind values that select this entry
Definition registry.hpp:124
VenueCapabilities caps
Definition registry.hpp:126
Factory make
Definition registry.hpp:127
std::string_view name
Every member has a default, so a registration names only the ones it needs.
Definition registry.hpp:122
std::string_view summary
one line for the connector table
Definition registry.hpp:123
Definition registry.hpp:59
std::string record_raw_dir
non-empty: append raw frames to <dir>/<venue>-<channel>.jsonl
Definition registry.hpp:61
bool pooled
The venue is a pool's primary or member: its account shares the IP's request weight with the pool's o...
Definition registry.hpp:69
bool dry_run
public market data only: no keys, no orders
Definition registry.hpp:60
bool busy_poll
[engine] spin_mode = "busy": Venue::poll() must make progress
Definition registry.hpp:62
VenueId pool_of
A pool member ([venues.
Definition registry.hpp:66
One [venues.
Definition registry.hpp:107
std::string_view doc
Definition registry.hpp:111
bool required
Definition registry.hpp:110
std::string_view key
Definition registry.hpp:108
KeyType type
Definition registry.hpp:109
Venue: the control-path interface every exchange connector implements (6.2).