Coverage Report

Created: 2026-08-14 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/asianoption.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003, 2004 Ferdinando Ametrano
5
 Copyright (C) 2004, 2007 StatPro Italia srl
6
 Copyright (C) 2025 Kareem Fareed
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
/*! \file asianoption.hpp
23
    \brief Asian option on a single asset
24
*/
25
26
#ifndef quantlib_asian_option_hpp
27
#define quantlib_asian_option_hpp
28
29
#include <ql/instruments/oneassetoption.hpp>
30
#include <ql/instruments/payoffs.hpp>
31
#include <ql/instruments/averagetype.hpp>
32
#include <ql/time/date.hpp>
33
#include <vector>
34
35
namespace QuantLib {
36
37
    //! Continuous-averaging Asian option
38
    /*! \ingroup instruments */
39
    class ContinuousAveragingAsianOption : public OneAssetOption {
40
      public:
41
        class arguments;
42
        class engine;
43
        /*! This constructor is for unseasoned (fresh) options where
44
            averaging has not yet started.
45
        */
46
        ContinuousAveragingAsianOption(
47
                Average::Type averageType,
48
                const ext::shared_ptr<StrikedTypePayoff>& payoff,
49
                const ext::shared_ptr<Exercise>& exercise);
50
51
        /*! This constructor is for seasoned options where averaging
52
            has already started. The start date is a contract term specifying
53
            when averaging began. The current average (market data) should be
54
            provided to the pricing engine.
55
        */
56
        ContinuousAveragingAsianOption(
57
                Average::Type averageType,
58
                Date startDate,
59
                const ext::shared_ptr<StrikedTypePayoff>& payoff,
60
                const ext::shared_ptr<Exercise>& exercise);
61
62
        void setupArguments(PricingEngine::arguments*) const override;
63
64
      protected:
65
        Average::Type averageType_;
66
        Date startDate_;
67
    };
68
69
    //! Discrete-averaging Asian option
70
    /*! \ingroup instruments */
71
    class DiscreteAveragingAsianOption : public OneAssetOption {
72
      public:
73
        class arguments;
74
        class engine;
75
        /*! This constructor takes the running sum or product of past fixings,
76
            depending on the average type.  The fixing dates passed here can be
77
            only the future ones.
78
        */
79
        DiscreteAveragingAsianOption(Average::Type averageType,
80
                                     Real runningAccumulator,
81
                                     Size pastFixings,
82
                                     std::vector<Date> fixingDates,
83
                                     const ext::shared_ptr<StrikedTypePayoff>& payoff,
84
                                     const ext::shared_ptr<Exercise>& exercise);
85
86
        /*! This constructor takes past fixings as a vector, defaulting to an empty
87
            vector representing an unseasoned option.  This constructor expects *all* fixing dates
88
            to be provided, including those in the past, and to be already sorted.  During the
89
            calculations, the option will compare them to the evaluation date to determine which
90
            are historic; it will then take as many values from allPastFixings as needed and ignore
91
            the others.  If not enough fixings are provided, it will raise an error.
92
        */
93
        DiscreteAveragingAsianOption(Average::Type averageType,
94
                                     std::vector<Date> fixingDates,
95
                                     const ext::shared_ptr<StrikedTypePayoff>& payoff,
96
                                     const ext::shared_ptr<Exercise>& exercise,
97
                                     std::vector<Real> allPastFixings = std::vector<Real>());
98
99
        void setupArguments(PricingEngine::arguments*) const override;
100
101
      protected:
102
        Average::Type averageType_;
103
        Real runningAccumulator_;
104
        Size pastFixings_;
105
        std::vector<Date> fixingDates_;
106
107
        // For backwards compatibility with the traditional interface, we keep track of
108
        // whether this option was initialised using the full array of seasoned fixings
109
        // (even if empty) or if a pastFixings and a runningAccumulator was provided
110
        bool allPastFixingsProvided_;
111
        std::vector<Real> allPastFixings_;
112
    };
113
114
    //! Extra %arguments for single-asset discrete-average Asian option
115
    class DiscreteAveragingAsianOption::arguments
116
        : public OneAssetOption::arguments {
117
      public:
118
596
        arguments() : averageType(Average::Type(-1)),
119
596
                      runningAccumulator(Null<Real>()),
120
596
                      pastFixings(Null<Size>()) {}
121
        void validate() const override;
122
        Average::Type averageType;
123
        Real runningAccumulator;
124
        Size pastFixings;
125
        std::vector<Date> fixingDates;
126
    };
127
128
    //! Extra %arguments for single-asset continuous-average Asian option
129
    class ContinuousAveragingAsianOption::arguments
130
        : public OneAssetOption::arguments {
131
      public:
132
87
        arguments() : averageType(Average::Type(-1))
133
87
                      {}
134
        void validate() const override;
135
        Average::Type averageType;
136
        Date startDate;
137
    };
138
139
    //! Discrete-averaging Asian %engine base class
140
    class DiscreteAveragingAsianOption::engine
141
        : public GenericEngine<DiscreteAveragingAsianOption::arguments,
142
                               DiscreteAveragingAsianOption::results> {};
143
144
    //! Continuous-averaging Asian %engine base class
145
    class ContinuousAveragingAsianOption::engine
146
        : public GenericEngine<ContinuousAveragingAsianOption::arguments,
147
                               ContinuousAveragingAsianOption::results> {};
148
149
}
150
151
152
#endif