algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
11
namespace
algotradelib
{
12
19
struct
TradingDay
{
26
Date
date_
{};
28
bool
open_
{
false
};
29
};
30
44
struct
TradingCalendar
{
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
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::Country
Country
A country (market) the library supports.
Definition
types.hpp:30
algotradelib::Date
A calendar date with no time of day or time zone.
Definition
types.hpp:1142
algotradelib::TradingCalendar
The trading days a broker reported for one market, ascending by date.
Definition
calendar.hpp:44
algotradelib::TradingCalendar::is_trading_day
std::optional< bool > is_trading_day(const Date &date) const
Looks up whether the market opens on a date.
Definition
calendar.hpp:69
algotradelib::TradingCalendar::next_trading_day
std::optional< Date > next_trading_day(const Date &date) const
Finds the first trading day after a date.
Definition
calendar.hpp:85
algotradelib::TradingCalendar::unreadable_days_
std::size_t unreadable_days_
How many rows from the broker had a date that could not be read.
Definition
calendar.hpp:60
algotradelib::TradingCalendar::received_at_
std::chrono::system_clock::time_point received_at_
When the calendar was received, on this machine's clock.
Definition
calendar.hpp:50
algotradelib::TradingCalendar::days_
std::vector< TradingDay > days_
The days the broker reported, ascending. A date outside them is uncovered, not closed.
Definition
calendar.hpp:48
algotradelib::TradingCalendar::previous_trading_day
std::optional< Date > previous_trading_day(const Date &date) const
Finds the last trading day before a date.
Definition
calendar.hpp:101
algotradelib::TradingCalendar::country_
Country country_
The market this calendar is for.
Definition
calendar.hpp:46
algotradelib::TradingCalendar::covered
std::optional< std::pair< Date, Date > > covered() const
Returns the first and last dates the calendar covers.
Definition
calendar.hpp:119
algotradelib::TradingDay
One calendar date and whether the market opens on it.
Definition
calendar.hpp:19
algotradelib::TradingDay::date_
Date date_
The market's local calendar date, as the broker reports it.
Definition
calendar.hpp:26
algotradelib::TradingDay::open_
bool open_
Whether the market opens on date_.
Definition
calendar.hpp:28
algotradelib
src
core
calendar.hpp
Generated by
1.18.0