Coverage Report

Created: 2026-03-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
0
    : process_(std::move(process)), currentAverage_(std::move(currentAverage)) {
34
0
        registerWith(process_);
35
0
        registerWith(currentAverage_);
36
0
    }
37
38
    ContinuousArithmeticAsianLevyEngine::ContinuousArithmeticAsianLevyEngine(
39
        ext::shared_ptr<GeneralizedBlackScholesProcess> process,
40
        Handle<Quote> currentAverage,
41
        Date startDate)
42
0
    : process_(std::move(process)), currentAverage_(std::move(currentAverage)),
43
0
      startDate_(startDate) {
44
0
        registerWith(process_);
45
0
        registerWith(currentAverage_);
46
0
    }
47
48
0
    void ContinuousArithmeticAsianLevyEngine::calculate() const {
49
0
        QL_REQUIRE(arguments_.averageType == Average::Arithmetic,
50
0
                   "not an Arithmetic average option");
51
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
52
0
                   "not an European Option");
53
54
        // Prefer start date from option if available, otherwise use constructor parameter.
55
        // At least one must be specified.
56
0
        Date startDate = (arguments_.startDate != Date()) ? arguments_.startDate : startDate_;
57
0
        QL_REQUIRE(startDate != Date(), "start date not provided");
58
0
        QL_REQUIRE(startDate <= process_->riskFreeRate()->referenceDate(),
59
0
                   "start date must be earlier than or equal to reference date");
60
61
0
        DayCounter rfdc  = process_->riskFreeRate()->dayCounter();
62
0
        DayCounter divdc = process_->dividendYield()->dayCounter();
63
0
        DayCounter voldc = process_->blackVolatility()->dayCounter();
64
0
        Real spot = process_->stateVariable()->value();
65
66
        // payoff
67
0
        ext::shared_ptr<StrikedTypePayoff> payoff =
68
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
69
0
        QL_REQUIRE(payoff, "non-plain payoff given");
70
71
        // original time to maturity
72
0
        Date maturity = arguments_.exercise->lastDate();
73
0
        Time T = rfdc.yearFraction(startDate,
74
0
                                   arguments_.exercise->lastDate());
75
        // remaining time to maturity
76
0
        Time T2 = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(),
77
0
                                    arguments_.exercise->lastDate());
78
79
0
        Real strike = payoff->strike();
80
81
0
        Volatility volatility =
82
0
            process_->blackVolatility()->blackVol(maturity, strike);
83
84
0
        CumulativeNormalDistribution N;
85
86
0
        Rate riskFreeRate = process_->riskFreeRate()->
87
0
            zeroRate(maturity, rfdc, Continuous, NoFrequency);
88
0
        Rate dividendYield = process_->dividendYield()->
89
0
            zeroRate(maturity, divdc, Continuous, NoFrequency);
90
0
        Real b = riskFreeRate - dividendYield;
91
92
0
        Real Se = (std::fabs(b) > 1000*QL_EPSILON) 
93
0
            ? Real((spot/(T*b))*(exp((b-riskFreeRate)*T2)-exp(-riskFreeRate*T2)))
94
0
            : Real(spot*T2/T * std::exp(-riskFreeRate*T2));
95
96
0
        Real X;
97
0
        if (T2 < T) {
98
0
            QL_REQUIRE(!currentAverage_.empty() && currentAverage_->isValid(),
99
0
                       "current average required for seasoned option");
100
0
            X = strike - ((T-T2)/T)*currentAverage_->value();
101
0
        } else {
102
0
            X = strike;
103
0
        }
104
105
0
        Real m = (std::fabs(b) > 1000*QL_EPSILON) ? ((exp(b*T2)-1)/b) : T2;
106
107
0
        Real M = (2*spot*spot/(b+volatility*volatility)) *
108
0
            (((exp((2*b+volatility*volatility)*T2)-1)
109
0
              / (2*b+volatility*volatility))-m);
110
111
0
        Real D = M/(T*T);
112
113
0
        Real V = log(D)-2*(riskFreeRate*T2+log(Se));
114
115
0
        Real d1 = (1/sqrt(V))*((log(D)/2)-log(X));
116
0
        Real d2 = d1-sqrt(V);
117
118
0
        if(payoff->optionType()==Option::Call)
119
0
            results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2);
120
0
        else
121
0
            results_.value = Se*N(d1) - X*exp(-riskFreeRate*T2)*N(d2)
122
0
                             - Se + X*exp(-riskFreeRate*T2);
123
0
    }
124
125
}