QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
equityindex.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2023 Marcin Rybacki
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/indexes/equityindex.hpp>
21#include <ql/settings.hpp>
22#include <utility>
23
24namespace QuantLib {
25
26 namespace {
27 Real resolveSpot(const Handle<Quote>& spot, Real lastFixing) {
28 QL_REQUIRE(!spot.empty() || lastFixing != Null<Real>(),
29 "Cannot forecast equity index, missing both spot and historical index");
30 return spot.empty() ? lastFixing : spot->value();
31 }
32 }
33
34 EquityIndex::EquityIndex(std::string name,
35 Calendar fixingCalendar,
38 Handle<Quote> spot)
39 : name_(std::move(name)), fixingCalendar_(std::move(fixingCalendar)),
40 interest_(std::move(interest)), dividend_(std::move(dividend)), spot_(std::move(spot)) {
41
45 registerWith(Settings::instance().evaluationDate());
47 }
48
49 Real EquityIndex::fixing(const Date& fixingDate, bool forecastTodaysFixing) const {
50
51 QL_REQUIRE(isValidFixingDate(fixingDate), "Fixing date " << fixingDate << " is not valid");
52
54
55 if (fixingDate > today || (fixingDate == today && forecastTodaysFixing))
56 return forecastFixing(fixingDate);
57
58 Real result = pastFixing(fixingDate);
59
60 if (result != Null<Real>())
61 // if historical fixing is present use it
62 return result;
63
64 if (fixingDate == today && !spot_.empty())
65 // Today's fixing is missing, but spot is
66 // provided, so use it as proxy
67 return spot_->value();
68
69 QL_FAIL("Missing " << name() << " fixing for " << fixingDate);
70 }
71
72 Real EquityIndex::pastFixing(const Date& fixingDate) const {
73 QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate << " is not a valid fixing date");
74 return timeSeries()[fixingDate];
75 }
76
77 Real EquityIndex::forecastFixing(const Date& fixingDate) const {
78 QL_REQUIRE(!interest_.empty(),
79 "null interest rate term structure set to this instance of " << name());
80
83
84 Real spot = resolveSpot(spot_, pastFixing(lastFixingDate));
85
86 Real forward;
87 if (!dividend_.empty()) {
88 forward = spot * dividend_->discount(fixingDate) / interest_->discount(fixingDate);
89 } else {
90 forward = spot / interest_->discount(fixingDate);
91 }
92 return forward;
93 }
94
95 ext::shared_ptr<EquityIndex> EquityIndex::clone(const Handle<YieldTermStructure>& interest,
96 const Handle<YieldTermStructure>& dividend,
97 const Handle<Quote>& spot) const {
98 return ext::make_shared<EquityIndex>(name(), fixingCalendar(), interest, dividend, spot);
99 }
100}
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Concrete date class.
Definition: date.hpp:125
Handle< Quote > spot_
virtual ext::shared_ptr< EquityIndex > clone(const Handle< YieldTermStructure > &interest, const Handle< YieldTermStructure > &dividend, const Handle< Quote > &spot) const
Definition: equityindex.cpp:95
Calendar fixingCalendar() const override
returns the calendar defining valid fixing dates
Definition: equityindex.hpp:74
virtual Real forecastFixing(const Date &fixingDate) const
It can be overridden to implement particular conventions.
Definition: equityindex.cpp:77
Handle< YieldTermStructure > dividend_
EquityIndex(std::string name, Calendar fixingCalendar, Handle< YieldTermStructure > interest={}, Handle< YieldTermStructure > dividend={}, Handle< Quote > spot={})
Definition: equityindex.cpp:34
virtual Real pastFixing(const Date &fixingDate) const
Definition: equityindex.cpp:72
std::string name() const override
Returns the name of the index.
Definition: equityindex.hpp:73
Handle< Quote > spot() const
index spot value
Definition: equityindex.hpp:89
bool isValidFixingDate(const Date &fixingDate) const override
returns TRUE if the fixing date is a valid one
Handle< YieldTermStructure > interest_
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
returns the fixing at the given date
Definition: equityindex.cpp:49
Shared handle to an observable.
Definition: handle.hpp:41
const TimeSeries< Real > & timeSeries() const
returns the fixing TimeSeries
Definition: index.hpp:65
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
STL namespace.