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
19struct TradingDay {
28 bool open_{false};
29};
30
46 Country country_{Country::KR};
48 std::vector<TradingDay> days_;
50 std::chrono::system_clock::time_point received_at_;
60 std::size_t unreadable_days_{0};
61
69 std::optional<bool> is_trading_day(const Date& date) const {
70 for (const auto& day : days_) {
71 if (day.date_ == date) {
72 return day.open_;
73 }
74 }
75 return std::nullopt;
76 }
77
85 std::optional<Date> next_trading_day(const Date& date) const {
86 for (const auto& day : days_) {
87 if (day.open_ && date < day.date_) {
88 return day.date_;
89 }
90 }
91 return std::nullopt;
92 }
93
101 std::optional<Date> previous_trading_day(const Date& date) const {
102 std::optional<Date> found;
103 for (const auto& day : days_) {
104 if (!(day.date_ < date)) {
105 break;
106 }
107 if (day.open_) {
108 found = day.date_;
109 }
110 }
111 return found;
112 }
113
119 std::optional<std::pair<Date, Date>> covered() const {
120 if (days_.empty()) {
121 return std::nullopt;
122 }
123 return std::make_pair(days_.front().date_, days_.back().date_);
124 }
125};
126
127} // namespace algotradelib
The library: broker-neutral types (core), and the modules below.
Country
A country (market) the library supports.
Definition types.hpp:30
A calendar date with no time of day or time zone.
Definition types.hpp:1142
The trading days a broker reported for one market, ascending by date.
Definition calendar.hpp:44
std::optional< bool > is_trading_day(const Date &date) const
Looks up whether the market opens on a date.
Definition calendar.hpp:69
std::optional< Date > next_trading_day(const Date &date) const
Finds the first trading day after a date.
Definition calendar.hpp:85
std::size_t unreadable_days_
How many rows from the broker had a date that could not be read.
Definition calendar.hpp:60
std::chrono::system_clock::time_point received_at_
When the calendar was received, on this machine's clock.
Definition calendar.hpp:50
std::vector< TradingDay > days_
The days the broker reported, ascending. A date outside them is uncovered, not closed.
Definition calendar.hpp:48
std::optional< Date > previous_trading_day(const Date &date) const
Finds the last trading day before a date.
Definition calendar.hpp:101
Country country_
The market this calendar is for.
Definition calendar.hpp:46
std::optional< std::pair< Date, Date > > covered() const
Returns the first and last dates the calendar covers.
Definition calendar.hpp:119
One calendar date and whether the market opens on it.
Definition calendar.hpp:19
Date date_
The market's local calendar date, as the broker reports it.
Definition calendar.hpp:26
bool open_
Whether the market opens on date_.
Definition calendar.hpp:28