Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
swap.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016-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
19/*! \file portfolio/builders/swap.hpp
20 \brief Engine builder for Swaps
21 \ingroup builders
22*/
23
24#pragma once
25
30
35
36#include <ql/pricingengines/swap/discountingswapengine.hpp>
37#include <ql/termstructures/yield/zerospreadedtermstructure.hpp>
38
39#include <boost/make_shared.hpp>
40
41namespace ore {
42namespace data {
43
44//! Engine Builder base class for Single Currency Swaps
45/*! Pricing engines are cached by currency
46 \ingroup builders
47*/
49 : public CachingPricingEngineBuilder<string, const Currency&, const std::string&, const std::string&> {
50public:
51 SwapEngineBuilderBase(const std::string& model, const std::string& engine)
52 : CachingEngineBuilder(model, engine, {"Swap"}) {}
53
54protected:
55 virtual string keyImpl(const Currency& ccy, const std::string& discountCurve,
56 const std::string& securitySpread) override {
57 return ccy.code() + discountCurve + securitySpread;
58 }
59};
60
61//! Engine Builder for Single Currency Swaps
62/*! This builder uses QuantLib::DiscountingSwapEngine
63 \ingroup builders
64*/
66public:
67 SwapEngineBuilder() : SwapEngineBuilderBase("DiscountedCashflows", "DiscountingSwapEngine") {}
68
69protected:
70 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const Currency& ccy, const std::string& discountCurve,
71 const std::string& securitySpread) override {
72 Handle<YieldTermStructure> yts =
73 discountCurve.empty() ? market_->discountCurve(ccy.code(), configuration(MarketContext::pricing))
75 if (!securitySpread.empty())
76 yts = Handle<YieldTermStructure>(QuantLib::ext::make_shared<ZeroSpreadedTermStructure>(
77 yts, market_->securitySpread(securitySpread, configuration(MarketContext::pricing))));
78 return QuantLib::ext::make_shared<QuantLib::DiscountingSwapEngine>(yts);
79 }
80};
81
82//! Engine Builder for Single Currency Swaps
83/*! This builder uses QuantExt::DiscountingSwapEngineMultiCurve
84 \ingroup builders
85*/
87public:
88 SwapEngineBuilderOptimised() : SwapEngineBuilderBase("DiscountedCashflows", "DiscountingSwapEngineOptimised") {}
89
90protected:
91 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const Currency& ccy, const std::string& discountCurve,
92 const std::string& securitySpread) override {
93
94 Handle<YieldTermStructure> yts =
95 discountCurve.empty() ? market_->discountCurve(ccy.code(), configuration(MarketContext::pricing))
97 if (!securitySpread.empty())
98 yts = Handle<YieldTermStructure>(QuantLib::ext::make_shared<ZeroSpreadedTermStructure>(
99 yts, market_->securitySpread(securitySpread, configuration(MarketContext::pricing))));
100 return QuantLib::ext::make_shared<QuantExt::DiscountingSwapEngineMultiCurve>(yts);
101 }
102};
103
104//! Engine Builder base class for Cross Currency Swaps
105/*! Pricing engines are cached by currencies (represented as a string list)
106
107 \ingroup builders
108*/
110 : public CachingPricingEngineBuilder<string, const std::vector<Currency>&, const Currency&> {
111public:
112 CrossCurrencySwapEngineBuilderBase(const std::string& model, const std::string& engine)
113 : CachingEngineBuilder(model, engine, {"CrossCurrencySwap"}) {}
114
115protected:
116 virtual string keyImpl(const std::vector<Currency>& ccys, const Currency& base) override {
117 std::ostringstream ccyskey;
118 ccyskey << base << "/";
119 for (Size i = 0; i < ccys.size(); ++i)
120 ccyskey << ccys[i] << ((i < ccys.size() - 1) ? "-" : "");
121 return ccyskey.str();
122 }
123};
124
125//! Discounted Cashflows Engine Builder for Cross Currency Swaps
127public:
129 : CrossCurrencySwapEngineBuilderBase("DiscountedCashflows", "DiscountingCrossCurrencySwapEngine") {}
130
131protected:
132 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const std::vector<Currency>& ccys,
133 const Currency& base) override {
134
135 std::vector<Handle<YieldTermStructure>> discountCurves;
136 std::vector<Handle<Quote>> fxQuotes;
137 std::string config = configuration(MarketContext::pricing);
138 std::string baseCcyCode = base.code();
139 for (Size i = 0; i < ccys.size(); ++i) {
140 string ccyCode = ccys[i].code();
141 discountCurves.push_back(xccyYieldCurve(market_, ccyCode, config));
142 string pair = ccyCode + baseCcyCode;
143 fxQuotes.push_back(market_->fxRate(pair, config));
144 }
145
146 return QuantLib::ext::make_shared<QuantExt::DiscountingCurrencySwapEngine>(discountCurves, fxQuotes, ccys, base);
147 }
148};
149
150//! Implementation of SwapEngineBuilderBase using MC pricer for external cam / AMC
151/*! \ingroup portfolio
152 */
154public:
155 CamAmcSwapEngineBuilder(const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
156 const std::vector<Date>& simulationDates)
157 : SwapEngineBuilderBase("CrossAssetModel", "AMC"), cam_(cam), simulationDates_(simulationDates) {}
158
159protected:
160 // the pricing engine depends on the ccy only, can use the caching from SwapEngineBuilderBase
161 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const Currency& ccy, const std::string& discountCurve,
162 const std::string& securitySpread) override;
163
164private:
165 QuantLib::ext::shared_ptr<PricingEngine> buildMcEngine(const QuantLib::ext::shared_ptr<QuantExt::LGM>& lgm,
166 const Handle<YieldTermStructure>& discountCurve,
167 const std::vector<Date>& simulationDates,
168 const std::vector<Size>& externalModelIndices);
169 const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel> cam_;
170 const std::vector<Date> simulationDates_;
171};
172
173} // namespace data
174} // namespace ore
Abstract template engine builder class.
Abstract template EngineBuilder class that can cache engines and coupon pricers.
Implementation of SwapEngineBuilderBase using MC pricer for external cam / AMC.
Definition: swap.hpp:153
const std::vector< Date > simulationDates_
Definition: swap.hpp:170
const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > cam_
Definition: swap.hpp:169
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, const std::string &discountCurve, const std::string &securitySpread) override
Definition: swap.cpp:52
QuantLib::ext::shared_ptr< PricingEngine > buildMcEngine(const QuantLib::ext::shared_ptr< QuantExt::LGM > &lgm, const Handle< YieldTermStructure > &discountCurve, const std::vector< Date > &simulationDates, const std::vector< Size > &externalModelIndices)
Definition: swap.cpp:34
CamAmcSwapEngineBuilder(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &simulationDates)
Definition: swap.hpp:155
Engine Builder base class for Cross Currency Swaps.
Definition: swap.hpp:110
CrossCurrencySwapEngineBuilderBase(const std::string &model, const std::string &engine)
Definition: swap.hpp:112
virtual string keyImpl(const std::vector< Currency > &ccys, const Currency &base) override
Definition: swap.hpp:116
Discounted Cashflows Engine Builder for Cross Currency Swaps.
Definition: swap.hpp:126
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const std::vector< Currency > &ccys, const Currency &base) override
Definition: swap.hpp:132
QuantLib::ext::shared_ptr< Market > market_
const string & engine() const
Return the engine name.
const string & model() const
Return the model name.
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
Engine Builder base class for Single Currency Swaps.
Definition: swap.hpp:49
virtual string keyImpl(const Currency &ccy, const std::string &discountCurve, const std::string &securitySpread) override
Definition: swap.hpp:55
SwapEngineBuilderBase(const std::string &model, const std::string &engine)
Definition: swap.hpp:51
Engine Builder for Single Currency Swaps.
Definition: swap.hpp:65
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, const std::string &discountCurve, const std::string &securitySpread) override
Definition: swap.hpp:70
Engine Builder for Single Currency Swaps.
Definition: swap.hpp:86
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, const std::string &discountCurve, const std::string &securitySpread) override
Definition: swap.hpp:91
Pricing Engine Factory.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
market data related utilties
Handle< YieldTermStructure > xccyYieldCurve(const QuantLib::ext::shared_ptr< Market > &market, const string &ccyCode, const string &configuration)
Definition: marketdata.cpp:41
Handle< YieldTermStructure > indexOrYieldCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &name, const std::string &configuration)
Definition: marketdata.cpp:65
Serializable Credit Default Swap.
Definition: namespaces.docs:23