QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
vanillaoption.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003 Ferdinando Ametrano
6 Copyright (C) 2007 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/exercise.hpp>
23#include <ql/instruments/impliedvolatility.hpp>
24#include <ql/instruments/vanillaoption.hpp>
25#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
26#include <ql/pricingengines/vanilla/analyticdividendeuropeanengine.hpp>
27#include <ql/pricingengines/vanilla/fdblackscholesvanillaengine.hpp>
28#include <memory>
29
30namespace QuantLib {
31
33 const ext::shared_ptr<StrikedTypePayoff>& payoff,
34 const ext::shared_ptr<Exercise>& exercise)
35 : OneAssetOption(payoff, exercise) {}
36
37
39 Real targetValue,
40 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
41 Real accuracy,
42 Size maxEvaluations,
43 Volatility minVol,
44 Volatility maxVol) const {
45 return impliedVolatility(targetValue, process, DividendSchedule(),
46 accuracy, maxEvaluations, minVol, maxVol);
47 }
48
50 Real targetValue,
51 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
52 const DividendSchedule& dividends,
53 Real accuracy,
54 Size maxEvaluations,
55 Volatility minVol,
56 Volatility maxVol) const {
57
58 QL_REQUIRE(!isExpired(), "option expired");
59
60 ext::shared_ptr<SimpleQuote> volQuote(new SimpleQuote);
61
62 ext::shared_ptr<GeneralizedBlackScholesProcess> newProcess =
64
65 // engines are built-in for the time being
66 std::unique_ptr<PricingEngine> engine;
67 switch (exercise_->type()) {
69 if (dividends.empty())
70 engine = std::make_unique<AnalyticEuropeanEngine>(newProcess);
71 else
72 engine = std::make_unique<AnalyticDividendEuropeanEngine>(newProcess, dividends);
73 break;
76 if (dividends.empty())
77 engine = std::make_unique<FdBlackScholesVanillaEngine>(newProcess);
78 else
79 engine = std::make_unique<FdBlackScholesVanillaEngine>(newProcess, dividends);
80 break;
81 default:
82 QL_FAIL("unknown exercise type");
83 }
84
86 *engine,
87 *volQuote,
88 targetValue,
89 accuracy,
90 maxEvaluations,
91 minVol, maxVol);
92 }
93
96
97 /* this is a workaround in case an engine is used for both vanilla
98 and dividend options. The dividends might have been set by another
99 instrument and need to be cleared. */
100 QL_DEPRECATED_DISABLE_WARNING
101 auto* arguments = dynamic_cast<DividendVanillaOption::arguments*>(args);
102 QL_DEPRECATED_ENABLE_WARNING
103 if (arguments != nullptr) {
104 arguments->cashFlow.clear();
105 }
106 }
107
108}
109
virtual void setupArguments(PricingEngine::arguments *) const
Definition: instrument.cpp:45
Base class for options on a single asset.
bool isExpired() const override
returns whether the instrument might have value greater than zero.
basic option arguments
Definition: option.hpp:57
ext::shared_ptr< Exercise > exercise_
Definition: option.hpp:50
market element returning a stored value
Definition: simplequote.hpp:33
void setupArguments(PricingEngine::arguments *) const override
Volatility impliedVolatility(Real price, const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Real accuracy=1.0e-4, Size maxEvaluations=100, Volatility minVol=1.0e-7, Volatility maxVol=4.0) const
VanillaOption(const ext::shared_ptr< StrikedTypePayoff > &, const ext::shared_ptr< Exercise > &)
static ext::shared_ptr< GeneralizedBlackScholesProcess > clone(const ext::shared_ptr< GeneralizedBlackScholesProcess > &, const ext::shared_ptr< SimpleQuote > &)
static Volatility calculate(const Instrument &instrument, const PricingEngine &engine, SimpleQuote &volQuote, Real targetValue, Real accuracy, Natural maxEvaluations, Volatility minVol, Volatility maxVol)
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule