Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bondutils.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
22
23namespace ore {
24namespace data {
25
26void populateFromBondReferenceData(std::string& subType,
27 std::string& issuerId, std::string& settlementDays, std::string& calendar,
28 std::string& issueDate, std::string& priceQuoteMethod, string& priceQuoteBaseValue,
29 std::string& creditCurveId, std::string& creditGroup, std::string& referenceCurveId,
30 std::string& incomeCurveId, std::string& volatilityCurveId,
31 std::vector<LegData>& coupons, const std::string& name,
32 const QuantLib::ext::shared_ptr<BondReferenceDatum>& bondRefData,
33 const std::string& startDate, const std::string& endDate) {
34 DLOG("populating data bond from reference data");
35 QL_REQUIRE(bondRefData, "populateFromBondReferenceData(): empty bond reference datum given");
36 if (subType.empty()) {
37 subType = bondRefData->bondData().subType;
38 TLOG("overwrite subType with '" << subType << "'");
39 }
40 if (issuerId.empty()) {
41 issuerId = bondRefData->bondData().issuerId;
42 TLOG("overwrite issuerId with '" << issuerId << "'");
43 }
44 if (settlementDays.empty()) {
45 settlementDays = bondRefData->bondData().settlementDays;
46 TLOG("overwrite settlementDays with '" << settlementDays << "'");
47 }
48 if (calendar.empty()) {
49 calendar = bondRefData->bondData().calendar;
50 TLOG("overwrite calendar with '" << calendar << "'");
51 }
52 if (issueDate.empty()) {
53 issueDate = bondRefData->bondData().issueDate;
54 TLOG("overwrite issueDate with '" << issueDate << "'");
55 }
56 if (priceQuoteMethod.empty()) {
57 priceQuoteMethod = bondRefData->bondData().priceQuoteMethod;
58 TLOG("overwrite priceQuoteMethod with '" << priceQuoteMethod << "'");
59 }
60 if (priceQuoteBaseValue.empty()) {
61 priceQuoteBaseValue = bondRefData->bondData().priceQuoteBaseValue;
62 TLOG("overwrite priceQuoteBaseValue with '" << priceQuoteBaseValue << "'");
63 }
64 if (creditCurveId.empty()) {
65 creditCurveId = bondRefData->bondData().creditCurveId;
66 TLOG("overwrite creditCurveId with '" << creditCurveId << "'");
67 }
68 if (creditGroup.empty()) {
69 creditGroup = bondRefData->bondData().creditGroup;
70 TLOG("overwrite creditGroup with '" << creditGroup << "'");
71 }
72 if (referenceCurveId.empty()) {
73 referenceCurveId = bondRefData->bondData().referenceCurveId;
74 TLOG("overwrite referenceCurveId with '" << referenceCurveId << "'");
75 }
76 if (incomeCurveId.empty()) {
77 incomeCurveId = bondRefData->bondData().incomeCurveId;
78 TLOG("overwrite incomeCurveId with '" << incomeCurveId << "'");
79 }
80 if (volatilityCurveId.empty()) {
81 volatilityCurveId = bondRefData->bondData().volatilityCurveId;
82 TLOG("overwrite volatilityCurveId with '" << volatilityCurveId << "'");
83 }
84 if (coupons.empty()) {
85 coupons = bondRefData->bondData().legData;
86 TLOG("overwrite coupons with " << coupons.size() << " LegData nodes");
87 }
88 if (!startDate.empty()) {
89 if (coupons.size() == 1 && coupons.front().schedule().rules().size() == 1 && coupons.front().schedule().dates().size() == 0) {
90 string oldStart = coupons.front().schedule().rules().front().startDate();
91 coupons.front().schedule().modifyRules().front().modifyStartDate() = startDate;
92 string newStart = coupons.front().schedule().rules().front().startDate();
93 DLOG("Modified start date " << oldStart << " -> " << newStart);
94 }
95 else {
96 StructuredTradeErrorMessage(bondRefData->bondData().issuerId, "Bond-linked", "update reference data",
97 "modifified start date cannot be applied to multiple legs/schedules")
98 .log();
99 }
100 }
101 if (!endDate.empty()) {
102 if (coupons.size() == 1 && coupons.front().schedule().rules().size() == 1 && coupons.front().schedule().dates().size() == 0) {
103 string oldEnd = coupons.front().schedule().rules().front().endDate();
104 coupons.front().schedule().modifyRules().front().modifyEndDate() = endDate;
105 string newEnd = coupons.front().schedule().rules().front().endDate();
106 DLOG("Modified end date " << oldEnd << " -> " << newEnd);
107 }
108 else {
109 StructuredTradeErrorMessage(bondRefData->bondData().issuerId, "Bond-linked", "update reference data",
110 "modifified end date cannot be applied to multiple legs/schedules")
111 .log();
112 }
113 }
114
115 DLOG("populating bond data from reference data done.");
116}
117
118Date getOpenEndDateReplacement(const std::string& replacementPeriodStr, const Calendar& calendar) {
119 if (replacementPeriodStr.empty())
120 return QuantLib::Null<QuantLib::Date>();
121 Date today = Settings::instance().evaluationDate();
122 Date result = Date::maxDate() - 365;
123 try {
124 // might throw because we are beyond the last allowed date
125 result = (calendar.empty() ? NullCalendar() : calendar).advance(today, parsePeriod(replacementPeriodStr));
126 } catch (...) {
127 }
128 DLOG("Compute open end date replacement as "
129 << QuantLib::io::iso_date(result) << " (today = " << QuantLib::io::iso_date(today)
130 << ", OpenEndDateReplacement from pricing engine config = " << replacementPeriodStr << ")");
131 return result;
132}
133
134} // namespace data
135} // namespace ore
bond utilities
void log() const
generate Boost log record to pass to corresponding sinks
Definition: log.cpp:491
Utility class for Structured Trade errors, contains the Trade ID and Type.
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Definition: parsers.cpp:171
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define TLOG(text)
Logging Macro (Level = Data)
Definition: log.hpp:556
Calendar calendar
Definition: utilities.cpp:441
Date getOpenEndDateReplacement(const std::string &replacementPeriodStr, const Calendar &calendar)
Definition: bondutils.cpp:118
void populateFromBondReferenceData(std::string &subType, std::string &issuerId, std::string &settlementDays, std::string &calendar, std::string &issueDate, std::string &priceQuoteMethod, string &priceQuoteBaseValue, std::string &creditCurveId, std::string &creditGroup, std::string &referenceCurveId, std::string &incomeCurveId, std::string &volatilityCurveId, std::vector< LegData > &coupons, const std::string &name, const QuantLib::ext::shared_ptr< BondReferenceDatum > &bondRefData, const std::string &startDate, const std::string &endDate)
Populate bond data from name and ReferenceDataManager.
Definition: bondutils.cpp:26
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Structured Trade Error class.
string name