Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
position_store.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <cstddef>
7#include <cstdint>
8#include <functional>
9#include <span>
10#include <string>
11#include <string_view>
12
13namespace slick::trader {
14
15// One open lot of a position: what FIFO accounting closes first.
21
22// Positions kept across restarts.
23//
24// Every position - quantity, average price, realized PnL and fees, and the open lots FIFO
25// accounting still has to close - lives in one fixed slot of a memory-mapped file
26// (MappedFile), so recording a fill is a copy into memory and never waits for the disk. A
27// position that was recorded is in the file even if the process is killed the next instant.
28//
29// A slot holds two copies of its position and a byte saying which is current. An update
30// writes the other copy in full, then flips the byte: a process that dies mid-update leaves
31// the position as it was before the fill, never half of each.
32//
33// It also keeps how far into each broker's fill journal the strategies have got (see
34// OrderFillEvent::journal_seq), which is what lets a broker replay its journal after a
35// crash without a fill being booked twice. A position records the journal number of the
36// fill it was last booked from in the same copy as the position itself, so a fill booked
37// is never one the store can think still to come.
38//
39// Single-threaded: owned by PositionTracker's writer thread.
41public:
42 // Open lots kept per position. A position built from more keeps the oldest
43 // MAX_LOTS - 1 as they are and the rest as one lot at their average price.
44 static constexpr std::size_t MAX_LOTS = 32;
45 // Longest strategy id or instrument name a slot can be keyed by.
46 static constexpr std::size_t MAX_KEY_LENGTH = 63;
47 static constexpr std::size_t DEFAULT_CAPACITY = 1024;
48 static constexpr uint32_t NO_SLOT = UINT32_MAX;
49 // Brokers whose journal positions the file has room for.
50 static constexpr std::size_t MAX_BROKERS = 16;
51 static_assert(static_cast<std::size_t>(BrokerId::_BROKER_ID_COUNT_) <= MAX_BROKERS);
52
53 // Opens the store at `path`, creating it with room for `capacity` positions. An
54 // existing store keeps what it holds, and grows if `capacity` is larger than it was.
55 // Throws std::runtime_error for a file that is not a position store, or one written
56 // with a different layout.
57 explicit PositionStore(const std::string& path, std::size_t capacity = DEFAULT_CAPACITY);
58
59 using Visitor = std::function<void(uint32_t slot, std::string_view strategy_id, const Position& position,
60 std::span<const PositionLot> lots)>;
61 // Every position held, with the slot it is in.
62 void for_each(const Visitor& visit) const;
63
64 // Records the position as it stands. `slot` is where it was recorded before - what the
65 // last call for it returned, or what for_each() reported - or NO_SLOT for one that is
66 // new. Returns its slot, or NO_SLOT when it could not be recorded: the store is full,
67 // or a key is too long. `journal_seq` is the journal number of the fill that made the
68 // position what it is, from `broker`'s journal; 0 for a fill that has none, which
69 // leaves the position's last one recorded.
70 uint32_t save(uint32_t slot, std::string_view strategy_id, const Position& position,
71 std::span<const PositionLot> lots, BrokerId broker = BrokerId::UNKNOWN,
72 uint64_t journal_seq = 0);
73
74 // How far into `broker`'s fill journal the strategies have got: the highest journal
75 // number recorded by mark_journaled(), or by save() for a position booked from it.
76 uint64_t journaled(BrokerId broker) const noexcept;
77 // The strategies have had every fill of `broker`'s journal up to `journal_seq`.
78 void mark_journaled(BrokerId broker, uint64_t journal_seq) noexcept;
79
80 void erase_strategy(std::string_view strategy_id);
81 void clear();
82
83 std::size_t size() const noexcept { return size_; }
84 std::size_t capacity() const noexcept { return capacity_; }
85 const std::string& path() const noexcept { return path_; }
86
87 // Asks the operating system to write the file out now. Blocks on the disk.
88 void flush() noexcept { file_.flush(); }
89
90private:
91 struct Slot;
92 Slot& slot_at(std::size_t index) noexcept;
93 const Slot& slot_at(std::size_t index) const noexcept;
94
95 struct FileHeader;
96 FileHeader& header() noexcept;
97
98 std::string path_;
99 MappedFile file_;
100 uint64_t journaled_[MAX_BROKERS]{};
101 std::size_t capacity_{0};
102 std::size_t size_{0};
103 std::size_t next_free_{0}; // no empty slot before this one
104 bool full_logged_{false};
105 bool lots_logged_{false};
106};
107
108} // namespace slick::trader
static constexpr uint32_t NO_SLOT
void for_each(const Visitor &visit) const
std::function< void(uint32_t slot, std::string_view strategy_id, const Position &position, std::span< const PositionLot > lots)> Visitor
PositionStore(const std::string &path, std::size_t capacity=DEFAULT_CAPACITY)
void erase_strategy(std::string_view strategy_id)
void mark_journaled(BrokerId broker, uint64_t journal_seq) noexcept
static constexpr std::size_t MAX_BROKERS
static constexpr std::size_t MAX_LOTS
std::size_t size() const noexcept
std::size_t capacity() const noexcept
uint32_t save(uint32_t slot, std::string_view strategy_id, const Position &position, std::span< const PositionLot > lots, BrokerId broker=BrokerId::UNKNOWN, uint64_t journal_seq=0)
static constexpr std::size_t MAX_KEY_LENGTH
static constexpr std::size_t DEFAULT_CAPACITY
const std::string & path() const noexcept
uint64_t journaled(BrokerId broker) const noexcept
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16