algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
stop.hpp
1#pragma once
2
4
5#include <array>
6#include <atomic>
7#include <cstdio>
8#include <optional>
9#include <string>
10#include <string_view>
11
12namespace algotradelib {
13namespace runner {
14
48
50inline constexpr std::array<StopIntent, 3> all_stop_intents{
54};
55
61inline constexpr std::string_view stop_intent_name(StopIntent intent) noexcept {
62 switch (intent) {
64 return "quiesce";
66 return "withdraw";
68 return "flatten";
69 }
70 return "";
71}
72
73static_assert(
76 [](StopIntent intent) {
77 return !stop_intent_name(intent).empty();
78 }
79 ),
80 "all_stop_intents must list every StopIntent"
81);
82
88inline std::optional<StopIntent> stop_intent_from(std::string_view text) noexcept {
90}
91
101inline constexpr int stop_intent_strength(StopIntent intent) noexcept {
102 switch (intent) {
104 return 0;
106 return 1;
108 return 2;
109 }
110 return 0;
111}
112
119inline constexpr bool stop_intent_closes(StopIntent intent) noexcept {
120 switch (intent) {
122 return false;
125 return true;
126 }
127 return true;
128}
129
136inline constexpr bool stop_intent_closes_positions(StopIntent intent) noexcept {
137 switch (intent) {
139 return true;
142 return false;
143 }
144 return false;
145}
146
153inline constexpr bool stop_intent_covers(StopIntent held, StopIntent candidate) noexcept {
154 return stop_intent_strength(held) >= stop_intent_strength(candidate);
155}
156
164public:
172 void request(StopIntent intent) noexcept {
173 const int desired = static_cast<int>(intent);
174 int current = requested_.load(std::memory_order_relaxed);
175 // A failed exchange reloads `current`, so the held intent's strength is read again each time.
176 while (current < 0 || stop_intent_strength(static_cast<StopIntent>(current)) < stop_intent_strength(intent)) {
177 if (requested_.compare_exchange_weak(current, desired, std::memory_order_release, std::memory_order_relaxed)) {
178 return;
179 }
180 }
181 }
182
187 bool requested() const noexcept {
188 return requested_.load(std::memory_order_acquire) >= 0;
189 }
190
195 std::optional<StopIntent> intent() const noexcept {
196 const int held = requested_.load(std::memory_order_acquire);
197 if (held < 0) {
198 return std::nullopt;
199 }
200 return static_cast<StopIntent>(held);
201 }
202
204 void clear() noexcept {
205 requested_.store(-1, std::memory_order_release);
206 }
207
208private:
209 // Negative means no request; otherwise a `StopIntent`.
210 std::atomic<int> requested_{-1};
211
212 // Asserted rather than assumed, because the signal-safety argument above rests entirely on it. If
213 // this ever fails on some platform, `request()` is no longer callable from a handler and the
214 // signal installer is unsound -- which is worth a compile error rather than a subtle deadlock in a
215 // handler that fires once a month.
216 static_assert(
217 std::atomic<int>::is_always_lock_free, "StopSource requires a lock-free atomic<int> to stay signal-safe"
218 );
219};
220
236public:
241 explicit FileStopChannel(std::string path = {}) : path_(std::move(path)) {}
242
247 bool enabled() const noexcept {
248 return !path_.empty();
249 }
250
255 const std::string& path() const noexcept {
256 return path_;
257 }
258
264 std::optional<StopIntent> poll() {
265 unrecognized_.clear();
266 if (path_.empty()) {
267 return std::nullopt;
268 }
269 std::FILE* handle = std::fopen(path_.c_str(), "rb");
270 if (handle == nullptr) {
271 return std::nullopt;
272 }
273 char buffer[64] = {};
274 const std::size_t read = std::fread(buffer, 1, sizeof(buffer) - 1, handle);
275 std::fclose(handle);
276
277 std::string_view text(buffer, read);
278 const auto begin = text.find_first_not_of(" \t\r\n");
279 if (begin == std::string_view::npos) {
280 return std::nullopt;
281 }
282 text.remove_prefix(begin);
283 const auto end = text.find_first_of(" \t\r\n");
284 if (end != std::string_view::npos) {
285 text = text.substr(0, end);
286 }
287
288 if (auto intent = stop_intent_from(text)) {
289 return intent;
290 }
291 unrecognized_.assign(text);
292 return std::nullopt;
293 }
294
299 const std::string& unrecognized() const noexcept {
300 return unrecognized_;
301 }
302
303private:
304 std::string path_;
305 std::string unrecognized_;
306};
307
308} // namespace runner
309} // namespace algotradelib
FileStopChannel(std::string path={})
Constructs a channel.
Definition stop.hpp:241
bool enabled() const noexcept
Tests whether a path was configured.
Definition stop.hpp:247
const std::string & path() const noexcept
Returns the watched path.
Definition stop.hpp:255
std::optional< StopIntent > poll()
Reads the file's first word.
Definition stop.hpp:264
const std::string & unrecognized() const noexcept
Returns the word the last poll did not recognize.
Definition stop.hpp:299
A stop request the loop polls; safe to set from a signal handler.
Definition stop.hpp:163
std::optional< StopIntent > intent() const noexcept
Returns the requested intent.
Definition stop.hpp:195
bool requested() const noexcept
Tests whether a stop has been requested.
Definition stop.hpp:187
void clear() noexcept
Clears the request (for tests, or a supervisor deliberately resuming); the loop never does.
Definition stop.hpp:204
void request(StopIntent intent) noexcept
Requests a stop, keeping whichever of this and an earlier intent is stronger.
Definition stop.hpp:172
A compile-time check that a list names every value of an enum, and a parser over the list.
Runs a strategy: the runner, the polling loop, the order journal, and stopping.
Definition clock.hpp:7
constexpr bool stop_intent_closes_positions(StopIntent intent) noexcept
Tests whether a stop intent closes positions with market orders, after withdrawing the working orders...
Definition stop.hpp:136
std::optional< StopIntent > stop_intent_from(std::string_view text) noexcept
Parses an intent's name.
Definition stop.hpp:88
constexpr int stop_intent_strength(StopIntent intent) noexcept
Returns an intent's strength: a stronger intent does everything a weaker one does.
Definition stop.hpp:101
constexpr std::array< StopIntent, 3 > all_stop_intents
Every StopIntent, in declaration order.
Definition stop.hpp:50
StopIntent
What an operator asks for when stopping a running loop.
Definition stop.hpp:29
@ FLATTEN
Withdraw, then ask the broker to close the positions with market orders.
Definition stop.hpp:46
@ WITHDRAW
Quiesce, then ask the broker to cancel the working orders.
Definition stop.hpp:40
@ QUIESCE
Stop sending new orders; nothing is cancelled or closed. The default, and what a signal means.
Definition stop.hpp:34
constexpr bool stop_intent_covers(StopIntent held, StopIntent candidate) noexcept
Tests whether one intent does everything another would.
Definition stop.hpp:153
constexpr bool stop_intent_closes(StopIntent intent) noexcept
Tests whether a stop intent asks for a closing pass (cancellations, and for FLATTEN closing orders) a...
Definition stop.hpp:119
constexpr std::string_view stop_intent_name(StopIntent intent) noexcept
Returns a lowercase name for an intent, as written in the control file ("withdraw").
Definition stop.hpp:61
The library: broker-neutral types (core), and the modules below.
constexpr std::optional< Enum > value_named(const std::array< Enum, N > &values, Name name, std::string_view text) noexcept
Returns the value of a list whose name is text, so a parser reads exactly what its writer writes.
constexpr bool lists_every_value(const std::array< Enum, N > &values, IsEnumerator is_enumerator) noexcept
Tests whether a list holds every value of an enum exactly once, in any order.