Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equityfxlegdata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
25#include <ql/cashflows/simplecashflow.hpp>
27
28#include <boost/algorithm/string/predicate.hpp>
29#include <boost/make_shared.hpp>
30
31using namespace ore::data;
32using namespace QuantExt;
33using namespace QuantLib;
34
35using boost::iequals;
42using QuantLib::Natural;
43using QuantLib::Real;
44using std::ostream;
45using std::string;
46using std::vector;
47using QuantExt::Leg;
48
49namespace ore {
50namespace data {
51
54 rates_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Rates", "Rate", "startDate", rateDates_, parseReal,
55 true);
56 initialMarginFactor_ = XMLUtils::getChildValueAsDouble(node, "InitialMarginFactor", true);
57 multiplier_ = 1;
58 if (XMLUtils::getChildNode(node, "Multiplier"))
59 multiplier_ = XMLUtils::getChildValueAsDouble(node, "Multiplier");
60 XMLNode* equityNode = XMLUtils::getChildNode(node, "EquityLegData");
61 QL_REQUIRE(equityNode, "no equityLegData provided");
62
63 QuantLib::ext::shared_ptr<ore::data::EquityLegData> ld = QuantLib::ext::make_shared<EquityLegData>();
64 ld->fromXML(equityNode);
65 equityLegData_ = ld;
66
67}
68
70 XMLNode* node = doc.allocNode(legNodeName());
71 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Rates", "Rate", rates_, "startDate", rateDates_);
72 XMLUtils::addChild(doc, node, "InitialMarginFactor", initialMarginFactor_);
73 XMLUtils::addChild(doc, node, "Multiplier", multiplier_);
74
75 XMLUtils::appendNode(node, equityLegData_->toXML(doc));
76 return node;
77}
78
79QuantExt::Leg makeEquityMarginLeg(const LegData& data, const QuantLib::ext::shared_ptr<EquityIndex2>& equityCurve,
80 const QuantLib::ext::shared_ptr<QuantExt::FxIndex>& fxIndex,
81 const QuantLib::Date& openEndDateReplacement) {
82 QuantLib::ext::shared_ptr<EquityMarginLegData> eqMarginLegData = QuantLib::ext::dynamic_pointer_cast<EquityMarginLegData>(data.concreteLegData());
83 QL_REQUIRE(eqMarginLegData, "Wrong LegType, expected EquityMargin, got " << data.legType());
84 QuantLib::ext::shared_ptr<ore::data::EquityLegData> eqLegData = eqMarginLegData->equityLegData();
85 QL_REQUIRE(eqLegData, "expected equityLegData");
86 QuantExt::Schedule schedule = makeSchedule(data.schedule(), openEndDateReplacement);
87 DayCounter dc = parseDayCounter(data.dayCounter());
88 BusinessDayConvention bdc = ore::data::parseBusinessDayConvention(data.paymentConvention());
89 bool isTotalReturn = eqLegData->returnType() == EquityReturnType::Total;
90 Real dividendFactor = eqLegData->dividendFactor();
91 Real initialPrice = eqLegData->initialPrice();
92 bool initialPriceIsInTargetCcy = false;
93
94 if (!eqLegData->initialPriceCurrency().empty()) {
95 // parse currencies to handle minor currencies
96 Currency initialPriceCurrency = parseCurrencyWithMinors(eqLegData->initialPriceCurrency());
97 Currency dataCurrency = parseCurrencyWithMinors(data.currency());
98 Currency eqCurrency;
99 // set equity currency
100 if (!equityCurve->currency().empty())
101 eqCurrency = equityCurve->currency();
102 else
103 TLOG("Cannot find currency for equity " << equityCurve->name());
104
105 // initial price currency must match leg or equity currency
106 QL_REQUIRE(initialPriceCurrency == dataCurrency ||
107 initialPriceCurrency == eqCurrency || eqCurrency.empty(),
108 "initial price ccy (" << initialPriceCurrency << ") must match either leg ccy ("
109 << dataCurrency << ") or equity ccy (if given, got '"
110 << eqCurrency << "')");
111 initialPriceIsInTargetCcy = initialPriceCurrency == dataCurrency;
112 // adjust for minor currency
113 initialPrice = convertMinorToMajorCurrency(eqLegData->initialPriceCurrency(), initialPrice);
114 }
115 bool notionalReset = eqLegData->notionalReset();
116 Natural fixingDays = eqLegData->fixingDays();
117 PaymentLag paymentLag = parsePaymentLag(data.paymentLag());
118 ScheduleData valuationData = eqLegData->valuationSchedule();
119 Schedule valuationSchedule;
120 if (valuationData.hasData())
121 valuationSchedule = makeSchedule(valuationData, openEndDateReplacement);
122 vector<double> notionals = buildScheduledVector(data.notionals(), data.notionalDates(), schedule);
123 vector<double> rates = buildScheduledVector(eqMarginLegData->rates(), eqMarginLegData->rateDates(), schedule);
124
125 applyAmortization(notionals, data, schedule, false);
126 Leg leg = EquityMarginLeg(schedule, equityCurve, fxIndex)
127 .withCouponRates(rates, dc)
128 .withInitialMarginFactor(eqMarginLegData->initialMarginFactor())
129 .withNotionals(notionals)
130 .withQuantity(eqLegData->quantity())
133 .withPaymentLag(boost::apply_visitor(PaymentLagInteger(), paymentLag))
134 .withTotalReturn(isTotalReturn)
135 .withDividendFactor(dividendFactor)
136 .withInitialPrice(initialPrice)
137 .withInitialPriceIsInTargetCcy(initialPriceIsInTargetCcy)
138 .withNotionalReset(notionalReset)
139 .withFixingDays(fixingDays)
140 .withValuationSchedule(valuationSchedule)
141 .withMultiplier(eqMarginLegData->multiplier());
142
143 QL_REQUIRE(leg.size() > 0, "Empty Equity Margin Leg");
144
145 return leg;
146}
147
148} // namespace data
149} // namespace ore
EquityMarginLeg & withPaymentLag(Natural paymentLag)
EquityMarginLeg & withQuantity(Real)
EquityMarginLeg & withNotionalReset(bool)
EquityMarginLeg & withNotionals(const std::vector< Real > &notionals)
EquityMarginLeg & withFixingDays(Natural)
EquityMarginLeg & withDividendFactor(Real)
EquityMarginLeg & withInitialPrice(Real)
EquityMarginLeg & withInitialPriceIsInTargetCcy(bool)
EquityMarginLeg & withPaymentAdjustment(BusinessDayConvention convention)
EquityMarginLeg & withTotalReturn(bool)
EquityMarginLeg & withPaymentDayCounter(const DayCounter &dayCounter)
EquityMarginLeg & withMultiplier(Real)
EquityMarginLeg & withValuationSchedule(const Schedule &valuationSchedule)
EquityMarginLeg & withInitialMarginFactor(const Real &marginFactor)
EquityMarginLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
virtual void fromXML(ore::data::XMLNode *node) override
QuantLib::ext::shared_ptr< ore::data::EquityLegData > equityLegData_
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
const string & legNodeName() const
Definition: legdata.hpp:70
Serializable object holding leg data.
Definition: legdata.hpp:844
Serializable schedule data.
Definition: schedule.hpp:202
bool hasData() const
Check if has any dates/rules/derived schedules.
Definition: schedule.hpp:223
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
XML Utilities Class.
Definition: xmlutils.hpp:119
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static void addChildrenWithOptionalAttributes(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values, const string &attrName, const vector< string > &attrs)
Definition: xmlutils.cpp:542
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
leg data for equityfx leg types
Currency parseCurrencyWithMinors(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:310
QuantLib::Real convertMinorToMajorCurrency(const std::string &s, QuantLib::Real value)
Convert a value from a minor ccy to major.
Definition: parsers.cpp:324
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
PaymentLag parsePaymentLag(const string &s)
Convert text to PaymentLag.
Definition: parsers.cpp:628
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
DayCounter parseDayCounter(const string &s)
Convert text to QuantLib::DayCounter.
Definition: parsers.cpp:209
leg data model and serialization
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define TLOG(text)
Logging Macro (Level = Data)
Definition: log.hpp:556
void applyAmortization(std::vector< Real > &notionals, const LegData &data, const Schedule &schedule, const bool annuityAllowed, const std::vector< Real > &rates)
Definition: legdata.cpp:2593
QuantExt::Leg makeEquityMarginLeg(const LegData &data, const QuantLib::ext::shared_ptr< EquityIndex2 > &equityCurve, const QuantLib::ext::shared_ptr< QuantExt::FxIndex > &fxIndex, const QuantLib::Date &openEndDateReplacement)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
vector< T > buildScheduledVector(const vector< T > &values, const vector< string > &dates, const Schedule &schedule, const bool checkAllValuesAppearInResult=false)
Definition: legdata.hpp:1061
boost::variant< QuantLib::Period, QuantLib::Natural > PaymentLag
Definition: types.hpp:32
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities