QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
barrieroption.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Neil Firth
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>
27#include <memory>
28
29namespace QuantLib {
30
32 Barrier::Type barrierType,
33 Real barrier,
34 Real rebate,
35 const ext::shared_ptr<StrikedTypePayoff>& payoff,
36 const ext::shared_ptr<Exercise>& exercise)
37 : OneAssetOption(payoff, exercise),
38 barrierType_(barrierType), barrier_(barrier), rebate_(rebate) {}
39
41
43
44 auto* moreArgs = dynamic_cast<BarrierOption::arguments*>(args);
45 QL_REQUIRE(moreArgs != nullptr, "wrong argument type");
46 moreArgs->barrierType = barrierType_;
47 moreArgs->barrier = barrier_;
48 moreArgs->rebate = rebate_;
49 }
50
51
53 Real targetValue,
54 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
55 Real accuracy,
56 Size maxEvaluations,
57 Volatility minVol,
58 Volatility maxVol) const {
59 return impliedVolatility(targetValue, process, DividendSchedule(),
60 accuracy, maxEvaluations, minVol, maxVol);
61 }
62
64 Real targetValue,
65 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
66 const DividendSchedule& dividends,
67 Real accuracy,
68 Size maxEvaluations,
69 Volatility minVol,
70 Volatility maxVol) const {
71 QL_REQUIRE(!isExpired(), "option expired");
72
73 ext::shared_ptr<SimpleQuote> volQuote(new SimpleQuote);
74
75 ext::shared_ptr<GeneralizedBlackScholesProcess> newProcess =
77
78 // engines are built-in for the time being
79 std::unique_ptr<PricingEngine> engine;
80 switch (exercise_->type()) {
82 if (dividends.empty())
83 engine = std::make_unique<AnalyticBarrierEngine>(newProcess);
84 else
85 engine = std::make_unique<FdBlackScholesBarrierEngine>(newProcess, dividends);
86 break;
89 QL_FAIL("engine not available for non-European barrier option");
90 break;
91 default:
92 QL_FAIL("unknown exercise type");
93 }
94
96 *engine,
97 *volQuote,
98 targetValue,
99 accuracy,
100 maxEvaluations,
101 minVol, maxVol);
102 }
103
104
106 : barrierType(Barrier::Type(-1)), barrier(Null<Real>()),
107 rebate(Null<Real>()) {}
108
110 OneAssetOption::arguments::validate();
111
112 switch (barrierType) {
113 case Barrier::DownIn:
114 case Barrier::UpIn:
115 case Barrier::DownOut:
116 case Barrier::UpOut:
117 break;
118 default:
119 QL_FAIL("unknown type");
120 }
121
122 QL_REQUIRE(barrier != Null<Real>(), "no barrier given");
123 QL_REQUIRE(rebate != Null<Real>(), "no rebate given");
124 }
125
126 bool BarrierOption::engine::triggered(Real underlying) const {
127 switch (arguments_.barrierType) {
128 case Barrier::DownIn:
129 case Barrier::DownOut:
130 return underlying < arguments_.barrier;
131 case Barrier::UpIn:
132 case Barrier::UpOut:
133 return underlying > arguments_.barrier;
134 default:
135 QL_FAIL("unknown type");
136 }
137 }
138
139}
140
Analytic barrier option engines.
Barrier option on a single asset.
Arguments for barrier option calculation
Barrier-option engine base class
bool triggered(Real underlying) const
Barrier::Type barrierType_
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
BarrierOption(Barrier::Type barrierType, Real barrier, Real rebate, const ext::shared_ptr< StrikedTypePayoff > &payoff, const ext::shared_ptr< Exercise > &exercise)
virtual void setupArguments(PricingEngine::arguments *) const
Definition: instrument.cpp:45
template class providing a null value for a given type.
Definition: null.hpp:76
Base class for options on a single asset.
bool isExpired() const override
returns whether the instrument might have value greater than zero.
ext::shared_ptr< Exercise > exercise_
Definition: option.hpp:50
market element returning a stored value
Definition: simplequote.hpp:33
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)
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Option exercise classes and payoff function.
Finite-differences Black/Scholes barrier-option engine.
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
Utilities for implied-volatility calculation.
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
Placeholder for enumerated barrier types.
Definition: barriertype.hpp:35