| 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 | |
| 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 | /*! \file option.hpp |
| 21 | \brief Base option class |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_option_hpp |
| 25 | #define quantlib_option_hpp |
| 26 | |
| 27 | #include <ql/instrument.hpp> |
| 28 | #include <utility> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | class Payoff; |
| 33 | class Exercise; |
| 34 | |
| 35 | //! base option class |
| 36 | class Option : public Instrument { |
| 37 | public: |
| 38 | class arguments; |
| 39 | enum Type { Put = -1, |
| 40 | Call = 1 |
| 41 | }; |
| 42 | Option(ext::shared_ptr<Payoff> payoff, ext::shared_ptr<Exercise> exercise) |
| 43 | : payoff_(std::move(payoff)), exercise_(std::move(exercise)) {} |
| 44 | void setupArguments(PricingEngine::arguments*) const override; |
| 45 | ext::shared_ptr<Payoff> payoff() const { return payoff_; } |
| 46 | ext::shared_ptr<Exercise> exercise() const { return exercise_; }; |
| 47 | protected: |
| 48 | // arguments |
| 49 | ext::shared_ptr<Payoff> payoff_; |
| 50 | ext::shared_ptr<Exercise> exercise_; |
| 51 | }; |
| 52 | |
| 53 | /*! \relates Option */ |
| 54 | std::ostream& operator<<(std::ostream&, Option::Type); |
| 55 | |
| 56 | //! basic %option %arguments |
| 57 | class Option::arguments : public virtual PricingEngine::arguments { |
| 58 | public: |
| 59 | arguments() = default; |
| 60 | void validate() const override { |
| 61 | QL_REQUIRE(payoff, "no payoff given" ); |
| 62 | QL_REQUIRE(exercise, "no exercise given" ); |
| 63 | } |
| 64 | ext::shared_ptr<Payoff> payoff; |
| 65 | ext::shared_ptr<Exercise> exercise; |
| 66 | }; |
| 67 | |
| 68 | //! additional %option results |
| 69 | class Greeks : public virtual PricingEngine::results { |
| 70 | public: |
| 71 | void reset() override { delta = gamma = theta = vega = rho = dividendRho = Null<Real>(); } |
| 72 | Real delta, gamma; |
| 73 | Real theta; |
| 74 | Real vega; |
| 75 | Real rho, dividendRho; |
| 76 | }; |
| 77 | |
| 78 | //! more additional %option results |
| 79 | class MoreGreeks : public virtual PricingEngine::results { |
| 80 | public: |
| 81 | void reset() override { |
| 82 | itmCashProbability = deltaForward = elasticity = thetaPerDay = |
| 83 | strikeSensitivity = Null<Real>(); |
| 84 | } |
| 85 | Real itmCashProbability, deltaForward, elasticity, thetaPerDay, |
| 86 | strikeSensitivity; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | // inline definitions |
| 91 | |
| 92 | inline void Option::setupArguments(PricingEngine::arguments* args) const { |
| 93 | auto* arguments = dynamic_cast<Option::arguments*>(args); |
| 94 | QL_REQUIRE(arguments != nullptr, "wrong argument type" ); |
| 95 | |
| 96 | arguments->payoff = payoff_; |
| 97 | arguments->exercise = exercise_; |
| 98 | } |
| 99 | |
| 100 | inline std::ostream& operator<<(std::ostream& out, Option::Type type) { |
| 101 | switch (type) { |
| 102 | case Option::Call: |
| 103 | return out << "Call" ; |
| 104 | case Option::Put: |
| 105 | return out << "Put" ; |
| 106 | default: |
| 107 | QL_FAIL("unknown option type" ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | |
| 114 | #endif |
| 115 | |