Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crossccyfixfloatswap.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <boost/make_shared.hpp>
22#include <ql/cashflows/fixedratecoupon.hpp>
23#include <ql/cashflows/iborcoupon.hpp>
24#include <ql/cashflows/simplecashflow.hpp>
25
26namespace QuantExt {
27
29 Type type, Real fixedNominal, const Currency& fixedCurrency, const Schedule& fixedSchedule, Rate fixedRate,
30 const DayCounter& fixedDayCount, BusinessDayConvention fixedPaymentBdc, Natural fixedPaymentLag,
31 const Calendar& fixedPaymentCalendar, Real floatNominal, const Currency& floatCurrency,
32 const Schedule& floatSchedule, const QuantLib::ext::shared_ptr<IborIndex>& floatIndex, Spread floatSpread,
33 BusinessDayConvention floatPaymentBdc, Natural floatPaymentLag, const Calendar& floatPaymentCalendar)
34 : CrossCcySwap(2), type_(type), fixedNominal_(fixedNominal), fixedCurrency_(fixedCurrency),
35 fixedSchedule_(fixedSchedule), fixedRate_(fixedRate), fixedDayCount_(fixedDayCount),
36 fixedPaymentBdc_(fixedPaymentBdc), fixedPaymentLag_(fixedPaymentLag), fixedPaymentCalendar_(fixedPaymentCalendar),
37 floatNominal_(floatNominal), floatCurrency_(floatCurrency), floatSchedule_(floatSchedule),
38 floatIndex_(floatIndex), floatSpread_(floatSpread), floatPaymentBdc_(floatPaymentBdc),
39 floatPaymentLag_(floatPaymentLag), floatPaymentCalendar_(floatPaymentCalendar) {
40
41 // Build the float leg
42 Leg floatLeg = IborLeg(floatSchedule_, floatIndex_)
43 .withNotionals(floatNominal_)
44 .withSpreads(floatSpread_)
45 .withPaymentAdjustment(floatPaymentBdc_)
46 .withPaymentLag(floatPaymentLag_)
47 .withPaymentCalendar(floatPaymentCalendar_);
48
49 // Register with each floating rate coupon
50 for (Leg::const_iterator it = floatLeg.begin(); it < floatLeg.end(); ++it)
51 registerWith(*it);
52
53 // Initial notional exchange
54 Date aDate = floatSchedule_.dates().front();
55 aDate = floatPaymentCalendar_.adjust(aDate, floatPaymentBdc_);
56 QuantLib::ext::shared_ptr<CashFlow> aCashflow = QuantLib::ext::make_shared<SimpleCashFlow>(-floatNominal_, aDate);
57 floatLeg.insert(floatLeg.begin(), aCashflow);
58
59 // Final notional exchange
60 aDate = floatLeg.back()->date();
61 aCashflow = QuantLib::ext::make_shared<SimpleCashFlow>(floatNominal_, aDate);
62 floatLeg.push_back(aCashflow);
63
64 // Build the fixed rate leg
65 Leg fixedLeg = FixedRateLeg(fixedSchedule_)
66 .withNotionals(fixedNominal_)
67 .withCouponRates(fixedRate_, fixedDayCount_)
68 .withPaymentAdjustment(fixedPaymentBdc_)
69 .withPaymentLag(fixedPaymentLag)
70 .withPaymentCalendar(fixedPaymentCalendar);
71
72 // Initial notional exchange
73 aDate = fixedSchedule_.dates().front();
74 aDate = fixedPaymentCalendar_.adjust(aDate, fixedPaymentBdc_);
75 aCashflow = QuantLib::ext::make_shared<SimpleCashFlow>(-fixedNominal_, aDate);
76 fixedLeg.insert(fixedLeg.begin(), aCashflow);
77
78 // Final notional exchange
79 aDate = fixedLeg.back()->date();
80 aCashflow = QuantLib::ext::make_shared<SimpleCashFlow>(fixedNominal_, aDate);
81 fixedLeg.push_back(aCashflow);
82
83 // Deriving from cross currency swap where:
84 // First leg should hold the pay flows
85 // Second leg should hold the receive flows
86 payer_[0] = -1.0;
87 payer_[1] = 1.0;
88 switch (type_) {
89 case Payer:
90 legs_[0] = fixedLeg;
92 legs_[1] = floatLeg;
94 break;
95 case Receiver:
96 legs_[1] = fixedLeg;
98 legs_[0] = floatLeg;
100 break;
101 default:
102 QL_FAIL("Unknown cross currency fix float swap type");
103 }
104}
105
106void CrossCcyFixFloatSwap::setupArguments(PricingEngine::arguments* a) const {
109 args->fixedRate = fixedRate_;
110 args->spread = floatSpread_;
111 }
112}
113
114void CrossCcyFixFloatSwap::fetchResults(const PricingEngine::results* r) const {
115
117
118 // Depending on the pricing engine used, we may have CrossCcyFixFloatSwap::results
119 if (const CrossCcyFixFloatSwap::results* res = dynamic_cast<const CrossCcyFixFloatSwap::results*>(r)) {
120 // If we have CrossCcyFixFloatSwap::results from the pricing engine
121 fairFixedRate_ = res->fairFixedRate;
122 fairSpread_ = res->fairSpread;
123 } else {
124 // If not, set them to Null to indicate a calculation is needed below
125 fairFixedRate_ = Null<Rate>();
126 fairSpread_ = Null<Spread>();
127 }
128
129 // Calculate fair rate and spread if they are still Null here
130 static Spread basisPoint = 1.0e-4;
131
132 Size idxFixed = type_ == Payer ? 0 : 1;
133 if (fairFixedRate_ == Null<Rate>() && legBPS_[idxFixed] != Null<Real>())
134 fairFixedRate_ = fixedRate_ - NPV_ / (legBPS_[idxFixed] / basisPoint);
135
136 Size idxFloat = type_ == Payer ? 1 : 0;
137 if (fairSpread_ == Null<Spread>() && legBPS_[idxFloat] != Null<Real>())
138 fairSpread_ = floatSpread_ - NPV_ / (legBPS_[idxFloat] / basisPoint);
139}
140
143 fairFixedRate_ = Null<Rate>();
144 fairSpread_ = Null<Spread>();
145}
146
149 QL_REQUIRE(fixedRate != Null<Rate>(), "Fixed rate cannot be null");
150 QL_REQUIRE(spread != Null<Spread>(), "Spread cannot be null");
151}
152
155 fairFixedRate = Null<Rate>();
156 fairSpread = Null<Spread>();
157}
158
159} // namespace QuantExt
QuantLib::ext::shared_ptr< QuantLib::IborIndex > floatIndex_
CrossCcyFixFloatSwap(Type type, QuantLib::Real fixedNominal, const QuantLib::Currency &fixedCurrency, const QuantLib::Schedule &fixedSchedule, QuantLib::Rate fixedRate, const QuantLib::DayCounter &fixedDayCount, QuantLib::BusinessDayConvention fixedPaymentBdc, QuantLib::Natural fixedPaymentLag, const QuantLib::Calendar &fixedPaymentCalendar, QuantLib::Real floatNominal, const QuantLib::Currency &floatCurrency, const QuantLib::Schedule &floatSchedule, const QuantLib::ext::shared_ptr< QuantLib::IborIndex > &floatIndex, QuantLib::Spread floatSpread, QuantLib::BusinessDayConvention floatPaymentBdc, QuantLib::Natural floatPaymentLag, const QuantLib::Calendar &floatPaymentCalendar)
Detailed constructor.
const QuantLib::Calendar & fixedPaymentCalendar() const
QuantLib::Rate fairFixedRate() const
void fetchResults(const QuantLib::PricingEngine::results *r) const override
QuantLib::Natural fixedPaymentLag() const
void setupArguments(QuantLib::PricingEngine::arguments *a) const override
QuantLib::BusinessDayConvention floatPaymentBdc_
QuantLib::BusinessDayConvention fixedPaymentBdc_
QuantLib::Spread fairSpread() const
void validate() const override
Cross currency swap.
void setupArguments(PricingEngine::arguments *args) const override
void setupExpired() const override
void fetchResults(const PricingEngine::results *) const override
std::vector< Currency > currencies_
Cross currency fixed vs float swap instrument.