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 {
73 return clock_->now();
74 }
75
80 std::chrono::steady_clock::time_point monotonic_now() const {
81 return clock_->monotonic_now();
82 }
83
88 void wait_for(std::chrono::nanoseconds duration) {
89 clock_->wait_for(duration);
90 }
91
92private:
93 Clock* clock_;
94};
95
103public:
108 explicit ManualClock(std::chrono::system_clock::time_point start = {}) : now_(start) {}
109
114 std::chrono::system_clock::time_point now() const noexcept {
115 return now_;
116 }
117
122 std::chrono::steady_clock::time_point monotonic_now() const noexcept {
123 return monotonic_;
124 }
125
130 void wait_for(std::chrono::nanoseconds duration) noexcept {
131 ++waits_;
132 if (duration > std::chrono::nanoseconds::zero()) {
133 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
134 monotonic_ += std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
135 waited_ += duration;
136 }
137 }
138
143 void advance(std::chrono::nanoseconds duration) noexcept {
144 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
145 if (duration > std::chrono::nanoseconds::zero()) {
146 monotonic_ += std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
147 }
148 }
149
154 void jump(std::chrono::nanoseconds duration) noexcept {
155 now_ += std::chrono::duration_cast<std::chrono::system_clock::duration>(duration);
156 }
157
162 std::size_t waits() const noexcept {
163 return waits_;
164 }
165
170 std::chrono::nanoseconds waited() const noexcept {
171 return waited_;
172 }
173
174private:
175 std::chrono::system_clock::time_point now_;
176 std::chrono::steady_clock::time_point monotonic_{};
177 std::size_t waits_{0};
178 std::chrono::nanoseconds waited_{std::chrono::nanoseconds::zero()};
179};
180
181} // namespace runner
182} // 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:88
std::chrono::steady_clock::time_point monotonic_now() const
Returns the referenced clock's monotonic reading.
Definition clock.hpp:80
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:114
ManualClock(std::chrono::system_clock::time_point start={})
Constructs a clock.
Definition clock.hpp:108
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:143
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:122
std::chrono::nanoseconds waited() const noexcept
Returns the total positive duration passed to wait_for.
Definition clock.hpp:170
std::size_t waits() const noexcept
Returns how many times wait_for was called.
Definition clock.hpp:162
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:154
void wait_for(std::chrono::nanoseconds duration) noexcept
Advances the clock by a duration instead of waiting, and counts the wait.
Definition clock.hpp:130
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.