Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/vanilla/analyticeuropeanengine.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003 Ferdinando Ametrano
5
 Copyright (C) 2007 StatPro Italia srl
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <https://www.quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
#include <ql/exercise.hpp>
22
#include <ql/pricingengines/blackcalculator.hpp>
23
#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
24
#include <utility>
25
26
namespace QuantLib {
27
28
    AnalyticEuropeanEngine::AnalyticEuropeanEngine(
29
        ext::shared_ptr<GeneralizedBlackScholesProcess> process)
30
0
    : process_(std::move(process)) {
31
0
        registerWith(process_);
32
0
    }
33
34
    AnalyticEuropeanEngine::AnalyticEuropeanEngine(
35
        ext::shared_ptr<GeneralizedBlackScholesProcess> process,
36
        Handle<YieldTermStructure> discountCurve)
37
0
    : process_(std::move(process)), discountCurve_(std::move(discountCurve)) {
38
0
        registerWith(process_);
39
0
        registerWith(discountCurve_);
40
0
    }
41
42
0
    void AnalyticEuropeanEngine::calculate() const {
43
44
        // if the discount curve is not specified, we default to the
45
        // risk free rate curve embedded within the GBM process
46
0
        ext::shared_ptr<YieldTermStructure> discountPtr = 
47
0
            discountCurve_.empty() ? 
48
0
            process_->riskFreeRate().currentLink() :
49
0
            discountCurve_.currentLink();
50
51
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
52
0
                   "not an European option");
53
54
0
        ext::shared_ptr<StrikedTypePayoff> payoff =
55
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
56
0
        QL_REQUIRE(payoff, "non-striked payoff given");
57
58
0
        Real variance =
59
0
            process_->blackVolatility()->blackVariance(
60
0
                                              arguments_.exercise->lastDate(),
61
0
                                              payoff->strike());
62
0
        DiscountFactor dividendDiscount =
63
0
            process_->dividendYield()->discount(
64
0
                                             arguments_.exercise->lastDate());
65
0
        DiscountFactor df = discountPtr->discount(arguments_.exercise->lastDate());
66
0
        DiscountFactor riskFreeDiscountForFwdEstimation =
67
0
            process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
68
0
        Real spot = process_->stateVariable()->value();
69
0
        QL_REQUIRE(spot > 0.0, "negative or null underlying given");
70
0
        Real forwardPrice = spot * dividendDiscount / riskFreeDiscountForFwdEstimation;
71
72
0
        BlackCalculator black(payoff, forwardPrice, std::sqrt(variance),df);
73
74
75
0
        results_.value = black.value();
76
0
        results_.delta = black.delta(spot);
77
0
        results_.deltaForward = black.deltaForward();
78
0
        results_.elasticity = black.elasticity(spot);
79
0
        results_.gamma = black.gamma(spot);
80
81
0
        DayCounter rfdc  = discountPtr->dayCounter();
82
0
        DayCounter divdc = process_->dividendYield()->dayCounter();
83
0
        DayCounter voldc = process_->blackVolatility()->dayCounter();
84
0
        Time t = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(),
85
0
                                   arguments_.exercise->lastDate());
86
0
        results_.rho = black.rho(t);
87
88
0
        t = divdc.yearFraction(process_->dividendYield()->referenceDate(),
89
0
                               arguments_.exercise->lastDate());
90
0
        results_.dividendRho = black.dividendRho(t);
91
92
0
        t = voldc.yearFraction(process_->blackVolatility()->referenceDate(),
93
0
                               arguments_.exercise->lastDate());
94
0
        results_.vega = black.vega(t);
95
0
        try {
96
0
            results_.theta = black.theta(spot, t);
97
0
            results_.thetaPerDay =
98
0
                black.thetaPerDay(spot, t);
99
0
        } catch (Error&) {
100
0
            results_.theta = Null<Real>();
101
0
            results_.thetaPerDay = Null<Real>();
102
0
        }
103
104
0
        results_.strikeSensitivity  = black.strikeSensitivity();
105
0
        results_.itmCashProbability = black.itmCashProbability();
106
107
0
        Real tte = process_->blackVolatility()->timeFromReference(arguments_.exercise->lastDate());
108
0
        results_.additionalResults["spot"] = spot;
109
0
        results_.additionalResults["dividendDiscount"] = dividendDiscount;
110
0
        results_.additionalResults["riskFreeDiscount"] = riskFreeDiscountForFwdEstimation;
111
0
        results_.additionalResults["forward"] = forwardPrice;
112
0
        results_.additionalResults["strike"] = payoff->strike();
113
0
        results_.additionalResults["volatility"] = Real(std::sqrt(variance / tte));
114
0
        results_.additionalResults["timeToExpiry"] = tte;
115
0
    }
116
117
}
118