Coverage Report

Created: 2025-08-28 06:30

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