QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticeuropeanengine.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) 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 <http://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>
24#include <utility>
25
26namespace QuantLib {
27
29 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
30 : process_(std::move(process)) {
31 registerWith(process_);
32 }
33
35 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
36 Handle<YieldTermStructure> discountCurve)
37 : process_(std::move(process)), discountCurve_(std::move(discountCurve)) {
38 registerWith(process_);
39 registerWith(discountCurve_);
40 }
41
43
44 // if the discount curve is not specified, we default to the
45 // risk free rate curve embedded within the GBM process
46 ext::shared_ptr<YieldTermStructure> discountPtr =
47 discountCurve_.empty() ?
48 process_->riskFreeRate().currentLink() :
49 discountCurve_.currentLink();
50
51 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
52 "not an European option");
53
54 ext::shared_ptr<StrikedTypePayoff> payoff =
55 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
56 QL_REQUIRE(payoff, "non-striked payoff given");
57
59 process_->blackVolatility()->blackVariance(
60 arguments_.exercise->lastDate(),
61 payoff->strike());
62 DiscountFactor dividendDiscount =
63 process_->dividendYield()->discount(
64 arguments_.exercise->lastDate());
65 DiscountFactor df = discountPtr->discount(arguments_.exercise->lastDate());
66 DiscountFactor riskFreeDiscountForFwdEstimation =
67 process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
68 Real spot = process_->stateVariable()->value();
69 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
70 Real forwardPrice = spot * dividendDiscount / riskFreeDiscountForFwdEstimation;
71
72 BlackCalculator black(payoff, forwardPrice, std::sqrt(variance),df);
73
74
75 results_.value = black.value();
76 results_.delta = black.delta(spot);
77 results_.deltaForward = black.deltaForward();
78 results_.elasticity = black.elasticity(spot);
79 results_.gamma = black.gamma(spot);
80
81 DayCounter rfdc = discountPtr->dayCounter();
82 DayCounter divdc = process_->dividendYield()->dayCounter();
83 DayCounter voldc = process_->blackVolatility()->dayCounter();
84 Time t = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(),
85 arguments_.exercise->lastDate());
86 results_.rho = black.rho(t);
87
88 t = divdc.yearFraction(process_->dividendYield()->referenceDate(),
89 arguments_.exercise->lastDate());
90 results_.dividendRho = black.dividendRho(t);
91
92 t = voldc.yearFraction(process_->blackVolatility()->referenceDate(),
93 arguments_.exercise->lastDate());
94 results_.vega = black.vega(t);
95 try {
96 results_.theta = black.theta(spot, t);
97 results_.thetaPerDay =
98 black.thetaPerDay(spot, t);
99 } catch (Error&) {
100 results_.theta = Null<Real>();
101 results_.thetaPerDay = Null<Real>();
102 }
103
104 results_.strikeSensitivity = black.strikeSensitivity();
105 results_.itmCashProbability = black.itmCashProbability();
106
107 Real tte = process_->blackVolatility()->timeFromReference(arguments_.exercise->lastDate());
108 results_.additionalResults["spot"] = spot;
109 results_.additionalResults["dividendDiscount"] = dividendDiscount;
110 results_.additionalResults["riskFreeDiscount"] = riskFreeDiscountForFwdEstimation;
111 results_.additionalResults["forward"] = forwardPrice;
112 results_.additionalResults["strike"] = payoff->strike();
113 results_.additionalResults["volatility"] = Real(std::sqrt(variance / tte));
114 results_.additionalResults["timeToExpiry"] = tte;
115 }
116
117}
118
Analytic European engine.
Black-formula calculator class.
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
const Instrument::results * results_
Definition: cdsoption.cpp:63
Handle< YieldTermStructure > discountCurve_
AnalyticEuropeanEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Black 1976 calculator class.
Real dividendRho(Time maturity) const
virtual Real delta(Real spot) const
Real vega(Time maturity) const
virtual Real gamma(Real spot) const
virtual Real elasticity(Real spot) const
virtual Real thetaPerDay(Real spot, Time maturity) const
virtual Real theta(Real spot, Time maturity) const
Real rho(Time maturity) const
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
Shared handle to an observable.
Definition: handle.hpp:41
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
Option exercise classes and payoff function.
LinearInterpolation variance
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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.