QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
equitycashflow.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/cashflows/equitycashflow.hpp>
21#include <ql/cashflows/indexedcashflow.hpp>
22#include <ql/indexes/equityindex.hpp>
23#include <ql/termstructures/yield/quantotermstructure.hpp>
24#include <ql/termstructures/yield/flatforward.hpp>
25#include <ql/time/calendars/nullcalendar.hpp>
26#include <ql/quotes/simplequote.hpp>
27#include <ql/time/daycounters/actual365fixed.hpp>
28
29namespace QuantLib {
30
31 namespace {
32 Handle<YieldTermStructure>
33 configureDividendHandle(const Handle<YieldTermStructure>& dividendHandle) {
34 if (dividendHandle.empty()) {
35 ext::shared_ptr<YieldTermStructure> flatTs(ext::make_shared<FlatForward>(
36 0, NullCalendar(), Handle<Quote>(ext::make_shared<SimpleQuote>(0.0)),
37 Actual365Fixed()));
38 return Handle<YieldTermStructure>(flatTs);
39 }
40 return dividendHandle;
41 }
42 }
43
44 void setCouponPricer(const Leg& leg, const ext::shared_ptr<EquityCashFlowPricer>& p) {
45 for (const auto& i : leg) {
46 ext::shared_ptr<EquityCashFlow> c =
47 ext::dynamic_pointer_cast<EquityCashFlow>(i);
48 if (c != nullptr)
49 c->setPricer(p);
50 }
51 }
52
54 ext::shared_ptr<EquityIndex> index,
55 const Date& baseDate,
56 const Date& fixingDate,
57 const Date& paymentDate,
58 bool growthOnly)
59 : IndexedCashFlow(notional, std::move(index), baseDate, fixingDate, paymentDate, growthOnly) {}
60
61 void EquityCashFlow::setPricer(const ext::shared_ptr<EquityCashFlowPricer>& pricer) {
62 if (pricer_ != nullptr)
65 if (pricer_ != nullptr)
67 update();
68 }
69
71 if (!pricer_)
73 pricer_->initialize(*this);
74 return notional() * pricer_->price();
75 }
76
78 Handle<YieldTermStructure> quantoCurrencyTermStructure,
79 Handle<BlackVolTermStructure> equityVolatility,
81 Handle<Quote> correlation)
82 : quantoCurrencyTermStructure_(std::move(quantoCurrencyTermStructure)),
83 equityVolatility_(std::move(equityVolatility)), fxVolatility_(std::move(fxVolatility)),
84 correlation_(std::move(correlation)){
89 }
90
92 index_ = ext::dynamic_pointer_cast<EquityIndex>(cashFlow.index());
93 if (!index_) {
94 QL_FAIL("Equity index required.");
95 }
96 baseDate_ = cashFlow.baseDate();
97 fixingDate_ = cashFlow.fixingDate();
98 QL_REQUIRE(fixingDate_ >= baseDate_, "Fixing date cannot fall before base date.");
99 growthOnlyPayoff_ = cashFlow.growthOnly();
100
101 QL_REQUIRE(!quantoCurrencyTermStructure_.empty(),
102 "Quanto currency term structure handle cannot be empty.");
103 QL_REQUIRE(!equityVolatility_.empty(),
104 "Equity volatility term structure handle cannot be empty.");
105 QL_REQUIRE(!fxVolatility_.empty(),
106 "FX volatility term structure handle cannot be empty.");
107 QL_REQUIRE(!correlation_.empty(), "Correlation handle cannot be empty.");
108
109 QL_REQUIRE(quantoCurrencyTermStructure_->referenceDate() ==
110 equityVolatility_->referenceDate() &&
111 equityVolatility_->referenceDate() == fxVolatility_->referenceDate(),
112 "Quanto currency term structure, equity and FX volatility need to have the same "
113 "reference date.");
114 }
115
117 Real strike = index_->fixing(fixingDate_);
118 Handle<YieldTermStructure> dividendHandle =
119 configureDividendHandle(index_->equityDividendCurve());
120
121 Handle<YieldTermStructure> quantoTermStructure(ext::make_shared<QuantoTermStructure>(
122 dividendHandle, quantoCurrencyTermStructure_,
123 index_->equityInterestRateCurve(), equityVolatility_, strike, fxVolatility_, 1.0,
124 correlation_->value()));
125 ext::shared_ptr<EquityIndex> quantoIndex =
127
128 Real I0 = quantoIndex->fixing(baseDate_);
129 Real I1 = quantoIndex->fixing(fixingDate_);
130
132 return I1 / I0 - 1.0;
133 return I1 / I0;
134 }
135}
Concrete date class.
Definition: date.hpp:125
ext::shared_ptr< EquityCashFlowPricer > pricer_
const ext::shared_ptr< EquityCashFlowPricer > & pricer() const
Real amount() const override
returns the amount of the cash flow
void setPricer(const ext::shared_ptr< EquityCashFlowPricer > &)
EquityCashFlow(Real notional, ext::shared_ptr< EquityIndex > index, const Date &baseDate, const Date &fixingDate, const Date &paymentDate, bool growthOnly=true)
ext::shared_ptr< EquityIndex > index_
EquityQuantoCashFlowPricer(Handle< YieldTermStructure > quantoCurrencyTermStructure, Handle< BlackVolTermStructure > equityVolatility, Handle< BlackVolTermStructure > fxVolatility, Handle< Quote > correlation)
void initialize(const EquityCashFlow &) override
Handle< YieldTermStructure > quantoTermStructure
Handle< BlackVolTermStructure > fxVolatility_
Handle< YieldTermStructure > quantoCurrencyTermStructure_
Handle< BlackVolTermStructure > equityVolatility_
Shared handle to an observable.
Definition: handle.hpp:41
Cash flow dependent on an index ratio.
virtual Date fixingDate() const
virtual Real notional() const
Real amount() const override
returns the amount of the cash flow
virtual Date baseDate() const
virtual bool growthOnly() const
virtual ext::shared_ptr< Index > index() const
void update() override
Definition: lazyobject.hpp:188
Size unregisterWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:245
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
void setCouponPricer(const Leg &leg, const ext::shared_ptr< FloatingRateCouponPricer > &pricer)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.