Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
risk_manager.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <slick/logger.hpp>
6#include <unordered_map>
7#include <string>
8#include <chrono>
9#include <string_view>
10
11namespace slick::trader {
12
13// Per-instrument risk limits
15 qty_t max_position_size{to_qty_t(1000000.0)}; // Max position for this instrument
16 price_t max_order_value{to_price_t(100000.0)}; // Max notional value per order
17};
18
19// Risk limits configuration
20struct RiskLimits {
21 // Default position limits (applies to all instruments without specific limits)
22 qty_t max_position_size{to_qty_t(1000000.0)}; // Max position per instrument
23 price_t max_order_value{to_price_t(100000.0)}; // Max notional value per order
24
25 // Per-instrument overrides (keyed by instrument name)
26 std::unordered_map<std::string, InstrumentRiskLimits> instrument_limits;
27
28 // Rate limits
29 uint32_t max_orders_per_second{100};
30
31 // Loss limits
32 price_t max_loss_per_strategy{to_price_t(-10000.0)}; // Max loss before halt
33
34 // Get effective limits for an instrument
35 InstrumentRiskLimits get_instrument_limits(std::string_view instrument) const {
36 auto it = instrument_limits.find(std::string(instrument));
37 if (it != instrument_limits.end()) {
38 return it->second;
39 }
40 // Return default limits
42 }
43};
44
45// Pre-trade risk manager
46// Validates orders before submission
48public:
49 explicit RiskManager(PositionTracker& position_tracker)
50 : position_tracker_(position_tracker) {}
51
52 ~RiskManager() = default;
53
54 // Set risk limits for a strategy
55 void set_limits(const std::string& strategy_id, const RiskLimits& limits);
56
57 // Get risk limits for a strategy
58 RiskLimits get_limits(const std::string& strategy_id) const;
59
60 // Validate order before submission
61 // Returns true if order passes all checks, false otherwise
62 bool validate_order(const std::string& strategy_id, std::string_view instrument,
63 Side side, qty_t quantity, price_t price, std::string* reject_reason = nullptr);
64
65 // Check if strategy is halted due to risk breach
66 bool is_halted(const std::string& strategy_id) const;
67 std::string get_halt_reason(const std::string& strategy_id) const;
68
69 // Manually halt/resume a strategy
70 void halt_strategy(const std::string& strategy_id, const std::string& reason);
71 void resume_strategy(const std::string& strategy_id);
72
73 // Clear all limits and halt states
74 void clear_all();
75
76private:
77 struct RateLimit {
78 uint32_t count{0};
79 uint64_t window_start_ns{0};
80 };
81
82 bool check_position_limit(const std::string& strategy_id, std::string_view instrument,
83 bool is_buy, qty_t order_qty, std::string* reason);
84
85 bool check_order_value_limit(std::string_view instrument, price_t price, qty_t quantity,
86 const RiskLimits& limits, std::string* reason);
87
88 bool check_rate_limit(const std::string& strategy_id, const RiskLimits& limits,
89 std::string* reason);
90
91 PositionTracker& position_tracker_;
92 std::unordered_map<std::string, RiskLimits> limits_;
93 std::unordered_map<std::string, RateLimit> rate_limits_;
94 std::unordered_map<std::string, std::string> halted_strategies_; // strategy_id -> reason
95};
96
97} // namespace slick::trader
void halt_strategy(const std::string &strategy_id, const std::string &reason)
bool validate_order(const std::string &strategy_id, std::string_view instrument, Side side, qty_t quantity, price_t price, std::string *reject_reason=nullptr)
RiskLimits get_limits(const std::string &strategy_id) const
void set_limits(const std::string &strategy_id, const RiskLimits &limits)
RiskManager(PositionTracker &position_tracker)
bool is_halted(const std::string &strategy_id) const
void resume_strategy(const std::string &strategy_id)
std::string get_halt_reason(const std::string &strategy_id) const
price_t to_price_t(double value)
Definition types.hpp:24
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16
qty_t to_qty_t(double value)
Definition types.hpp:32
InstrumentRiskLimits get_instrument_limits(std::string_view instrument) const
std::unordered_map< std::string, InstrumentRiskLimits > instrument_limits