Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
order_event_writer.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <slick/logger.hpp>
11#include <slick/queue.hpp>
12#include <slick/stream_buffer_multiplexer.hpp>
13#include <nlohmann/json.hpp>
14#include <cstdint>
15#include <memory>
16#include <string>
17
18namespace slick::trader {
19
20// An order connector's output: its producer buffer in the event multiplexer, and the
21// order events written into it. Single-threaded: the connector's worker is the one writer.
23public:
24 explicit OrderEventWriter(BrokerId broker) noexcept : broker_(broker) {}
25
26 // Adds the producer, sized by `queue_size` / `control_size` and named by `shm_name`
27 // in `config` when it sets them.
28 void open(slick::stream_buffer_multiplexer& mux, const nlohmann::json& config) {
29 shm_name_ = config.value("shm_name", std::string());
30 buffer_ = mux.add_producer(order_producer_id(broker_), config.value("queue_size", 1u << 24),
31 config.value("control_size", 1u << 18),
32 shm_name_.empty() ? nullptr : shm_name_.c_str());
33 }
34 [[nodiscard]] bool is_open() const noexcept { return buffer_ != nullptr; }
35
36 // `fees` is the fallback for a fill that carries no fee of its own.
37 void register_connector(slick::queue<uint8_t>& control_queue, const FeeStructure& fees) const {
39 [&](OrderConnectorRegisterEvent& event) {
40 event.broker_id = broker_;
41 event.producer_id = order_producer_id(broker_);
42 event.fee_structure = fees;
43 set_shm_name(event, shm_name_);
44 });
45 }
46
47 template <typename E>
48 void publish(const E& event) {
49 EventSerializer::publish<E>(*buffer_, [&](E& out) { out = event; });
50 }
51
52 // Hands the strategies every fill in `journal` again, before any session connects.
53 // The strategy manager drops those they had before the restart.
54 void replay(const FillJournal& journal, std::string_view who) {
55 if (journal.size() == 0) {
56 return;
57 }
58 auto fills = journal.fills();
59 LOG_INFO("{}: replaying {} journaled fill(s); those the strategies had before the restart are dropped", who,
60 fills.size());
61 for (auto& fill : fills) {
62 fill.replayed = true;
63 publish(fill);
64 }
65 }
66
67 [[nodiscard]] BrokerId broker() const noexcept { return broker_; }
68
69private:
70 BrokerId broker_;
71 std::string shm_name_;
72 std::shared_ptr<slick::stream_buffer_multiplexer::producer_buffer> buffer_;
73};
74
75// Reads one request off the order request queue and, if it is for `broker`, hands it to
76// `on_new(const NewOrderEvent&)`, `on_modify(const ModifyOrderEvent&)` or
77// `on_cancel(const CancelOrderEvent&)`. Every connector reads every request on the shared
78// queue, so each request type is filtered by broker here. Returns whether a request was
79// read, for whatever broker.
80template <typename New, typename Modify, typename Cancel>
81bool dispatch_order_request(slick::queue<uint8_t>& queue, uint64_t& cursor, BrokerId broker, New&& on_new,
82 Modify&& on_modify, Cancel&& on_cancel) {
83 auto [data, size] = queue.read(cursor);
84 if (!data) {
85 return false;
86 }
87 const auto* event = reinterpret_cast<const Event*>(data);
88 if (!is_request_for(*event, broker)) {
89 return true; // another broker's
90 }
91 switch (event->type) {
93 on_new(*reinterpret_cast<const NewOrderEvent*>(data));
94 break;
96 on_modify(*reinterpret_cast<const ModifyOrderEvent*>(data));
97 break;
99 on_cancel(*reinterpret_cast<const CancelOrderEvent*>(data));
100 break;
101 default:
102 break;
103 }
104 return true;
105}
106
107} // namespace slick::trader
static bool publish(slick::SlickQueue< uint8_t > &queue, uint32_t size, populate_event_fn &&populate_event_func)
std::vector< OrderFillEvent > fills() const
size_t size() const noexcept
BrokerId broker() const noexcept
void register_connector(slick::queue< uint8_t > &control_queue, const FeeStructure &fees) const
void open(slick::stream_buffer_multiplexer &mux, const nlohmann::json &config)
OrderEventWriter(BrokerId broker) noexcept
void replay(const FillJournal &journal, std::string_view who)
bool dispatch_order_request(slick::queue< uint8_t > &queue, uint64_t &cursor, BrokerId broker, New &&on_new, Modify &&on_modify, Cancel &&on_cancel)
bool is_request_for(const Event &request, BrokerId broker) noexcept
constexpr uint32_t order_producer_id(BrokerId broker) noexcept
void set_shm_name(E &event, const std::string &name)