/src/quantlib/ql/pricingengines/asian/continuousarithmeticasianlevyengine.cpp
Line | Count | Source |
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 | | <https://www.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> |
21 | | #include <ql/pricingengines/asian/continuousarithmeticasianlevyengine.hpp> |
22 | | #include <ql/math/distributions/normaldistribution.hpp> |
23 | | #include <ql/pricingengines/blackcalculator.hpp> |
24 | | #include <utility> |
25 | | |
26 | | using namespace std; |
27 | | |
28 | | namespace QuantLib { |
29 | | |
30 | | ContinuousArithmeticAsianLevyEngine::ContinuousArithmeticAsianLevyEngine( |
31 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process, |
32 | | Handle<Quote> currentAverage, |
33 | | Date startDate) |
34 | 0 | : process_(std::move(process)), currentAverage_(std::move(currentAverage)), |
35 | 0 | startDate_(startDate) { |
36 | 0 | registerWith(process_); |
37 | 0 | registerWith(currentAverage_); |
38 | 0 | } |
39 | | |
40 | 0 | void ContinuousArithmeticAsianLevyEngine::calculate() const { |
41 | 0 | QL_REQUIRE(arguments_.averageType == Average::Arithmetic, |
42 | 0 | "not an Arithmetic average option"); |
43 | 0 | QL_REQUIRE(arguments_.exercise->type() == Exercise::European, |
44 | 0 | "not an European Option"); |
45 | 0 | QL_REQUIRE(startDate_ <= process_->riskFreeRate()->referenceDate(), |
46 | 0 | "startDate must be earlier than or equal to reference date"); |
47 | | |
48 | 0 | DayCounter rfdc = process_->riskFreeRate()->dayCounter(); |
49 | 0 | DayCounter divdc = process_->dividendYield()->dayCounter(); |
50 | 0 | DayCounter voldc = process_->blackVolatility()->dayCounter(); |
51 | 0 | Real spot = process_->stateVariable()->value(); |
52 | | |
53 | | // payoff |
54 | 0 | ext::shared_ptr<StrikedTypePayoff> payoff = |
55 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); |
56 | 0 | QL_REQUIRE(payoff, "non-plain payoff given"); |
57 | | |
58 | | // original time to maturity |
59 | 0 | Date maturity = arguments_.exercise->lastDate(); |
60 | 0 | Time T = rfdc.yearFraction(startDate_, |
61 | 0 | arguments_.exercise->lastDate()); |
62 | | // remaining time to maturity |
63 | 0 | Time T2 = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(), |
64 | 0 | arguments_.exercise->lastDate()); |
65 | |
|
66 | 0 | Real strike = payoff->strike(); |
67 | |
|
68 | 0 | Volatility volatility = |
69 | 0 | process_->blackVolatility()->blackVol(maturity, strike); |
70 | |
|
71 | 0 | CumulativeNormalDistribution N; |
72 | |
|
73 | 0 | Rate riskFreeRate = process_->riskFreeRate()-> |
74 | 0 | zeroRate(maturity, rfdc, Continuous, NoFrequency); |
75 | 0 | Rate dividendYield = process_->dividendYield()-> |
76 | 0 | zeroRate(maturity, divdc, Continuous, NoFrequency); |
77 | 0 | Real b = riskFreeRate - dividendYield; |
78 | |
|
79 | 0 | Real Se = (std::fabs(b) > 1000*QL_EPSILON) |
80 | 0 | ? Real((spot/(T*b))*(exp((b-riskFreeRate)*T2)-exp(-riskFreeRate*T2))) |
81 | 0 | : Real(spot*T2/T * std::exp(-riskFreeRate*T2)); |
82 | |
|
83 | 0 | Real X; |
84 | 0 | if (T2 < T) { |
85 | 0 | QL_REQUIRE(!currentAverage_.empty() && currentAverage_->isValid(), |
86 | 0 | "current average required"); |
87 | 0 | X = strike - ((T-T2)/T)*currentAverage_->value(); |
88 | 0 | } else { |
89 | 0 | X = strike; |
90 | 0 | } |
91 | | |
92 | 0 | Real m = (std::fabs(b) > 1000*QL_EPSILON) ? ((exp(b*T2)-1)/b) : T2; |
93 | |
|
94 | 0 | Real M = (2*spot*spot/(b+volatility*volatility)) * |
95 | 0 | (((exp((2*b+volatility*volatility)*T2)-1) |
96 | 0 | / (2*b+volatility*volatility))-m); |
97 | |
|
98 | 0 | Real D = M/(T*T); |
99 | |
|
100 | 0 | Real V = log(D)-2*(riskFreeRate*T2+log(Se)); |
101 | |
|
102 | 0 | Real d1 = (1/sqrt(V))*((log(D)/2)-log(X)); |
103 | 0 | Real d2 = d1-sqrt(V); |
104 | |
|
105 | 0 | if(payoff->optionType()==Option::Call) |
106 | 0 | results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2); |
107 | 0 | else |
108 | 0 | results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2) |
109 | 0 | - Se + X*exp(-riskFreeRate*T2); |
110 | 0 | } |
111 | | |
112 | | } |