Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
pcap_exchange.hpp
Go to the documentation of this file.
1#pragma once
2
4
6#include <nlohmann/json.hpp>
7
8#include <cstdint>
9#include <format>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <string_view>
14#include <unordered_map>
15
19namespace slick::sim::exch {
20
22[[nodiscard]] inline bool is_enabled_feed(const nlohmann::json &feed, std::string_view type) {
23 return feed.value("enabled", true) && feed.value("type", std::string()) == type;
24}
25
27[[nodiscard]] inline bool has_enabled_feed(const nlohmann::json &instance, std::string_view type) {
28 for (const auto &feed : instance.value("md_feeds", nlohmann::json::array())) {
29 if (is_enabled_feed(feed, type)) {
30 return true;
31 }
32 }
33 return false;
34}
35
38inline void claim_pcap_control_ports(std::unordered_map<uint16_t, std::string> &ports, const nlohmann::json &instance,
39 std::string_view feed_type, const std::string &owner, std::string_view venue) {
40 for (const auto &feed : instance.value("md_feeds", nlohmann::json::array())) {
41 if (is_enabled_feed(feed, feed_type) && feed.contains("control_channel")) {
42 const uint16_t port = feed["control_channel"].value("port", uint16_t{0});
43 if (port != 0) {
44 claim(ports, port, owner, "pcap control channel port", venue);
45 }
46 }
47 }
48}
49
52template <typename Queue>
53[[nodiscard]] std::shared_ptr<md_feed::ReplayGate> make_replay_gate(const nlohmann::json &instance,
54 std::string_view feed_type, Queue &md_queue) {
55 if (!has_enabled_feed(instance, feed_type)) {
56 return nullptr;
57 }
58 return std::make_shared<md_feed::ReplayGate>([&md_queue] { return md_queue.initial_reading_index(); });
59}
60
62inline void reject_pcap_with_live_feeds(bool has_pcap_feed, size_t live_feeds, std::string_view venue,
63 std::string_view kind, const std::string &name) {
64 if (has_pcap_feed && live_feeds != 0) {
65 throw std::runtime_error(std::format("{}: {} '{}' mixes a pcap feed with live feeds", venue, kind, name));
66 }
67}
68
71template <typename Event, typename Feed>
72[[nodiscard]] Event replay_stamped(Event event, const Feed *feed) {
73 if (feed) {
74 event.transact_time = feed->event_time_ns();
75 }
76 return event;
77}
78
79} // namespace slick::sim::exch
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
Event replay_stamped(Event event, const Feed *feed)
event with its time set from the replay: a replay is stamped on the wall clock, not with the capture'...
bool has_enabled_feed(const nlohmann::json &instance, std::string_view type)
True when the instance's md_feeds has an enabled feed of type.
void claim(std::unordered_map< T, std::string > &claimed, const T &value, const std::string &owner, std::string_view what, std::string_view venue)
Records that instance owner uses value.
std::shared_ptr< md_feed::ReplayGate > make_replay_gate(const nlohmann::json &instance, std::string_view feed_type, Queue &md_queue)
The gate a replaying instance's publisher waits on, or null when its config has no enabled feed of fe...
void claim_pcap_control_ports(std::unordered_map< uint16_t, std::string > &ports, const nlohmann::json &instance, std::string_view feed_type, const std::string &owner, std::string_view venue)
Claims the TCP control port of each enabled pcap feed of instance for owner.
bool is_enabled_feed(const nlohmann::json &feed, std::string_view type)
True for a md_feeds entry of type that is not disabled.
void reject_pcap_with_live_feeds(bool has_pcap_feed, size_t live_feeds, std::string_view venue, std::string_view kind, const std::string &name)
Throws when a replay and live feeds would both write the one book.