Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
instrument_directory.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstddef>
5#include <functional>
6#include <string>
7#include <string_view>
8#include <type_traits>
9#include <unordered_map>
10#include <utility>
11
12namespace slick::trader {
13
14// The instruments an order worker knows, by symbol and by the exchange's numeric id
15// (`IdMember`, a pointer to the instrument's id field). Looked up by symbol without
16// building a string. Entries never move once added, so a pointer to one stays good.
17//
18// Single-threaded: owned by the order worker.
19template <typename Instrument, auto IdMember>
21public:
22 using Id = std::remove_cvref_t<decltype(std::declval<Instrument&>().*IdMember)>;
23
24 // Adds `instrument`, or replaces the one with the same id.
25 const Instrument& add(Instrument instrument) {
26 const Id id = instrument.*IdMember;
27 by_symbol_.insert_or_assign(instrument.symbol, id);
28 auto& slot = by_id_[id];
29 slot = std::move(instrument);
30 return slot;
31 }
32 [[nodiscard]] const Instrument* by_symbol(std::string_view symbol) const {
33 auto it = by_symbol_.find(symbol);
34 return it == by_symbol_.end() ? nullptr : by_id(it->second);
35 }
36 [[nodiscard]] const Instrument* by_id(Id id) const {
37 auto it = by_id_.find(id);
38 return it == by_id_.end() ? nullptr : &it->second;
39 }
40 [[nodiscard]] size_t size() const noexcept { return by_id_.size(); }
41
42 template <typename F>
43 void for_each(F&& f) const {
44 for (const auto& [id, instrument] : by_id_) {
45 f(instrument);
46 }
47 }
48
49private:
50 std::unordered_map<Id, Instrument> by_id_; // node-based: addresses stay valid
51 std::unordered_map<std::string, Id, StringHash, std::equal_to<>> by_symbol_;
52};
53
54} // namespace slick::trader
const Instrument * by_symbol(std::string_view symbol) const
const Instrument & add(Instrument instrument)
std::remove_cvref_t< decltype(std::declval< Instrument & >().*IdMember)> Id
const Instrument * by_id(Id id) const