/src/quantlib/ql/instruments/doublebarrieroption.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2015 Thema Consulting SA |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <http://quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/exercise.hpp> |
21 | | #include <ql/instruments/doublebarrieroption.hpp> |
22 | | #include <ql/instruments/impliedvolatility.hpp> |
23 | | #include <ql/pricingengines/barrier/analyticdoublebarrierengine.hpp> |
24 | | #include <memory> |
25 | | |
26 | | namespace QuantLib { |
27 | | |
28 | | DoubleBarrierOption::DoubleBarrierOption( |
29 | | DoubleBarrier::Type barrierType, |
30 | | Real barrier_lo, |
31 | | Real barrier_hi, |
32 | | Real rebate, |
33 | | const ext::shared_ptr<StrikedTypePayoff>& payoff, |
34 | | const ext::shared_ptr<Exercise>& exercise) |
35 | 0 | : OneAssetOption(payoff, exercise), |
36 | 0 | barrierType_(barrierType), barrier_lo_(barrier_lo), |
37 | 0 | barrier_hi_(barrier_hi), rebate_(rebate) {} Unexecuted instantiation: QuantLib::DoubleBarrierOption::DoubleBarrierOption(QuantLib::DoubleBarrier::Type, double, double, double, boost::shared_ptr<QuantLib::StrikedTypePayoff> const&, boost::shared_ptr<QuantLib::Exercise> const&) Unexecuted instantiation: QuantLib::DoubleBarrierOption::DoubleBarrierOption(QuantLib::DoubleBarrier::Type, double, double, double, boost::shared_ptr<QuantLib::StrikedTypePayoff> const&, boost::shared_ptr<QuantLib::Exercise> const&) |
38 | | |
39 | 0 | void DoubleBarrierOption::setupArguments(PricingEngine::arguments* args) const { |
40 | |
|
41 | 0 | OneAssetOption::setupArguments(args); |
42 | |
|
43 | 0 | auto* moreArgs = dynamic_cast<DoubleBarrierOption::arguments*>(args); |
44 | 0 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type"); |
45 | 0 | moreArgs->barrierType = barrierType_; |
46 | 0 | moreArgs->barrier_lo = barrier_lo_; |
47 | 0 | moreArgs->barrier_hi = barrier_hi_; |
48 | 0 | moreArgs->rebate = rebate_; |
49 | 0 | } |
50 | | |
51 | | |
52 | | Volatility DoubleBarrierOption::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 | |
|
60 | 0 | QL_REQUIRE(!isExpired(), "option expired"); |
61 | | |
62 | 0 | ext::shared_ptr<SimpleQuote> volQuote(new SimpleQuote); |
63 | |
|
64 | 0 | ext::shared_ptr<GeneralizedBlackScholesProcess> newProcess = |
65 | 0 | detail::ImpliedVolatilityHelper::clone(process, volQuote); |
66 | | |
67 | | // engines are built-in for the time being |
68 | 0 | std::unique_ptr<PricingEngine> engine; |
69 | 0 | switch (exercise_->type()) { |
70 | 0 | case Exercise::European: |
71 | 0 | engine = std::make_unique<AnalyticDoubleBarrierEngine>(newProcess); |
72 | 0 | break; |
73 | 0 | case Exercise::American: |
74 | 0 | case Exercise::Bermudan: |
75 | 0 | QL_FAIL("engine not available for non-European barrier option"); |
76 | 0 | break; |
77 | 0 | default: |
78 | 0 | QL_FAIL("unknown exercise type"); |
79 | 0 | } |
80 | | |
81 | 0 | return detail::ImpliedVolatilityHelper::calculate(*this, |
82 | 0 | *engine, |
83 | 0 | *volQuote, |
84 | 0 | targetValue, |
85 | 0 | accuracy, |
86 | 0 | maxEvaluations, |
87 | 0 | minVol, maxVol); |
88 | 0 | } |
89 | | |
90 | | |
91 | | DoubleBarrierOption::arguments::arguments() |
92 | 0 | : barrierType(DoubleBarrier::Type(-1)), barrier_lo(Null<Real>()), |
93 | 0 | barrier_hi(Null<Real>()), rebate(Null<Real>()) {} Unexecuted instantiation: QuantLib::DoubleBarrierOption::arguments::arguments() Unexecuted instantiation: QuantLib::DoubleBarrierOption::arguments::arguments() |
94 | | |
95 | 0 | void DoubleBarrierOption::arguments::validate() const { |
96 | 0 | OneAssetOption::arguments::validate(); |
97 | |
|
98 | 0 | QL_REQUIRE(barrierType == DoubleBarrier::KnockIn || |
99 | 0 | barrierType == DoubleBarrier::KnockOut || |
100 | 0 | barrierType == DoubleBarrier::KIKO || |
101 | 0 | barrierType == DoubleBarrier::KOKI, |
102 | 0 | "Invalid barrier type"); |
103 | | |
104 | 0 | QL_REQUIRE(barrier_lo != Null<Real>(), "no low barrier given"); |
105 | 0 | QL_REQUIRE(barrier_hi != Null<Real>(), "no high barrier given"); |
106 | 0 | QL_REQUIRE(rebate != Null<Real>(), "no rebate given"); |
107 | 0 | } |
108 | | |
109 | 0 | bool DoubleBarrierOption::engine::triggered(Real underlying) const { |
110 | 0 | return underlying <= arguments_.barrier_lo || underlying >= arguments_.barrier_hi; |
111 | 0 | } |
112 | | |
113 | | } |
114 | | |