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 
29 namespace QuantLib {
30 
31     class Payoff;
32     class Exercise;
33 
34     //! base option class
35     class Option : public Instrument {
36       public:
37         class arguments;
38         enum Type { Put = -1,
39                     Call = 1
40         };
Option(const ext::shared_ptr<Payoff> & payoff,const ext::shared_ptr<Exercise> & exercise)41         Option(const ext::shared_ptr<Payoff>& payoff,
42                const ext::shared_ptr<Exercise>& exercise)
43         : payoff_(payoff), exercise_(exercise) {}
44         void setupArguments(PricingEngine::arguments*) const;
payoff()45         ext::shared_ptr<Payoff> payoff() { return payoff_; }
exercise()46         ext::shared_ptr<Exercise> exercise() { 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:
arguments()59         arguments() {}
validate() const60         void validate() const {
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:
reset()71         void reset() {
72             delta =  gamma = theta = vega =
73                 rho = dividendRho = Null<Real>();
74         }
75         Real delta, gamma;
76         Real theta;
77         Real vega;
78         Real rho, dividendRho;
79     };
80 
81     //! more additional %option results
82     class MoreGreeks : public virtual PricingEngine::results {
83       public:
reset()84         void reset() {
85             itmCashProbability = deltaForward = elasticity = thetaPerDay =
86                 strikeSensitivity = Null<Real>();
87         }
88         Real itmCashProbability, deltaForward, elasticity, thetaPerDay,
89              strikeSensitivity;
90     };
91 
92 
93     // inline definitions
94 
setupArguments(PricingEngine::arguments * args) const95     inline void Option::setupArguments(PricingEngine::arguments* args) const {
96         Option::arguments* arguments =
97             dynamic_cast<Option::arguments*>(args);
98         QL_REQUIRE(arguments != 0, "wrong argument type");
99 
100         arguments->payoff = payoff_;
101         arguments->exercise = exercise_;
102     }
103 
operator <<(std::ostream & out,Option::Type type)104     inline std::ostream& operator<<(std::ostream& out, Option::Type type) {
105         switch (type) {
106           case Option::Call:
107             return out << "Call";
108           case Option::Put:
109             return out << "Put";
110           default:
111             QL_FAIL("unknown option type");
112         }
113     }
114 
115 }
116 
117 
118 #endif
119