Coverage Report

Created: 2025-12-08 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/bondforward.cpp
Line
Count
Source
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
 <https://www.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
0
    : Forward(dayCounter, calendar, businessDayConvention, settlementDays,
40
0
              ext::shared_ptr<Payoff>(new ForwardTypePayoff(type,strike)),
41
0
              valueDate, maturityDate, discountCurve), bond_(bond) {
42
43
0
        incomeDiscountCurve_ = incomeDiscountCurve;
44
0
        registerWith(incomeDiscountCurve_);
45
0
        registerWith(bond);
46
0
    }
Unexecuted instantiation: QuantLib::BondForward::BondForward(QuantLib::Date const&, QuantLib::Date const&, QuantLib::Position::Type, double, unsigned int, QuantLib::DayCounter const&, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, boost::shared_ptr<QuantLib::Bond> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&)
Unexecuted instantiation: QuantLib::BondForward::BondForward(QuantLib::Date const&, QuantLib::Date const&, QuantLib::Position::Type, double, unsigned int, QuantLib::DayCounter const&, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, boost::shared_ptr<QuantLib::Bond> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&)
47
48
49
0
    Real BondForward::cleanForwardPrice() const {
50
0
        return forwardValue() - bond_->accruedAmount(maturityDate_);
51
0
    }
52
53
54
0
    Real BondForward::forwardPrice() const {
55
0
        return forwardValue();
56
0
    }
57
58
59
    Real BondForward::spotIncome(
60
0
        const Handle<YieldTermStructure>& incomeDiscountCurve) const {
61
62
0
        Real income = 0.0;
63
0
        Date settlement = settlementDate();
64
0
        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
0
        for (auto& i : cf) {
73
0
            if (!i->hasOccurred(settlement, false)) {
74
0
                if (i->hasOccurred(maturityDate_, false)) {
75
0
                    income += i->amount() * incomeDiscountCurve->discount(i->date());
76
0
                } else {
77
0
                    break;
78
0
                }
79
0
            }
80
0
        }
81
82
0
        return income;
83
0
    }
84
85
86
0
    Real BondForward::spotValue() const { 
87
0
        return bond_->dirtyPrice();
88
0
    }
89
90
91
0
    void BondForward::performCalculations() const {
92
93
0
        underlyingSpotValue_ = spotValue();
94
0
        underlyingIncome_    = spotIncome(incomeDiscountCurve_);
95
96
0
        Forward::performCalculations();
97
0
    }
98
99
}
100