Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
strategy_config.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <nlohmann/json.hpp>
5#include <string>
6#include <unordered_map>
7#include <optional>
8
9namespace slick::trader {
10
11// Per-strategy configuration
12// Loaded from JSON config files
14 std::string name; // Strategy identifier (matches plugin name)
15 bool enabled{true}; // Whether to load and run this strategy
16
17 // Risk limits
19
20 // Custom parameters (strategy-specific)
21 // Strategies can access these via get_parameter()
22 nlohmann::json parameters = nlohmann::json::object();
23
24 // Parse from JSON
25 static StrategyConfig from_json(const nlohmann::json& j);
26
27 // Get typed parameter with default
28 template<typename T>
29 T get_parameter(const std::string& key, const T& default_value) const {
30 if (parameters.contains(key)) {
31 try {
32 return parameters[key].get<T>();
33 } catch (const std::exception&) {
34 return default_value;
35 }
36 }
37 return default_value;
38 }
39};
40
41// Parse strategy configuration from JSON
42inline StrategyConfig StrategyConfig::from_json(const nlohmann::json& j) {
43 StrategyConfig config;
44
45 config.name = j.value("name", "");
46 config.enabled = j.value("enabled", true);
47
48 // Parse risk limits if present
49 if (j.contains("risk_limits")) {
50 const auto& rl = j["risk_limits"];
51
52 if (rl.contains("max_position_size")) {
53 config.risk_limits.max_position_size = to_qty_t(rl["max_position_size"].get<double>());
54 }
55
56 if (rl.contains("max_order_value")) {
57 config.risk_limits.max_order_value = to_price_t(rl["max_order_value"].get<double>());
58 }
59
60 if (rl.contains("max_orders_per_second")) {
61 config.risk_limits.max_orders_per_second = rl["max_orders_per_second"].get<uint32_t>();
62 }
63
64 if (rl.contains("max_loss_per_strategy")) {
65 config.risk_limits.max_loss_per_strategy = to_price_t(rl["max_loss_per_strategy"].get<double>());
66 }
67
68 // Parse per-instrument limits
69 if (rl.contains("instruments") && rl["instruments"].is_object()) {
70 for (const auto& [instrument, inst_limits] : rl["instruments"].items()) {
72
73 if (inst_limits.contains("max_position_size")) {
74 limits.max_position_size = to_qty_t(inst_limits["max_position_size"].get<double>());
75 }
76
77 if (inst_limits.contains("max_order_value")) {
78 limits.max_order_value = to_price_t(inst_limits["max_order_value"].get<double>());
79 }
80
81 config.risk_limits.instrument_limits[instrument] = limits;
82 }
83 }
84 }
85
86 // Store custom parameters: an object, empty when the file has none
87 if (j.contains("parameters") && j["parameters"].is_object()) {
88 config.parameters = j["parameters"];
89 }
90
91 return config;
92}
93
94} // namespace slick::trader
price_t to_price_t(double value)
Definition types.hpp:24
qty_t to_qty_t(double value)
Definition types.hpp:32
std::unordered_map< std::string, InstrumentRiskLimits > instrument_limits
static StrategyConfig from_json(const nlohmann::json &j)
T get_parameter(const std::string &key, const T &default_value) const