QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
equityindex.cpp
Go to the documentation of this file.
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
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 : EquityIndex(std::move(name),
40 std::move(fixingCalendar),
41 Currency(),
42 std::move(interest),
43 std::move(dividend),
44 std::move(spot)) {}
45
46 EquityIndex::EquityIndex(std::string name,
47 Calendar fixingCalendar,
48 Currency currency,
51 Handle<Quote> spot)
52 : name_(std::move(name)), fixingCalendar_(std::move(fixingCalendar)),
53 currency_(std::move(currency)), interest_(std::move(interest)),
54 dividend_(std::move(dividend)), spot_(std::move(spot)) {
55
59 registerWith(Settings::instance().evaluationDate());
61 }
62
63 Real EquityIndex::fixing(const Date& fixingDate, bool forecastTodaysFixing) const {
64
65 QL_REQUIRE(isValidFixingDate(fixingDate), "Fixing date " << fixingDate << " is not valid");
66
68
69 if (fixingDate > today || (fixingDate == today && forecastTodaysFixing))
70 return forecastFixing(fixingDate);
71
72 Real result = pastFixing(fixingDate);
73
74 if (result != Null<Real>())
75 // if historical fixing is present use it
76 return result;
77
78 if (fixingDate == today && !spot_.empty())
79 // Today's fixing is missing, but spot is
80 // provided, so use it as proxy
81 return spot_->value();
82
83 QL_FAIL("Missing " << name() << " fixing for " << fixingDate);
84 }
85
86 Real EquityIndex::forecastFixing(const Date& fixingDate) const {
87 QL_REQUIRE(!interest_.empty(),
88 "null interest rate term structure set to this instance of " << name());
89
92
93 Real spot = resolveSpot(spot_, pastFixing(lastFixingDate));
94
95 Real forward;
96 if (!dividend_.empty()) {
97 forward = spot * dividend_->discount(fixingDate) / interest_->discount(fixingDate);
98 } else {
99 forward = spot / interest_->discount(fixingDate);
100 }
101 return forward;
102 }
103
104 ext::shared_ptr<EquityIndex> EquityIndex::clone(const Handle<YieldTermStructure>& interest,
105 const Handle<YieldTermStructure>& dividend,
106 const Handle<Quote>& spot) const {
107 return ext::make_shared<EquityIndex>(name(), fixingCalendar(), currency(), interest,
108 dividend, spot);
109 }
110}
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
Base class for equity indexes.
Definition: equityindex.hpp:63
Handle< Quote > spot_
virtual ext::shared_ptr< EquityIndex > clone(const Handle< YieldTermStructure > &interest, const Handle< YieldTermStructure > &dividend, const Handle< Quote > &spot) const
Calendar fixingCalendar() const override
returns the calendar defining valid fixing dates
Definition: equityindex.hpp:85
virtual Real forecastFixing(const Date &fixingDate) const
It can be overridden to implement particular conventions.
Definition: equityindex.cpp:86
Handle< YieldTermStructure > dividend_
std::string name() const override
Returns the name of the index.
Definition: equityindex.hpp:84
Handle< Quote > spot() const
index spot value
Definition: equityindex.hpp:98
bool isValidFixingDate(const Date &fixingDate) const override
returns TRUE if the fixing date is a valid one
Currency currency() const
The index currency.
Definition: equityindex.hpp:92
Handle< YieldTermStructure > interest_
EquityIndex(std::string name, Calendar fixingCalendar, Currency currency, Handle< YieldTermStructure > interest={}, Handle< YieldTermStructure > dividend={}, Handle< Quote > spot={})
Definition: equityindex.cpp:46
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
returns the fixing at the given date
Definition: equityindex.cpp:63
Shared handle to an observable.
Definition: handle.hpp:41
ext::shared_ptr< Observable > notifier() const
Definition: index.hpp:113
virtual Real pastFixing(const Date &fixingDate) const
returns a past fixing at the given date
Definition: index.hpp:131
template class providing a null value for a given type.
Definition: null.hpp:59
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
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
base class for equity indexes
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:37
STL namespace.
global repository for run-time library settings