Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crossccyfixfloatswaphelper.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
19#include <boost/make_shared.hpp>
20#include <ql/cashflows/fixedratecoupon.hpp>
21#include <ql/math/comparison.hpp>
22#include <ql/utilities/null_deleter.hpp>
25
27using QuantLib::close;
28using QuantLib::FixedRateCoupon;
29using QuantLib::YieldTermStructure;
30
31namespace QuantExt {
32
34 const Handle<Quote>& rate, const Handle<Quote>& spotFx, Natural settlementDays, const Calendar& paymentCalendar,
35 BusinessDayConvention paymentConvention, const Period& tenor, const Currency& fixedCurrency,
36 Frequency fixedFrequency, BusinessDayConvention fixedConvention, const DayCounter& fixedDayCount,
37 const QuantLib::ext::shared_ptr<IborIndex>& index, const Handle<YieldTermStructure>& floatDiscount,
38 const Handle<Quote>& spread, bool endOfMonth)
39 : RelativeDateRateHelper(rate), spotFx_(spotFx), settlementDays_(settlementDays), paymentCalendar_(paymentCalendar),
40 paymentConvention_(paymentConvention), tenor_(tenor), fixedCurrency_(fixedCurrency),
41 fixedFrequency_(fixedFrequency), fixedConvention_(fixedConvention), fixedDayCount_(fixedDayCount), index_(index),
42 floatDiscount_(floatDiscount), spread_(spread), endOfMonth_(endOfMonth) {
43
44 QL_REQUIRE(!spotFx_.empty(), "Spot FX quote cannot be empty.");
45 QL_REQUIRE(fixedCurrency_ != index_->currency(), "Fixed currency should not equal float leg currency.");
46
47 registerWith(spotFx_);
48 registerWith(index_);
49 registerWith(floatDiscount_);
50 registerWith(spread_);
51
53}
54
56 // Maybe FX spot quote or spread quote changed
57 if (!close(spotFx_->value(), swap_->fixedNominal()) ||
58 (!spread_.empty() && !close(spread_->value(), swap_->floatSpread()))) {
60 }
61
62 // Maybe evaluation date changed. RelativeDateRateHelper will take care of this
63 // Note: if initializeDates() was called in above if statement, it will not be called
64 // again in RelativeDateRateHelper::update() because evaluationDate_ is set in
65 // initializeDates(). So, redundant instrument builds are avoided.
66 RelativeDateRateHelper::update();
67}
68
70 QL_REQUIRE(termStructure_, "Term structure needs to be set");
71 swap_->deepUpdate();
72 return swap_->fairFixedRate();
73}
74
75void CrossCcyFixFloatSwapHelper::setTermStructure(YieldTermStructure* yts) {
76 QuantLib::ext::shared_ptr<YieldTermStructure> temp(yts, null_deleter());
77 termStructureHandle_.linkTo(temp, false);
78 RelativeDateRateHelper::setTermStructure(yts);
79}
80
81void CrossCcyFixFloatSwapHelper::accept(AcyclicVisitor& v) {
82 if (Visitor<CrossCcyFixFloatSwapHelper>* v1 = dynamic_cast<Visitor<CrossCcyFixFloatSwapHelper>*>(&v))
83 v1->visit(*this);
84 else
85 RateHelper::accept(v);
86}
87
89
90 // Swap start and end
91 Date referenceDate = evaluationDate_ = Settings::instance().evaluationDate();
92 referenceDate = paymentCalendar_.adjust(referenceDate);
93 Date start = paymentCalendar_.advance(referenceDate, settlementDays_ * Days);
94 Date end = start + tenor_;
95
96 // Nominals
97 Real floatNominal = 1.0;
98 Real fixedNominal = spotFx_->value();
99
100 // Fixed schedule
101 Schedule fixedSchedule(start, end, Period(fixedFrequency_), paymentCalendar_, fixedConvention_, fixedConvention_,
102 DateGeneration::Backward, endOfMonth_);
103
104 // Float schedule
105 Schedule floatSchedule(start, end, index_->tenor(), paymentCalendar_, paymentConvention_, paymentConvention_,
106 DateGeneration::Backward, endOfMonth_);
107
108 // Create the swap
109 Natural paymentLag = 0;
110 Spread floatSpread = spread_.empty() ? 0.0 : spread_->value();
111 swap_.reset(new CrossCcyFixFloatSwap(CrossCcyFixFloatSwap::Payer, fixedNominal, fixedCurrency_, fixedSchedule, 0.0,
112 fixedDayCount_, paymentConvention_, paymentLag, paymentCalendar_, floatNominal,
113 index_->currency(), floatSchedule, index_, floatSpread, paymentConvention_,
114 paymentLag, paymentCalendar_));
115
116 earliestDate_ = swap_->startDate();
117 maturityDate_ = swap_->maturityDate();
118
119 // Swap is Payer => first leg is fixed leg
120 latestRelevantDate_ = earliestDate_;
121 for (Size i = 0; i < swap_->leg(0).size(); ++i) {
122 latestRelevantDate_ = std::max(latestRelevantDate_, swap_->leg(0)[i]->date());
123 }
124 pillarDate_ = latestDate_ = latestRelevantDate_;
125
126 // Attach engine
127 QuantLib::ext::shared_ptr<PricingEngine> engine = QuantLib::ext::make_shared<CrossCcySwapEngine>(
129 swap_->setPricingEngine(engine);
130}
131
132} // namespace QuantExt
CrossCcyFixFloatSwapHelper(const QuantLib::Handle< QuantLib::Quote > &rate, const QuantLib::Handle< QuantLib::Quote > &spotFx, QuantLib::Natural settlementDays, const QuantLib::Calendar &paymentCalendar, QuantLib::BusinessDayConvention paymentConvention, const QuantLib::Period &tenor, const QuantLib::Currency &fixedCurrency, QuantLib::Frequency fixedFrequency, QuantLib::BusinessDayConvention fixedConvention, const QuantLib::DayCounter &fixedDayCount, const QuantLib::ext::shared_ptr< QuantLib::IborIndex > &index, const QuantLib::Handle< QuantLib::YieldTermStructure > &floatDiscount, const Handle< Quote > &spread=Handle< Quote >(), bool endOfMonth=false)
QuantLib::Handle< QuantLib::Quote > spotFx_
QuantLib::BusinessDayConvention paymentConvention_
QuantLib::Handle< QuantLib::Quote > spread_
void accept(QuantLib::AcyclicVisitor &) override
QuantLib::RelinkableHandle< QuantLib::YieldTermStructure > termStructureHandle_
QuantLib::Real impliedQuote() const override
QuantLib::BusinessDayConvention fixedConvention_
QuantLib::ext::shared_ptr< CrossCcyFixFloatSwap > swap_
void setTermStructure(QuantLib::YieldTermStructure *) override
QuantLib::Handle< QuantLib::YieldTermStructure > floatDiscount_
QuantLib::ext::shared_ptr< QuantLib::IborIndex > index_
Cross currency swap engine.
Cross currency fixed vs. float swap helper.
Cross currency swap engine.
SimpleQuote & spread_
RelativeDateBootstrapHelper< YieldTermStructure > RelativeDateRateHelper