Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
broker_status.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <atomic>
5#include <array>
6#include <cstdint>
7#include <cstring>
8#include <limits>
9
10namespace slick::trader {
11
12// Plugins index the host's registry with their own build of this struct: a change to its
13// layout is a change to STRATEGY_API_VERSION.
15 static constexpr uint64_t NO_MD_REQUEST_CURSOR = std::numeric_limits<uint64_t>::max();
16
17 std::atomic<bool> md_enabled{false};
18 std::atomic<bool> md_connected{false};
19 std::atomic<bool> order_enabled{false};
20 std::atomic<bool> order_connected{false};
21 char name[32]{};
22};
23
24class BrokerStatusRegistry {
25public:
26 // Process-wide singleton — but strategy plugins are shared libraries that link
27 // slick-trader-core statically, so on Windows each module gets its own copy of
28 // the function-local static below. A plugin would then read an all-false registry
29 // that nothing ever writes, and every order it submits would be skipped as
30 // "broker order connector disconnected". StrategyLoader injects the host's
31 // instance into each plugin via set_instance(), the same way it forwards the
32 // logger. See SLICK_TRADER_EXPORT_STRATEGY / set_broker_status_registry().
33 static BrokerStatusRegistry& instance() {
34 static BrokerStatusRegistry module_local;
35 auto* shared = shared_.load(std::memory_order_acquire);
36 return shared ? *shared : module_local;
37 }
38
39 // Point this module's registry at the host's instance. Called once per plugin at
40 // load time, before create_strategy(); never called in the host, which keeps
41 // using its own module_local.
42 static void set_instance(BrokerStatusRegistry* registry) {
43 shared_.store(registry, std::memory_order_release);
44 }
45
46 void set_name(BrokerId id, const char* name) {
47 auto& s = slot(id);
48 std::strncpy(s.name, name, sizeof(s.name) - 1);
49 s.name[sizeof(s.name) - 1] = '\0';
50 }
51
52 void set_md_enabled(BrokerId id, bool v);
53 void set_md_connected(BrokerId id, bool v);
54 void set_order_enabled(BrokerId id, bool v);
55 void set_order_connected(BrokerId id, bool v);
56
57 bool is_md_enabled(BrokerId id) const { return slot(id).md_enabled.load(std::memory_order_acquire); }
58 bool is_md_connected(BrokerId id) const { return slot(id).md_connected.load(std::memory_order_acquire); }
59 bool is_order_enabled(BrokerId id) const { return slot(id).order_enabled.load(std::memory_order_acquire); }
60 bool is_order_connected(BrokerId id) const { return slot(id).order_connected.load(std::memory_order_acquire); }
61
62 // A broker this process built: its connectors name it when they are made.
63 bool is_registered(BrokerId id) const { return slot(id).name[0] != '\0'; }
64
65 // Why `id` cannot take an order now, or nullptr when it can.
66 const char* order_unavailable_reason(BrokerId id) const {
67 if (!is_order_enabled(id)) {
68 return "Broker order connector disabled";
69 }
70 if (!is_order_connected(id)) {
71 return "Broker order connector disconnected";
72 }
73 return nullptr;
74 }
75
76 // How far the market data connector has read its request queue, NO_MD_REQUEST_CURSOR until
77 // it first reads. The queue is lossy, so StrategyManager writes no further ahead of it than
78 // the queue holds. Release: the connector is done with every request before `cursor` once it
79 // says so, and the next write may land on them. process_md_request() calls it.
80 void set_md_request_cursor(BrokerId id, uint64_t cursor) noexcept {
81 request_cursors_[static_cast<size_t>(id)].value.store(cursor, std::memory_order_release);
82 }
83 uint64_t md_request_cursor(BrokerId id) const noexcept {
84 return request_cursors_[static_cast<size_t>(id)].value.load(std::memory_order_acquire);
85 }
86
87 const BrokerStatus& get(BrokerId id) const { return slot(id); }
88
89 const std::array<BrokerStatus, static_cast<size_t>(BrokerId::_BROKER_ID_COUNT_)>& all() const {
90 return statuses_;
91 }
92
93private:
94 BrokerStatusRegistry() = default;
95
96 BrokerStatus& slot(BrokerId id) {
97 return statuses_[static_cast<size_t>(id)];
98 }
99 const BrokerStatus& slot(BrokerId id) const {
100 return statuses_[static_cast<size_t>(id)];
101 }
102
103 // Module-local redirect slot: null in the host, set to the host's registry in
104 // every plugin. Deliberately not shared across modules — each module needs its own.
105 static inline std::atomic<BrokerStatusRegistry*> shared_{nullptr};
106
107 std::array<BrokerStatus, static_cast<size_t>(BrokerId::_BROKER_ID_COUNT_)> statuses_;
108 // A line each: every connector writes its own, and the status flags beside are read often.
109 struct alignas(64) RequestCursor {
110 std::atomic<uint64_t> value{BrokerStatus::NO_MD_REQUEST_CURSOR};
111 };
112 std::array<RequestCursor, static_cast<size_t>(BrokerId::_BROKER_ID_COUNT_)> request_cursors_;
113};
114
115} // namespace slick::trader
void set_md_request_cursor(BrokerId id, uint64_t cursor) noexcept
bool is_order_enabled(BrokerId id) const
void set_order_enabled(BrokerId id, bool v)
bool is_md_enabled(BrokerId id) const
void set_name(BrokerId id, const char *name)
bool is_order_connected(BrokerId id) const
void set_md_connected(BrokerId id, bool v)
const char * order_unavailable_reason(BrokerId id) const
void set_order_connected(BrokerId id, bool v)
bool is_md_connected(BrokerId id) const
bool is_registered(BrokerId id) const
static BrokerStatusRegistry & instance()
const BrokerStatus & get(BrokerId id) const
static void set_instance(BrokerStatusRegistry *registry)
const std::array< BrokerStatus, static_cast< size_t >(BrokerId::_BROKER_ID_COUNT_)> & all() const
uint64_t md_request_cursor(BrokerId id) const noexcept
void set_md_enabled(BrokerId id, bool v)
std::atomic< bool > order_enabled
std::atomic< bool > order_connected
std::atomic< bool > md_connected
std::atomic< bool > md_enabled
static constexpr uint64_t NO_MD_REQUEST_CURSOR