Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cms.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
22
23#include <boost/make_shared.hpp>
24
25using namespace QuantLib;
26
27namespace ore {
28namespace data {
29
30GFunctionFactory::YieldCurveModel ycmFromString(const string& s) {
31 if (s == "Standard")
32 return GFunctionFactory::Standard;
33 else if (s == "ExactYield")
34 return GFunctionFactory::ExactYield;
35 else if (s == "ParallelShifts")
36 return GFunctionFactory::ParallelShifts;
37 else if (s == "NonParallelShifts")
38 return GFunctionFactory::NonParallelShifts;
39 else
40 QL_FAIL("unknown string for YieldCurveModel");
41}
42
43QuantLib::ext::shared_ptr<FloatingRateCouponPricer> AnalyticHaganCmsCouponPricerBuilder::engineImpl(const string& key) {
44
45 std::string ccyCode = key;
46 QuantLib::ext::shared_ptr<IborIndex> index;
47 if (tryParseIborIndex(key, index))
48 ccyCode = index->currency().code();
49 Real rev = parseReal(engineParameter("MeanReversion", {key, ccyCode}, true));
50 string ycmstr = engineParameter("YieldCurveModel");
51 GFunctionFactory::YieldCurveModel ycm = ycmFromString(ycmstr);
52
53 Handle<Quote> revQuote(QuantLib::ext::shared_ptr<Quote>(new SimpleQuote(rev)));
54 Handle<SwaptionVolatilityStructure> vol = market_->swaptionVol(key, configuration(MarketContext::pricing));
55
56 QuantLib::ext::shared_ptr<FloatingRateCouponPricer> pricer = QuantLib::ext::make_shared<AnalyticHaganPricer>(vol, ycm, revQuote);
57
58 // Return the cached pricer
59 return pricer;
60}
61
62QuantLib::ext::shared_ptr<FloatingRateCouponPricer> NumericalHaganCmsCouponPricerBuilder::engineImpl(const string& key) {
63
64 std::string ccyCode = key;
65 QuantLib::ext::shared_ptr<IborIndex> index;
66 if (tryParseIborIndex(key, index))
67 ccyCode = index->currency().code();
68 Real rev = parseReal(engineParameter("MeanReversion", {key, ccyCode}, true));
69 string ycmstr = engineParameter("YieldCurveModel");
70 GFunctionFactory::YieldCurveModel ycm = ycmFromString(ycmstr);
71 Rate llim = parseReal(engineParameter("LowerLimit"));
72 Rate ulim = parseReal(engineParameter("UpperLimit"));
73 Real prec = parseReal(engineParameter("Precision"));
74
75 Handle<Quote> revQuote(QuantLib::ext::shared_ptr<Quote>(new SimpleQuote(rev)));
76 Handle<SwaptionVolatilityStructure> vol = market_->swaptionVol(key, configuration(MarketContext::pricing));
77
78 QuantLib::ext::shared_ptr<FloatingRateCouponPricer> pricer =
79 QuantLib::ext::make_shared<NumericHaganPricer>(vol, ycm, revQuote, llim, ulim, prec);
80
81 // Return the cached pricer
82 return pricer;
83}
84
85QuantLib::ext::shared_ptr<FloatingRateCouponPricer> LinearTSRCmsCouponPricerBuilder::engineImpl(const string& key) {
86
87 std::string ccyCode = key;
88 QuantLib::ext::shared_ptr<IborIndex> index;
89 if (tryParseIborIndex(key, index))
90 ccyCode = index->currency().code();
91 Real rev = parseReal(engineParameter("MeanReversion", {key, ccyCode}, true));
92 string policy = engineParameter("Policy");
93
94 Handle<Quote> revQuote(QuantLib::ext::shared_ptr<Quote>(new SimpleQuote(rev)));
95 Handle<SwaptionVolatilityStructure> vol = market_->swaptionVol(key, configuration(MarketContext::pricing));
96 Handle<YieldTermStructure> yts = market_->discountCurve(ccyCode, configuration(MarketContext::pricing));
97
98 string lowerBoundStr =
99 (vol->volatilityType() == ShiftedLognormal) ? "LowerRateBoundLogNormal" : "LowerRateBoundNormal";
100 string upperBoundStr =
101 (vol->volatilityType() == ShiftedLognormal) ? "UpperRateBoundLogNormal" : "UpperRateBoundNormal";
102
103 LinearTsrPricer::Settings settings;
104 if (policy == "RateBound") {
105 Real lower = parseReal(engineParameter(lowerBoundStr));
106 Real upper = parseReal(engineParameter(upperBoundStr));
107 settings.withRateBound(lower, upper);
108 } else if (policy == "VegaRatio") {
109 Real lower = parseReal(engineParameter(lowerBoundStr));
110 Real upper = parseReal(engineParameter(upperBoundStr));
111 Real vega = parseReal(engineParameter("VegaRatio"));
112 settings.withVegaRatio(vega, lower, upper);
113 } else if (policy == "PriceThreshold") {
114 Real lower = parseReal(engineParameter(lowerBoundStr));
115 Real upper = parseReal(engineParameter(upperBoundStr));
116 Real threshold = parseReal(engineParameter("PriceThreshold"));
117 settings.withPriceThreshold(threshold, lower, upper);
118 } else if (policy == "BsStdDev") {
119 Real lower = parseReal(engineParameter(lowerBoundStr));
120 Real upper = parseReal(engineParameter(upperBoundStr));
121 Real stddevs = parseReal(engineParameter("BSStdDevs"));
122 settings.withPriceThreshold(stddevs, lower, upper);
123 } else
124 QL_FAIL("unknown string for policy parameter");
125
126 QuantLib::ext::shared_ptr<FloatingRateCouponPricer> pricer =
127 QuantLib::ext::make_shared<LinearTsrPricer>(vol, revQuote, yts, settings);
128
129 // Return the cached pricer
130 return pricer;
131}
132} // namespace data
133} // namespace ore
virtual QuantLib::ext::shared_ptr< FloatingRateCouponPricer > engineImpl(const string &key) override
Definition: cms.cpp:43
QuantLib::ext::shared_ptr< Market > market_
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
virtual QuantLib::ext::shared_ptr< FloatingRateCouponPricer > engineImpl(const string &key) override
Definition: cms.cpp:85
virtual QuantLib::ext::shared_ptr< FloatingRateCouponPricer > engineImpl(const string &key) override
Definition: cms.cpp:62
builder that returns an engine to price capped floored ibor legs
bool tryParseIborIndex(const string &s, QuantLib::ext::shared_ptr< IborIndex > &index)
Try to convert std::string to QuantLib::IborIndex.
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
GFunctionFactory::YieldCurveModel ycmFromString(const string &s)
Definition: cms.cpp:30
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.