/src/quantlib/ql/instruments/barrieroption.cpp
Line | Count | Source |
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 | | <https://www.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/barrieroption.hpp> |
24 | | #include <ql/instruments/impliedvolatility.hpp> |
25 | | #include <ql/pricingengines/barrier/analyticbarrierengine.hpp> |
26 | | #include <ql/pricingengines/barrier/fdblackscholesbarrierengine.hpp> |
27 | | #include <memory> |
28 | | |
29 | | namespace QuantLib { |
30 | | |
31 | | BarrierOption::BarrierOption( |
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 | 0 | : OneAssetOption(payoff, exercise), |
38 | 0 | barrierType_(barrierType), barrier_(barrier), rebate_(rebate) {}Unexecuted instantiation: QuantLib::BarrierOption::BarrierOption(QuantLib::Barrier::Type, double, double, boost::shared_ptr<QuantLib::StrikedTypePayoff> const&, boost::shared_ptr<QuantLib::Exercise> const&) Unexecuted instantiation: QuantLib::BarrierOption::BarrierOption(QuantLib::Barrier::Type, double, double, boost::shared_ptr<QuantLib::StrikedTypePayoff> const&, boost::shared_ptr<QuantLib::Exercise> const&) |
39 | | |
40 | 0 | void BarrierOption::setupArguments(PricingEngine::arguments* args) const { |
41 | |
|
42 | 0 | OneAssetOption::setupArguments(args); |
43 | |
|
44 | 0 | auto* moreArgs = dynamic_cast<BarrierOption::arguments*>(args); |
45 | 0 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type"); |
46 | 0 | moreArgs->barrierType = barrierType_; |
47 | 0 | moreArgs->barrier = barrier_; |
48 | 0 | moreArgs->rebate = rebate_; |
49 | 0 | } |
50 | | |
51 | | |
52 | | Volatility BarrierOption::impliedVolatility( |
53 | | Real targetValue, |
54 | | const ext::shared_ptr<GeneralizedBlackScholesProcess>& process, |
55 | | Real accuracy, |
56 | | Size maxEvaluations, |
57 | | Volatility minVol, |
58 | 0 | Volatility maxVol) const { |
59 | 0 | return impliedVolatility(targetValue, process, DividendSchedule(), |
60 | 0 | accuracy, maxEvaluations, minVol, maxVol); |
61 | 0 | } |
62 | | |
63 | | Volatility BarrierOption::impliedVolatility( |
64 | | Real targetValue, |
65 | | const ext::shared_ptr<GeneralizedBlackScholesProcess>& process, |
66 | | const DividendSchedule& dividends, |
67 | | Real accuracy, |
68 | | Size maxEvaluations, |
69 | | Volatility minVol, |
70 | 0 | Volatility maxVol) const { |
71 | 0 | QL_REQUIRE(!isExpired(), "option expired"); |
72 | | |
73 | 0 | ext::shared_ptr<SimpleQuote> volQuote(new SimpleQuote); |
74 | |
|
75 | 0 | ext::shared_ptr<GeneralizedBlackScholesProcess> newProcess = |
76 | 0 | detail::ImpliedVolatilityHelper::clone(process, volQuote); |
77 | | |
78 | | // engines are built-in for the time being |
79 | 0 | std::unique_ptr<PricingEngine> engine; |
80 | 0 | switch (exercise_->type()) { |
81 | 0 | case Exercise::European: |
82 | 0 | if (dividends.empty()) |
83 | 0 | engine = std::make_unique<AnalyticBarrierEngine>(newProcess); |
84 | 0 | else |
85 | 0 | engine = std::make_unique<FdBlackScholesBarrierEngine>(newProcess, dividends); |
86 | 0 | break; |
87 | 0 | case Exercise::American: |
88 | 0 | case Exercise::Bermudan: |
89 | 0 | QL_FAIL("engine not available for non-European barrier option"); |
90 | 0 | break; |
91 | 0 | default: |
92 | 0 | QL_FAIL("unknown exercise type"); |
93 | 0 | } |
94 | | |
95 | 0 | return detail::ImpliedVolatilityHelper::calculate(*this, |
96 | 0 | *engine, |
97 | 0 | *volQuote, |
98 | 0 | targetValue, |
99 | 0 | accuracy, |
100 | 0 | maxEvaluations, |
101 | 0 | minVol, maxVol); |
102 | 0 | } |
103 | | |
104 | | |
105 | | BarrierOption::arguments::arguments() |
106 | 0 | : barrierType(Barrier::Type(-1)), barrier(Null<Real>()), |
107 | 0 | rebate(Null<Real>()) {}Unexecuted instantiation: QuantLib::BarrierOption::arguments::arguments() Unexecuted instantiation: QuantLib::BarrierOption::arguments::arguments() |
108 | | |
109 | 0 | void BarrierOption::arguments::validate() const { |
110 | 0 | OneAssetOption::arguments::validate(); |
111 | |
|
112 | 0 | switch (barrierType) { |
113 | 0 | case Barrier::DownIn: |
114 | 0 | case Barrier::UpIn: |
115 | 0 | case Barrier::DownOut: |
116 | 0 | case Barrier::UpOut: |
117 | 0 | break; |
118 | 0 | default: |
119 | 0 | QL_FAIL("unknown type"); |
120 | 0 | } |
121 | | |
122 | 0 | QL_REQUIRE(barrier != Null<Real>(), "no barrier given"); |
123 | 0 | QL_REQUIRE(rebate != Null<Real>(), "no rebate given"); |
124 | 0 | } |
125 | | |
126 | 0 | bool BarrierOption::engine::triggered(Real underlying) const { |
127 | 0 | switch (arguments_.barrierType) { |
128 | 0 | case Barrier::DownIn: |
129 | 0 | case Barrier::DownOut: |
130 | 0 | return underlying < arguments_.barrier; |
131 | 0 | case Barrier::UpIn: |
132 | 0 | case Barrier::UpOut: |
133 | 0 | return underlying > arguments_.barrier; |
134 | 0 | default: |
135 | | QL_FAIL("unknown type"); |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | | } |
140 | | |