QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
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
26namespace 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");
38 }
39
42 Calendar calendar = overnightIndex_->fixingCalendar();
43 DayCounter dayCounter = overnightIndex_->dayCounter();
44 Handle<YieldTermStructure> forwardCurve = overnightIndex_->forwardingTermStructure();
45 Real avg = 0;
46 Date d1 = valueDate_;
49 Real fwd;
50 while (d1 < maturityDate_) {
51 Date d2 = calendar.advance(d1, 1, 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, dayCounter, Simple).rate();
58 }
59 avg += fwd * dayCounter.yearFraction(d1, d2);
60 d1 = d2;
61 }
62
63 return avg / dayCounter.yearFraction(valueDate_, maturityDate_);
64 }
65
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
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, 1, Days);
87 prod *= 1 + r * dayCounter.yearFraction(d1, d2);
88 d1 = d2;
89 }
90 }
91 DiscountFactor forwardDiscount = forwardCurve->discount(maturityDate_);
92 if (valueDate_ > today) {
93 forwardDiscount /= forwardCurve->discount(valueDate_);
94 }
95 prod /= forwardDiscount;
96
97 return (prod - 1) / dayCounter.yearFraction(valueDate_, maturityDate_);
98 }
99
101 switch (averagingMethod_) {
103 return averagedRate();
104 break;
106 return compoundedRate();
107 break;
108 default:
109 QL_FAIL("unknown compounding convention (" << Integer(averagingMethod_) << ")");
110 }
111 }
112
115 }
116
118 return convexityAdjustment_.empty() ? 0.0 : convexityAdjustment_->value();
119 }
120
122 Rate R = convexityAdjustment() + rate();
123 NPV_ = 100.0 * (1.0 - R);
124 }
125
126}
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
Definition: event.cpp:28
Shared handle to an observable.
Definition: handle.hpp:41
const TimeSeries< Real > & getHistory(const std::string &name) const
returns the (possibly empty) history of the index fixings
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< OvernightIndex > overnightIndex_
void performCalculations() const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
OvernightIndexFuture(ext::shared_ptr< OvernightIndex > overnightIndex, const Date &valueDate, const Date &maturityDate, Handle< Quote > convexityAdjustment=Handle< Quote >(), RateAveraging::Type averagingMethod=RateAveraging::Compound)
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Container for historical data.
Definition: timeseries.hpp:51
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
Array prod(const SparseMatrix &A, const Array &x)
STL namespace.