| 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 | : OneAssetOption(payoff, exercise), |
| 36 | barrierType_(barrierType), barrier_lo_(barrier_lo), |
| 37 | barrier_hi_(barrier_hi), rebate_(rebate) {} |
| 38 | |
| 39 | void DoubleBarrierOption::setupArguments(PricingEngine::arguments* args) const { |
| 40 | |
| 41 | OneAssetOption::setupArguments(args); |
| 42 | |
| 43 | auto* moreArgs = dynamic_cast<DoubleBarrierOption::arguments*>(args); |
| 44 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type" ); |
| 45 | moreArgs->barrierType = barrierType_; |
| 46 | moreArgs->barrier_lo = barrier_lo_; |
| 47 | moreArgs->barrier_hi = barrier_hi_; |
| 48 | moreArgs->rebate = rebate_; |
| 49 | } |
| 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 | Volatility maxVol) const { |
| 59 | |
| 60 | QL_REQUIRE(!isExpired(), "option expired" ); |
| 61 | |
| 62 | ext::shared_ptr<SimpleQuote> volQuote(new SimpleQuote); |
| 63 | |
| 64 | ext::shared_ptr<GeneralizedBlackScholesProcess> newProcess = |
| 65 | detail::ImpliedVolatilityHelper::clone(process, volQuote); |
| 66 | |
| 67 | // engines are built-in for the time being |
| 68 | std::unique_ptr<PricingEngine> engine; |
| 69 | switch (exercise_->type()) { |
| 70 | case Exercise::European: |
| 71 | engine = std::make_unique<AnalyticDoubleBarrierEngine>(args&: newProcess); |
| 72 | break; |
| 73 | case Exercise::American: |
| 74 | case Exercise::Bermudan: |
| 75 | QL_FAIL("engine not available for non-European barrier option" ); |
| 76 | break; |
| 77 | default: |
| 78 | QL_FAIL("unknown exercise type" ); |
| 79 | } |
| 80 | |
| 81 | return detail::ImpliedVolatilityHelper::calculate(instrument: *this, |
| 82 | engine: *engine, |
| 83 | volQuote&: *volQuote, |
| 84 | targetValue, |
| 85 | accuracy, |
| 86 | maxEvaluations, |
| 87 | minVol, maxVol); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | DoubleBarrierOption::arguments::arguments() |
| 92 | : barrierType(DoubleBarrier::Type(-1)), barrier_lo(Null<Real>()), |
| 93 | barrier_hi(Null<Real>()), rebate(Null<Real>()) {} |
| 94 | |
| 95 | void DoubleBarrierOption::arguments::validate() const { |
| 96 | OneAssetOption::arguments::validate(); |
| 97 | |
| 98 | QL_REQUIRE(barrierType == DoubleBarrier::KnockIn || |
| 99 | barrierType == DoubleBarrier::KnockOut || |
| 100 | barrierType == DoubleBarrier::KIKO || |
| 101 | barrierType == DoubleBarrier::KOKI, |
| 102 | "Invalid barrier type" ); |
| 103 | |
| 104 | QL_REQUIRE(barrier_lo != Null<Real>(), "no low barrier given" ); |
| 105 | QL_REQUIRE(barrier_hi != Null<Real>(), "no high barrier given" ); |
| 106 | QL_REQUIRE(rebate != Null<Real>(), "no rebate given" ); |
| 107 | } |
| 108 | |
| 109 | bool DoubleBarrierOption::engine::triggered(Real underlying) const { |
| 110 | return underlying <= arguments_.barrier_lo || underlying >= arguments_.barrier_hi; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |