QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticdividendeuropeanengine.cpp
Go to the documentation of this file.
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>
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 registerWith(process_);
32 }
33
35
36 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
37 "not an European option");
38
39 ext::shared_ptr<StrikedTypePayoff> payoff =
40 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
41 QL_REQUIRE(payoff, "non-striked payoff given");
42
43 Date settlementDate = process_->riskFreeRate()->referenceDate();
44 Real riskless = 0.0;
45 Size i;
46 for (i=0; i<dividends_.size(); i++) {
47 const Date cashFlowDate = dividends_[i]->date();
48
49 if ( cashFlowDate >= settlementDate
50 && cashFlowDate <= arguments_.exercise->lastDate()) {
51
52 riskless += dividends_[i]->amount() *
53 process_->riskFreeRate()->discount(cashFlowDate) /
54 process_->dividendYield()->discount(cashFlowDate);
55 }
56 }
57
58 Real spot = process_->stateVariable()->value() - riskless;
59 QL_REQUIRE(spot > 0.0,
60 "negative or null underlying after subtracting dividends");
61
62 DiscountFactor dividendDiscount =
63 process_->dividendYield()->discount(
64 arguments_.exercise->lastDate());
65 DiscountFactor riskFreeDiscount =
66 process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
67 Real forwardPrice = spot * dividendDiscount / riskFreeDiscount;
68
70 process_->blackVolatility()->blackVariance(
71 arguments_.exercise->lastDate(),
72 payoff->strike());
73
74 BlackCalculator black(payoff, forwardPrice, std::sqrt(variance),
75 riskFreeDiscount);
76
77 results_.value = black.value();
78 results_.delta = black.delta(spot);
79 results_.gamma = black.gamma(spot);
80
81 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
82 DayCounter dydc = process_->dividendYield()->dayCounter();
83 DayCounter voldc = process_->blackVolatility()->dayCounter();
84 Time t = voldc.yearFraction(
85 process_->blackVolatility()->referenceDate(),
86 arguments_.exercise->lastDate());
87 results_.vega = black.vega(t);
88
89 Real delta_theta = 0.0, delta_rho = 0.0;
90 for (i = 0; i < dividends_.size(); i++) {
91 Date d = dividends_[i]->date();
92
93 if ( d >= settlementDate
94 && d <= arguments_.exercise->lastDate()) {
95
96 delta_theta -= dividends_[i]->amount() *
97 ( process_->riskFreeRate()->zeroRate(d,rfdc,Continuous,Annual).rate()
98 - process_->dividendYield()->zeroRate(d,dydc,Continuous,Annual).rate()) *
99 process_->riskFreeRate()->discount(d) /
100 process_->dividendYield()->discount(d);
101
102 Time t = process_->time(d);
103 delta_rho += dividends_[i]->amount() * t *
104 process_->riskFreeRate()->discount(t) /
105 process_->dividendYield()->discount(t);
106 }
107 }
108 t = process_->time(arguments_.exercise->lastDate());
109 try {
110 results_.theta = black.theta(spot, t) +
111 delta_theta * black.delta(spot);
112 } catch (Error&) {
113 results_.theta = Null<Real>();
114 }
115
116 results_.rho = black.rho(t) +
117 delta_rho * black.delta(spot);
118 }
119
120}
121
Analytic discrete-dividend European engine.
Black-formula calculator class.
const Instrument::results * results_
Definition: cdsoption.cpp:63
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
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Option exercise classes and payoff function.
LinearInterpolation variance
@ 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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
STL namespace.