| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2021 Marcin Rybacki |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/cashflows/fixedratecoupon.hpp> |
| 21 | #include <ql/cashflows/simplecashflow.hpp> |
| 22 | #include <ql/cashflows/subperiodcoupon.hpp> |
| 23 | #include <ql/indexes/iborindex.hpp> |
| 24 | #include <ql/instruments/zerocouponswap.hpp> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace QuantLib { |
| 28 | |
| 29 | namespace { |
| 30 | ext::shared_ptr<CashFlow> |
| 31 | compoundedSubPeriodicCoupon(const Date& paymentDate, |
| 32 | const Date& startDate, |
| 33 | const Date& maturityDate, |
| 34 | Real nominal, |
| 35 | const ext::shared_ptr<IborIndex>& index) { |
| 36 | auto floatCpn = ext::make_shared<SubPeriodsCoupon>( |
| 37 | args: paymentDate, args&: nominal, args: startDate, args: maturityDate, args: index->fixingDays(), args: index); |
| 38 | floatCpn->setPricer( |
| 39 | ext::shared_ptr<FloatingRateCouponPricer>(new CompoundingRatePricer)); |
| 40 | return floatCpn; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | ZeroCouponSwap::ZeroCouponSwap(Type type, |
| 45 | Real baseNominal, |
| 46 | const Date& startDate, |
| 47 | const Date& maturityDate, |
| 48 | ext::shared_ptr<IborIndex> iborIndex, |
| 49 | const Calendar& paymentCalendar, |
| 50 | BusinessDayConvention paymentConvention, |
| 51 | Natural paymentDelay) |
| 52 | : Swap(2), type_(type), baseNominal_(baseNominal), iborIndex_(std::move(iborIndex)), |
| 53 | startDate_(startDate), maturityDate_(maturityDate) { |
| 54 | |
| 55 | QL_REQUIRE(!(baseNominal < 0.0), "base nominal cannot be negative" ); |
| 56 | QL_REQUIRE(startDate < maturityDate, |
| 57 | "start date (" << startDate |
| 58 | << ") later than or equal to maturity date (" |
| 59 | << maturityDate << ")" ); |
| 60 | |
| 61 | paymentDate_ = paymentCalendar.advance(maturityDate, n: paymentDelay, unit: Days, convention: paymentConvention); |
| 62 | |
| 63 | legs_[1].push_back(x: compoundedSubPeriodicCoupon(paymentDate: paymentDate_, startDate, maturityDate, |
| 64 | nominal: baseNominal_, index: iborIndex_)); |
| 65 | for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i) |
| 66 | registerWith(h: *i); |
| 67 | |
| 68 | switch (type_) { |
| 69 | case Payer: |
| 70 | payer_[0] = -1.0; |
| 71 | payer_[1] = +1.0; |
| 72 | break; |
| 73 | case Receiver: |
| 74 | payer_[0] = +1.0; |
| 75 | payer_[1] = -1.0; |
| 76 | break; |
| 77 | default: |
| 78 | QL_FAIL("unknown zero coupon swap type" ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | ZeroCouponSwap::ZeroCouponSwap(Type type, |
| 83 | Real baseNominal, |
| 84 | const Date& startDate, |
| 85 | const Date& maturityDate, |
| 86 | Real fixedPayment, |
| 87 | ext::shared_ptr<IborIndex> iborIndex, |
| 88 | const Calendar& paymentCalendar, |
| 89 | BusinessDayConvention paymentConvention, |
| 90 | Natural paymentDelay) |
| 91 | : ZeroCouponSwap(type, |
| 92 | baseNominal, |
| 93 | startDate, |
| 94 | maturityDate, |
| 95 | std::move(iborIndex), |
| 96 | paymentCalendar, |
| 97 | paymentConvention, |
| 98 | paymentDelay) { |
| 99 | |
| 100 | legs_[0].push_back( |
| 101 | x: ext::shared_ptr<CashFlow>(new SimpleCashFlow(fixedPayment, paymentDate_))); |
| 102 | } |
| 103 | |
| 104 | ZeroCouponSwap::ZeroCouponSwap(Type type, |
| 105 | Real baseNominal, |
| 106 | const Date& startDate, |
| 107 | const Date& maturityDate, |
| 108 | Rate fixedRate, |
| 109 | const DayCounter& fixedDayCounter, |
| 110 | ext::shared_ptr<IborIndex> iborIndex, |
| 111 | const Calendar& paymentCalendar, |
| 112 | BusinessDayConvention paymentConvention, |
| 113 | Natural paymentDelay) |
| 114 | : ZeroCouponSwap(type, |
| 115 | baseNominal, |
| 116 | startDate, |
| 117 | maturityDate, |
| 118 | std::move(iborIndex), |
| 119 | paymentCalendar, |
| 120 | paymentConvention, |
| 121 | paymentDelay) { |
| 122 | |
| 123 | InterestRate interest(fixedRate, fixedDayCounter, Compounded, Annual); |
| 124 | legs_[0].push_back(x: ext::shared_ptr<CashFlow>( |
| 125 | new FixedRateCoupon(paymentDate_, baseNominal_, interest, startDate, maturityDate))); |
| 126 | } |
| 127 | |
| 128 | Real ZeroCouponSwap::fixedLegNPV() const { |
| 129 | return legNPV(j: 0); |
| 130 | } |
| 131 | |
| 132 | Real ZeroCouponSwap::floatingLegNPV() const { |
| 133 | return legNPV(j: 1); |
| 134 | } |
| 135 | |
| 136 | Real ZeroCouponSwap::fairFixedPayment() const { |
| 137 | // Knowing that for the fair payment NPV = 0.0, where: |
| 138 | // NPV = (discount at fixed amount pay date) * (payer\receiver * fixed amount) |
| 139 | // + (discount at float amount pay date) * (-payer\receiver * float amount) |
| 140 | // we have: |
| 141 | // fair amount = NPV float / discount at fixed amount pay date |
| 142 | // with NPV float corrected for the payer sign. |
| 143 | Real scaling = payer(j: 1) ? -1.0 : 1.0; |
| 144 | return floatingLegNPV() / (endDiscounts(j: 0) * scaling); |
| 145 | } |
| 146 | |
| 147 | Rate ZeroCouponSwap::fairFixedRate(const DayCounter& dayCounter) const { |
| 148 | // Given the relation between the fixed payment (N^FIX) and the fixed rate (R), |
| 149 | // N^FIX = N * [(1 + R)^T - 1], |
| 150 | // the compound factor C = (1 + R)^T |
| 151 | // can be equivalently expressed as: |
| 152 | // C = N^FIX / N + 1 |
| 153 | Real compound = fairFixedPayment() / baseNominal_ + 1.0; |
| 154 | return InterestRate::impliedRate(compound, resultDC: dayCounter, comp: Compounded, freq: Annual, d1: startDate_, |
| 155 | d2: maturityDate_); |
| 156 | } |
| 157 | |
| 158 | const Leg& ZeroCouponSwap::fixedLeg() const { return leg(j: 0); } |
| 159 | |
| 160 | const Leg& ZeroCouponSwap::floatingLeg() const { return leg(j: 1); } |
| 161 | |
| 162 | Real ZeroCouponSwap::fixedPayment() const { return fixedLeg()[0]->amount(); } |
| 163 | } |
| 164 | |