• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 8467932009

28 Mar 2024 01:18PM UTC coverage: 72.497% (+0.07%) from 72.426%
8467932009

Pull #1593

github

web-flow
Merge 9b4efa33c into d6f6c13a5
Pull Request #1593: allow swaptions to take OvernightIndexedSwap

103 of 127 new or added lines in 13 files covered. (81.1%)

373 existing lines in 21 files now uncovered.

54966 of 75818 relevant lines covered (72.5%)

8708317.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/ql/instruments/makeswaption.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2

3
/*
4
 Copyright (C) 2007, 2008, 2014 Ferdinando Ametrano
5
 Copyright (C) 2007 Giorgio Facchinetti
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/cashflows/cashflows.hpp>
22
#include <ql/exercise.hpp>
23
#include <ql/indexes/swapindex.hpp>
24
#include <ql/instruments/makeois.hpp>
25
#include <ql/instruments/makeswaption.hpp>
26
#include <ql/instruments/makevanillaswap.hpp>
27
#include <ql/pricingengines/swap/discountingswapengine.hpp>
28
#include <ql/optional.hpp>
29
#include <ql/settings.hpp>
30
#include <utility>
31

32
namespace QuantLib {
33

34
    MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
104✔
35
                               const Period& optionTenor,
36
                               Rate strike)
104✔
37
    : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical),
104✔
38
      settlementMethod_(Settlement::PhysicalOTC), optionTenor_(optionTenor),
104✔
39
      optionConvention_(ModifiedFollowing), fixingDate_(Null<Date>()), strike_(strike),
104✔
40
      underlyingType_(Swap::Payer), nominal_(1.0) {}
104✔
41

42
    MakeSwaption::MakeSwaption(ext::shared_ptr<SwapIndex> swapIndex,
×
43
                               const Date& fixingDate,
44
                               Rate strike)
×
45
    : swapIndex_(std::move(swapIndex)), delivery_(Settlement::Physical),
×
46
      settlementMethod_(Settlement::PhysicalOTC), optionConvention_(ModifiedFollowing),
×
47
      fixingDate_(fixingDate), strike_(strike), underlyingType_(Swap::Payer) {}
×
48

49
    MakeSwaption::operator Swaption() const {
104✔
50
        ext::shared_ptr<Swaption> swaption = *this;
104✔
51
        return *swaption;
208✔
52
    }
53

54
    MakeSwaption::operator ext::shared_ptr<Swaption>() const {
104✔
55

56
        const Calendar& fixingCalendar = swapIndex_->fixingCalendar();
104✔
57
        Date refDate = Settings::instance().evaluationDate();
104✔
58
        // if the evaluation date is not a business day
59
        // then move to the next business day
60
        refDate = fixingCalendar.adjust(refDate);
104✔
61
        if (fixingDate_ == Null<Date>())
104✔
62
            fixingDate_ = fixingCalendar.advance(refDate, optionTenor_,
104✔
63
                                                 optionConvention_);
104✔
64
        if (exerciseDate_ == Null<Date>()) {
104✔
65
            exercise_ = ext::shared_ptr<Exercise>(new
208✔
66
                EuropeanExercise(fixingDate_));
208✔
67
        } else {
68
            QL_REQUIRE(exerciseDate_ <= fixingDate_,
×
69
                       "exercise date (" << exerciseDate_ << ") must be less "
70
                       "than or equal to fixing date (" << fixingDate_ << ")");
71
            exercise_ = ext::shared_ptr<Exercise>(new
×
72
                EuropeanExercise(exerciseDate_));
×
73
        }
74

75
        Rate usedStrike;
76
        ext::shared_ptr<OvernightIndexedSwapIndex> OIswap_index = ext::dynamic_pointer_cast<OvernightIndexedSwapIndex>(swapIndex_);
104✔
77
        if (strike_ == Null<Rate>()) {
104✔
78
            // ATM on curve(s) attached to index
79
            QL_REQUIRE(!swapIndex_->forwardingTermStructure().empty(),
208✔
80
                       "null term structure set to this instance of " <<
81
                       swapIndex_->name());
82
            if (OIswap_index) {
104✔
NEW
83
                auto temp = OIswap_index->underlyingSwap(fixingDate_);
×
NEW
84
                temp->setPricingEngine(
×
NEW
85
                    ext::make_shared<DiscountingSwapEngine>(
×
NEW
86
                        swapIndex_->exogenousDiscount()
×
UNCOV
87
                        ? swapIndex_->discountingTermStructure()
×
88
                        : swapIndex_->forwardingTermStructure(),
NEW
89
                        false
×
90
                    )
91
                );
NEW
92
                usedStrike = temp->fairRate();
×
93
            } else {
94
                auto temp = swapIndex_->underlyingSwap(fixingDate_);
104✔
95
                temp->setPricingEngine(
312✔
96
                    ext::make_shared<DiscountingSwapEngine>(
208✔
97
                        swapIndex_->exogenousDiscount()
104✔
98
                        ? swapIndex_->discountingTermStructure()
104✔
99
                        : swapIndex_->forwardingTermStructure(),
100
                        false
104✔
101
                    )
102
                );
103
                usedStrike = temp->fairRate();
104✔
104
            }
105
        } else {
106
            usedStrike = strike_;
107
        }
108

109
        BusinessDayConvention bdc = swapIndex_->fixedLegConvention();
104✔
110
        if (OIswap_index) {
104✔
111
            underlyingSwap_ =
NEW
112
                (ext::shared_ptr<OvernightIndexedSwap>)(
×
NEW
113
                    MakeOIS(swapIndex_->tenor(),
×
NEW
114
                            OIswap_index->overnightIndex(), usedStrike)
×
NEW
115
                    .withEffectiveDate(swapIndex_->valueDate(fixingDate_))
×
NEW
116
                    .withPaymentCalendar(swapIndex_->fixingCalendar())
×
NEW
117
                    .withFixedLegDayCount(swapIndex_->dayCounter())
×
NEW
118
                    .withPaymentAdjustment(bdc)
×
NEW
119
                    .withFixedLegConvention(bdc)
×
NEW
120
                    .withFixedLegTerminationDateConvention(bdc)
×
NEW
121
                    .withType(underlyingType_)
×
NEW
122
                    .withNominal(nominal_)
×
NEW
123
                    );
×
124
        } else {
125
            underlyingSwap_ =
126
                (ext::shared_ptr<VanillaSwap>)(
104✔
127
                    MakeVanillaSwap(swapIndex_->tenor(),
208✔
128
                                    swapIndex_->iborIndex(), usedStrike)
208✔
129
                    .withEffectiveDate(swapIndex_->valueDate(fixingDate_))
104✔
130
                    .withFixedLegCalendar(swapIndex_->fixingCalendar())
208✔
131
                    .withFixedLegDayCount(swapIndex_->dayCounter())
104✔
132
                    .withFixedLegTenor(swapIndex_->fixedLegTenor())
208✔
133
                    .withFixedLegConvention(bdc)
104✔
134
                    .withFixedLegTerminationDateConvention(bdc)
104✔
135
                    .withType(underlyingType_)
104✔
136
                    .withNominal(nominal_)
104✔
137
                    .withIndexedCoupons(useIndexedCoupons_)
104✔
138
                    );
104✔
139
        }
140
        ext::shared_ptr<Swaption> swaption = ext::make_shared<Swaption>(
141
            underlyingSwap_, exercise_, delivery_, settlementMethod_);
104✔
142
        swaption->setPricingEngine(engine_);
104✔
143
        return swaption;
104✔
144
    }
145

146
    MakeSwaption& MakeSwaption::withSettlementType(Settlement::Type delivery) {
×
147
        delivery_ = delivery;
×
148
        return *this;
×
149
    }
150

151
    MakeSwaption& MakeSwaption::withSettlementMethod(
×
152
        Settlement::Method settlementMethod) {
153
        settlementMethod_ = settlementMethod;
×
154
        return *this;
×
155
    }
156

157
    MakeSwaption&
158
    MakeSwaption::withOptionConvention(BusinessDayConvention bdc) {
×
159
        optionConvention_ = bdc;
×
160
        return *this;
×
161
    }
162

163
    MakeSwaption& MakeSwaption::withExerciseDate(const Date& date) {
×
164
        exerciseDate_ = date;
×
165
        return *this;
×
166
    }
167

168
    MakeSwaption& MakeSwaption::withUnderlyingType(const Swap::Type type) {
×
169
        underlyingType_ = type;
×
170
        return *this;
×
171
    }
172

173
    MakeSwaption& MakeSwaption::withPricingEngine(
96✔
174
                             const ext::shared_ptr<PricingEngine>& engine) {
175
        engine_ = engine;
96✔
176
        return *this;
96✔
177
    }
178

179
    MakeSwaption& MakeSwaption::withNominal(Real n) {
×
180
        nominal_ = n;
×
181
        return *this;
×
182
    }
183

184
    MakeSwaption& MakeSwaption::withIndexedCoupons(const ext::optional<bool>& b) {
×
185
        useIndexedCoupons_ = b;
×
186
        return *this;
×
187
    }
188

189
    MakeSwaption& MakeSwaption::withAtParCoupons(bool b) {
×
190
        useIndexedCoupons_ = !b;
×
191
        return *this;
×
192
    }
193

194
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc