Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/pricingengines/vanilla/analyticdividendeuropeanengine.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) 2004, 2007 StatPro Italia 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
#include <ql/exercise.hpp>
21
#include <ql/pricingengines/blackcalculator.hpp>
22
#include <ql/pricingengines/vanilla/analyticdividendeuropeanengine.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    AnalyticDividendEuropeanEngine::AnalyticDividendEuropeanEngine(
28
        ext::shared_ptr<GeneralizedBlackScholesProcess> process,
29
        DividendSchedule dividends)
30
0
    : process_(std::move(process)), dividends_(std::move(dividends)) {
31
0
        registerWith(process_);
32
0
    }
33
34
0
    void AnalyticDividendEuropeanEngine::calculate() const {
35
36
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
37
0
                   "not an European option");
38
39
0
        ext::shared_ptr<StrikedTypePayoff> payoff =
40
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
41
0
        QL_REQUIRE(payoff, "non-striked payoff given");
42
43
0
        Date settlementDate = process_->riskFreeRate()->referenceDate();
44
0
        Real riskless = 0.0;
45
0
        Size i;
46
0
        for (i=0; i<dividends_.size(); i++) {
47
0
            const Date cashFlowDate = dividends_[i]->date();
48
49
0
            if (   cashFlowDate >= settlementDate
50
0
                && cashFlowDate <= arguments_.exercise->lastDate()) {
51
52
0
                riskless += dividends_[i]->amount() *
53
0
                    process_->riskFreeRate()->discount(cashFlowDate) /
54
0
                    process_->dividendYield()->discount(cashFlowDate);
55
0
            }
56
0
        }
57
58
0
        Real spot = process_->stateVariable()->value() - riskless;
59
0
        QL_REQUIRE(spot > 0.0,
60
0
                   "negative or null underlying after subtracting dividends");
61
62
0
        DiscountFactor dividendDiscount =
63
0
            process_->dividendYield()->discount(
64
0
                                             arguments_.exercise->lastDate());
65
0
        DiscountFactor riskFreeDiscount =
66
0
            process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
67
0
        Real forwardPrice = spot * dividendDiscount / riskFreeDiscount;
68
69
0
        Real variance =
70
0
            process_->blackVolatility()->blackVariance(
71
0
                                              arguments_.exercise->lastDate(),
72
0
                                              payoff->strike());
73
74
0
        BlackCalculator black(payoff, forwardPrice, std::sqrt(variance),
75
0
                              riskFreeDiscount);
76
77
0
        results_.value = black.value();
78
0
        results_.delta = black.delta(spot);
79
0
        results_.gamma = black.gamma(spot);
80
81
0
        DayCounter rfdc = process_->riskFreeRate()->dayCounter();
82
0
        DayCounter dydc = process_->dividendYield()->dayCounter();
83
0
        DayCounter voldc = process_->blackVolatility()->dayCounter();
84
0
        Time t = voldc.yearFraction(
85
0
                                 process_->blackVolatility()->referenceDate(),
86
0
                                 arguments_.exercise->lastDate());
87
0
        results_.vega = black.vega(t);
88
89
0
        Real delta_theta = 0.0, delta_rho = 0.0;
90
0
        for (i = 0; i < dividends_.size(); i++) {
91
0
            Date d = dividends_[i]->date();
92
93
0
            if (   d >= settlementDate
94
0
                && d <= arguments_.exercise->lastDate()) {
95
96
0
                delta_theta -= dividends_[i]->amount() *
97
0
                  (  process_->riskFreeRate()->zeroRate(d,rfdc,Continuous,Annual).rate()
98
0
                   - process_->dividendYield()->zeroRate(d,dydc,Continuous,Annual).rate()) *
99
0
                  process_->riskFreeRate()->discount(d) /
100
0
                  process_->dividendYield()->discount(d);
101
102
0
                Time t = process_->time(d);
103
0
                delta_rho += dividends_[i]->amount() * t *
104
0
                             process_->riskFreeRate()->discount(t) /
105
0
                             process_->dividendYield()->discount(t);
106
0
            }
107
0
        }
108
0
        t = process_->time(arguments_.exercise->lastDate());
109
0
        try {
110
0
            results_.theta = black.theta(spot, t) +
111
0
                             delta_theta * black.delta(spot);
112
0
        } catch (Error&) {
113
0
            results_.theta = Null<Real>();
114
0
        }
115
116
0
        results_.rho = black.rho(t) +
117
0
                       delta_rho * black.delta(spot);
118
0
    }
119
120
}
121