Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
connector_support.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <slick/logger.hpp>
6#include <atomic>
7#include <chrono>
8#include <cstdint>
9#include <cstdlib>
10#include <string>
11#include <string_view>
12
13namespace slick::trader {
14
15// An environment variable's value, or `fallback` when it is unset or empty. Credentials
16// come from here, never from a config file on disk.
17inline std::string env_or(const char* name, std::string fallback) {
18 const char* value = std::getenv(name);
19 return value && *value ? std::string(value) : std::move(fallback);
20}
21
22// The wall clock minus the framework's monotonic clock (get_timestamp_ns()), taken once:
23// timers run on the monotonic clock, exchanges want wall-clock times on the wire.
24inline int64_t wall_clock_offset_ns() {
25 const auto wall_ns =
26 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
27 .count();
28 return static_cast<int64_t>(wall_ns) - static_cast<int64_t>(get_timestamp_ns());
29}
30
31inline uint64_t wall_clock_ns() {
32 return static_cast<uint64_t>(
33 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
34 .count());
35}
36
37// A connector's up/down state as BrokerStatusRegistry shows it, and its `connected`
38// flag: set, and logged, only when it changes.
40public:
41 enum class Component : uint8_t { MARKET_DATA, ORDER };
42
43 StatusLatch(BrokerId broker, Component component) noexcept : broker_(broker), component_(component) {}
44
45 // Returns true when `up` changed the state.
46 bool update(bool up, std::string_view who, std::string_view up_text, std::string_view down_text) {
47 if (up == up_) {
48 return false;
49 }
50 up_ = up;
51 connected_.store(up, std::memory_order_release);
52 publish(up);
53 LOG_INFO("{}: {}", who, up ? up_text : down_text);
54 return true;
55 }
56
57 // Down, without a log line: the connector is stopping.
58 void reset() {
59 up_ = false;
60 connected_.store(false, std::memory_order_release);
61 publish(false);
62 }
63
64 [[nodiscard]] bool up() const noexcept { return connected_.load(std::memory_order_acquire); }
65
66private:
67 void publish(bool up) {
68 auto& registry = BrokerStatusRegistry::instance();
69 if (component_ == Component::MARKET_DATA) {
70 registry.set_md_connected(broker_, up);
71 } else {
72 registry.set_order_connected(broker_, up);
73 }
74 }
75
76 BrokerId broker_;
77 Component component_;
78 bool up_ = false;
79 std::atomic_bool connected_{false};
80};
81
82} // namespace slick::trader
static BrokerStatusRegistry & instance()
bool update(bool up, std::string_view who, std::string_view up_text, std::string_view down_text)
StatusLatch(BrokerId broker, Component component) noexcept
std::string env_or(const char *name, std::string fallback)
uint64_t get_timestamp_ns()
Definition time.hpp:7
uint64_t wall_clock_ns()
int64_t wall_clock_offset_ns()