QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticperformanceengine.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 : process_(std::move(process)) {
31 }
32
34
37 "this engine cannot price options already started");
42 "this engine cannot price capped/floored options");
43
44 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
45 "not an European option");
46
47 ext::shared_ptr<PercentageStrikePayoff> moneyness =
48 ext::dynamic_pointer_cast<PercentageStrikePayoff>(
49 arguments_.payoff);
50 QL_REQUIRE(moneyness, "wrong payoff given");
51
52 std::vector<Date> resetDates = arguments_.resetDates;
53 resetDates.push_back(arguments_.exercise->lastDate());
54
55 Real underlying = process_->stateVariable()->value();
56 QL_REQUIRE(underlying > 0.0, "negative or null underlying");
57
58 ext::shared_ptr<StrikedTypePayoff> payoff(
59 new PlainVanillaPayoff(moneyness->optionType(), 1.0));
60
61 results_.value = 0.0;
62 results_.delta = results_.gamma = 0.0;
63 results_.theta = 0.0;
64 results_.rho = results_.dividendRho = 0.0;
65 results_.vega = 0.0;
66
67 for (Size i = 1; i < resetDates.size(); i++) {
68
69 DiscountFactor discount =
70 process_->riskFreeRate()->discount(resetDates[i-1]);
71 DiscountFactor rDiscount =
72 process_->riskFreeRate()->discount(resetDates[i]) /
73 process_->riskFreeRate()->discount(resetDates[i-1]);
74 DiscountFactor qDiscount =
75 process_->dividendYield()->discount(resetDates[i]) /
76 process_->dividendYield()->discount(resetDates[i-1]);
77 Real forward = (1.0/moneyness->strike())*qDiscount/rDiscount;
79 process_->blackVolatility()->blackForwardVariance(
80 resetDates[i-1],resetDates[i],
81 underlying * moneyness->strike());
82
83 BlackCalculator black(payoff, forward, std::sqrt(variance), rDiscount);
84
85 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
86 DayCounter divdc = process_->dividendYield()->dayCounter();
87 DayCounter voldc = process_->blackVolatility()->dayCounter();
88
89 results_.value += discount * moneyness->strike() * black.value();
90 results_.delta += 0.0;
91 results_.gamma += 0.0;
92 results_.theta += process_->riskFreeRate()->forwardRate(
93 resetDates[i-1], resetDates[i], rfdc, Continuous, NoFrequency) *
94 discount * moneyness->strike() * black.value();
95
96 Time dt = rfdc.yearFraction(resetDates[i-1],resetDates[i]);
97 Time t = rfdc.yearFraction(
98 process_->riskFreeRate()->referenceDate(),
99 resetDates[i-1]);
100 results_.rho += discount * moneyness->strike() *
101 (black.rho(dt) - t * black.value());
102
103 dt = divdc.yearFraction(resetDates[i-1],resetDates[i]);
104 results_.dividendRho += discount * moneyness->strike() *
105 black.dividendRho(dt);
106
107 dt = voldc.yearFraction(resetDates[i-1], resetDates[i]);
108 results_.vega += discount * moneyness->strike() * black.vega(dt);
109 }
110
111 }
112
113}
114
Analytic performance engine.
Black-formula calculator class.
AnalyticPerformanceEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Black 1976 calculator class.
Real dividendRho(Time maturity) const
Real vega(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
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
Plain-vanilla payoff.
Definition: payoffs.hpp:105
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
@ NoFrequency
null frequency
Definition: frequency.hpp:37
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
STL namespace.