algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
data.hpp
1#pragma once
2
3#include <algorithm>
4#include <chrono>
5#include <cstddef>
6#include <optional>
7#include <utility>
8#include <vector>
9
10namespace algotradelib {
11namespace mock {
12
28template <typename T>
29class DataFeed {
30public:
32 using Entry = std::pair<std::chrono::system_clock::time_point, T>;
33
38 explicit DataFeed(std::vector<Entry> entries) : entries_(std::move(entries)) {
39 std::stable_sort(entries_.begin(), entries_.end(), [](const Entry& a, const Entry& b) {
40 return a.first < b.first;
41 });
42 }
43
49 std::optional<std::vector<T>> operator()(std::chrono::system_clock::time_point now) {
50 std::vector<T> out;
51 while (next_ < entries_.size() && !(now < entries_[next_].first)) {
52 out.push_back(entries_[next_].second);
53 ++next_;
54 }
55 return out;
56 }
57
62 std::size_t remaining() const noexcept {
63 return entries_.size() - next_;
64 }
65
66private:
67 std::vector<Entry> entries_;
68 std::size_t next_{0};
69};
70
71} // namespace mock
72} // namespace algotradelib
DataFeed(std::vector< Entry > entries)
Builds a feed over records in any order; records published at one instant keep their order.
Definition data.hpp:38
std::pair< std::chrono::system_clock::time_point, T > Entry
A record and when it was published.
Definition data.hpp:32
std::optional< std::vector< T > > operator()(std::chrono::system_clock::time_point now)
Returns the records published at or before now and not returned before, in time order.
Definition data.hpp:49
std::size_t remaining() const noexcept
Returns how many records are still to be released.
Definition data.hpp:62
The mock broker: an in-memory market over bars the caller gives, bound like any broker.
Definition client.hpp:11
The library: broker-neutral types (core), and the modules below.