QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
equitycashflow.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
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
102 "Quanto currency term structure handle cannot be 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}
Actual/365 (Fixed) day counter.
const Real correlation_
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
equity cash flow
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
flat forward rate term structure
QL_REAL Real
real number
Definition: types.hpp:50
Cash flow dependent on an index ratio (NOT a coupon, i.e. no accruals)
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.
Calendar for reproducing theoretical calculations.
Quanto term structure.
simple quote class