Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
fill_history.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4#include <cstddef>
5#include <cstdint>
6#include <string>
7#include <vector>
8
9namespace slick::trader {
10
11// The last `capacity` fills the dashboard was sent, for a Fills window opened later. Each is kept
12// as the message it went out as, serialized once, with a `seq` that grows by one per fill: a page
13// that read the history drops what it gets again over the WebSocket by it.
14// Single-threaded: the WebServer's event loop owns it.
16public:
17 explicit FillHistory(size_t capacity) : ring_(capacity) {}
18
19 // Stamps `fill` with the next seq, keeps it, and returns it as text to send. The oldest goes
20 // once the ring is full.
21 const std::string& push(nlohmann::json& fill) {
22 fill["seq"] = ++seq_;
23 std::string& slot = ring_[next_];
24 slot = fill.dump();
25 next_ = (next_ + 1) % ring_.size();
26 size_ += size_ < ring_.size();
27 return slot;
28 }
29
30 // The fills kept as a JSON array, oldest first, into `out`.
31 void write_json(std::string& out) const {
32 out.assign("[");
33 const size_t first = (next_ + ring_.size() - size_) % ring_.size();
34 for (size_t i = 0; i < size_; ++i) {
35 if (i != 0) {
36 out += ',';
37 }
38 out += ring_[(first + i) % ring_.size()];
39 }
40 out += ']';
41 }
42
43 size_t size() const noexcept { return size_; }
44 uint64_t last_seq() const noexcept { return seq_; }
45
46private:
47 std::vector<std::string> ring_;
48 size_t next_ = 0;
49 size_t size_ = 0;
50 uint64_t seq_ = 0;
51};
52
53} // namespace slick::trader
uint64_t last_seq() const noexcept
FillHistory(size_t capacity)
const std::string & push(nlohmann::json &fill)
void write_json(std::string &out) const
size_t size() const noexcept