Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
forwardbond.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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/forwardbond.hpp
20 \brief Engine builder for forward bonds
21 \ingroup builders
22*/
23
24#pragma once
25
32
36
37#include <ql/termstructures/yield/zerospreadedtermstructure.hpp>
38
39#include <boost/make_shared.hpp>
40
41namespace ore {
42namespace data {
43
45 : public CachingPricingEngineBuilder<string, const string&, const Currency&, const string&, const bool,
46 const string&, const string&, const string&> {
47protected:
48 fwdBondEngineBuilder(const std::string& model, const std::string& engine)
49 : CachingEngineBuilder(model, engine, {"ForwardBond"}) {}
50
51 virtual string keyImpl(const string& id, const Currency& ccy, const string& creditCurveId, const bool hasCreditRisk,
52 const string& securityId, const string& referenceCurveId,
53 const string& incomeCurveId) override {
54
55 // id is _not_ part of the key
56 std::string returnString = ccy.code() + "_" + creditCurveId + "_" + (hasCreditRisk ? "1_" : "0_") + securityId +
57 "_" + referenceCurveId + "_" + incomeCurveId;
58
59 return returnString;
60 }
61};
62
64public:
66 : fwdBondEngineBuilder("DiscountedCashflows", "DiscountingForwardBondEngine") {}
67
68protected:
69 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const string& id, const Currency& ccy,
70 const string& creditCurveId, const bool hasCreditRisk,
71 const string& securityId, const string& referenceCurveId,
72 const string& incomeCurveId) override {
73
74 string tsperiodStr = engineParameters_.at("TimestepPeriod");
75 Period tsperiod = parsePeriod(tsperiodStr);
76 Handle<YieldTermStructure> yts = market_->yieldCurve(referenceCurveId, configuration(MarketContext::pricing));
77
78 Handle<YieldTermStructure> discountTS =
79 market_->discountCurve(ccy.code(), configuration(MarketContext::pricing));
80 // fall back on reference curve if no income curve is specified
81 Handle<YieldTermStructure> incomeTS = market_->yieldCurve(
82 incomeCurveId.empty() ? referenceCurveId : incomeCurveId, configuration(MarketContext::pricing));
83 Handle<DefaultProbabilityTermStructure> dpts;
84 // credit curve may not always be used. If credit curve ID is empty proceed without it
85 if (!creditCurveId.empty())
86 dpts =
88 ->curve();
89 Handle<Quote> recovery;
90 try {
91 recovery = market_->recoveryRate(securityId, configuration(MarketContext::pricing));
92 } catch (...) {
93 // otherwise fall back on curve recovery
94 WLOG("security specific recovery rate not found for security ID "
95 << securityId << ", falling back on the recovery rate for credit curve Id " << creditCurveId);
96 if (!creditCurveId.empty())
97 recovery = market_->recoveryRate(creditCurveId, configuration(MarketContext::pricing));
98 }
99
100 Handle<Quote> bondSpread;
101 try {
102 // spread is optional, pass empty handle to engine if not given (will be treated as 0 spread there)
103 bondSpread = market_->securitySpread(securityId, configuration(MarketContext::pricing));
104 } catch (...) {
105 }
106
107 if (!hasCreditRisk) {
108 dpts = Handle<DefaultProbabilityTermStructure>();
109 }
110
111 return QuantLib::ext::make_shared<QuantExt::DiscountingForwardBondEngine>(discountTS, incomeTS, yts, bondSpread, dpts,
112 recovery, tsperiod);
113 }
114};
115
116} // namespace data
117} // namespace ore
Abstract template engine builder class.
Abstract template EngineBuilder class that can cache engines and coupon pricers.
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const string &id, const Currency &ccy, const string &creditCurveId, const bool hasCreditRisk, const string &securityId, const string &referenceCurveId, const string &incomeCurveId) override
Definition: forwardbond.hpp:69
QuantLib::ext::shared_ptr< Market > market_
const string & engine() const
Return the engine name.
map< string, string > engineParameters_
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)
fwdBondEngineBuilder(const std::string &model, const std::string &engine)
Definition: forwardbond.hpp:48
virtual string keyImpl(const string &id, const Currency &ccy, const string &creditCurveId, const bool hasCreditRisk, const string &securityId, const string &referenceCurveId, const string &incomeCurveId) override
Definition: forwardbond.hpp:51
Pricing Engine Factory.
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Definition: parsers.cpp:171
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
market data related utilties
QuantLib::Handle< QuantExt::CreditCurve > securitySpecificCreditCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &securityId, const std::string &creditCurveId, const std::string &configuration)
Definition: marketdata.cpp:98
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Structured Trade Error class.
string conversion utilities