|
| 1 | +/* |
| 2 | + Copyright (C) 2022 Quaternion Risk Management Ltd |
| 3 | +
|
| 4 | + This file is part of QuantLib, a free-software/open-source library |
| 5 | + for financial quantitative analysts and developers - http://quantlib.org/ |
| 6 | +
|
| 7 | + QuantLib is free software: you can redistribute it and/or modify it |
| 8 | + under the terms of the QuantLib license. You should have received a |
| 9 | + copy of the license along with this program; if not, please email |
| 10 | + <quantlib-dev@lists.sf.net>. The license is also available online at |
| 11 | + <http://quantlib.org/license.shtml>. |
| 12 | +
|
| 13 | + This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | + FOR A PARTICULAR PURPOSE. See the license for more details. |
| 16 | +*/ |
| 17 | + |
| 18 | +/*! \file qle/utilities/time.hpp |
| 19 | + \brief time related utilities. |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <ql/errors.hpp> |
| 23 | +#include <ql/math/comparison.hpp> |
| 24 | +#include <ql/time/dategenerationrule.hpp> |
| 25 | +#include <ql/time/period.hpp> |
| 26 | +#include <ql/time/schedule.hpp> |
| 27 | +#include <ql/types.hpp> |
| 28 | +#include <ql/utilities/time.hpp> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace QuantLib { |
| 32 | + |
| 33 | + Real periodToTime(const Period& p) { |
| 34 | + switch (p.units()) { |
| 35 | + case Days: |
| 36 | + return static_cast<Real>(p.length()) / 365.25; |
| 37 | + case Weeks: |
| 38 | + return static_cast<Real>(p.length()) * 7.0 / 365.25; |
| 39 | + case Months: |
| 40 | + return static_cast<Real>(p.length()) / 12.0; |
| 41 | + case Years: |
| 42 | + return static_cast<Real>(p.length()); |
| 43 | + default: |
| 44 | + QL_FAIL("periodToTime(): time unit (" << p.units() << ") not handled"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + QuantLib::Period implyIndexTerm(const Date& startDate, const Date& endDate) { |
| 49 | + static const std::vector<Period> eligibleTerms = { |
| 50 | + 5 * Years, 7 * Years, 10 * Years, 3 * Years, 1 * Years, |
| 51 | + 2 * Years, 4 * Years, 6 * Years, 8 * Years, 9 * Years}; |
| 52 | + static const int gracePeriod = 15; |
| 53 | + |
| 54 | + for (auto const& p : eligibleTerms) { |
| 55 | + if (std::abs(cdsMaturity(startDate, p, DateGeneration::CDS2015) - endDate) < |
| 56 | + gracePeriod) { |
| 57 | + return p; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return 0 * Days; |
| 62 | + } |
| 63 | + |
| 64 | + QuantLib::Date |
| 65 | + lowerDate(const Real t, const QuantLib::Date& refDate, const QuantLib::DayCounter& dc) { |
| 66 | + if (close_enough(t, 0.0)) |
| 67 | + return refDate; |
| 68 | + QL_REQUIRE(t > 0.0, "lowerDate(" |
| 69 | + << t << "," << refDate << "," << dc.name() |
| 70 | + << ") was called with negative time, this is not allowed."); |
| 71 | + bool done = false; |
| 72 | + Date d = refDate + static_cast<int>(t * 365.25); |
| 73 | + Real tmp = dc.yearFraction(refDate, d); |
| 74 | + Size attempts = 0; |
| 75 | + while ((tmp < t || close_enough(tmp, t)) && (++attempts < 10000)) { |
| 76 | + ++d; |
| 77 | + tmp = dc.yearFraction(refDate, d); |
| 78 | + done = true; |
| 79 | + } |
| 80 | + QL_REQUIRE(attempts < 10000, "lowerDate(" << t << "," << refDate << "," << dc.name() |
| 81 | + << ") could not be computed."); |
| 82 | + if (done) |
| 83 | + return --d; |
| 84 | + while ((tmp > t && !close_enough(tmp, t)) && (++attempts < 10000)) { |
| 85 | + --d; |
| 86 | + tmp = dc.yearFraction(refDate, d); |
| 87 | + done = true; |
| 88 | + } |
| 89 | + QL_REQUIRE(attempts < 10000, "lowerDate(" << t << "," << refDate << "," << dc.name() |
| 90 | + << ") could not be computed."); |
| 91 | + if (done) |
| 92 | + return d; |
| 93 | + QL_FAIL("lowerDate(" << t << "," << refDate << "," << dc.name() |
| 94 | + << ") could not be computed."); |
| 95 | + } |
| 96 | + |
| 97 | + QuantLib::Period tenorFromLength(const QuantLib::Real length) { |
| 98 | + if (std::abs(length - std::round(length)) < 1.0 / 365.25) |
| 99 | + return std::lround(length) * Years; |
| 100 | + if (std::abs(length * 12.0 - std::round(length * 12.0)) < 12.0 / 365.25) |
| 101 | + return std::lround(length * 12.0) * Months; |
| 102 | + return std::lround(length * 365.25) * Days; |
| 103 | + } |
| 104 | + |
| 105 | + QuantLib::Integer daylightSavingCorrection(const std::string& location, |
| 106 | + const QuantLib::Date& start, |
| 107 | + const QuantLib::Date& end) { |
| 108 | + Integer result = 0; |
| 109 | + if (location == "Null") { |
| 110 | + result = 0; |
| 111 | + } else if (location == "US") { |
| 112 | + for (Integer y = start.year(); y <= end.year(); ++y) { |
| 113 | + Date d1 = Date::nthWeekday(2, Sunday, March, y); |
| 114 | + Date d2 = Date::nthWeekday(1, Sunday, November, y); |
| 115 | + if (start <= d1 && end > d1) |
| 116 | + --result; |
| 117 | + if (start <= d2 && end > d2) |
| 118 | + ++result; |
| 119 | + } |
| 120 | + } else { |
| 121 | + QL_FAIL("daylightSavings(" |
| 122 | + << location |
| 123 | + << ") not supported. Contact dev to add support for this location."); |
| 124 | + } |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | +} // namespace QuantExt |
0 commit comments