25#include <ql/errors.hpp>
26#include <ql/math/comparison.hpp>
27#include <ql/time/dategenerationrule.hpp>
28#include <ql/time/period.hpp>
29#include <ql/time/schedule.hpp>
30#include <ql/types.hpp>
40 return static_cast<Real
>(p.length()) / 365.25;
42 return static_cast<Real
>(p.length()) * 7.0 / 365.25;
44 return static_cast<Real
>(p.length()) / 12.0;
46 return static_cast<Real
>(p.length());
48 QL_FAIL(
"periodToTime(): time unit (" << p.units() <<
") not handled");
53 static const std::vector<Period> eligibleTerms = {5 * Years, 7 * Years, 10 * Years, 3 * Years, 1 * Years,
54 2 * Years, 4 * Years, 6 * Years, 8 * Years, 9 * Years};
55 static const int gracePeriod = 15;
57 for (
auto const& p : eligibleTerms) {
58 if (std::abs(cdsMaturity(startDate, p, DateGeneration::CDS2015) - endDate) < gracePeriod) {
66QuantLib::Date
lowerDate(
const Real t,
const QuantLib::Date& refDate,
const QuantLib::DayCounter& dc) {
69 QL_REQUIRE(t > 0.0,
"lowerDate(" << t <<
"," << refDate <<
"," << dc.name()
70 <<
") was called with negative time, this is not allowed.");
72 Date d = refDate +
static_cast<int>(t * 365.25);
73 Real tmp = dc.yearFraction(refDate, d);
75 while ((tmp < t ||
close_enough(tmp, t)) && (++attempts < 10000)) {
77 tmp = dc.yearFraction(refDate, d);
80 QL_REQUIRE(attempts < 10000,
"lowerDate(" << t <<
"," << refDate <<
"," << dc.name() <<
") could not be computed.");
83 while ((tmp > t && !
close_enough(tmp, t)) && (++attempts < 10000)) {
85 tmp = dc.yearFraction(refDate, d);
88 QL_REQUIRE(attempts < 10000,
"lowerDate(" << t <<
"," << refDate <<
"," << dc.name() <<
") could not be computed.");
91 QL_FAIL(
"lowerDate(" << t <<
"," << refDate <<
"," << dc.name() <<
") could not be computed.");
95 if (std::abs(length - std::round(length)) < 1.0 / 365.25)
96 return std::lround(length) * Years;
97 if (std::abs(length * 12.0 - std::round(length * 12.0)) < 12.0 / 365.25)
98 return std::lround(length * 12.0) * Months;
99 return std::lround(length * 365.25) * Days;
103 const QuantLib::Date& end) {
105 if (location ==
"Null") {
107 }
else if (location ==
"US") {
108 for (Integer y = start.year(); y <= end.year(); ++y) {
109 Date d1 = Date::nthWeekday(2, Sunday, March, y);
110 Date d2 = Date::nthWeekday(1, Sunday, November, y);
111 if (start <= d1 && end > d1)
113 if (start <= d2 && end > d2)
117 QL_FAIL(
"daylightSavings(" << location <<
") not supported. Contact dev to add support for this location.");
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
QuantLib::Period tenorFromLength(const QuantLib::Real length)
Real periodToTime(const Period &p)
QuantLib::Date lowerDate(const Real t, const QuantLib::Date &refDate, const QuantLib::DayCounter &dc)
QuantLib::Integer daylightSavingCorrection(const std::string &location, const QuantLib::Date &start, const QuantLib::Date &end)
QuantLib::Period implyIndexTerm(const Date &startDate, const Date &endDate)