QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
continuousarithmeticasianlevyengine.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) 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/exercise.hpp>
24#include <utility>
25
26using namespace std;
27
28namespace QuantLib {
29
31 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
32 Handle<Quote> currentAverage,
33 Date startDate)
34 : process_(std::move(process)), currentAverage_(std::move(currentAverage)),
35 startDate_(startDate) {
38 }
39
42 "not an Arithmetic average option");
43 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
44 "not an European Option");
45 QL_REQUIRE(startDate_ <= process_->riskFreeRate()->referenceDate(),
46 "startDate must be earlier than or equal to reference date");
47
48 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
49 DayCounter divdc = process_->dividendYield()->dayCounter();
50 DayCounter voldc = process_->blackVolatility()->dayCounter();
51 Real spot = process_->stateVariable()->value();
52
53 // payoff
54 ext::shared_ptr<StrikedTypePayoff> payoff =
55 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
56 QL_REQUIRE(payoff, "non-plain payoff given");
57
58 // original time to maturity
59 Date maturity = arguments_.exercise->lastDate();
61 arguments_.exercise->lastDate());
62 // remaining time to maturity
63 Time T2 = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(),
64 arguments_.exercise->lastDate());
65
66 Real strike = payoff->strike();
67
68 Volatility volatility =
69 process_->blackVolatility()->blackVol(maturity, strike);
70
72
73 Rate riskFreeRate = process_->riskFreeRate()->
74 zeroRate(maturity, rfdc, Continuous, NoFrequency);
75 Rate dividendYield = process_->dividendYield()->
76 zeroRate(maturity, divdc, Continuous, NoFrequency);
77 Real b = riskFreeRate - dividendYield;
78
79 Real Se = (std::fabs(b) > 1000*QL_EPSILON)
80 ? Real((spot/(T*b))*(exp((b-riskFreeRate)*T2)-exp(-riskFreeRate*T2)))
81 : Real(spot*T2/T * std::exp(-riskFreeRate*T2));
82
83 Real X;
84 if (T2 < T) {
85 QL_REQUIRE(!currentAverage_.empty() && currentAverage_->isValid(),
86 "current average required");
87 X = strike - ((T-T2)/T)*currentAverage_->value();
88 } else {
89 X = strike;
90 }
91
92 Real m = (std::fabs(b) > 1000*QL_EPSILON) ? ((exp(b*T2)-1)/b) : T2;
93
94 Real M = (2*spot*spot/(b+volatility*volatility)) *
95 (((exp((2*b+volatility*volatility)*T2)-1)
96 / (2*b+volatility*volatility))-m);
97
98 Real D = M/(T*T);
99
100 Real V = log(D)-2*(riskFreeRate*T2+log(Se));
101
102 Real d1 = (1/sqrt(V))*((log(D)/2)-log(X));
103 Real d2 = d1-sqrt(V);
104
105 if(payoff->optionType()==Option::Call)
106 results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2);
107 else
108 results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2)
109 - Se + X*exp(-riskFreeRate*T2);
110 }
111
112}
Black-formula calculator class.
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
ContinuousArithmeticAsianLevyEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, Handle< Quote > currentAverage, Date startDate)
Cumulative normal distribution function.
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
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Levy engine for continuous arithmetic Asian options.
#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.
ext::function< Real(Real)> b
@ NoFrequency
null frequency
Definition: frequency.hpp:37
#define QL_EPSILON
Definition: qldefines.hpp:178
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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.
normal, cumulative and inverse cumulative distributions