QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
schedule.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2007, 2011 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2006, 2009 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file schedule.hpp
23 \brief date schedule
24*/
25
26#ifndef quantlib_schedule_hpp
27#define quantlib_schedule_hpp
28
30#include <ql/utilities/null.hpp>
31#include <ql/time/period.hpp>
33#include <ql/errors.hpp>
34#include <ql/optional.hpp>
35
36namespace QuantLib {
37
38 //! Payment schedule
39 /*! \ingroup datetime */
40 class Schedule {
41 public:
42 /*! constructor that takes any list of dates, and optionally
43 meta information that can be used by client classes. Note
44 that neither the list of dates nor the meta information is
45 checked for plausibility in any sense. */
47 const std::vector<Date>&,
50 const ext::optional<BusinessDayConvention>& terminationDateConvention = ext::nullopt,
51 const ext::optional<Period>& tenor = ext::nullopt,
52 const ext::optional<DateGeneration::Rule>& rule = ext::nullopt,
53 const ext::optional<bool>& endOfMonth = ext::nullopt,
54 std::vector<bool> isRegular = std::vector<bool>(0));
55 /*! rule based constructor */
56 Schedule(Date effectiveDate,
57 const Date& terminationDate,
58 const Period& tenor,
60 BusinessDayConvention convention,
61 BusinessDayConvention terminationDateConvention,
63 bool endOfMonth,
64 const Date& firstDate = Date(),
65 const Date& nextToLastDate = Date());
66 Schedule() = default;
67 //! \name Element access
68 //@{
69 Size size() const { return dates_.size(); }
70 const Date& operator[](Size i) const;
71 const Date& at(Size i) const;
72 const Date& date(Size i) const;
73 const std::vector<Date>& dates() const { return dates_; }
74 bool empty() const { return dates_.empty(); }
75 const Date& front() const;
76 const Date& back() const;
77 //@}
78 //! \name Other inspectors
79 //@{
80 Date previousDate(const Date& refDate) const;
81 Date nextDate(const Date& refDate) const;
82 bool hasIsRegular() const;
83 bool isRegular(Size i) const;
84 const std::vector<bool>& isRegular() const;
85 const Calendar& calendar() const;
86 const Date& startDate() const;
87 const Date& endDate() const;
88 bool hasTenor() const;
89 const Period& tenor() const;
93 bool hasRule() const;
95 bool hasEndOfMonth() const;
96 bool endOfMonth() const;
97 //@}
98 //! \name Iterators
99 //@{
100 typedef std::vector<Date>::const_iterator const_iterator;
101 const_iterator begin() const { return dates_.begin(); }
102 const_iterator end() const { return dates_.end(); }
103 const_iterator lower_bound(const Date& d = Date()) const;
104 //@}
105 //! \name Utilities
106 //@{
107 //! truncated schedule
108 Schedule after(const Date& truncationDate) const;
109 Schedule until(const Date& truncationDate) const;
110 //@}
111 private:
112 ext::optional<Period> tenor_;
115 ext::optional<BusinessDayConvention> terminationDateConvention_;
116 ext::optional<DateGeneration::Rule> rule_;
117 ext::optional<bool> endOfMonth_;
119 std::vector<Date> dates_;
120 std::vector<bool> isRegular_;
121 };
122
123
124 //! helper class
125 /*! This class provides a more comfortable interface to the
126 argument list of Schedule's constructor.
127 */
129 public:
130 MakeSchedule& from(const Date& effectiveDate);
131 MakeSchedule& to(const Date& terminationDate);
140 MakeSchedule& endOfMonth(bool flag=true);
143 operator Schedule() const;
144 private:
147 ext::optional<Period> tenor_;
148 ext::optional<BusinessDayConvention> convention_;
149 ext::optional<BusinessDayConvention> terminationDateConvention_;
151 bool endOfMonth_ = false;
153 };
154
155 /*! Helper function for returning the date on or before date \p d that is the 20th of the month and obeserves the
156 given date generation \p rule if it is relevant.
157 */
159
160 // inline definitions
161
162 inline const Date& Schedule::date(Size i) const {
163 return dates_.at(i);
164 }
165
166 inline const Date& Schedule::operator[](Size i) const {
167 #if defined(QL_EXTRA_SAFETY_CHECKS)
168 return dates_.at(i);
169 #else
170 return dates_[i];
171 #endif
172 }
173
174 inline const Date& Schedule::at(Size i) const {
175 return dates_.at(i);
176 }
177
178 inline const Date& Schedule::front() const {
179 QL_REQUIRE(!dates_.empty(), "no front date for empty schedule");
180 return dates_.front();
181 }
182
183 inline const Date& Schedule::back() const {
184 QL_REQUIRE(!dates_.empty(), "no back date for empty schedule");
185 return dates_.back();
186 }
187
188 inline const Calendar& Schedule::calendar() const {
189 return calendar_;
190 }
191
192 inline const Date& Schedule::startDate() const {
193 return dates_.front();
194 }
195
196 inline const Date &Schedule::endDate() const { return dates_.back(); }
197
198 inline bool Schedule::hasTenor() const {
199 return static_cast<bool>(tenor_);
200 }
201
202 inline const Period& Schedule::tenor() const {
204 "full interface (tenor) not available");
205 return *tenor_; // NOLINT(bugprone-unchecked-optional-access)
206 }
207
209 return convention_;
210 }
211
212 inline bool
214 return static_cast<bool>(terminationDateConvention_);
215 }
216
220 "full interface (termination date bdc) not available");
221 return *terminationDateConvention_; // NOLINT(bugprone-unchecked-optional-access)
222 }
223
224 inline bool Schedule::hasRule() const {
225 return static_cast<bool>(rule_);
226 }
227
229 QL_REQUIRE(hasRule(), "full interface (rule) not available");
230 return *rule_; // NOLINT(bugprone-unchecked-optional-access)
231 }
232
233 inline bool Schedule::hasEndOfMonth() const {
234 return static_cast<bool>(endOfMonth_);
235 }
236
237 inline bool Schedule::endOfMonth() const {
239 "full interface (end of month) not available");
240 return *endOfMonth_; // NOLINT(bugprone-unchecked-optional-access)
241 }
242
243}
244
245#endif
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
MakeSchedule & withConvention(BusinessDayConvention)
Definition: schedule.cpp:552
ext::optional< BusinessDayConvention > convention_
Definition: schedule.hpp:148
MakeSchedule & withTerminationDateConvention(BusinessDayConvention)
Definition: schedule.cpp:557
ext::optional< Period > tenor_
Definition: schedule.hpp:147
ext::optional< BusinessDayConvention > terminationDateConvention_
Definition: schedule.hpp:149
MakeSchedule & withRule(DateGeneration::Rule)
Definition: schedule.cpp:563
MakeSchedule & backwards()
Definition: schedule.cpp:573
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:532
MakeSchedule & withTenor(const Period &)
Definition: schedule.cpp:537
DateGeneration::Rule rule_
Definition: schedule.hpp:150
MakeSchedule & withFirstDate(const Date &d)
Definition: schedule.cpp:583
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:527
MakeSchedule & endOfMonth(bool flag=true)
Definition: schedule.cpp:578
MakeSchedule & withFrequency(Frequency)
Definition: schedule.cpp:542
MakeSchedule & forwards()
Definition: schedule.cpp:568
MakeSchedule & withNextToLastDate(const Date &d)
Definition: schedule.cpp:588
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:547
Calendar for reproducing theoretical calculations.
Payment schedule.
Definition: schedule.hpp:40
Calendar calendar_
Definition: schedule.hpp:113
Date nextDate(const Date &refDate) const
Definition: schedule.cpp:495
const Date & endDate() const
Definition: schedule.hpp:196
ext::optional< Period > tenor_
Definition: schedule.hpp:112
const_iterator begin() const
Definition: schedule.hpp:101
ext::optional< BusinessDayConvention > terminationDateConvention_
Definition: schedule.hpp:115
std::vector< Date > dates_
Definition: schedule.hpp:119
const Date & front() const
Definition: schedule.hpp:178
const Calendar & calendar() const
Definition: schedule.hpp:188
const std::vector< Date > & dates() const
Definition: schedule.hpp:73
std::vector< bool > isRegular_
Definition: schedule.hpp:120
bool hasRule() const
Definition: schedule.hpp:224
bool empty() const
Definition: schedule.hpp:74
std::vector< Date >::const_iterator const_iterator
Definition: schedule.hpp:100
Date previousDate(const Date &refDate) const
Definition: schedule.cpp:503
bool endOfMonth() const
Definition: schedule.hpp:237
BusinessDayConvention convention_
Definition: schedule.hpp:114
const Date & startDate() const
Definition: schedule.hpp:192
ext::optional< bool > endOfMonth_
Definition: schedule.hpp:117
ext::optional< DateGeneration::Rule > rule_
Definition: schedule.hpp:116
Schedule after(const Date &truncationDate) const
truncated schedule
Definition: schedule.cpp:420
const Date & at(Size i) const
Definition: schedule.hpp:174
const Date & operator[](Size i) const
Definition: schedule.hpp:166
DateGeneration::Rule rule() const
Definition: schedule.hpp:228
bool hasTenor() const
Definition: schedule.hpp:198
bool hasEndOfMonth() const
Definition: schedule.hpp:233
const_iterator lower_bound(const Date &d=Date()) const
Definition: schedule.cpp:488
const std::vector< bool > & isRegular() const
Definition: schedule.cpp:522
const Date & date(Size i) const
Definition: schedule.hpp:162
Size size() const
Definition: schedule.hpp:69
const_iterator end() const
Definition: schedule.hpp:102
bool hasTerminationDateBusinessDayConvention() const
Definition: schedule.hpp:213
BusinessDayConvention businessDayConvention() const
Definition: schedule.hpp:208
const Date & back() const
Definition: schedule.hpp:183
bool hasIsRegular() const
Definition: schedule.cpp:511
Schedule until(const Date &truncationDate) const
Definition: schedule.cpp:454
const Period & tenor() const
Definition: schedule.hpp:202
BusinessDayConvention terminationDateBusinessDayConvention() const
Definition: schedule.hpp:218
date generation rule
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
std::size_t Size
size of a container
Definition: types.hpp:58
constexpr const boost::none_t & nullopt
Definition: optional.hpp:44
Definition: any.hpp:37
Date previousTwentieth(const Date &d, DateGeneration::Rule rule)
Definition: schedule.cpp:635
null values
Calendar for reproducing theoretical calculations.
Maps optional to either the boost or std implementation.
period- and frequency-related classes and enumerations