| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2018 Roy Zywina |
| 5 | Copyright (C) 2019 Eisuke Tani |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <ql/instruments/overnightindexfuture.hpp> |
| 22 | #include <ql/indexes/indexmanager.hpp> |
| 23 | #include <ql/event.hpp> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | OvernightIndexFuture::OvernightIndexFuture(ext::shared_ptr<OvernightIndex> overnightIndex, |
| 29 | const Date& valueDate, |
| 30 | const Date& maturityDate, |
| 31 | Handle<Quote> convexityAdjustment, |
| 32 | RateAveraging::Type averagingMethod) |
| 33 | : overnightIndex_(std::move(overnightIndex)), valueDate_(valueDate), |
| 34 | maturityDate_(maturityDate), convexityAdjustment_(std::move(convexityAdjustment)), |
| 35 | averagingMethod_(averagingMethod) { |
| 36 | QL_REQUIRE(overnightIndex_, "null overnight index" ); |
| 37 | registerWith(h: overnightIndex_); |
| 38 | } |
| 39 | |
| 40 | Real OvernightIndexFuture::averagedRate() const { |
| 41 | Date today = Settings::instance().evaluationDate(); |
| 42 | Calendar calendar = overnightIndex_->fixingCalendar(); |
| 43 | DayCounter dayCounter = overnightIndex_->dayCounter(); |
| 44 | Handle<YieldTermStructure> forwardCurve = overnightIndex_->forwardingTermStructure(); |
| 45 | Real avg = 0; |
| 46 | Date d1 = valueDate_; |
| 47 | const TimeSeries<Real>& history = IndexManager::instance() |
| 48 | .getHistory(name: overnightIndex_->name()); |
| 49 | Real fwd; |
| 50 | while (d1 < maturityDate_) { |
| 51 | Date d2 = calendar.advance(d1, n: 1, unit: Days); |
| 52 | if (d1 < today) { |
| 53 | fwd = history[d1]; |
| 54 | QL_REQUIRE(fwd != Null<Real>(), "missing rate on " << |
| 55 | d1 << " for index " << overnightIndex_->name()); |
| 56 | } else { |
| 57 | fwd = forwardCurve->forwardRate(d1, d2, resultDayCounter: dayCounter, comp: Simple).rate(); |
| 58 | } |
| 59 | avg += fwd * dayCounter.yearFraction(d1, d2); |
| 60 | d1 = d2; |
| 61 | } |
| 62 | |
| 63 | return avg / dayCounter.yearFraction(d1: valueDate_, d2: maturityDate_); |
| 64 | } |
| 65 | |
| 66 | Real OvernightIndexFuture::compoundedRate() const { |
| 67 | Date today = Settings::instance().evaluationDate(); |
| 68 | Calendar calendar = overnightIndex_->fixingCalendar(); |
| 69 | DayCounter dayCounter = overnightIndex_->dayCounter(); |
| 70 | Handle<YieldTermStructure> forwardCurve = overnightIndex_->forwardingTermStructure(); |
| 71 | Real prod = 1; |
| 72 | if (today > valueDate_) { |
| 73 | // can't value on a weekend inside reference period because we |
| 74 | // won't know the reset rate until start of next business day. |
| 75 | // user can supply an estimate if they really want to do this |
| 76 | today = calendar.adjust(today); |
| 77 | // for valuations inside the reference period, index quotes |
| 78 | // must have been populated in the history |
| 79 | const TimeSeries<Real>& history = IndexManager::instance() |
| 80 | .getHistory(name: overnightIndex_->name()); |
| 81 | Date d1 = valueDate_; |
| 82 | while (d1 < today) { |
| 83 | Real r = history[d1]; |
| 84 | QL_REQUIRE(r != Null<Real>(), "missing rate on " << |
| 85 | d1 << " for index " << overnightIndex_->name()); |
| 86 | Date d2 = calendar.advance(d1, n: 1, unit: Days); |
| 87 | prod *= 1 + r * dayCounter.yearFraction(d1, d2); |
| 88 | d1 = d2; |
| 89 | } |
| 90 | } |
| 91 | DiscountFactor forwardDiscount = forwardCurve->discount(d: maturityDate_); |
| 92 | if (valueDate_ > today) { |
| 93 | forwardDiscount /= forwardCurve->discount(d: valueDate_); |
| 94 | } |
| 95 | prod /= forwardDiscount; |
| 96 | |
| 97 | return (prod - 1) / dayCounter.yearFraction(d1: valueDate_, d2: maturityDate_); |
| 98 | } |
| 99 | |
| 100 | Real OvernightIndexFuture::rate() const { |
| 101 | switch (averagingMethod_) { |
| 102 | case RateAveraging::Simple: |
| 103 | return averagedRate(); |
| 104 | break; |
| 105 | case RateAveraging::Compound: |
| 106 | return compoundedRate(); |
| 107 | break; |
| 108 | default: |
| 109 | QL_FAIL("unknown compounding convention (" << Integer(averagingMethod_) << ")" ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool OvernightIndexFuture::isExpired() const { |
| 114 | return detail::simple_event(maturityDate_).hasOccurred(); |
| 115 | } |
| 116 | |
| 117 | Real OvernightIndexFuture::convexityAdjustment() const { |
| 118 | return convexityAdjustment_.empty() ? 0.0 : convexityAdjustment_->value(); |
| 119 | } |
| 120 | |
| 121 | void OvernightIndexFuture::performCalculations() const { |
| 122 | Rate R = convexityAdjustment() + rate(); |
| 123 | NPV_ = 100.0 * (1.0 - R); |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |