Coverage Report

Created: 2026-03-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/models/equity/hestonmodelhelper.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2005 Klaus Spanderen
5
 Copyright (C) 2007 StatPro Italia srl
6
 Copyright (C) 2015 Peter Caspers
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
#include <ql/exercise.hpp>
23
#include <ql/instruments/payoffs.hpp>
24
#include <ql/models/equity/hestonmodelhelper.hpp>
25
#include <ql/pricingengines/blackformula.hpp>
26
#include <ql/processes/hestonprocess.hpp>
27
#include <ql/quotes/simplequote.hpp>
28
#include <utility>
29
30
31
namespace QuantLib {
32
33
    HestonModelHelper::HestonModelHelper(const Period& maturity,
34
                                         Calendar calendar,
35
                                         const Real s0,
36
                                         const Real strikePrice,
37
                                         const Handle<Quote>& volatility,
38
                                         const Handle<YieldTermStructure>& riskFreeRate,
39
                                         const Handle<YieldTermStructure>& dividendYield,
40
                                         BlackCalibrationHelper::CalibrationErrorType errorType)
41
0
    : BlackCalibrationHelper(volatility, errorType), maturity_(maturity),
42
0
      calendar_(std::move(calendar)), s0_(Handle<Quote>(ext::make_shared<SimpleQuote>(s0))),
43
0
      strikePrice_(strikePrice), riskFreeRate_(riskFreeRate), dividendYield_(dividendYield) {
44
0
        registerWith(riskFreeRate);
45
0
        registerWith(dividendYield);
46
0
    }
Unexecuted instantiation: QuantLib::HestonModelHelper::HestonModelHelper(QuantLib::Period const&, QuantLib::Calendar, double, double, QuantLib::Handle<QuantLib::Quote> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::BlackCalibrationHelper::CalibrationErrorType)
Unexecuted instantiation: QuantLib::HestonModelHelper::HestonModelHelper(QuantLib::Period const&, QuantLib::Calendar, double, double, QuantLib::Handle<QuantLib::Quote> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::BlackCalibrationHelper::CalibrationErrorType)
47
48
    HestonModelHelper::HestonModelHelper(const Period& maturity,
49
                                         Calendar calendar,
50
                                         const Handle<Quote>& s0,
51
                                         const Real strikePrice,
52
                                         const Handle<Quote>& volatility,
53
                                         const Handle<YieldTermStructure>& riskFreeRate,
54
                                         const Handle<YieldTermStructure>& dividendYield,
55
                                         BlackCalibrationHelper::CalibrationErrorType errorType)
56
0
    : BlackCalibrationHelper(volatility, errorType), maturity_(maturity),
57
0
      calendar_(std::move(calendar)), s0_(s0), strikePrice_(strikePrice),
58
0
      riskFreeRate_(riskFreeRate), dividendYield_(dividendYield) {
59
0
        registerWith(s0);
60
0
        registerWith(riskFreeRate);
61
0
        registerWith(dividendYield);
62
0
    }
Unexecuted instantiation: QuantLib::HestonModelHelper::HestonModelHelper(QuantLib::Period const&, QuantLib::Calendar, QuantLib::Handle<QuantLib::Quote> const&, double, QuantLib::Handle<QuantLib::Quote> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::BlackCalibrationHelper::CalibrationErrorType)
Unexecuted instantiation: QuantLib::HestonModelHelper::HestonModelHelper(QuantLib::Period const&, QuantLib::Calendar, QuantLib::Handle<QuantLib::Quote> const&, double, QuantLib::Handle<QuantLib::Quote> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::Handle<QuantLib::YieldTermStructure> const&, QuantLib::BlackCalibrationHelper::CalibrationErrorType)
63
64
0
    void HestonModelHelper::performCalculations() const {
65
0
        exerciseDate_ =
66
0
            calendar_.advance(riskFreeRate_->referenceDate(), maturity_);
67
0
        tau_ = riskFreeRate_->timeFromReference(exerciseDate_);
68
0
        type_ = strikePrice_ * riskFreeRate_->discount(tau_) >=
69
0
                        s0_->value() * dividendYield_->discount(tau_)
70
0
                    ? Option::Call
71
0
                    : Option::Put;
72
0
        ext::shared_ptr<StrikedTypePayoff> payoff(
73
0
            new PlainVanillaPayoff(type_, strikePrice_));
74
0
        ext::shared_ptr<Exercise> exercise =
75
0
            ext::make_shared<EuropeanExercise>(exerciseDate_);
76
0
        option_ = ext::make_shared<VanillaOption>(payoff, exercise);
77
0
        BlackCalibrationHelper::performCalculations();
78
0
    }
79
80
0
    Real HestonModelHelper::modelValue() const {
81
0
        calculate();
82
0
        option_->setPricingEngine(engine_);
83
0
        return option_->NPV();
84
0
    }
85
86
0
    Real HestonModelHelper::blackPrice(Real volatility) const {
87
0
        calculate();
88
0
        const Real stdDev = volatility * std::sqrt(maturity());
89
0
        return blackFormula(
90
0
            type_, strikePrice_ * riskFreeRate_->discount(tau_),
91
0
            s0_->value() * dividendYield_->discount(tau_), stdDev);
92
0
    }
93
}
94