Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
strategy_config_event.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <algorithm>
5#include <cstring>
6#include <string_view>
7#include <type_traits>
8
9namespace slick::trader {
10
11constexpr size_t MAX_STRATEGY_NAME_LENGTH = 64;
12
13// Upper bound on the JSON text one event may carry. Nothing structural — the producer
14// buffer would take far more — but a strategy's "parameters" block is a handful of
15// scalars, so anything approaching this is a caller mistake worth rejecting at the API
16// edge rather than silently truncating on the wire.
17constexpr size_t MAX_STRATEGY_CONFIG_LENGTH = 64 * 1024;
18
19// A strategy parameter update, handed from the WebServer thread to the StrategyManager
20// worker thread over the event bus.
21//
22// The parameters travel as JSON *text* in a trailing flexible array, the same idiom
23// Level2UpdateEvent uses for its price levels: the bus carries trivially copyable records
24// only, and nlohmann::json is neither trivially copyable nor fixed-size. The consumer
25// parses it back on the worker thread, which is the whole point — the strategy's
26// on_config() then runs on the thread that owns its state.
27//
28// Publish with size_for(json.size()), never sizeof(StrategyConfigEvent).
29#pragma pack(push, 1)
30struct StrategyConfigEvent : public Event {
32 uint32_t config_length; // bytes of JSON text in `config`, unterminated
33 char config[0]; // JSON text, published inline behind the header
34
40
41 void set_strategy(std::string_view name) {
42 const size_t len = std::min(name.size(), MAX_STRATEGY_NAME_LENGTH - 1);
43 std::memcpy(strategy, name.data(), len);
44 strategy[len] = '\0';
45 }
46
47 void set_config(std::string_view json_text) {
48 config_length = static_cast<uint32_t>(json_text.size());
49 std::memcpy(config, json_text.data(), json_text.size());
50 }
51
52 std::string_view config_text() const {
53 return {config, config_length};
54 }
55
56 static uint32_t size_for(size_t json_length) {
57 return static_cast<uint32_t>(sizeof(StrategyConfigEvent) + json_length);
58 }
59};
60#pragma pack(pop)
61
62static_assert(std::is_trivially_copyable_v<StrategyConfigEvent>,
63 "StrategyConfigEvent must be trivially copyable");
64
65} // namespace slick::trader
constexpr size_t MAX_STRATEGY_CONFIG_LENGTH
constexpr size_t MAX_STRATEGY_NAME_LENGTH
Event(EventType t=EventType::TRADE)
Definition event.hpp:63
static uint32_t size_for(size_t json_length)
void set_config(std::string_view json_text)
void set_strategy(std::string_view name)
char strategy[MAX_STRATEGY_NAME_LENGTH]