Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commodityapo.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/commodityapo.hpp
20\brief Engine builder for commodity average price options
21\ingroup builders
22*/
23
24#pragma once
25
27
30
32
33#include <boost/make_shared.hpp>
34
35namespace ore {
36namespace data {
37
38//! Engine builder base class for Commodity Average Price Options
39/*! Pricing engines are cached by currency and underlying name
40-
41\ingroup builders
42*/
44 : public CachingPricingEngineBuilder<string, const Currency&, const string&, const string&,
45 const QuantLib::ext::shared_ptr<QuantExt::CommodityAveragePriceOption>&> {
46public:
47 CommodityApoBaseEngineBuilder(const std::string& model, const std::string& engine,
48 const std::set<std::string>& tradeTypes)
50
51protected:
52 std::string keyImpl(const Currency& ccy, const string& name, const string& id,
53 const QuantLib::ext::shared_ptr<QuantExt::CommodityAveragePriceOption>&) override {
54 return id;
55 }
56};
57
58//! Analytical Engine builder for Commodity Average Price Options
59/*! Pricing engines are cached by currency and underlying name
60-
61\ingroup builders
62*/
64public:
66 : CommodityApoBaseEngineBuilder("Black", "AnalyticalApproximation", {"CommodityAveragePriceOption"}) {}
67
68protected:
69 QuantLib::ext::shared_ptr<QuantLib::PricingEngine>
70 engineImpl(const Currency& ccy, const string& name, const string& id,
71 const QuantLib::ext::shared_ptr<QuantExt::CommodityAveragePriceOption>& apo) override {
72
73 Handle<QuantLib::BlackVolTermStructure> vol =
74 market_->commodityVolatility(name, configuration(MarketContext::pricing));
75 Handle<YieldTermStructure> yts = market_->discountCurve(ccy.code(), configuration(MarketContext::pricing));
76
77 Real beta = 0;
78 auto param = engineParameters_.find("beta");
79 if (param != engineParameters_.end())
80 beta = parseReal(param->second);
81 else {
82 ALOG("Missing engine parameter 'beta' for " << model() << " " << EngineBuilder::engine()
83 << ", using default value " << beta);
84 }
85
86 bool dontCalibrate = false;
87 if (auto g = globalParameters_.find("Calibrate"); g != globalParameters_.end()) {
88 dontCalibrate = !parseBool(g->second);
89 }
90
91 auto modelBuilder = QuantLib::ext::make_shared<CommodityApoModelBuilder>(yts, vol, apo, dontCalibrate);
92 modelBuilders_.insert(std::make_pair(id, modelBuilder));
93
94 return QuantLib::ext::make_shared<QuantExt::CommodityAveragePriceOptionAnalyticalEngine>(yts, modelBuilder->model(),
95 beta);
96 };
97};
98
99//! Monte Carlo Engine builder for Commodity Average Price Options
100/*! Pricing engines are cached by currency and underlying name
101-
102\ingroup builders
103*/
105public:
107 : CommodityApoBaseEngineBuilder("Black", "MonteCarlo",
108 {"CommodityAveragePriceOption", "CommodityAveragePriceBarrierOption"}) {}
109
110protected:
111 QuantLib::ext::shared_ptr<QuantLib::PricingEngine>
112 engineImpl(const Currency& ccy, const string& name, const string& id,
113 const QuantLib::ext::shared_ptr<QuantExt::CommodityAveragePriceOption>& apo) override {
114
115 Handle<QuantLib::BlackVolTermStructure> vol =
116 market_->commodityVolatility(name, configuration(MarketContext::pricing));
117 Handle<YieldTermStructure> yts = market_->discountCurve(ccy.code(), configuration(MarketContext::pricing));
118
119 Size samples = 10000;
120 auto param = engineParameters_.find("samples");
121 if (param != engineParameters_.end())
122 samples = parseInteger(param->second);
123 else {
124 ALOG("Missing engine parameter 'samples' for " << model() << " " << EngineBuilder::engine()
125 << ", using default value " << samples);
126 }
127
128 Real beta = 0;
129 param = engineParameters_.find("beta");
130 if (param != engineParameters_.end())
131 beta = parseReal(param->second);
132 else {
133 ALOG("Missing engine parameter 'beta' for " << model() << " " << EngineBuilder::engine()
134 << ", using default value " << beta);
135 }
136
137 bool dontCalibrate = false;
138 if (auto g = globalParameters_.find("Calibrate"); g != globalParameters_.end()) {
139 dontCalibrate = !parseBool(g->second);
140 }
141
142 auto modelBuilder = QuantLib::ext::make_shared<CommodityApoModelBuilder>(yts, vol, apo, dontCalibrate);
143 modelBuilders_.insert(std::make_pair(id, modelBuilder));
144
145 return QuantLib::ext::make_shared<QuantExt::CommodityAveragePriceOptionMonteCarloEngine>(yts, modelBuilder->model(),
146 samples, beta);
147 };
148};
149
150} // namespace data
151} // namespace ore
Abstract template engine builder class.
Abstract template EngineBuilder class that can cache engines and coupon pricers.
Analytical Engine builder for Commodity Average Price Options.
QuantLib::ext::shared_ptr< QuantLib::PricingEngine > engineImpl(const Currency &ccy, const string &name, const string &id, const QuantLib::ext::shared_ptr< QuantExt::CommodityAveragePriceOption > &apo) override
Engine builder base class for Commodity Average Price Options.
CommodityApoBaseEngineBuilder(const std::string &model, const std::string &engine, const std::set< std::string > &tradeTypes)
std::string keyImpl(const Currency &ccy, const string &name, const string &id, const QuantLib::ext::shared_ptr< QuantExt::CommodityAveragePriceOption > &) override
Monte Carlo Engine builder for Commodity Average Price Options.
QuantLib::ext::shared_ptr< QuantLib::PricingEngine > engineImpl(const Currency &ccy, const string &name, const string &id, const QuantLib::ext::shared_ptr< QuantExt::CommodityAveragePriceOption > &apo) override
QuantLib::ext::shared_ptr< Market > market_
const string & engine() const
Return the engine name.
const set< string > & tradeTypes() const
Return the possible trade types.
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)
set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders_
std::map< std::string, std::string > globalParameters_
model builder for commodityapos
Pricing Engine Factory.
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name