Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
timeperiod.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21
22using std::vector;
23using std::string;
24
25namespace ore {
26namespace data {
27
28TimePeriod::TimePeriod(const std::vector<Date>& dates, Size mporDays, const QuantLib::Calendar& calendar) {
29 QL_REQUIRE(dates.size() % 2 == 0, "TimePeriod: dates size must be an even number, got " << dates.size());
30 for (Size i = 0; i < dates.size(); ++i) {
31 if (i % 2 == 0) {
32 Date sDate = dates[i];
33 if (!calendar.empty() && mporDays != QuantLib::Null<Size>())
34 sDate = calendar.advance(sDate, -mporDays * QuantLib::Days);
35 startDates_.push_back(sDate);
36 }
37 else
38 endDates_.push_back(dates[i]);
39 }
40}
41
42bool TimePeriod::contains(const Date& d) const {
43 for (Size i = 0; i < startDates_.size(); ++i) {
44 if (d >= startDates_[i] && d <= endDates_[i])
45 return true;
46 }
47 return false;
48}
49
50std::ostream& operator<<(std::ostream& out, const TimePeriod& t) {
51 for (Size i = 0; i < t.numberOfContiguousParts(); ++i) {
52 out << QuantLib::io::iso_date(t.startDates()[i]) << " to " << QuantLib::io::iso_date(t.endDates()[i]);
53 if (i < t.numberOfContiguousParts() - 1)
54 out << " + ";
55 }
56 return out;
57}
58
59TimePeriod totalTimePeriod(std::vector<std::string> timePeriods, Size mporDays, const QuantLib::Calendar& calendar) {
60 vector<Date> allDates;
61 for (const string& s : timePeriods) {
62 auto dates = parseListOfValues<Date>(s, &parseDate);
63 for (const auto& d : dates)
64 allDates.push_back(d);
65 }
66
67 TimePeriod period(allDates);
68
69 Date minDate = *std::min_element(period.startDates().begin(), period.startDates().end());
70 Date maxDate = *std::max_element(period.endDates().begin(), period.endDates().end());
71
72 vector<Date> finalDates;
73 finalDates.push_back(minDate);
74 finalDates.push_back(maxDate);
75
76 TimePeriod totalPeriod(finalDates, mporDays, calendar);
77 return totalPeriod;
78}
79
80} // namespace data
81} // namespace ore
Handles non-contiguous time period.
Definition: timeperiod.hpp:41
TimePeriod(const std::vector< Date > &dates, Size mporDays=QuantLib::Null< Size >(), const QuantLib::Calendar &calendar=QuantLib::Calendar())
Definition: timeperiod.cpp:28
std::vector< Date > startDates_
Definition: timeperiod.hpp:57
std::vector< Date > endDates_
Definition: timeperiod.hpp:57
const std::vector< Date > & endDates() const
Definition: timeperiod.hpp:53
const std::vector< Date > & startDates() const
Definition: timeperiod.hpp:52
bool contains(const Date &d) const
Definition: timeperiod.cpp:42
Size numberOfContiguousParts() const
Definition: timeperiod.hpp:51
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
@ data
Definition: log.hpp:77
Calendar calendar
Definition: utilities.cpp:441
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
TimePeriod totalTimePeriod(std::vector< std::string > timePeriods, Size mporDays, const QuantLib::Calendar &calendar)
Definition: timeperiod.cpp:59
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
non-contiguous time period handling