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/instruments/oneassetoption.hpp>
23 #include <ql/exercise.hpp>
24 #include <ql/event.hpp>
25 
26 namespace QuantLib {
27 
OneAssetOption(const ext::shared_ptr<Payoff> & payoff,const ext::shared_ptr<Exercise> & exercise)28     OneAssetOption::OneAssetOption(
29         const ext::shared_ptr<Payoff>& payoff,
30         const ext::shared_ptr<Exercise>& exercise)
31     : Option(payoff, exercise) {}
32 
isExpired() const33     bool OneAssetOption::isExpired() const {
34         return detail::simple_event(exercise_->lastDate()).hasOccurred();
35     }
36 
delta() const37     Real OneAssetOption::delta() const {
38         calculate();
39         QL_REQUIRE(delta_ != Null<Real>(), "delta not provided");
40         return delta_;
41     }
42 
deltaForward() const43     Real OneAssetOption::deltaForward() const {
44         calculate();
45         QL_REQUIRE(deltaForward_ != Null<Real>(),
46                    "forward delta not provided");
47         return deltaForward_;
48     }
49 
elasticity() const50     Real OneAssetOption::elasticity() const {
51         calculate();
52         QL_REQUIRE(elasticity_ != Null<Real>(), "elasticity not provided");
53         return elasticity_;
54     }
55 
gamma() const56     Real OneAssetOption::gamma() const {
57         calculate();
58         QL_REQUIRE(gamma_ != Null<Real>(), "gamma not provided");
59         return gamma_;
60     }
61 
theta() const62     Real OneAssetOption::theta() const {
63         calculate();
64         QL_REQUIRE(theta_ != Null<Real>(), "theta not provided");
65         return theta_;
66     }
67 
thetaPerDay() const68     Real OneAssetOption::thetaPerDay() const {
69         calculate();
70         QL_REQUIRE(thetaPerDay_ != Null<Real>(), "theta per-day not provided");
71         return thetaPerDay_;
72     }
73 
vega() const74     Real OneAssetOption::vega() const {
75         calculate();
76         QL_REQUIRE(vega_ != Null<Real>(), "vega not provided");
77         return vega_;
78     }
79 
rho() const80     Real OneAssetOption::rho() const {
81         calculate();
82         QL_REQUIRE(rho_ != Null<Real>(), "rho not provided");
83         return rho_;
84     }
85 
dividendRho() const86     Real OneAssetOption::dividendRho() const {
87         calculate();
88         QL_REQUIRE(dividendRho_ != Null<Real>(), "dividend rho not provided");
89         return dividendRho_;
90     }
91 
strikeSensitivity() const92     Real OneAssetOption::strikeSensitivity() const {
93         calculate();
94         QL_REQUIRE(strikeSensitivity_ != Null<Real>(),
95                    "strike sensitivity not provided");
96         return strikeSensitivity_;
97     }
98 
itmCashProbability() const99     Real OneAssetOption::itmCashProbability() const {
100         calculate();
101         QL_REQUIRE(itmCashProbability_ != Null<Real>(),
102                    "in-the-money cash probability not provided");
103         return itmCashProbability_;
104     }
105 
setupExpired() const106     void OneAssetOption::setupExpired() const {
107         Option::setupExpired();
108         delta_ = deltaForward_ = elasticity_ = gamma_ = theta_ =
109             thetaPerDay_ = vega_ = rho_ = dividendRho_ =
110             strikeSensitivity_ = itmCashProbability_ = 0.0;
111     }
112 
fetchResults(const PricingEngine::results * r) const113     void OneAssetOption::fetchResults(const PricingEngine::results* r) const {
114         Option::fetchResults(r);
115         const Greeks* results = dynamic_cast<const Greeks*>(r);
116         QL_ENSURE(results != 0,
117                   "no greeks returned from pricing engine");
118         /* no check on null values - just copy.
119            this allows:
120            a) to decide in derived options what to do when null
121            results are returned (throw? numerical calculation?)
122            b) to implement slim engines which only calculate the
123            value---of course care must be taken not to call
124            the greeks methods when using these.
125         */
126         delta_          = results->delta;
127         gamma_          = results->gamma;
128         theta_          = results->theta;
129         vega_           = results->vega;
130         rho_            = results->rho;
131         dividendRho_    = results->dividendRho;
132 
133         const MoreGreeks* moreResults = dynamic_cast<const MoreGreeks*>(r);
134         QL_ENSURE(moreResults != 0,
135                   "no more greeks returned from pricing engine");
136         /* no check on null values - just copy.
137            this allows:
138            a) to decide in derived options what to do when null
139            results are returned (throw? numerical calculation?)
140            b) to implement slim engines which only calculate the
141            value---of course care must be taken not to call
142            the greeks methods when using these.
143         */
144         deltaForward_       = moreResults->deltaForward;
145         elasticity_         = moreResults->elasticity;
146         thetaPerDay_        = moreResults->thetaPerDay;
147         strikeSensitivity_  = moreResults->strikeSensitivity;
148         itmCashProbability_ = moreResults->itmCashProbability;
149     }
150 
151 }
152 
153