| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2006 Allen Kuo |
| 5 | Copyright (C) 2022 Marcin Rybacki |
| 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/bondforward.hpp> |
| 22 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 23 | #include <ql/cashflow.hpp> |
| 24 | |
| 25 | namespace QuantLib { |
| 26 | |
| 27 | BondForward::BondForward( |
| 28 | const Date& valueDate, |
| 29 | const Date& maturityDate, |
| 30 | Position::Type type, |
| 31 | Real strike, |
| 32 | Natural settlementDays, |
| 33 | const DayCounter& dayCounter, |
| 34 | const Calendar& calendar, |
| 35 | BusinessDayConvention businessDayConvention, |
| 36 | const ext::shared_ptr<Bond>& bond, |
| 37 | const Handle<YieldTermStructure>& discountCurve, |
| 38 | const Handle<YieldTermStructure>& incomeDiscountCurve) |
| 39 | : Forward(dayCounter, calendar, businessDayConvention, settlementDays, |
| 40 | ext::shared_ptr<Payoff>(new ForwardTypePayoff(type,strike)), |
| 41 | valueDate, maturityDate, discountCurve), bond_(bond) { |
| 42 | |
| 43 | incomeDiscountCurve_ = incomeDiscountCurve; |
| 44 | registerWith(h: incomeDiscountCurve_); |
| 45 | registerWith(h: bond); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | Real BondForward::cleanForwardPrice() const { |
| 50 | return forwardValue() - bond_->accruedAmount(d: maturityDate_); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | Real BondForward::forwardPrice() const { |
| 55 | return forwardValue(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | Real BondForward::spotIncome( |
| 60 | const Handle<YieldTermStructure>& incomeDiscountCurve) const { |
| 61 | |
| 62 | Real income = 0.0; |
| 63 | Date settlement = settlementDate(); |
| 64 | Leg cf = bond_->cashflows(); |
| 65 | |
| 66 | /* |
| 67 | the following assumes |
| 68 | 1. cashflows are in ascending order ! |
| 69 | 2. considers as income: all coupons paid between settlementDate() |
| 70 | and contract delivery/maturity date |
| 71 | */ |
| 72 | for (auto& i : cf) { |
| 73 | if (!i->hasOccurred(refDate: settlement, includeRefDate: false)) { |
| 74 | if (i->hasOccurred(refDate: maturityDate_, includeRefDate: false)) { |
| 75 | income += i->amount() * incomeDiscountCurve->discount(d: i->date()); |
| 76 | } else { |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return income; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | Real BondForward::spotValue() const { |
| 87 | return bond_->dirtyPrice(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void BondForward::performCalculations() const { |
| 92 | |
| 93 | underlyingSpotValue_ = spotValue(); |
| 94 | underlyingIncome_ = spotIncome(incomeDiscountCurve: incomeDiscountCurve_); |
| 95 | |
| 96 | Forward::performCalculations(); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | |