Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
md_request_queues.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <slick/queue.hpp>
6#include <array>
7#include <cstdint>
8#include <memory>
9#include <stdexcept>
10#include <string>
11
12namespace slick::trader {
13
14// A market data request queue per broker. StrategyManager is the one writer of each and the
15// broker's market data connector its one reader, so a connector that stops reading holds up its
16// own requests alone, and the writer never laps one it waits on. Made at startup, before
17// StrategyManager, and not changed after.
19public:
20 static constexpr size_t BROKER_COUNT = static_cast<size_t>(BrokerId::_BROKER_ID_COUNT_);
21
22 // Each queue `size` bytes. With `shm_name`, each in shared memory named "<shm_name>_<BROKER>".
23 explicit MdRequestQueues(uint32_t size, std::string shm_name = {})
24 : size_(size), shm_name_(std::move(shm_name)) {}
25
26 // `broker`'s queue, made on the first call.
27 slick::queue<uint8_t>& get(BrokerId broker) {
28 const auto i = static_cast<size_t>(broker);
29 if (broker == BrokerId::UNKNOWN || i >= BROKER_COUNT) {
30 throw std::invalid_argument("no market data request queue for broker " + std::to_string(i));
31 }
32 auto& queue = queues_[i];
33 if (!queue) {
34 const std::string name = shm_name_.empty() ? std::string() : shm_name_ + "_" + to_string(broker);
35 queue = std::make_unique<slick::queue<uint8_t>>(size_, name.empty() ? nullptr : name.c_str());
36 // Nothing is read off a new queue yet. A cursor a connector of another queue reported
37 // would let StrategyManager write over requests this one's has not read.
39 }
40 return *queue;
41 }
42
43 // `broker`'s queue, or nullptr when none was made, as for a broker this engine did not build.
44 slick::queue<uint8_t>* find(BrokerId broker) const noexcept {
45 const auto i = static_cast<size_t>(broker);
46 return i < BROKER_COUNT ? queues_[i].get() : nullptr;
47 }
48
49private:
50 uint32_t size_;
51 std::string shm_name_;
52 std::array<std::unique_ptr<slick::queue<uint8_t>>, BROKER_COUNT> queues_;
53};
54
55} // namespace slick::trader
void set_md_request_cursor(BrokerId id, uint64_t cursor) noexcept
static BrokerStatusRegistry & instance()
slick::queue< uint8_t > & get(BrokerId broker)
static constexpr size_t BROKER_COUNT
slick::queue< uint8_t > * find(BrokerId broker) const noexcept
MdRequestQueues(uint32_t size, std::string shm_name={})
const char * to_string(BrokerId bid)
Definition event.hpp:78
static constexpr uint64_t NO_MD_REQUEST_CURSOR