Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equityindex.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/indexes/equityindex.cpp
20\brief equity index class for holding equity fixing histories and forwarding.
21\ingroup indexes
22*/
23
25
26#include <boost/make_shared.hpp>
27
28namespace QuantExt {
29
30EquityIndex2::EquityIndex2(const std::string& familyName, const Calendar& fixingCalendar, const Currency& currency,
31 const Handle<Quote> spotQuote, const Handle<YieldTermStructure>& rate,
32 const Handle<YieldTermStructure>& dividend)
33 : familyName_(familyName), currency_(currency), rate_(rate), dividend_(dividend), spotQuote_(spotQuote),
34 fixingCalendar_(fixingCalendar) {
35
37 registerWith(spotQuote_);
38 registerWith(rate_);
39 registerWith(dividend_);
40 registerWith(Settings::instance().evaluationDate());
41 registerWith(IndexManager::instance().notifier(name()));
42}
43
44Real EquityIndex2::fixing(const Date& fixingDate, bool forecastTodaysFixing) const {
45 return fixing(fixingDate, forecastTodaysFixing, false);
46}
47
48Real EquityIndex2::fixing(const Date& fixingDate, bool forecastTodaysFixing, bool incDividend) const {
49
50 QL_REQUIRE(isValidFixingDate(fixingDate),
51 "Fixing date " << fixingDate << " is not valid for equity index '" << name_ << "'");
52
53 Date today = Settings::instance().evaluationDate();
54
55 if (fixingDate > today || (fixingDate == today && forecastTodaysFixing))
56 return forecastFixing(fixingDate, incDividend);
57
58 Real result = Null<Decimal>();
59
60 if (fixingDate < today || Settings::instance().enforcesTodaysHistoricFixings()) {
61 // must have been fixed
62 // do not catch exceptions
63 result = pastFixing(fixingDate);
64 QL_REQUIRE(result != Null<Real>(), "Missing equity index fixing " << name() << " fixing for " << fixingDate);
65 } else {
66 try {
67 // might have been fixed
68 result = pastFixing(fixingDate);
69 } catch (Error&) {
70 ; // fall through and forecast
71 }
72 if (result == Null<Real>())
73 return forecastFixing(fixingDate, incDividend);
74 }
75
76 return result;
77}
78
79Real EquityIndex2::forecastFixing(const Date& fixingDate) const { return forecastFixing(fixingDate, false); }
80
81Real EquityIndex2::forecastFixing(const Date& fixingDate, bool incDividend) const {
82 QL_REQUIRE(!rate_.empty(), "null term structure set to this instance of " << name());
83 return forecastFixing(rate_->timeFromReference(fixingDate), incDividend);
84}
85
86Real EquityIndex2::forecastFixing(const Time& fixingTime) const { return forecastFixing(fixingTime, false); }
87
88Real EquityIndex2::forecastFixing(const Time& fixingTime, bool incDividend) const {
89 QL_REQUIRE(!spotQuote_.empty(), "null spot quote set to this instance of " << name());
90 QL_REQUIRE(!rate_.empty() && !dividend_.empty(), "null term structure set to this instance of " << name());
91
92 // we base the forecast always on the spot quote (and not on today's fixing)
93 Real price = spotQuote_->value();
94
95 // compute the forecast applying the usual no arbitrage principle
96 Real forward;
97 if (incDividend) {
98 forward = price / rate_->discount(fixingTime);
99 } else {
100 forward = price * dividend_->discount(fixingTime) / rate_->discount(fixingTime);
101 }
102 return forward;
103}
104
105void EquityIndex2::addDividend(const Dividend& dividend, bool forceOverwrite) {
106 std::string tag = name();
107 std::set<Dividend> divs = DividendManager::instance().getHistory(tag);
108 if (!forceOverwrite) {
109 bool duplicateFixing = false;
110 for (const auto& d : divs) {
111 if (d == dividend)
112 duplicateFixing = true;
113 }
114 QL_REQUIRE(!duplicateFixing, "At least one duplicated fixing provided: ("
115 << dividend.name << ", " << dividend.exDate << ", " << dividend.rate << ")");
116 }
117 divs.insert(dividend);
118 DividendManager::instance().setHistory(tag, divs);
119}
120
121Real EquityIndex2::dividendsBetweenDates(const Date& startDate, const Date& endDate) const {
122 const Date& today = Settings::instance().evaluationDate();
123
124 const std::set<Dividend>& history = dividendFixings();
125 Real dividends = 0.0;
126
127 if (!history.empty()) {
128 for (std::set<Dividend>::const_iterator fd = history.begin();
129 fd != history.end() && fd->exDate <= std::min(endDate, today); ++fd) {
130 if (fd->exDate >= startDate)
131 dividends += fd->rate;
132 }
133 }
134 return dividends;
135}
136
137QuantLib::ext::shared_ptr<EquityIndex2> EquityIndex2::clone(const Handle<Quote> spotQuote, const Handle<YieldTermStructure>& rate,
138 const Handle<YieldTermStructure>& dividend) const {
139 return QuantLib::ext::make_shared<EquityIndex2>(familyName(), fixingCalendar(), currency(), spotQuote, rate, dividend);
140}
141
142} // namespace QuantExt
const Handle< YieldTermStructure > rate_
Definition: equityindex.hpp:97
const Handle< YieldTermStructure > dividend_
Definition: equityindex.hpp:97
virtual const std::set< Dividend > & dividendFixings() const
Definition: equityindex.hpp:67
virtual QuantLib::ext::shared_ptr< EquityIndex2 > clone(const Handle< Quote > spotQuote, const Handle< YieldTermStructure > &rate, const Handle< YieldTermStructure > &dividend) const
Calendar fixingCalendar() const override
virtual void addDividend(const Dividend &fixing, bool forceOverwrite=false)
stores the historical dividend at the given date
virtual Real forecastFixing(const Date &fixingDate) const
Definition: equityindex.cpp:79
const Handle< Quote > spotQuote_
Definition: equityindex.hpp:99
std::string name() const override
EquityIndex2(const std::string &familyName, const Calendar &fixingCalendar, const Currency &currency, const Handle< Quote > spotQuote=Handle< Quote >(), const Handle< YieldTermStructure > &rate=Handle< YieldTermStructure >(), const Handle< YieldTermStructure > &dividend=Handle< YieldTermStructure >())
Definition: equityindex.cpp:30
bool isValidFixingDate(const Date &fixingDate) const override
Currency currency() const
Definition: equityindex.hpp:54
virtual Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
Real dividendsBetweenDates(const Date &startDate, const Date &endDate) const
std::string familyName() const
Definition: equityindex.hpp:76
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
Definition: equityindex.cpp:44
equity index class for holding equity fixing histories and forwarding.
QuantLib::Real rate
Dividend rate.
std::string name
Index name.
QuantLib::Date exDate
Ex dividend date.