Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crosscurrencyswap.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 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
22
23
24namespace ore {
25namespace data {
26
27CrossCurrencySwap::CrossCurrencySwap(const Envelope& env, const vector<LegData>& legData) : Swap(env, legData, "CrossCurrencySwap") {}
28
29CrossCurrencySwap::CrossCurrencySwap(const Envelope& env, const LegData& leg0, const LegData& leg1)
30 : Swap(env, leg0, leg1, "CrossCurrencySwap") {}
31
33 // This function will attempt to set legIndexCcy to the other ccy from the first Indexing index.
34 auto getIndexingCurrency = [this](const LegData& legData, const Currency& legCcy, Currency& legIndexCcy) {
35 vector<Indexing> indexings = legData.indexing();
36 if (!indexings.empty() && indexings.front().hasData()) {
37 Indexing indexing = indexings.front();
38 if (!boost::starts_with(indexing.index(), "FX-")) {
40 tradeType(), id(), "Trade validation (checkCrossCurrencySwap)",
41 "Could not set fixed leg currency to Indexing currency for trade validation. Index (" +
42 indexing.index() + ") should start with 'FX-'")
43 .log();
44 return;
45 }
46
47 auto index = parseFxIndex(indexing.index());
48 Currency srcCurrency = index->sourceCurrency();
49 Currency tgtCurrency = index->targetCurrency();
50
51 if (legCcy != srcCurrency && legCcy != tgtCurrency) {
52 StructuredTradeWarningMessage(tradeType(), id(), "Trade validation (checkCrossCurrencySwap)",
53 "Could not set fixed leg currency to Indexing currency for trade "
54 "validation. Expected the leg currency (" +
55 legCcy.code() +
56 ") be equal to either of the currencies in the index (" +
57 indexing.index() + ")")
58 .log();
59 return;
60 }
61
62 legIndexCcy = legCcy == srcCurrency ? tgtCurrency : srcCurrency;
63 }
64 };
65
66 // Cross Currency Swap legs must be either Fixed, Floating or Cashflow and exactly two of Fixed and/or Floating
67 vector<Size> legDataIdx;
68 for (Size i = 0; i < legData_.size(); i++) {
69 if (legData_[i].legType() == "Fixed" || legData_[i].legType() == "Floating")
70 legDataIdx.push_back(i);
71 else if (legData_[i].legType() == "Cashflow")
72 continue;
73 else
74 QL_FAIL("CrossCurrencySwap leg #" << i + 1 << " must be Fixed, Floating or Cashflow");
75 }
76 QL_REQUIRE(legDataIdx.size() == 2,
77 "A Cross Currency Swap must have 2 legs that are either Fixed or Floating: " + id());
78
79 const LegData& legData0 = legData_[legDataIdx[0]];
80 const LegData& legData1 = legData_[legDataIdx[1]];
81
82 // Check leg currencies
83 const Currency legCcy0 = parseCurrencyWithMinors(legData0.currency());
84 const Currency legCcy1 = parseCurrencyWithMinors(legData1.currency());
85
86 // Require leg currencies to be different. If they are the same, we do a further check of the underlying currencies
87 // (Indexings for Fixed leg; Floating leg index for Floating leg) and compare these.
88 if (legCcy0 == legCcy1) {
89
90 // Get relevant index currency for the first leg - defaults to the leg ccy
91 Currency legIndexCcy0 = legCcy0;
92 if (legData0.legType() == "Fixed") {
93 getIndexingCurrency(legData0, legCcy0, legIndexCcy0);
94 } else if (legData0.legType() == "Floating") {
95 auto floatingLeg = QuantLib::ext::dynamic_pointer_cast<FloatingLegData>(legData0.concreteLegData());
96 if (floatingLeg)
97 legIndexCcy0 = parseIborIndex(floatingLeg->index())->currency();
98 }
99
100 // Get relevant index currency for the second leg - defaults to the leg ccy
101 Currency legIndexCcy1 = legCcy1;
102 if (legData1.legType() == "Fixed") {
103 getIndexingCurrency(legData1, legCcy1, legIndexCcy1);
104 } else if (legData1.legType() == "Floating") {
105 auto floatingLeg = QuantLib::ext::dynamic_pointer_cast<FloatingLegData>(legData1.concreteLegData());
106 if (floatingLeg)
107 legIndexCcy1 = parseIborIndex(floatingLeg->index()) ->currency();
108 }
109
110 QL_REQUIRE(legIndexCcy0 != legIndexCcy1, "Cross currency swap legs must have different currencies.");
111 }
112}
113
114void CrossCurrencySwap::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
115
116 DLOG("CrossCurrencySwap::build() called for " << id());
117
118 Swap::build(engineFactory);
119
120 checkCrossCurrencySwap();
121}
122
123} // namespace data
124} // namespace ore
CrossCurrencySwap()
Default constructor.
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding indexing data.
Definition: indexing.hpp:39
const string & index() const
Definition: indexing.hpp:60
void log() const
generate Boost log record to pass to corresponding sinks
Definition: log.cpp:491
Serializable object holding leg data.
Definition: legdata.hpp:844
const string & currency() const
Definition: legdata.hpp:873
const std::vector< Indexing > & indexing() const
Definition: legdata.hpp:894
const string & legType() const
Definition: legdata.hpp:890
QuantLib::ext::shared_ptr< LegAdditionalData > concreteLegData() const
Definition: legdata.hpp:891
Utility classes for Structured warnings, contains the Trade ID and Type.
Serializable Swap, Single and Cross Currency.
Definition: swap.hpp:36
Cross Currency Swap data model and serialization.
QuantLib::ext::shared_ptr< IborIndex > parseIborIndex(const string &s, const Handle< YieldTermStructure > &h)
Convert std::string to QuantLib::IborIndex.
QuantLib::ext::shared_ptr< FxIndex > parseFxIndex(const string &s, const Handle< Quote > &fxSpot, const Handle< YieldTermStructure > &sourceYts, const Handle< YieldTermStructure > &targetYts, const bool useConventions)
Convert std::string to QuantExt::FxIndex.
Currency parseCurrencyWithMinors(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:310
Map text representations to QuantLib/QuantExt types.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Classes for structured trade warnings.