QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analyticwriterextensibleoptionengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2011 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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/experimental/exoticoptions/analyticwriterextensibleoptionengine.hpp>
21#include <ql/math/distributions/bivariatenormaldistribution.hpp>
22#include <ql/pricingengines/blackformula.hpp>
23#include <utility>
24
25using namespace std;
26
27namespace QuantLib {
28
30 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
31 : process_(std::move(process)) {
33 }
34
36 // We take all the arguments:
37
38 ext::shared_ptr<PlainVanillaPayoff> payoff1 =
39 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
40 QL_REQUIRE(payoff1, "not a plain vanilla payoff");
41
42 ext::shared_ptr<PlainVanillaPayoff> payoff2 =
43 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff2);
44 QL_REQUIRE(payoff2, "not a plain vanilla payoff");
45
46 ext::shared_ptr<Exercise> exercise1 = arguments_.exercise;
47
48 ext::shared_ptr<Exercise> exercise2 = arguments_.exercise2;
49
50
51 // We create and apply the calculate process:
52
53 Option::Type type = payoff1->optionType();
54
55 // STEP 1:
56
57 // S = spot
58 Real spot = process_->stateVariable()->value();
59
60 // For the B&S formulae:
61 DayCounter dividendDC = process_->dividendYield()->dayCounter();
62 Rate dividend = process_->dividendYield()->zeroRate(
63 exercise1->lastDate(), dividendDC, Continuous, NoFrequency);
64
65 DayCounter riskFreeDC = process_->riskFreeRate()->dayCounter();
66 Rate riskFree = process_->riskFreeRate()->zeroRate(
67 exercise1->lastDate(), riskFreeDC, Continuous, NoFrequency);
68
69 // The time to maturity:
70 Time t1 = riskFreeDC.yearFraction(
71 process_->riskFreeRate()->referenceDate(),
72 arguments_.exercise->lastDate());
73 Time t2 = riskFreeDC.yearFraction(
74 process_->riskFreeRate()->referenceDate(),
75 arguments_.exercise2->lastDate());
76
77 // b = r-q:
78 Real b = riskFree - dividend;
79
80 Real forwardPrice = spot * std::exp(b*t1);
81
82 Volatility volatility = process_->blackVolatility()->blackVol(
83 exercise1->lastDate(), payoff1->strike());
84
85 Real stdDev = volatility*std::sqrt(t1);
86
87 Real discount = std::exp(-riskFree*t1);
88
89 // Call the B&S method:
90 Real black = blackFormula(type, payoff1->strike(),
91 forwardPrice, stdDev, discount);
92
93 // STEP 2:
94
95 // Standard bivariate normal distribution:
96 Real ro = std::sqrt(t1/t2);
97 Real z1 = (std::log(spot/payoff2->strike()) +
98 (b+std::pow(volatility, 2)/2)*t2)/(volatility*std::sqrt(t2));
99 Real z2 = (std::log(spot/payoff1->strike()) +
100 (b+std::pow(volatility, 2)/2)*t1)/(volatility*std::sqrt(t1));
101
102 // Call the bivariate method:
104
105
106 // STEP 3:
107
108 Real bivariate1, bivariate2, result;
109
110 // Final computing:
111 if (type == Option::Call) {
112 // Call case:
113 bivariate1 = biv(z1, -z2);
114 bivariate2 = biv(z1-volatility*std::sqrt(t2),
115 -z2+volatility*std::sqrt(t1));
116 result = black + spot*std::exp((b-riskFree)*t2)*bivariate1
117 - payoff2->strike()*std::exp((-riskFree)*t2)*bivariate2;
118 } else {
119 // Put case:
120 bivariate1 = biv(-z1, z2);
121 bivariate2 = biv(-z1+volatility*std::sqrt(t2),
122 z2-volatility*std::sqrt(t1));
123 result = black - spot*std::exp((b-riskFree)*t2)*bivariate1
124 + payoff2->strike()*std::exp((-riskFree)*t2)*bivariate2;
125 }
126
127 // Save the result:
128 results_.value = result;
129 }
130
131}
AnalyticWriterExtensibleOptionEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Cumulative bivariate normal distibution function (West 2004)
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
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
@ 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 Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
STL namespace.