Coverage Report

Created: 2025-11-04 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/forward.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
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
 <https://www.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/event.hpp>
21
#include <ql/instruments/forward.hpp>
22
#include <ql/termstructures/yieldtermstructure.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    Forward::Forward(DayCounter dayCounter,
28
                     Calendar calendar,
29
                     BusinessDayConvention businessDayConvention,
30
                     Natural settlementDays,
31
                     ext::shared_ptr<Payoff> payoff,
32
                     const Date& valueDate,
33
                     const Date& maturityDate,
34
                     Handle<YieldTermStructure> discountCurve)
35
0
    : dayCounter_(std::move(dayCounter)), calendar_(std::move(calendar)),
36
0
      businessDayConvention_(businessDayConvention), settlementDays_(settlementDays),
37
0
      payoff_(std::move(payoff)), valueDate_(valueDate), maturityDate_(maturityDate),
38
0
      discountCurve_(std::move(discountCurve)) {
39
40
0
        maturityDate_ = calendar_.adjust(maturityDate_,
41
0
                                         businessDayConvention_);
42
43
0
        registerWith(Settings::instance().evaluationDate());
44
0
        registerWith(discountCurve_);
45
0
    }
46
47
48
0
    Date Forward::settlementDate() const {
49
0
        Date d = calendar_.advance(Settings::instance().evaluationDate(),
50
0
                                   settlementDays_, Days);
51
0
        return std::max(d,valueDate_);
52
0
    }
53
54
55
0
    bool Forward::isExpired() const {
56
0
        return detail::simple_event(maturityDate_)
57
0
               .hasOccurred(settlementDate());
58
0
    }
59
60
61
0
    Real Forward::forwardValue() const {
62
0
        calculate();
63
0
        return (underlyingSpotValue_ - underlyingIncome_ )/
64
0
               discountCurve_->discount(maturityDate_);
65
0
    }
66
67
68
    InterestRate Forward::impliedYield(Real underlyingSpotValue,
69
                                       Real forwardValue,
70
                                       Date settlementDate,
71
                                       Compounding comp,
72
0
                                       const DayCounter& dayCounter) {
73
74
0
        Time t = dayCounter.yearFraction(settlementDate,maturityDate_) ;
75
0
        Real compoundingFactor = forwardValue/
76
0
            (underlyingSpotValue-spotIncome(incomeDiscountCurve_)) ;
77
0
        return InterestRate::impliedRate(compoundingFactor,
78
0
                                         dayCounter, comp, Annual,
79
0
                                         t);
80
0
    }
81
82
83
0
    void Forward::performCalculations() const {
84
85
0
        QL_REQUIRE(!discountCurve_.empty(),
86
0
                   "null term structure set to Forward");
87
88
0
        ext::shared_ptr<ForwardTypePayoff> ftpayoff =
89
0
            ext::dynamic_pointer_cast<ForwardTypePayoff>(payoff_);
90
0
        Real fwdValue = forwardValue();
91
0
        NPV_ = (*ftpayoff)(fwdValue) * discountCurve_->discount(maturityDate_);
92
0
    }
93
94
}