algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
clock.hpp
1#pragma once
2
3#include <chrono>
4#include <thread>
5
6namespace algotradelib {
7namespace runner {
8
21public:
26 std::chrono::system_clock::time_point now() const noexcept {
27 return std::chrono::system_clock::now();
28 }
29
34 std::chrono::steady_clock::time_point monotonic_now() const noexcept {
35 return std::chrono::steady_clock::now();
36 }
37
42 void wait_for(std::chrono::nanoseconds duration) const {
43 if (duration <= std::chrono::nanoseconds::zero()) {
44 return;
45 }
46 std::this_thread::sleep_for(duration);
47 }
48};
49
59template <typename Clock>
61public:
66 explicit ClockReference(Clock& clock) noexcept : clock_(&clock) {}
67
72 std::chrono::system_clock::time_point now() const { return clock_->now(); }
73
78 std::chrono::steady_clock::time_point monotonic_now() const { return clock_->monotonic_now(); }
79
84 void wait_for(std::chrono::nanoseconds duration) { clock_->wait_for(duration); }
85
86private:
87 Clock* clock_;
88};
89
97public:
102 explicit ManualClock(std::chrono::system_clock::time_point start = {}) : now_(start) {}
103
108 std::chrono::system_clock::time_point now() const noexcept { return now_; }
109
114 std::chrono::steady_clock::time_point monotonic_now() const noexcept { return monotonic_; }
115
120 void wait_for(std::chrono::nanoseconds duration) noexcept {
121 ++waits_;
122 if (duration > std::chrono::nanoseconds::zero()) {
123 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
124 monotonic_ += std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
125 waited_ += duration;
126 }
127 }
128
133 void advance(std::chrono::nanoseconds duration) noexcept {
134 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
135 if (duration > std::chrono::nanoseconds::zero()) {
136 monotonic_ += std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
137 }
138 }
139
144 void jump(std::chrono::nanoseconds duration) noexcept {
145 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
146 }
147
152 std::size_t waits() const noexcept { return waits_; }
153
158 std::chrono::nanoseconds waited() const noexcept { return waited_; }
159
160private:
161 std::chrono::system_clock::time_point now_;
162 std::chrono::steady_clock::time_point monotonic_{};
163 std::size_t waits_{0};
164 std::chrono::nanoseconds waited_{std::chrono::nanoseconds::zero()};
165};
166
167} // namespace runner
168} // namespace algotradelib
ClockReference(Clock &clock) noexcept
Refers to a clock.
Definition clock.hpp:66
void wait_for(std::chrono::nanoseconds duration)
Waits on the referenced clock.
Definition clock.hpp:84
std::chrono::steady_clock::time_point monotonic_now() const
Returns the referenced clock's monotonic reading.
Definition clock.hpp:78
std::chrono::system_clock::time_point now() const
Returns the referenced clock's wall-clock time.
Definition clock.hpp:72
std::chrono::system_clock::time_point now() const noexcept
Returns the current time.
Definition clock.hpp:108
ManualClock(std::chrono::system_clock::time_point start={})
Constructs a clock.
Definition clock.hpp:102
void advance(std::chrono::nanoseconds duration) noexcept
Advances the clock without counting a wait: time that passed, as inside a slow call.
Definition clock.hpp:133
std::chrono::steady_clock::time_point monotonic_now() const noexcept
Returns the monotonic reading: how far wait_for and advance have moved the clock.
Definition clock.hpp:114
std::chrono::nanoseconds waited() const noexcept
Returns the total positive duration passed to wait_for.
Definition clock.hpp:158
std::size_t waits() const noexcept
Returns how many times wait_for was called.
Definition clock.hpp:152
void jump(std::chrono::nanoseconds duration) noexcept
Moves the wall-clock reading alone, forward or back, as an operator or NTP setting the clock does.
Definition clock.hpp:144
void wait_for(std::chrono::nanoseconds duration) noexcept
Advances the clock by a duration instead of waiting, and counts the wait.
Definition clock.hpp:120
The real clock a live loop uses: wall-clock time to stamp events, monotonic time to wait and to measu...
Definition clock.hpp:20
std::chrono::steady_clock::time_point monotonic_now() const noexcept
Returns a reading that only moves forward, for measuring elapsed time.
Definition clock.hpp:34
void wait_for(std::chrono::nanoseconds duration) const
Blocks for a duration, measured on the monotonic clock.
Definition clock.hpp:42
std::chrono::system_clock::time_point now() const noexcept
Returns the current wall-clock time.
Definition clock.hpp:26
Runs a strategy: the runner, the polling loop, the order journal, and stopping.
Definition clock.hpp:7
The library: broker-neutral types (core), and the modules below.