• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 14936704017

09 May 2025 07:58PM UTC coverage: 73.327%. Remained the same
14936704017

push

github

web-flow
Remove dangling unreachable break clauses (#2219)

56238 of 76695 relevant lines covered (73.33%)

8685070.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/ql/instruments/overnightindexfuture.cpp
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,
20✔
29
                                               const Date& valueDate,
30
                                               const Date& maturityDate,
31
                                               Handle<Quote> convexityAdjustment,
32
                                               RateAveraging::Type averagingMethod)
20✔
33
    : overnightIndex_(std::move(overnightIndex)), valueDate_(valueDate),
20✔
34
      maturityDate_(maturityDate), convexityAdjustment_(std::move(convexityAdjustment)),
20✔
35
      averagingMethod_(averagingMethod) {
20✔
36
        QL_REQUIRE(overnightIndex_, "null overnight index");
20✔
37
        registerWith(overnightIndex_);
40✔
38
        registerWith(convexityAdjustment_);
20✔
39
        registerWith(Settings::instance().evaluationDate());
20✔
40
    }
20✔
41

42
    Real OvernightIndexFuture::averagedRate() const {
94✔
43
        Date today = Settings::instance().evaluationDate();
94✔
44
        Calendar calendar = overnightIndex_->fixingCalendar();
94✔
45
        DayCounter dayCounter = overnightIndex_->dayCounter();
94✔
46
        Handle<YieldTermStructure> forwardCurve = overnightIndex_->forwardingTermStructure();
94✔
47
        Real avg = 0;
48
        Date d1 = valueDate_;
94✔
49
        // d1 could be a holiday
50
        Date fixingDate = calendar.adjust(d1, Preceding);
94✔
51
        const auto& history = overnightIndex_->timeSeries();
94✔
52
        Real fwd;
53
        while (d1 < maturityDate_) {
2,038✔
54
            Date d2 = calendar.advance(d1, 1, Days);
1,944✔
55
            if (fixingDate < today) {
1,944✔
56
                fwd = history[fixingDate];
288✔
57
                QL_REQUIRE(fwd != Null<Real>(),
288✔
58
                           "missing rate on " << fixingDate << " for index " << overnightIndex_->name());
59
            } else if (fixingDate == today) {
1,656✔
60
                fwd = history[fixingDate];
16✔
61
                if (fwd == Null<Real>())
16✔
62
                    fwd = forwardCurve->forwardRate(fixingDate, d2, dayCounter, Simple).rate();
32✔
63
            } else {
64
                fwd = forwardCurve->forwardRate(fixingDate, d2, dayCounter, Simple).rate();
3,280✔
65
            }
66
            // The rate is accrued starting from d1 even when the fixing date is earlier.
67
            // d2 might be beyond the maturity date if the latter is a holiday.
68
            avg += fwd * dayCounter.yearFraction(d1, std::min(d2, maturityDate_));
1,974✔
69
            fixingDate = d1 = d2;
1,944✔
70
        }
71

72
        return avg / dayCounter.yearFraction(valueDate_, maturityDate_);
188✔
73
    }
74

75
    Real OvernightIndexFuture::compoundedRate() const {
203✔
76
        Date today = Settings::instance().evaluationDate();
203✔
77
        Calendar calendar = overnightIndex_->fixingCalendar();
203✔
78
        DayCounter dayCounter = overnightIndex_->dayCounter();
203✔
79
        Handle<YieldTermStructure> forwardCurve = overnightIndex_->forwardingTermStructure();
203✔
80
        Real prod = 1;
81
        Date forwardDiscountStart = valueDate_;
203✔
82
        if (today > valueDate_) {
203✔
83
            // can't value on a weekend inside reference period because we
84
            // won't know the reset rate until start of next business day.
85
            // user can supply an estimate if they really want to do this
86
            today = calendar.adjust(today);
19✔
87
            forwardDiscountStart = today;
19✔
88
            // for valuations inside the reference period, index quotes
89
            // must have been populated in the history
90
            const auto& history = overnightIndex_->timeSeries();
19✔
91
            Date d1 = valueDate_;
19✔
92
            // d1 could be a holiday
93
            Date fixingDate = calendar.adjust(d1, Preceding);
19✔
94
            while (d1 < today) {
133✔
95
                Real r = history[fixingDate];
114✔
96
                QL_REQUIRE(r != Null<Real>(),
114✔
97
                           "missing rate on " << fixingDate << " for index " << overnightIndex_->name());
98
                Date d2 = calendar.advance(d1, 1, Days);
114✔
99
                // The rate is accrued starting from d1 even when the fixing date is earlier.
100
                // We can't get to the maturity date inside this loop,
101
                // so we don't need to cap d2 like we do in averagedRate above.
102
                prod *= 1 + r * dayCounter.yearFraction(d1, d2);
114✔
103
                fixingDate = d1 = d2;
114✔
104
            }
105
            // here d1 == today, and we might have today's fixing already
106
            if (today < maturityDate_) {
19✔
107
                Real r = history[today];
19✔
108
                if (r != Null<Real>()) {
19✔
109
                    Date tomorrow = calendar.advance(today, 1, Days);
19✔
110
                    prod *= 1 + r * dayCounter.yearFraction(today, tomorrow);
19✔
111
                    forwardDiscountStart = tomorrow;
19✔
112
                }
113
            }
114
        }
115
        // the telescopic part goes from the end of the last known fixing to the maturity
116
        DiscountFactor forwardDiscount =
117
            forwardCurve->discount(maturityDate_) / forwardCurve->discount(forwardDiscountStart);
203✔
118
        prod /= forwardDiscount;
203✔
119

120
        return (prod - 1) / dayCounter.yearFraction(valueDate_, maturityDate_);
406✔
121
    }
122

123
    Real OvernightIndexFuture::rate() const {
297✔
124
        switch (averagingMethod_) {
297✔
125
          case RateAveraging::Simple:
94✔
126
            return averagedRate();
94✔
127
          case RateAveraging::Compound:
203✔
128
            return compoundedRate();
203✔
129
          default:
×
130
              QL_FAIL("unknown compounding convention (" << Integer(averagingMethod_) << ")");
×
131
        }
132
    }
133

134
    bool OvernightIndexFuture::isExpired() const {
297✔
135
        return detail::simple_event(maturityDate_).hasOccurred();
297✔
136
    }
137

138
    Real OvernightIndexFuture::convexityAdjustment() const {
297✔
139
        return convexityAdjustment_.empty() ? 0.0 : convexityAdjustment_->value();
297✔
140
    }
141

142
    void OvernightIndexFuture::performCalculations() const {
297✔
143
        Rate R = convexityAdjustment() + rate();
297✔
144
        NPV_ = 100.0 * (1.0 - R);
297✔
145
    }
297✔
146

147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc