| 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 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | VanillaOption::VanillaOption( |
| 33 | const ext::shared_ptr<StrikedTypePayoff>& payoff, |
| 34 | const ext::shared_ptr<Exercise>& exercise) |
| 35 | : OneAssetOption(payoff, exercise) {} |
| 36 | |
| 37 | |
| 38 | Volatility VanillaOption::impliedVolatility( |
| 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(price: targetValue, process, dividends: DividendSchedule(), |
| 46 | accuracy, maxEvaluations, minVol, maxVol); |
| 47 | } |
| 48 | |
| 49 | Volatility VanillaOption::impliedVolatility( |
| 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 = |
| 63 | detail::ImpliedVolatilityHelper::clone(process, volQuote); |
| 64 | |
| 65 | // engines are built-in for the time being |
| 66 | std::unique_ptr<PricingEngine> engine; |
| 67 | switch (exercise_->type()) { |
| 68 | case Exercise::European: |
| 69 | if (dividends.empty()) |
| 70 | engine = std::make_unique<AnalyticEuropeanEngine>(args&: newProcess); |
| 71 | else |
| 72 | engine = std::make_unique<AnalyticDividendEuropeanEngine>(args&: newProcess, args: dividends); |
| 73 | break; |
| 74 | case Exercise::American: |
| 75 | case Exercise::Bermudan: |
| 76 | if (dividends.empty()) |
| 77 | engine = std::make_unique<FdBlackScholesVanillaEngine>(args&: newProcess); |
| 78 | else |
| 79 | engine = std::make_unique<FdBlackScholesVanillaEngine>(args&: newProcess, args: dividends); |
| 80 | break; |
| 81 | default: |
| 82 | QL_FAIL("unknown exercise type" ); |
| 83 | } |
| 84 | |
| 85 | return detail::ImpliedVolatilityHelper::calculate(instrument: *this, |
| 86 | engine: *engine, |
| 87 | volQuote&: *volQuote, |
| 88 | targetValue, |
| 89 | accuracy, |
| 90 | maxEvaluations, |
| 91 | minVol, maxVol); |
| 92 | } |
| 93 | |
| 94 | void VanillaOption::setupArguments(PricingEngine::arguments* args) const { |
| 95 | OneAssetOption::setupArguments(args); |
| 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 | |
| 110 | |