-
Notifications
You must be signed in to change notification settings - Fork 2k
Const Notional Cross Currency Swaps #2248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
paolodelia99
wants to merge
18
commits into
lballabio:master
Choose a base branch
from
paolodelia99:feature/const-notional-xccy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
af4d044
Added base CrossCcySwap, CrossCcySwapEngine and related tests
paolodelia99 48e48aa
Update generated headers
github-actions[bot] 3732556
Update CrossCcySwap tests to account for usingAtParCoupons setting
paolodelia99 a880201
Added missing imports in crossccyswapengine.hpp
paolodelia99 5884906
Fix tag typo in QuantLib.vcxproj
paolodelia99 2d8185c
Refactor xccy tests file
paolodelia99 63f267f
Add crossccyfixfloatswap
paolodelia99 960ab15
Fixed typo in comments
paolodelia99 59df14c
add usingAtParCoupons, and fix code format in crossccyfixfloatswap.cpp
paolodelia99 9b62fab
Update generated headers
github-actions[bot] a65859e
Update copyright list in license
github-actions[bot] ecdda37
Add missing import in crossccyfixfloatswap tests
paolodelia99 cecf124
Added crossccybasisswap and related tests
paolodelia99 77e673f
Fix typo in crossccybasisswap filename in Makefile.am
paolodelia99 e35c609
Update generated headers
github-actions[bot] 496d95d
Add constructor docstring in crossccybasisswap.hpp
paolodelia99 9c82a36
Add constructor docstring in crossccyfixfloatswap.hpp
paolodelia99 68c8353
Rename xccyswapengine constructor params
paolodelia99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Added base CrossCcySwap, CrossCcySwapEngine and related tests
- Loading branch information
commit af4d04446a3313501132ad696639b57bb71a7fc3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
||
/* | ||
Copyright (C) 2016 Quaternion Risk Management Ltd | ||
All rights reserved. | ||
|
||
This file is part of QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
|
||
QuantLib is free software: you can redistribute it and/or modify it | ||
under the terms of the QuantLib license. You should have received a | ||
copy of the license along with this program; if not, please email | ||
<[email protected]>. The license is also available online at | ||
<http://quantlib.org/license.shtml>. | ||
|
||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
|
||
#include <ql/instruments/crossccyswap.hpp> | ||
|
||
namespace QuantLib { | ||
|
||
CrossCcySwap::CrossCcySwap(const Leg& firstLeg, const Currency& firstLegCcy, const Leg& secondLeg, | ||
const Currency& secondLegCcy) | ||
: Swap(firstLeg, secondLeg), inCcyLegNPV_(2, 0.0), inCcyLegBPS_(2, 0.0), npvDateDiscounts_(2, 0.0) { | ||
currencies_.resize(2); | ||
currencies_[0] = firstLegCcy; | ||
currencies_[1] = secondLegCcy; | ||
} | ||
|
||
CrossCcySwap::CrossCcySwap(const std::vector<Leg>& legs, const std::vector<bool>& payer, | ||
const std::vector<Currency>& currencies) | ||
: Swap(legs, payer), currencies_(currencies), inCcyLegNPV_(legs.size(), 0.0), inCcyLegBPS_(legs.size(), 0.0), npvDateDiscounts_(legs.size(), 0.0) { | ||
QL_REQUIRE(payer.size() == currencies_.size(), "Size mismatch " | ||
"between payer (" | ||
<< payer.size() << ") and currencies (" << currencies_.size() | ||
<< ")"); | ||
} | ||
|
||
CrossCcySwap::CrossCcySwap(Size legs) | ||
: Swap(legs), currencies_(legs), inCcyLegNPV_(legs, 0.0), inCcyLegBPS_(legs, 0.0), npvDateDiscounts_(legs, 0.0) {} | ||
|
||
void CrossCcySwap::setupArguments(PricingEngine::arguments* args) const { | ||
|
||
Swap::setupArguments(args); | ||
|
||
CrossCcySwap::arguments* arguments = dynamic_cast<CrossCcySwap::arguments*>(args); | ||
QL_REQUIRE(arguments, "The arguments are not of type " | ||
"cross currency swap"); | ||
|
||
arguments->currencies = currencies_; | ||
} | ||
|
||
void CrossCcySwap::fetchResults(const PricingEngine::results* r) const { | ||
|
||
Swap::fetchResults(r); | ||
|
||
const CrossCcySwap::results* results = dynamic_cast<const CrossCcySwap::results*>(r); | ||
QL_REQUIRE(results, "The results are not of type " | ||
"cross currency swap"); | ||
|
||
if (!results->inCcyLegNPV.empty()) { | ||
QL_REQUIRE(results->inCcyLegNPV.size() == inCcyLegNPV_.size(), | ||
"Wrong number of in currency leg NPVs returned by engine"); | ||
inCcyLegNPV_ = results->inCcyLegNPV; | ||
} else { | ||
std::fill(inCcyLegNPV_.begin(), inCcyLegNPV_.end(), Null<Real>()); | ||
} | ||
|
||
if (!results->inCcyLegBPS.empty()) { | ||
QL_REQUIRE(results->inCcyLegBPS.size() == inCcyLegBPS_.size(), | ||
"Wrong number of in currency leg BPSs returned by engine"); | ||
inCcyLegBPS_ = results->inCcyLegBPS; | ||
} else { | ||
std::fill(inCcyLegBPS_.begin(), inCcyLegBPS_.end(), Null<Real>()); | ||
} | ||
|
||
if (!results->npvDateDiscounts.empty()) { | ||
QL_REQUIRE(results->npvDateDiscounts.size() == npvDateDiscounts_.size(), | ||
"Wrong number of npv date discounts returned by engine"); | ||
npvDateDiscounts_ = results->npvDateDiscounts; | ||
} else { | ||
std::fill(npvDateDiscounts_.begin(), npvDateDiscounts_.end(), Null<DiscountFactor>()); | ||
} | ||
} | ||
|
||
void CrossCcySwap::setupExpired() const { | ||
Swap::setupExpired(); | ||
std::fill(inCcyLegBPS_.begin(), inCcyLegBPS_.end(), 0.0); | ||
std::fill(inCcyLegNPV_.begin(), inCcyLegNPV_.end(), 0.0); | ||
std::fill(npvDateDiscounts_.begin(), npvDateDiscounts_.end(), 0.0); | ||
} | ||
|
||
void CrossCcySwap::arguments::validate() const { | ||
Swap::arguments::validate(); | ||
QL_REQUIRE(legs.size() == currencies.size(), "Number of legs is not equal to number of currencies"); | ||
} | ||
|
||
void CrossCcySwap::results::reset() { | ||
Swap::results::reset(); | ||
inCcyLegNPV.clear(); | ||
inCcyLegBPS.clear(); | ||
npvDateDiscounts.clear(); | ||
} | ||
} // namespace QuantLib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
||
/* | ||
Copyright (C) 2016 Quaternion Risk Management Ltd | ||
All rights reserved. | ||
|
||
This file is part of QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
|
||
QuantLib is free software: you can redistribute it and/or modify it | ||
under the terms of the QuantLib license. You should have received a | ||
copy of the license along with this program; if not, please email | ||
<[email protected]>. The license is also available online at | ||
<http://quantlib.org/license.shtml>. | ||
|
||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
|
||
/*! \file crossccyswap.hpp | ||
\brief Swap instrument with legs involving two currencies | ||
|
||
\ingroup instruments | ||
*/ | ||
|
||
#ifndef quantlib_cross_ccy_swap_hpp | ||
#define quantlib_cross_ccy_swap_hpp | ||
|
||
#include <ql/currency.hpp> | ||
#include <ql/instruments/swap.hpp> | ||
|
||
namespace QuantLib { | ||
|
||
//! Cross currency swap | ||
/*! The first leg holds the pay currency cashflows and second leg holds | ||
the receive currency cashflows. | ||
|
||
\ingroup instruments | ||
*/ | ||
class CrossCcySwap : public QuantLib::Swap { | ||
public: | ||
class arguments; | ||
class results; | ||
class engine; | ||
//! \name Constructors | ||
//@{ | ||
//! Constructs a cross-currency swap with two legs and their respective currencies | ||
/*! | ||
First leg is paid and the second is received. | ||
|
||
\param firstLeg The sequence of cash flows for the first leg of the swap. | ||
\param firstLegCcy The currency in which the first leg's cash flows are denominated. | ||
\param secondLeg The sequence of cash flows for the second leg of the swap. | ||
\param secondLegCcy The currency in which the second leg's cash flows are denominated. | ||
|
||
\note The notional amounts, payment schedules, and other details of each leg must be | ||
set up in the provided Leg objects before constructing the swap. | ||
*/ | ||
CrossCcySwap(const Leg& firstLeg, const Currency& firstLegCcy, const Leg& secondLeg, const Currency& secondLegCcy); | ||
//! Constructs a cross-currency swap with multiple legs and their respective currencies | ||
/*! | ||
Initializes a cross-currency swap with an arbitrary number of legs, each specified | ||
by a sequence of cash flows (Leg) and associated with its own currency. The payer vector | ||
determines the direction of each leg (payer or receiver). | ||
|
||
\param legs A vector of cash flow sequences, one for each leg of the swap. | ||
\param payer A vector of booleans indicating the direction of each leg: | ||
\c true for payer, \c false for receiver. | ||
\param currencies A vector of currencies, one for each leg, specifying the currency | ||
in which the corresponding leg's cash flows are denominated. | ||
|
||
\note The sizes of the \p legs, \p payer, and \p currencies vectors must all be equal. | ||
\warning The notional amounts, payment schedules, and other details of each leg must be | ||
set up in the provided Leg objects before constructing the swap. | ||
*/ | ||
CrossCcySwap(const std::vector<Leg>& legs, const std::vector<bool>& payer, const std::vector<Currency>& currencies); | ||
//@} | ||
//! \name Instrument interface | ||
//@{ | ||
void setupArguments(PricingEngine::arguments* args) const override; | ||
void fetchResults(const PricingEngine::results*) const override; | ||
//@} | ||
//! \name Additional interface | ||
//@{ | ||
const Currency& legCurrency(Size j) const { | ||
QL_REQUIRE(j < legs_.size(), "leg# " << j << " doesn't exist!"); | ||
return currencies_[j]; | ||
} | ||
Real inCcyLegBPS(Size j) const { | ||
QL_REQUIRE(j < legs_.size(), "leg# " << j << " doesn't exist!"); | ||
calculate(); | ||
return inCcyLegBPS_[j]; | ||
} | ||
Real inCcyLegNPV(Size j) const { | ||
QL_REQUIRE(j < legs_.size(), "leg #" << j << " doesn't exist!"); | ||
calculate(); | ||
return inCcyLegNPV_[j]; | ||
} | ||
DiscountFactor npvDateDiscounts(Size j) const { | ||
QL_REQUIRE(j < legs_.size(), "leg #" << j << " doesn't exist!"); | ||
calculate(); | ||
return npvDateDiscounts_[j]; | ||
} | ||
//@} | ||
protected: | ||
//! \name Constructors | ||
//@{ | ||
/*! This constructor can be used by derived classes that will | ||
build their legs themselves. | ||
*/ | ||
CrossCcySwap(Size legs); | ||
//@} | ||
//! \name Instrument interface | ||
//@{ | ||
void setupExpired() const override; | ||
//@} | ||
|
||
std::vector<Currency> currencies_; | ||
|
||
private: | ||
mutable std::vector<Real> inCcyLegNPV_; | ||
mutable std::vector<Real> inCcyLegBPS_; | ||
mutable std::vector<DiscountFactor> npvDateDiscounts_; | ||
}; | ||
|
||
//! \ingroup instruments | ||
class CrossCcySwap::arguments : public Swap::arguments { | ||
public: | ||
std::vector<Currency> currencies; | ||
void validate() const override; | ||
}; | ||
|
||
//! \ingroup instruments | ||
class CrossCcySwap::results : public Swap::results { | ||
public: | ||
std::vector<Real> inCcyLegNPV; | ||
std::vector<Real> inCcyLegBPS; | ||
std::vector<DiscountFactor> npvDateDiscounts; | ||
void reset() override; | ||
}; | ||
|
||
//! \ingroup instruments | ||
class CrossCcySwap::engine : public GenericEngine<CrossCcySwap::arguments, CrossCcySwap::results> {}; | ||
} // namespace QuantLib | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want an arbitrary number of legs here and the complexity that follows? @pcaspers, did you ever have a use for this in the past?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I know of, but we might break our processing if we restrict the class to 2 legs now. What kind of increased complexity do you have in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just that you have to loop over the legs, no big deal. Ok to have a vector.