algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
calendar.hpp
1#pragma once
2
3#include "./types.hpp"
4
5#include <chrono>
6#include <cstddef>
7#include <optional>
8#include <utility>
9#include <vector>
10
11namespace algotradelib {
12
20 std::chrono::system_clock::time_point opens_at_{};
22 std::chrono::system_clock::time_point closes_at_{};
23};
24
31struct TradingDay {
40 bool open_{false};
47 std::optional<SessionHours> regular_{};
55 std::optional<SessionHours> extended_{};
56};
57
74 Country country_{Country::KR};
76 std::vector<TradingDay> days_;
78 std::chrono::system_clock::time_point received_at_;
88 std::size_t unreadable_days_{0};
89
97 std::optional<bool> is_trading_day(const Date& date) const {
98 // `days_` should be kept ascending, so a bisection finds a covered date; a miss is confirmed by the scan below, so a
99 // calendar filled out of order still finds every date it covers. Written by hand rather than with
100 // `std::lower_bound`, whose precondition (a sorted range) an out-of-order calendar would break. A date listed twice
101 // with different answers in an out-of-order calendar may get either.
102 std::size_t low = 0;
103 std::size_t high = days_.size();
104 while (low < high) {
105 const std::size_t middle = low + (high - low) / 2;
106 if (days_[middle].date_ < date) {
107 low = middle + 1;
108 } else {
109 high = middle;
110 }
111 }
112 if (low < days_.size() && days_[low].date_ == date) {
113 return days_[low].open_;
114 }
115 for (const auto& day : days_) {
116 if (day.date_ == date) {
117 return day.open_;
118 }
119 }
120 return std::nullopt;
121 }
122
130 std::optional<Date> next_trading_day(const Date& date) const {
131 for (const auto& day : days_) {
132 if (day.open_ && date < day.date_) {
133 return day.date_;
134 }
135 }
136 return std::nullopt;
137 }
138
146 std::optional<Date> previous_trading_day(const Date& date) const {
147 std::optional<Date> found;
148 for (const auto& day : days_) {
149 if (!(day.date_ < date)) {
150 break;
151 }
152 if (day.open_) {
153 found = day.date_;
154 }
155 }
156 return found;
157 }
158
164 std::optional<std::pair<Date, Date>> covered() const {
165 if (days_.empty()) {
166 return std::nullopt;
167 }
168 return std::make_pair(days_.front().date_, days_.back().date_);
169 }
170};
171
172} // namespace algotradelib
The library: broker-neutral types (core), and the modules below.
Country
A country (market) the library supports.
Definition types.hpp:32
A calendar date with no time of day or time zone.
Definition types.hpp:1412
One session's bounds, as instants.
Definition calendar.hpp:18
std::chrono::system_clock::time_point opens_at_
When the session opens (inclusive).
Definition calendar.hpp:20
std::chrono::system_clock::time_point closes_at_
When the session closes (exclusive).
Definition calendar.hpp:22
The trading days a broker reported for one market, ascending by date.
Definition calendar.hpp:72
std::optional< bool > is_trading_day(const Date &date) const
Looks up whether the market opens on a date.
Definition calendar.hpp:97
std::optional< Date > next_trading_day(const Date &date) const
Finds the first trading day after a date.
Definition calendar.hpp:130
std::size_t unreadable_days_
How many rows from the broker had a date that could not be read.
Definition calendar.hpp:88
std::chrono::system_clock::time_point received_at_
When the calendar was received, on this machine's clock.
Definition calendar.hpp:78
std::vector< TradingDay > days_
The days the broker reported, ascending. A date outside them is uncovered, not closed.
Definition calendar.hpp:76
std::optional< Date > previous_trading_day(const Date &date) const
Finds the last trading day before a date.
Definition calendar.hpp:146
Country country_
The market this calendar is for.
Definition calendar.hpp:74
std::optional< std::pair< Date, Date > > covered() const
Returns the first and last dates the calendar covers.
Definition calendar.hpp:164
One calendar date and whether the market opens on it.
Definition calendar.hpp:31
Date date_
The market's local calendar date, as the broker reports it.
Definition calendar.hpp:38
bool open_
Whether the market opens on date_.
Definition calendar.hpp:40
std::optional< SessionHours > extended_
From the start of the first session the broker states for the day to the end of the last,...
Definition calendar.hpp:55
std::optional< SessionHours > regular_
The exchange's regular session on an open day, where the adapter fills it.
Definition calendar.hpp:47