46 std::string
host{
"0.0.0.0"};
56 std::string_view config_path,
61 slick::stream_buffer_multiplexer& mux)
62 : strategy_manager_(strategy_manager)
63 , position_tracker_(position_tracker)
64 , pnl_calculator_(pnl_calculator)
65 , risk_manager_(risk_manager)
67 , event_cursor_(event_mux_.initial_reading_index())
68 , config_path_(config_path)
88 return running_.load(std::memory_order_acquire);
92 uint16_t
get_port()
const {
return config_.port; }
98 void close_connections();
101 void setup_routes(uWS::App &app);
102 void handle_get_strategies(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
103 void handle_get_positions(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
104 void handle_get_pnl(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
105 void handle_get_strategy_positions(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
106 const std::string& strategy_id);
107 void handle_get_strategy_pnl(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
108 const std::string& strategy_id);
109 void handle_post_order(uWS::HttpResponse<false>* res, uWS::HttpRequest* req, std::string_view body);
110 void handle_post_strategy_control(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
111 const std::string& strategy_id, std::string_view body);
112 void handle_get_risk_limits(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
113 const std::string& strategy_id);
114 void handle_put_risk_limits(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
115 const std::string& strategy_id, std::string_view body);
116 void handle_get_strategy_config(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
117 const std::string& strategy_id);
118 void handle_put_strategy_config(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
119 const std::string& strategy_id, std::string_view body);
120 void handle_post_halt_strategy(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
121 const std::string& strategy_id, std::string_view body);
122 void handle_post_resume_strategy(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
123 const std::string& strategy_id);
124 void handle_get_brokers(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
125 void handle_get_ladder_config(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
126 void handle_put_ladder_config(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
127 const std::string& body);
128 void handle_get_orders(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
129 void handle_get_fills(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
130 void handle_delete_order(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
131 const std::string& strategy_id,
const std::string& order_id,
132 std::string_view body);
133 void handle_put_order(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
134 const std::string& strategy_id,
const std::string& order_id,
135 std::string_view body);
136 void handle_delete_all_orders(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
138 enum class WorkspaceChange { SAVE,
REMOVE, USE };
139 void handle_workspace_change(uWS::HttpResponse<false>* res, std::string_view body, WorkspaceChange change);
140 void handle_post_sim_replay(uWS::HttpResponse<false>* res);
144 bool publish_strategy_config(
const std::string& strategy_id,
const nlohmann::json& params);
147 void save_strategy_parameters(
const std::string& strategy_id,
const nlohmann::json& params);
151 bool save_to_strategy_config_file(
const std::string& configs_folder,
152 const std::string& strategy_id,
153 const nlohmann::json& params);
156 void handle_ws_open(uWS::WebSocket<false, true, void*>* ws);
157 void handle_ws_message(uWS::WebSocket<false, true, void*>* ws, std::string_view message);
158 void handle_ws_close(uWS::WebSocket<false, true, void*>* ws);
160 using Socket = uWS::WebSocket<false, true, void*>;
165 void send_text(Socket* ws, std::string_view message);
166 void close_lagging();
171 static constexpr unsigned int WS_MAX_BACKPRESSURE = 16u << 20;
174 void send_json_response(uWS::HttpResponse<false>* res,
const std::string&
json,
int status = 200);
175 void send_error_response(uWS::HttpResponse<false>* res,
const std::string& error,
int status = 400);
176 void add_cors_headers(uWS::HttpResponse<false>* res);
179 void process_events();
181 void handle_order_ack(
const OrderAckEvent& ack);
182 void handle_order_fill(
const OrderFillEvent& fill);
183 void handle_order_cancel(
const OrderCancelEvent& cancel);
184 void handle_order_reject(
const OrderRejectEvent& reject);
186 void broadcast_level2_update(
const Level2UpdateEvent& event);
188 void broadcast_trade(
const TradeEvent& event);
190 void broadcast(std::string_view message);
192 void broadcast_fill(
json& message);
197 void index_listings();
198 void send_instruments(Socket* ws);
199 void send_instruments_added(uint64_t now_ns);
205 bool watched(std::string_view instrument);
206 void unwatched(std::string_view instrument);
207 void release_holds(uint64_t now_ns);
209 bool publish_ladder_subscription(
BrokerId broker, std::string_view instrument,
bool subscribe,
213 void broadcast_pnl_update();
216 void check_and_broadcast_broker_status();
219 void service_sim(uint64_t now_ns);
221 std::string_view environment()
const;
223 std::string sim_json(std::string_view type = {})
const;
224 void make_health_json();
226 StrategyManager& strategy_manager_;
227 PositionTracker& position_tracker_;
228 PnLCalculator& pnl_calculator_;
229 RiskManager& risk_manager_;
232 std::atomic_bool running_{
false};
233 std::thread worker_thread_;
235 uWS::App* app_{
nullptr};
242 std::mutex loop_mutex_;
243 uWS::Loop* loop_{
nullptr};
245 slick::stream_buffer_multiplexer& event_mux_;
246 uint64_t event_cursor_{0};
250 static constexpr uint64_t PNL_BROADCAST_INTERVAL_NS = 250'000'000;
251 bool pnl_changed_{
false};
252 uint64_t last_pnl_broadcast_ns_{0};
266 static constexpr uint64_t INSTRUMENTS_ADDED_INTERVAL_NS = 250'000'000;
268 uint32_t listings_indexed_{0};
270 std::vector<const std::string*> listings_added_;
271 uint64_t last_instruments_sent_ns_{0};
272 std::string instruments_message_;
274 std::unordered_set<uWS::WebSocket<false, true, void*>*> instrument_clients_;
280 uint64_t release_at_ns;
283 size_t releasing_{0};
286 static constexpr uint64_t PROCESS_EVENTS_BUDGET_NS = 1'000'000;
289 std::string level2_message_;
290 std::string trade_message_;
296 std::shared_ptr<slick::stream_buffer_multiplexer::producer_buffer> config_producer_buffer_;
311 std::unordered_set<uWS::WebSocket<false, true, void*>*> all_ws_clients_;
314 std::unordered_set<Socket*> lagging_;
318 Level2Streams<Socket> level2_streams_;
321 nlohmann::json ladder_config_;
324 std::string health_json_;
325 std::string environment_;
328 std::unique_ptr<SimControlLink> sim_;
331 WorkspaceStore workspaces_;
334 std::string config_path_;
337 struct TrackedOrder {
338 std::string strategy_id;
339 std::string client_order_id;
340 std::string order_id;
341 std::string instrument;
346 double filled_qty{0.0};
347 double remaining_qty{0.0};
348 std::string status{
"PENDING_NEW"};
349 uint64_t created_time{0};
350 uint64_t last_update_time{0};
354 std::unordered_map<std::string, TrackedOrder> open_orders_;
356 static constexpr size_t FILL_HISTORY_CAPACITY = 1000;
357 FillHistory fills_{FILL_HISTORY_CAPACITY};
359 std::unordered_map<std::string, std::string> oid_to_cid_;
362 struct BrokerSnapshot {
363 bool md_enabled{
false};
364 bool md_connected{
false};
365 bool order_enabled{
false};
366 bool order_connected{
false};