Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
averageonindexedcouponpricer.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21namespace QuantExt {
22
24
25 coupon_ = dynamic_cast<const AverageONIndexedCoupon*>(&coupon);
26 QL_REQUIRE(coupon_, "AverageONIndexedCoupon required");
27
28 overnightIndex_ = QuantLib::ext::dynamic_pointer_cast<OvernightIndex>(coupon_->index());
29 QL_REQUIRE(overnightIndex_, "OvernightIndex required");
30
31 gearing_ = coupon_->gearing();
32 spread_ = coupon_->spread();
33 accrualPeriod_ = coupon_->accrualPeriod();
34}
35
37
38 std::vector<Date> fixingDates = coupon_->fixingDates();
39 std::vector<Time> accrualFractions = coupon_->dt();
40 Size numPeriods = accrualFractions.size();
41 Real accumulatedRate = 0;
42 QL_REQUIRE(coupon_->rateCutoff() < numPeriods,
43 "rate cutoff (" << coupon_->rateCutoff() << ") must be less than number of fixings in period ("
44 << numPeriods << ")");
45 Size nCutoff = numPeriods - coupon_->rateCutoff();
46
48 Size i = 0;
49 Date valuationDate = Settings::instance().evaluationDate();
50 // Deal with past fixings.
51 while (i < numPeriods && fixingDates[std::min(i, nCutoff)] < valuationDate) {
52 Rate pastFixing = overnightIndex_->pastFixing(fixingDates[std::min(i, nCutoff)]);
53 QL_REQUIRE(pastFixing != Null<Real>(),
54 "Missing " << overnightIndex_->name() << " fixing for " << fixingDates[std::min(i, nCutoff)]);
55 accumulatedRate += pastFixing * accrualFractions[i];
56 ++i;
57 }
58 // Use valuation date's fixing also if available.
59 if (i < numPeriods && fixingDates[std::min(i, nCutoff)] == valuationDate) {
60 Rate valuationDateFixing = overnightIndex_->pastFixing(valuationDate);
61 if (valuationDateFixing != Null<Real>()) {
62 accumulatedRate += valuationDateFixing * accrualFractions[i];
63 ++i;
64 }
65 }
66 // Use Takada approximation (2011) for forecasting.
67 if (i < numPeriods) {
68 Handle<YieldTermStructure> projectionCurve = overnightIndex_->forwardingTermStructure();
69 QL_REQUIRE(!projectionCurve.empty(),
70 "Null term structure set to this instance of " << overnightIndex_->name());
71
72 // handle the part until the rate cutoff (might be empty, i.e. startForecast = endForecast)
73 Date startForecast = coupon_->valueDates()[i];
74 Date endForecast = coupon_->valueDates()[std::max(nCutoff, i)];
75 DiscountFactor startDiscount = projectionCurve->discount(startForecast);
76 DiscountFactor endDiscount = projectionCurve->discount(endForecast);
77
78 // handle the rate cutoff period (if there is any, i.e. if nCutoff < n)
79 if (nCutoff < numPeriods) {
80 // forward discount factor for one calendar day on the cutoff date
81 DiscountFactor discountCutoffDate = projectionCurve->discount(coupon_->valueDates()[nCutoff] + 1) /
82 projectionCurve->discount(coupon_->valueDates()[nCutoff]);
83 // keep the above forward discount factor constant during the cutoff period
84 endDiscount *=
85 std::pow(discountCutoffDate, coupon_->valueDates()[numPeriods] - coupon_->valueDates()[nCutoff]);
86 }
87
88 accumulatedRate += log(startDiscount / endDiscount);
89 }
90 } else if (approximationType_ == None) {
91 std::vector<Rate> fixings = coupon_->indexFixings();
92 for (Size i = 0; i < numPeriods; ++i) {
93 accumulatedRate += fixings[i] * accrualFractions[i];
94 }
95 } else {
96 QL_FAIL("Invalid Approximation for AverageONIndexedCouponPricer");
97 }
98 // Return factor * rate + spread
99 Rate tau = overnightIndex_->dayCounter().yearFraction(coupon_->valueDates().front(), coupon_->valueDates().back());
100 Rate rate = gearing_ * accumulatedRate / tau + spread_;
101 return rate;
102}
103
104} // namespace QuantExt
Pricer for average overnight indexed coupons.
Natural rateCutoff() const
rate cutoff associated with the coupon
const std::vector< Date > & valueDates() const
value dates for the rates to be averaged
const std::vector< Time > & dt() const
accrual periods for the averaging
const std::vector< Rate > & indexFixings() const
fixings to be averaged
const std::vector< Date > & fixingDates() const
fixing dates for the rates to be averaged
QuantLib::ext::shared_ptr< OvernightIndex > overnightIndex_
void initialize(const FloatingRateCoupon &coupon) override
CompiledFormula log(CompiledFormula x)