QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
quantoengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003, 2004 Ferdinando Ametrano
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_quanto_engine_hpp
26#define quantlib_quanto_engine_hpp
27
28#include <ql/instruments/payoffs.hpp>
29#include <ql/instruments/quantovanillaoption.hpp>
30#include <ql/processes/blackscholesprocess.hpp>
31#include <ql/termstructures/yield/quantotermstructure.hpp>
32#include <utility>
33
34namespace QuantLib {
35
37
48 template <class Instr, class Engine>
50 public GenericEngine<typename Instr::arguments,
51 QuantoOptionResults<typename Instr::results> > {
52 public:
53 QuantoEngine(ext::shared_ptr<GeneralizedBlackScholesProcess>,
54 Handle<YieldTermStructure> foreignRiskFreeRate,
55 Handle<BlackVolTermStructure> exchangeRateVolatility,
56 Handle<Quote> correlation);
57 void calculate() const override;
58
59 protected:
60 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
64 };
65
66
67 // template definitions
68
69 template <class Instr, class Engine>
71 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
72 Handle<YieldTermStructure> foreignRiskFreeRate,
73 Handle<BlackVolTermStructure> exchangeRateVolatility,
74 Handle<Quote> correlation)
75 : process_(std::move(process)), foreignRiskFreeRate_(std::move(foreignRiskFreeRate)),
76 exchangeRateVolatility_(std::move(exchangeRateVolatility)),
77 correlation_(std::move(correlation)) {
82 }
83
84 template <class Instr, class Engine>
86
87 // ATM exchangeRate level needed here
88 Real exchangeRateATMlevel = 1.0;
89
90 // determine strike from payoff
91 ext::shared_ptr<StrikedTypePayoff> payoff =
92 ext::dynamic_pointer_cast<StrikedTypePayoff>(
93 this->arguments_.payoff);
94 QL_REQUIRE(payoff, "non-striked payoff given");
95 Real strike = payoff->strike();
96
97 Handle<Quote> spot = process_->stateVariable();
98 QL_REQUIRE(spot->value() > 0.0, "negative or null underlying");
99 Handle<YieldTermStructure> riskFreeRate = process_->riskFreeRate();
100 // dividendTS needs modification
101 Handle<YieldTermStructure> dividendYield(
102 ext::shared_ptr<YieldTermStructure>(
103 new QuantoTermStructure(process_->dividendYield(),
104 process_->riskFreeRate(),
105 foreignRiskFreeRate_,
106 process_->blackVolatility(),
107 strike,
108 exchangeRateVolatility_,
109 exchangeRateATMlevel,
110 correlation_->value())));
111 Handle<BlackVolTermStructure> blackVol = process_->blackVolatility();
112
113 ext::shared_ptr<GeneralizedBlackScholesProcess> quantoProcess(
114 new GeneralizedBlackScholesProcess(spot, dividendYield,
115 riskFreeRate, blackVol));
116
117 ext::shared_ptr<Engine> originalEngine(new Engine(quantoProcess));
118 originalEngine->reset();
119 auto* originalArguments =
120 dynamic_cast<typename Instr::arguments*>(originalEngine->getArguments());
121 QL_REQUIRE(originalArguments, "wrong engine type");
122
123 *originalArguments = this->arguments_;
124
125 originalArguments->validate();
126 originalEngine->calculate();
127
128 const auto* originalResults =
129 dynamic_cast<const typename Instr::results*>(originalEngine->getResults());
130 QL_REQUIRE(originalResults, "wrong engine type");
131
132 this->results_.value = originalResults->value;
133 this->results_.delta = originalResults->delta;
134 this->results_.gamma = originalResults->gamma;
135 this->results_.theta = originalResults->theta;
136 if (originalResults->rho != Null<Real>() &&
137 originalResults->dividendRho != Null<Real>()) {
138 this->results_.rho = originalResults->rho +
139 originalResults->dividendRho;
140 this->results_.dividendRho = originalResults->dividendRho;
141 } else {
142 this->results_.rho = this->results_.dividendRho = Null<Real>();
143 }
144 Volatility exchangeRateFlatVol =
145 exchangeRateVolatility_->blackVol(
146 this->arguments_.exercise->lastDate(),
147 exchangeRateATMlevel);
148 if (originalResults->vega != Null<Real>()
149 && originalResults->dividendRho != Null<Real>()) {
150 this->results_.vega = originalResults->vega +
151 correlation_->value() * exchangeRateFlatVol *
152 originalResults->dividendRho;
153 } else {
154 this->results_.vega = Null<Real>();
155 }
156
157 if (originalResults->dividendRho != Null<Real>()) {
158 Volatility volatility = process_->blackVolatility()->blackVol(
159 this->arguments_.exercise->lastDate(),
160 process_->stateVariable()->value());
161 this->results_.qvega = correlation_->value() *
162 process_->blackVolatility()->blackVol(
163 this->arguments_.exercise->lastDate(),
164 process_->stateVariable()->value()) *
165 originalResults->dividendRho;
166 this->results_.qrho = - originalResults->dividendRho;
167 this->results_.qlambda = exchangeRateFlatVol *
168 volatility * originalResults->dividendRho;
169 } else {
170 this->results_.qvega = this->results_.qrho =
171 this->results_.qlambda = Null<Real>();
172 }
173 }
174
175}
176
177
178#endif
Generalized Black-Scholes stochastic process.
template base class for option pricing engines
Shared handle to an observable.
Definition: handle.hpp:41
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Handle< Quote > correlation_
Handle< YieldTermStructure > foreignRiskFreeRate_
QuantoEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >, Handle< YieldTermStructure > foreignRiskFreeRate, Handle< BlackVolTermStructure > exchangeRateVolatility, Handle< Quote > correlation)
void calculate() const override
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Handle< BlackVolTermStructure > exchangeRateVolatility_
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Definition: any.hpp:35
STL namespace.