QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analyticdividendeuropeanengine.cpp
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
25namespace QuantLib {
26
28 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
29 DividendSchedule dividends)
30 : process_(std::move(process)), dividends_(std::move(dividends)),
31 explicitDividends_(true) {
33 }
34
36 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
37 : process_(std::move(process)), explicitDividends_(false) {
39 }
40
42
43 // dividends will eventually be moved out of arguments, but for now we need the switch
44 QL_DEPRECATED_DISABLE_WARNING
46 QL_DEPRECATED_ENABLE_WARNING
47
48 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
49 "not an European option");
50
51 ext::shared_ptr<StrikedTypePayoff> payoff =
52 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
53 QL_REQUIRE(payoff, "non-striked payoff given");
54
55 Date settlementDate = process_->riskFreeRate()->referenceDate();
56 Real riskless = 0.0;
57 Size i;
58 for (i=0; i<dividendSchedule.size(); i++) {
59 const Date cashFlowDate = dividendSchedule[i]->date();
60
61 if ( cashFlowDate >= settlementDate
62 && cashFlowDate <= arguments_.exercise->lastDate()) {
63
64 riskless += dividendSchedule[i]->amount() *
65 process_->riskFreeRate()->discount(cashFlowDate) /
66 process_->dividendYield()->discount(cashFlowDate);
67 }
68 }
69
70 Real spot = process_->stateVariable()->value() - riskless;
71 QL_REQUIRE(spot > 0.0,
72 "negative or null underlying after subtracting dividends");
73
74 DiscountFactor dividendDiscount =
75 process_->dividendYield()->discount(
76 arguments_.exercise->lastDate());
77 DiscountFactor riskFreeDiscount =
78 process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
79 Real forwardPrice = spot * dividendDiscount / riskFreeDiscount;
80
81 Real variance =
82 process_->blackVolatility()->blackVariance(
83 arguments_.exercise->lastDate(),
84 payoff->strike());
85
86 BlackCalculator black(payoff, forwardPrice, std::sqrt(variance),
87 riskFreeDiscount);
88
89 results_.value = black.value();
90 results_.delta = black.delta(spot);
91 results_.gamma = black.gamma(spot);
92
93 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
94 DayCounter dydc = process_->dividendYield()->dayCounter();
95 DayCounter voldc = process_->blackVolatility()->dayCounter();
96 Time t = voldc.yearFraction(
97 process_->blackVolatility()->referenceDate(),
98 arguments_.exercise->lastDate());
99 results_.vega = black.vega(t);
100
101 Real delta_theta = 0.0, delta_rho = 0.0;
102 for (i = 0; i < dividendSchedule.size(); i++) {
103 Date d = dividendSchedule[i]->date();
104
105 if ( d >= settlementDate
106 && d <= arguments_.exercise->lastDate()) {
107
108 delta_theta -= dividendSchedule[i]->amount() *
109 ( process_->riskFreeRate()->zeroRate(d,rfdc,Continuous,Annual).rate()
110 - process_->dividendYield()->zeroRate(d,dydc,Continuous,Annual).rate()) *
111 process_->riskFreeRate()->discount(d) /
112 process_->dividendYield()->discount(d);
113
114 Time t = process_->time(d);
115 delta_rho += dividendSchedule[i]->amount() * t *
116 process_->riskFreeRate()->discount(t) /
117 process_->dividendYield()->discount(t);
118 }
119 }
120 t = process_->time(arguments_.exercise->lastDate());
121 try {
122 results_.theta = black.theta(spot, t) +
123 delta_theta * black.delta(spot);
124 } catch (Error&) {
125 results_.theta = Null<Real>();
126 }
127
128 results_.rho = black.rho(t) +
129 delta_rho * black.delta(spot);
130 }
131
132}
133
AnalyticDividendEuropeanEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, DividendSchedule dividends)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Black 1976 calculator class.
virtual Real delta(Real spot) const
Real vega(Time maturity) const
virtual Real gamma(Real spot) const
virtual Real theta(Real spot, Time maturity) const
Real rho(Time maturity) const
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Base error class.
Definition: errors.hpp:39
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
@ Annual
once a year
Definition: frequency.hpp:39
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
STL namespace.