14#include <unordered_set>
28 static constexpr const char*
ID =
"MANUAL";
43 std::vector<SubscriptionRequest> subs;
44 for (
size_t b = 0; b < held_.size(); ++b) {
46 std::vector<std::string> instruments;
47 for (
const auto& [instrument, feeds] : held_[b]) {
48 if (feeds & (1u << i)) {
49 instruments.push_back(instrument);
52 if (!instruments.empty()) {
66 const uint32_t count = count_.load(std::memory_order_acquire);
67 for (uint32_t i = from; i < count; ++i) {
69 if constexpr (std::is_invocable_v<F&, BrokerId, std::string_view, price_t, qty_t>) {
70 f(slot.broker, std::string_view(slot.instrument), slot.tick_size, slot.min_qty);
72 f(slot.broker, std::string_view(slot.instrument), slot.tick_size);
78 uint32_t
listed_count() const noexcept {
return count_.load(std::memory_order_acquire); }
84 const auto b =
static_cast<size_t>(broker);
88 listed_[b].contains(instrument)) {
91 const uint32_t n = count_.load(std::memory_order_relaxed);
96 LOG_WARN(
"Strategy {}: listing the maximum of {} instruments, so {} and any it is offered after "
102 listed_[b].emplace(instrument);
108 slot.broker = broker;
109 slot.tick_size = tick_size;
110 slot.min_qty = min_qty;
111 const size_t len = std::min(instrument.size(),
sizeof(slot.instrument) - 1);
112 std::memcpy(slot.instrument, instrument.data(), len);
113 slot.instrument[len] =
'\0';
114 count_.store(n + 1, std::memory_order_release);
119 bool lists(std::string_view instrument)
const {
120 return all_ || symbols_.contains(instrument) ||
121 std::ranges::any_of(prefixes_, [&](
const std::string& prefix) {
return instrument.starts_with(prefix); });
128 const auto b =
static_cast<size_t>(broker);
129 return feeds != 0 && b < held_.size() && (subscribed || listed_[b].contains(instrument)) &&
130 held_[b].try_emplace(std::string(instrument), feeds).second;
133 const auto b =
static_cast<size_t>(broker);
134 if (b >= held_.size()) {
137 const auto it = held_[b].find(instrument);
138 if (it == held_[b].end()) {
141 const uint8_t feeds = it->second;
146 for (
auto& held : held_) {
161 const auto it = params.find(
"instruments");
162 all_ = it == params.end() || !it->is_array();
164 LOG_INFO(
"Strategy {}: lists every instrument brokers define",
ID);
167 for (
const auto& entry : *it) {
168 if (!entry.is_string()) {
171 auto symbol = entry.get<std::string>();
172 if (symbol.ends_with(
'*')) {
174 prefixes_.push_back(std::move(symbol));
176 symbols_.insert(std::move(symbol));
179 LOG_INFO(
"Strategy {}: lists {} symbol(s) and {} prefix(es) as brokers define them",
ID, symbols_.size(),
196 std::atomic<uint32_t> count_{0};
198 std::array<StringSet, BROKER_COUNT> listed_;
199 std::array<StringMap<uint8_t>, BROKER_COUNT> held_;
201 std::vector<std::string> prefixes_;
void on_config(const nlohmann::json ¶ms) override
static constexpr const char * ID
bool hold(BrokerId broker, std::string_view instrument, bool subscribed=false, uint8_t feeds=LADDER_ALL)
static constexpr std::array< SubscriptionType, 2 > SUBSCRIPTIONS
uint32_t listed_count() const noexcept
std::vector< SubscriptionRequest > get_subscriptions() const override
bool lists(std::string_view instrument) const
bool add_listing(BrokerId broker, std::string_view instrument, price_t tick_size=0, qty_t min_qty=0)
bool on_initialize() override
uint8_t release(BrokerId broker, std::string_view instrument)
uint32_t for_each_listed(uint32_t from, F &&f) const
static constexpr uint32_t CHUNK_SIZE
static constexpr uint32_t MAX_INSTRUMENTS
StrategyBase(const std::string &name, const std::string &version)
std::unordered_set< std::string, StringHash, std::equal_to<> > StringSet
constexpr size_t MAX_INSTRUMENT_LENGTH