Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
strategy_base.hpp
Go to the documentation of this file.
1#pragma once
2
13#include <slick/logger.hpp>
14#include <nlohmann/json.hpp>
15#include <atomic>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <vector>
21
22namespace slick::trader {
23
24// Base class for strategies
25// Provides common functionality and utilities
26class StrategyBase : public IStrategy {
27public:
28 StrategyBase(const std::string& name, const std::string& version)
29 : name_(name)
30 , version_(version) {}
31
32 virtual ~StrategyBase() override = default;
33
34 // IStrategy interface
35 const char* name() const override { return name_.c_str(); }
36 const char* version() const override { return version_.c_str(); }
37
38 // Default: no subscriptions (strategies should override this)
39 std::vector<SubscriptionRequest> get_subscriptions() const override {
40 return {};
41 }
42
44 const json& config,
45 PositionTracker& position_tracker,
46 PnLCalculator& pnl_calculator,
47 RiskManager& risk_manager,
48 slick::queue<uint8_t>& order_request_queue
49 ) override {
50 LOG_INFO("Strategy {}: Initializing", name_);
51 position_tracker_ = &position_tracker;
52 pnl_calculator_ = &pnl_calculator;
53 risk_manager_ = &risk_manager;
54 order_request_queue_ = &order_request_queue;
55 config_params_ = config;
56 on_config(config);
57 return on_initialize();
58 }
59
60 void start() override {
61 if (!running_.load(std::memory_order_relaxed)) {
62 LOG_INFO("Strategy {}: Starting", name_);
63 running_.store(true, std::memory_order_release);
64 on_start();
65 }
66 }
67
68 void stop() override {
69 if(running_.load(std::memory_order_relaxed)) {
70 LOG_INFO("Strategy {}: Stopping", name_);
71 on_stop();
72 running_.store(false, std::memory_order_release);
73 }
74 }
75
76 void shutdown() override {
77 stop();
78 LOG_INFO("Strategy {}: Shutting down", name_);
80 }
81
82 // Apply a parameter update. Call this from the StrategyManager worker thread ONLY:
83 // on_config() is where a strategy reconfigures state that only its own thread owns.
84 // The WebServer does not call it directly — PUT /api/strategies/:id/config publishes a
85 // STRATEGY_CONFIG event and StrategyManager::dispatch_strategy_config() lands it here.
86 // (StrategyBase::initialize() also calls on_config(), on the loading thread, before the
87 // worker exists.)
88 void set_config(const nlohmann::json& params) {
89 config_params_ = params;
90 on_config(params); // Call derived class hook
91 }
92
93 // Default implementations - strategies can override if needed
94 void on_trade(const TradeEvent& event) override {}
95 void on_quote(const QuoteEvent& event) override {}
96 void on_order_book(std::string_view /*instrument*/, const slick::orderbook::OrderBookL2& /*book*/) override {}
97
98 // Maintains order tracking state, then calls on_ack() hook for derived classes
99 void on_order_ack(const OrderAckEvent& event) override {
100 const std::string client_id(event.client_order_id);
101 const std::string order_id(event.order_id);
102
103 // Every strategy receives every ack, so foreign ones have to be dropped here —
104 // before the "unknown client_order_id" branch below adopts them and starts
105 // booking another strategy's fills into this strategy's position and PnL.
106 if (!owns_order(client_id, order_id)) {
107 LOG_DEBUG("Strategy {}: ignoring ACK for foreign order client_order_id={} order_id={}",
108 name_, client_id, order_id);
109 return;
110 }
111
112 LOG_INFO("Strategy {}: on_order_ack client_order_id={} order_id={}", name_, client_id, order_id);
113 // Coinbase sends a second OPEN-status ACK with empty client_order_id; ignore it to
114 // prevent a phantom "" entry from overwriting order_id_to_client_id_ mappings.
115 if (client_id.empty()) {
116 LOG_DEBUG("Strategy {}: ignoring ACK with empty client_order_id for order_id={}", name_, order_id);
117 on_ack(event);
118 return;
119 }
120 auto it = orders_by_client_id_.find(client_id);
121 if (it == orders_by_client_id_.end()) {
122 // Ours by client_order_id but not tracked yet (e.g. replayed from a broker
123 // reconnect snapshot) — owns_order() has already vouched for the prefix.
124 LOG_INFO("Strategy {}: ACK for unknown client_order_id={} - inserting new entry", name_, client_id);
125 Order order;
126 order.client_order_id = client_id;
127 order.order_id = order_id;
128 order.instrument = std::string(event.instrument);
129 order.broker_id = event.broker_id;
130 order.side = event.side;
131 order.type = event.order_type;
132 order.time_in_force = event.time_in_force;
133 order.price = event.price;
134 order.stop_price = event.stop_price;
135 order.qty = event.quantity;
136 order.remaining_qty = event.quantity;
137 order.status = event.status;
138 order.created_time = event.event_time_ns;
139 order.last_update_time = event.timestamp_ns;
140 order.post_only = event.post_only;
141 order_id_to_client_id_[order.order_id] = client_id;
142 orders_by_client_id_.emplace(client_id, std::move(order));
143 } else {
144 // Update existing entry with broker-assigned order_id and any modified fields
145 // (covers both initial new-order ACK and modify-ack with updated price/qty)
146 LOG_INFO("Strategy {}: ACK updating existing entry client_order_id={} order_id={}", name_, client_id, order_id);
147 it->second.order_id = order_id;
148 it->second.status = event.status;
149 it->second.price = event.price;
150 it->second.stop_price = event.stop_price;
151 it->second.qty = event.quantity;
152 it->second.remaining_qty = event.quantity;
153 it->second.time_in_force = event.time_in_force;
154 it->second.post_only = event.post_only;
155 it->second.last_update_time = event.timestamp_ns;
156 order_id_to_client_id_[it->second.order_id] = client_id;
157 }
158 on_ack(event);
159 }
160
161 // Automatically track fills in position tracker and maintain order state
162 void on_order_fill(const OrderFillEvent& event) override {
163 const std::string client_id(event.client_order_id);
164 const std::string order_id(event.order_id);
165 auto cid_it = order_id_to_client_id_.find(order_id);
166 // An order this strategy does not track can still be its own: one that ended
167 // before a restart, whose fills arrive after it - a filled spread order's leg
168 // fills, or a fill replayed from the broker's journal. Its client_order_id says.
169 if (cid_it == order_id_to_client_id_.end() && !owns_order(client_id, order_id)) {
170 // Fills fan out to every strategy; this one was placed by another.
171 LOG_DEBUG("Strategy {}: ignoring fill for foreign order_id={}", name_, order_id);
172 return;
173 }
174
175 LOG_INFO("Strategy {}: on_order_fill order_id={} qty={} price={} side={}",
176 name_, order_id, from_qty_t(event.fill_quantity), from_price_t(event.fill_price),
177 event.side == Side::BUY ? "BUY" : "SELL");
178
179 if (event.scope == FillScope::LEG) {
180 // One leg of a multi-leg order's fill: the position it leaves, in the leg's
181 // instrument. The order itself moved with the fill of the order that preceded it.
182 position_tracker_->on_fill(name_, event);
183 on_leg_fill(event);
184 return;
185 }
186
187 auto order_it = orders_by_client_id_.find(cid_it != order_id_to_client_id_.end() ? cid_it->second : client_id);
188 if (order_it != orders_by_client_id_.end()) {
189 auto& order = order_it->second;
190 const qty_t new_filled = order.filled_qty + event.fill_quantity;
191 if (new_filled > 0) {
192 order.avg_fill_price = to_price_t(
193 (from_price_t(order.avg_fill_price) * from_qty_t(order.filled_qty)
195 / from_qty_t(new_filled));
196 }
197 order.filled_qty = new_filled;
198 order.remaining_qty = event.remaining_quantity;
199 order.last_update_time = event.timestamp_ns;
200 order.status = (order.remaining_qty == 0)
203 } else {
204 LOG_INFO("Strategy {}: fill for client_order_id={}, an order not tracked since a restart - booked",
205 name_, client_id);
206 }
207
209 position_tracker_->on_fill(name_, event);
210 }
211 on_fill(event); // Call derived class hook
212 }
213
214 void on_order_reject(const OrderRejectEvent& event) override {
215 const std::string client_id(event.client_order_id);
216 const std::string order_id(event.order_id);
217 if (!owns_order(client_id, order_id)) {
218 LOG_DEBUG("Strategy {}: ignoring reject for foreign order client_order_id={} order_id={}",
219 name_, client_id, order_id);
220 return;
221 }
222 auto it = orders_by_client_id_.find(client_id);
223 if (it != orders_by_client_id_.end()) {
224 it->second.status = OrderStatus::REJECTED;
225 it->second.last_update_time = event.timestamp_ns;
226 }
227 on_reject(event);
228 }
229
230 void on_order_cancel(const OrderCancelEvent& event) override {
231 const std::string client_id(event.client_order_id);
232 const std::string order_id(event.order_id);
233 if (!owns_order(client_id, order_id)) {
234 LOG_DEBUG("Strategy {}: ignoring cancel for foreign order client_order_id={} order_id={}",
235 name_, client_id, order_id);
236 return;
237 }
238 auto cid_it = order_id_to_client_id_.find(order_id);
239 if (cid_it != order_id_to_client_id_.end()) {
240 auto& order = orders_by_client_id_.at(cid_it->second);
241 order.status = OrderStatus::CANCELLED;
242 order.remaining_qty = event.remaining_quantity;
243 order.last_update_time = event.timestamp_ns;
244 }
245 on_cancel(event);
246 }
247
248protected:
249 friend class WebServer; // Allow WebServer to call submit_order
250 friend class StrategyManager; // Allow StrategyManager to call close_all_positions
251
252 // Lifecycle hooks for derived classes
253 virtual bool on_initialize() { return true; }
254
255 virtual void on_start() {}
256 virtual void on_stop() {}
257 virtual void on_shutdown() {}
258
259 // Configuration hook (called when config is set)
260 virtual void on_config(const nlohmann::json& params) {}
261
262 // Order lifecycle hooks — override these instead of on_order_ack/reject/cancel
263 // Called after order state has been updated in the internal order map
264 virtual void on_ack(const OrderAckEvent& event) {}
265 virtual void on_reject(const OrderRejectEvent& event) {}
266 virtual void on_cancel(const OrderCancelEvent& event) {}
267
268 // Fill hook (called after position tracking and order state update). For an order in a
269 // multi-leg instrument event.scope is FillScope::ORDER: the order moved, at its own price,
270 // and its positions follow through on_leg_fill().
271 virtual void on_fill(const OrderFillEvent& event) {}
272
273 // One leg's share of a multi-leg order's fill (FillScope::LEG), after its position has
274 // been booked: the event's instrument, side, price and quantity are the leg's.
275 virtual void on_leg_fill(const OrderFillEvent& event) {}
276
277 virtual std::string generate_order_id() {
278 return name_ + "_" + std::to_string(get_timestamp_ns());
279 }
280
281 // Broker order events fan out to every strategy, so each one has to recognise its
282 // own. generate_order_id() stamps the strategy name as a "<name>_" prefix, and that
283 // prefix is the only link from a broker event back to the strategy that placed it.
284 // Override this together with generate_order_id() if a strategy uses a different id
285 // scheme, and keep strategy names distinct enough that no name is a "<name>_"-prefix
286 // of another.
287 virtual bool owns_client_order_id(std::string_view client_order_id) const {
288 return client_order_id.size() > name_.size() + 1
289 && client_order_id.starts_with(name_)
290 && client_order_id[name_.size()] == '_';
291 }
292
293 // Ownership test for an incoming broker event. An id we already track settles it
294 // outright; the prefix check is the fallback for orders we placed but have not
295 // recorded yet — one rejected pre-trade in submit_order(), or one replayed from a
296 // broker reconnect snapshot. Events that omit client_order_id (Coinbase does so on
297 // follow-ups) are resolved through the broker order_id recorded at ack time.
298 bool owns_order(const std::string& client_order_id, const std::string& order_id) const {
299 if (!client_order_id.empty()) {
300 return orders_by_client_id_.contains(client_order_id)
301 || owns_client_order_id(client_order_id);
302 }
303 return !order_id.empty() && order_id_to_client_id_.contains(order_id);
304 }
305
306 // Helper methods for order submission
307 // The order's client id, or empty when it is turned down here, before it leaves the engine,
308 // with the reason in `refusal` if given one.
309 std::string submit_order(std::string_view instrument, BrokerId broker_id, Side side, qty_t quantity, OrderType type, price_t price = NULL_PRICE, TimeInForce tif = TimeInForce::FOK, bool post_only = true, std::string* refusal = nullptr) {
310 auto client_order_id = generate_order_id();
311
312 // CME and ICE have no post-only instruction: their order entry would only check the order
313 // against its own view of the book and reject it, so the flag is dropped for them.
314 if (broker_id == BrokerId::CME || broker_id == BrokerId::ICE) {
315 post_only = false;
316 }
317
318 // Track which broker handles each instrument (used by close_all_positions)
319 instrument_broker_map_[std::string(instrument)] = broker_id;
320
321 // Pre-trade risk validation
322 std::string reject_reason;
323 if (!risk_manager_->validate_order(name_, instrument, side, quantity, price, &reject_reason)) {
324 LOG_WARN("Strategy {}: Order {} REJECTED by risk: {}",
325 name_, client_order_id, reject_reason);
326
327 // Publish internal reject event back to strategy
328 OrderRejectEvent reject;
330 reject.set_instrument(instrument);
331 reject.set_client_order_id(client_order_id.data());
332 reject.set_reason(reject_reason.c_str());
333 on_order_reject(reject);
334 if (refusal) {
335 *refusal = std::move(reject_reason);
336 }
337 return {};
338 }
339
340 // Check broker connectivity before enqueuing — avoids orders stuck in PENDING_NEW
341 if (const char* reason = BrokerStatusRegistry::instance().order_unavailable_reason(broker_id)) {
342 OrderRejectEvent broker_reject;
343 broker_reject.timestamp_ns = get_timestamp_ns();
344 broker_reject.set_instrument(instrument);
345 broker_reject.set_client_order_id(client_order_id.data());
346 broker_reject.set_reason(reason);
347 on_order_reject(broker_reject);
348 if (refusal) {
349 *refusal = reason;
350 }
351 return {};
352 }
353
354 EventSerializer::publish<NewOrderEvent>(*order_request_queue_, [=](NewOrderEvent& order) {
355 order.broker_id = broker_id;
356 order.set_instrument(instrument.data());
357 order.set_client_order_id(client_order_id.data());
358 order.side = side;
359 order.order_type = type;
360 order.price = price;
361 order.quantity = quantity;
362 order.time_in_force = tif;
363 order.post_only = post_only;
364 });
365
366 // Insert PENDING_NEW entry so get_order() works immediately after submit
367 Order pending;
368 pending.client_order_id = client_order_id;
369 pending.instrument = std::string(instrument);
370 pending.broker_id = broker_id;
371 pending.side = side;
372 pending.type = type;
373 pending.price = price;
374 pending.qty = quantity;
375 pending.remaining_qty = quantity;
376 pending.time_in_force = tif;
377 pending.post_only = post_only;
378 pending.status = OrderStatus::PENDING_NEW;
379 pending.created_time = get_timestamp_ns();
380 pending.last_update_time = pending.created_time;
381 orders_by_client_id_.emplace(client_order_id, std::move(pending));
382
383 LOG_DEBUG("Strategy {}: Submitted order {} for {} {}@{}",
384 name_, client_order_id, instrument,
385 from_qty_t(quantity), from_price_t(price));
386 return client_order_id;
387 }
388
389 void cancel_order(const std::string& order_id, const std::string& client_order_id) {
390 const auto it = orders_by_client_id_.find(client_order_id);
391 EventSerializer::publish<CancelOrderEvent>(*order_request_queue_, [&](CancelOrderEvent& ev) {
392 // Addressed to the broker that holds the order, with its instrument: every
393 // connector reads the queue, and only that one may act on the cancel.
394 if (it != orders_by_client_id_.end()) {
395 ev.broker_id = it->second.broker_id;
396 ev.set_instrument(it->second.instrument);
397 ev.side = it->second.side;
398 }
399 ev.set_order_id(order_id.c_str());
400 ev.set_client_order_id(client_order_id.c_str());
401 });
402 LOG_DEBUG("Strategy {}: Cancelled order {}", name_, order_id);
403 }
404
405 // Cancel all open orders, including one with a modify in flight (is_open() leaves it out).
406 // One still waiting for its ack has no broker order id yet and is skipped.
408 for (const auto& [cid, order] : orders_by_client_id_) {
409 if ((order.is_open() || order.status == OrderStatus::PENDING_MODIFY) && !order.order_id.empty()) {
410 cancel_order(order.order_id, order.client_order_id);
411 }
412 }
413 }
414
415 void modify_order(Order& order, price_t new_price, qty_t new_quantity, price_t new_stop_price = NULL_PRICE) {
416 EventSerializer::publish<ModifyOrderEvent>(*order_request_queue_, [&order, new_price, new_quantity, new_stop_price](ModifyOrderEvent& ev) {
417 ev.broker_id = order.broker_id;
418 ev.set_order_id(order.order_id);
420 ev.set_instrument(order.instrument);
421 ev.side = order.side;
422 ev.new_price = new_price;
423 ev.new_quantity = new_quantity;
424 ev.new_stop_price = new_stop_price;
425 ev.new_post_only = order.post_only;
427 });
428
431
432 LOG_INFO("Strategy {}: Modify order {} new_price={} new_qty={}",
433 name_, order.order_id, from_price_t(new_price), from_qty_t(new_quantity));
434 }
435
436 void modify_order(const std::string& client_order_id,
437 price_t new_price,
438 qty_t new_quantity,
439 price_t new_stop_price = NULL_PRICE) {
440 auto it = orders_by_client_id_.find(client_order_id);
441 if (it != orders_by_client_id_.end()) [[likely]] {
442 modify_order(it->second, new_price, new_quantity, new_stop_price);
443 }
444 }
445
446 // Emergency close: cancel open orders then submit aggressive limit orders to flatten all positions.
447 // Bypasses risk manager validation — this is a safety operation triggered by risk breach.
448 // Limit prices are set far into the market (10% away from mark price) to ensure execution.
451
452 const auto positions = position_tracker_->get_all_positions(name_);
453 for (const auto& [instrument, pos] : positions) {
454 if (pos.is_flat()) continue;
455
456 auto broker_it = instrument_broker_map_.find(instrument);
457 if (broker_it == instrument_broker_map_.end()) {
458 LOG_WARN("Strategy {}: Cannot close position for {} — no broker mapping known",
459 name_, instrument);
460 continue;
461 }
462
463 // Determine closing side and quantity
464 const Side close_side = pos.is_long() ? Side::SELL : Side::BUY;
465 const qty_t close_qty = pos.is_long() ? pos.quantity : -pos.quantity;
466
467 // Use mark price if available, otherwise fall back to avg entry price
468 price_t ref_price = pos.avg_price;
469 if (pnl_calculator_->has_mark_price(instrument)) {
470 ref_price = pnl_calculator_->get_mark_price(instrument);
471 }
472
473 // Place limit price 10% into the market to guarantee execution
474 const double ref_double = from_price_t(ref_price);
475 const price_t close_price = pos.is_long()
476 ? to_price_t(ref_double * 0.90) // Sell well below mark — crosses any bid
477 : to_price_t(ref_double * 1.10); // Buy well above mark — crosses any ask
478
479 const BrokerId broker_id = broker_it->second;
480 const auto client_order_id = generate_order_id();
481
482 LOG_WARN("Strategy {}: Emergency close {} qty={} limit@{} (ref={})",
483 name_, instrument, from_qty_t(close_qty),
484 from_price_t(close_price), from_price_t(ref_price));
485
486 // Submit directly — skip risk manager for safety closure
487 EventSerializer::publish<NewOrderEvent>(*order_request_queue_,
488 [&](NewOrderEvent& order) {
489 order.broker_id = broker_id;
490 order.set_instrument(instrument.c_str());
491 order.set_client_order_id(client_order_id.c_str());
492 order.side = close_side;
494 order.price = close_price;
495 order.quantity = close_qty;
497 order.post_only = false;
498 });
499
500 // Track as PENDING_NEW so get_order() works immediately
501 Order pending;
502 pending.client_order_id = client_order_id;
503 pending.instrument = instrument;
504 pending.broker_id = broker_id;
505 pending.side = close_side;
506 pending.type = OrderType::LIMIT;
507 pending.price = close_price;
508 pending.qty = close_qty;
509 pending.remaining_qty = close_qty;
510 pending.time_in_force = TimeInForce::IOC;
511 pending.post_only = false;
512 pending.status = OrderStatus::PENDING_NEW;
513 pending.created_time = get_timestamp_ns();
514 pending.last_update_time = pending.created_time;
515 orders_by_client_id_.emplace(client_order_id, std::move(pending));
516 }
517 }
518
519 // Returns all orders (open and done) for this strategy
520 const std::unordered_map<std::string, Order>& get_orders() const {
521 return orders_by_client_id_;
522 }
523
524 // Lookup by client_order_id (available immediately after submit_order)
525 const Order* get_order(const std::string& client_order_id) const {
526 auto it = orders_by_client_id_.find(client_order_id);
527 return it != orders_by_client_id_.end() ? &it->second : nullptr;
528 }
529
530 // Where one of this strategy's working orders stands in the queue at its price, read
531 // from the market-by-order book on_order_book_l3() hands over. Nothing until the order
532 // is acknowledged and shows on the feed, and nothing once it is filled or cancelled.
533 std::optional<QueuePosition> get_queue_position(const slick::orderbook::OrderBookL3& book,
534 const std::string& client_order_id) const {
535 const Order* order = get_order(client_order_id);
536 return order ? queue_position(book, std::string_view(order->order_id)) : std::nullopt;
537 }
538
539 // Lookup by broker-assigned order_id (available after on_order_ack)
540 const Order* get_order_by_id(const std::string& order_id) const {
541 auto it = order_id_to_client_id_.find(order_id);
542 if (it == order_id_to_client_id_.end()) return nullptr;
543 auto oit = orders_by_client_id_.find(it->second);
544 return oit != orders_by_client_id_.end() ? &oit->second : nullptr;
545 }
546
547 // Returns pointers to all open orders
548 std::vector<const Order*> get_open_orders() const {
549 std::vector<const Order*> result;
550 for (const auto& [cid, order] : orders_by_client_id_) {
551 if (order.is_open()) result.push_back(&order);
552 }
553 return result;
554 }
555
556 // Remove done (filled/cancelled/rejected) orders from the map
558 for (auto it = orders_by_client_id_.begin(); it != orders_by_client_id_.end(); ) {
559 if (it->second.is_done()) {
560 order_id_to_client_id_.erase(it->second.order_id);
561 it = orders_by_client_id_.erase(it);
562 } else {
563 ++it;
564 }
565 }
566 }
567
568 bool is_running() const { return running_.load(std::memory_order_acquire); }
569
570 const std::string& get_name() const { return name_; }
571
572 // Configuration access
573 template<typename T>
574 T get_parameter(const std::string& key, const T& default_value) const {
575 if (config_params_.contains(key)) {
576 try {
577 return config_params_[key].get<T>();
578 } catch (const std::exception&) {
579 return default_value;
580 }
581 }
582 return default_value;
583 }
584
585 const nlohmann::json& get_config() const { return config_params_; }
586
587 // Position & PnL access
588 PositionTracker* get_position_tracker() { return position_tracker_; }
589 PnLCalculator* get_pnl_calculator() { return pnl_calculator_; }
590 RiskManager* get_risk_manager() { return risk_manager_; }
591
592private:
593 std::string name_;
594 std::string version_;
595 std::atomic_bool running_{false};
596 slick::queue<uint8_t>* order_request_queue_{nullptr};
597
598 // Configuration parameters
599 nlohmann::json config_params_;
600
601 // Position & PnL tracking (non-owning pointers, set by TradingEngine)
602 PositionTracker* position_tracker_{nullptr};
603 PnLCalculator* pnl_calculator_{nullptr};
604 RiskManager* risk_manager_{nullptr};
605
606 // Order tracking — keyed by client_order_id; reverse map for broker order_id lookup
607 std::unordered_map<std::string, Order> orders_by_client_id_;
608 std::unordered_map<std::string, std::string> order_id_to_client_id_;
609
610 // Maps instrument -> last used BrokerId (populated in submit_order, used by close_all_positions)
611 std::unordered_map<std::string, BrokerId> instrument_broker_map_;
612};
613
614} // namespace slick::trader
static BrokerStatusRegistry & instance()
static bool publish(slick::SlickQueue< uint8_t > &queue, uint32_t size, populate_event_fn &&populate_event_func)
void modify_order(Order &order, price_t new_price, qty_t new_quantity, price_t new_stop_price=NULL_PRICE)
bool initialize(const json &config, PositionTracker &position_tracker, PnLCalculator &pnl_calculator, RiskManager &risk_manager, slick::queue< uint8_t > &order_request_queue) override
const char * name() const override
virtual bool owns_client_order_id(std::string_view client_order_id) const
void on_order_fill(const OrderFillEvent &event) override
void on_order_ack(const OrderAckEvent &event) override
const char * version() const override
void on_order_reject(const OrderRejectEvent &event) override
const nlohmann::json & get_config() const
const Order * get_order_by_id(const std::string &order_id) const
virtual void on_config(const nlohmann::json &params)
PositionTracker * get_position_tracker()
void on_quote(const QuoteEvent &event) override
void on_trade(const TradeEvent &event) override
const std::string & get_name() const
void on_order_book(std::string_view, const slick::orderbook::OrderBookL2 &) override
const Order * get_order(const std::string &client_order_id) const
StrategyBase(const std::string &name, const std::string &version)
PnLCalculator * get_pnl_calculator()
std::vector< SubscriptionRequest > get_subscriptions() const override
T get_parameter(const std::string &key, const T &default_value) const
void cancel_order(const std::string &order_id, const std::string &client_order_id)
virtual void on_leg_fill(const OrderFillEvent &event)
void on_order_cancel(const OrderCancelEvent &event) override
virtual std::string generate_order_id()
void modify_order(const std::string &client_order_id, price_t new_price, qty_t new_quantity, price_t new_stop_price=NULL_PRICE)
const std::unordered_map< std::string, Order > & get_orders() const
std::string submit_order(std::string_view instrument, BrokerId broker_id, Side side, qty_t quantity, OrderType type, price_t price=NULL_PRICE, TimeInForce tif=TimeInForce::FOK, bool post_only=true, std::string *refusal=nullptr)
bool owns_order(const std::string &client_order_id, const std::string &order_id) const
std::optional< QueuePosition > get_queue_position(const slick::orderbook::OrderBookL3 &book, const std::string &client_order_id) const
virtual void on_ack(const OrderAckEvent &event)
virtual ~StrategyBase() override=default
void set_config(const nlohmann::json &params)
virtual void on_cancel(const OrderCancelEvent &event)
virtual void on_fill(const OrderFillEvent &event)
virtual void on_reject(const OrderRejectEvent &event)
std::vector< const Order * > get_open_orders() const
double from_price_t(price_t value)
Definition types.hpp:36
price_t to_price_t(double value)
Definition types.hpp:24
uint64_t get_timestamp_ns()
Definition time.hpp:7
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16
std::optional< QueuePosition > queue_position(const slick::orderbook::OrderBookL3 &book, slick::orderbook::OrderId order_id) noexcept
nlohmann::json json
double from_qty_t(qty_t value)
Definition types.hpp:40
char instrument[MAX_INSTRUMENT_LENGTH]
Definition event.hpp:61
uint64_t timestamp_ns
Definition event.hpp:58
void set_instrument(std::string_view instr)
Definition event.hpp:70
BrokerId broker_id
Definition event.hpp:60
void set_client_order_id(std::string_view id)
char order_id[MAX_ORDER_ID_LENGTH]
void set_order_id(std::string_view id)
void set_reason(std::string_view r)
std::string client_order_id
Definition order.hpp:12
uint64_t created_time
Definition order.hpp:25
uint64_t last_update_time
Definition order.hpp:26
std::string order_id
Definition order.hpp:11
TimeInForce time_in_force
Definition order.hpp:17
OrderStatus status
Definition order.hpp:24
std::string instrument
Definition order.hpp:13
BrokerId broker_id
Definition order.hpp:14