Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/instruments/cpicapfloor.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2010, 2011 Chris Kenyon
5
 Copyright (C) 2021 Ralf Konrad Eckel
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
22
#include <ql/cashflows/cashflows.hpp>
23
#include <ql/cashflows/cashflowvectors.hpp>
24
#include <ql/cashflows/couponpricer.hpp>
25
#include <ql/cashflows/fixedratecoupon.hpp>
26
#include <ql/cashflows/iborcoupon.hpp>
27
#include <ql/cashflows/simplecashflow.hpp>
28
#include <ql/indexes/inflationindex.hpp>
29
#include <ql/instruments/cpicapfloor.hpp>
30
#include <ql/termstructures/yieldtermstructure.hpp>
31
#include <ql/time/schedule.hpp>
32
#include <utility>
33
34
35
namespace QuantLib {
36
37
    CPICapFloor::CPICapFloor(Option::Type type,
38
                             Real nominal,
39
                             const Date& startDate, // start date of contract (only)
40
                             Real baseCPI,
41
                             const Date& maturity, // this is pre-adjustment!
42
                             Calendar fixCalendar,
43
                             BusinessDayConvention fixConvention,
44
                             Calendar payCalendar,
45
                             BusinessDayConvention payConvention,
46
                             Rate strike,
47
                             ext::shared_ptr<ZeroInflationIndex>  index,
48
                             const Period& observationLag,
49
                             CPI::InterpolationType observationInterpolation)
50
0
    : type_(type), nominal_(nominal), startDate_(startDate), baseCPI_(baseCPI), maturity_(maturity),
51
0
      fixCalendar_(std::move(fixCalendar)), fixConvention_(fixConvention),
52
0
      payCalendar_(std::move(payCalendar)), payConvention_(payConvention), strike_(strike),
53
0
      index_(std::move(index)), observationLag_(observationLag),
54
0
      observationInterpolation_(observationInterpolation) {
55
0
        QL_REQUIRE(index_, "no inflation index passed");
56
0
        QL_REQUIRE(fixCalendar_ != Calendar(), "no fixing calendar passed");
57
0
        QL_REQUIRE(payCalendar_ != Calendar(), "no payment calendar passed");
58
59
0
        if (!detail::CPI::isInterpolated(observationInterpolation_)) {
60
0
            QL_REQUIRE(observationLag_ >= index_->availabilityLag(),
61
0
                       "CPIcapfloor's observationLag must be at least availabilityLag of inflation index: "
62
0
                       <<"when the observation is effectively flat"
63
0
                       << observationLag_ << " vs " << index_->availabilityLag());
64
0
        } else {
65
0
            QL_REQUIRE(observationLag_ > index_->availabilityLag(),
66
0
                       "CPIcapfloor's observationLag must be greater than availabilityLag of inflation index: "
67
0
                       <<"when the observation is effectively linear"
68
0
                       << observationLag_ << " vs " << index_->availabilityLag());
69
0
        }
70
0
    }
Unexecuted instantiation: QuantLib::CPICapFloor::CPICapFloor(QuantLib::Option::Type, double, QuantLib::Date const&, double, QuantLib::Date const&, QuantLib::Calendar, QuantLib::BusinessDayConvention, QuantLib::Calendar, QuantLib::BusinessDayConvention, double, boost::shared_ptr<QuantLib::ZeroInflationIndex>, QuantLib::Period const&, QuantLib::CPI::InterpolationType)
Unexecuted instantiation: QuantLib::CPICapFloor::CPICapFloor(QuantLib::Option::Type, double, QuantLib::Date const&, double, QuantLib::Date const&, QuantLib::Calendar, QuantLib::BusinessDayConvention, QuantLib::Calendar, QuantLib::BusinessDayConvention, double, boost::shared_ptr<QuantLib::ZeroInflationIndex>, QuantLib::Period const&, QuantLib::CPI::InterpolationType)
71
72
73
    // when you fix - but remember that there is an observation interpolation factor as well
74
0
    Date CPICapFloor::fixingDate() const {
75
0
        return fixCalendar_.adjust(maturity_ - observationLag_, fixConvention_);
76
0
    }
77
78
79
0
    Date CPICapFloor::payDate() const {
80
0
        return payCalendar_.adjust(maturity_, payConvention_);
81
0
    }
82
83
84
0
    bool CPICapFloor::isExpired() const {
85
0
        return (Settings::instance().evaluationDate() > maturity_);
86
0
    }
87
88
89
0
    void CPICapFloor::arguments::validate() const {
90
        // nothing yet
91
0
    }
92
93
94
0
    void CPICapFloor::setupArguments(PricingEngine::arguments* args) const {
95
96
        // correct PricingEngine?
97
0
        auto* arguments = dynamic_cast<CPICapFloor::arguments*>(args);
98
0
        QL_REQUIRE(arguments != nullptr, "wrong argument type, not CPICapFloor::arguments*");
99
100
        // data move
101
0
        arguments->type = type_;
102
0
        arguments->nominal = nominal_;
103
0
        arguments->startDate = startDate_;
104
0
        arguments->baseCPI = baseCPI_;
105
0
        arguments->maturity = maturity_;
106
0
        arguments->fixCalendar = fixCalendar_;
107
0
        arguments->fixConvention = fixConvention_;
108
0
        arguments->payCalendar = payCalendar_;
109
0
        arguments->payConvention = payConvention_;
110
0
        arguments->fixDate = fixingDate();
111
0
        arguments->payDate = payDate();
112
0
        arguments->strike = strike_;
113
0
        arguments->index = index_;
114
0
        arguments->observationLag = observationLag_;
115
0
        arguments->observationInterpolation = observationInterpolation_;
116
0
    }
117
118
}