Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
calendaradjustmentconfig.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
23#include <ql/time/calendar.hpp>
24#include <string>
25namespace ore {
26namespace data {
27
29
30void CalendarAdjustmentConfig::addHolidays(const string& calname, const Date& d) {
31 additionalHolidays_[normalisedName(calname)].insert(d);
32}
33
34void CalendarAdjustmentConfig::addBusinessDays(const string& calname, const Date& d) {
35 additionalBusinessDays_[normalisedName(calname)].insert(d);
36}
37
38void CalendarAdjustmentConfig::addBaseCalendar(const string& calname, const string& s) {
39 baseCalendars_[normalisedName(calname)] = s;
40}
41
42const set<Date>& CalendarAdjustmentConfig::getHolidays(const string& calname) const {
43 auto holidays = additionalHolidays_.find(normalisedName(calname));
44 if (holidays != additionalHolidays_.end()) {
45 return holidays->second;
46 } else {
47 static set<Date> empty;
48 return empty;
49 }
50}
51
52const set<Date>& CalendarAdjustmentConfig::getBusinessDays(const string& calname) const {
53 auto businessDays = additionalBusinessDays_.find(normalisedName(calname));
54 if (businessDays != additionalBusinessDays_.end()) {
55 return businessDays->second;
56 } else {
57 static set<Date> empty;
58 return empty;
59 }
60}
61
63 set<string> cals;
64 for (auto it : additionalHolidays_) {
65 cals.insert(it.first);
66 }
67 for (auto it : additionalBusinessDays_) {
68 cals.insert(it.first);
69 }
70 return cals;
71}
72
73
74const string& CalendarAdjustmentConfig::getBaseCalendar(const string& calname) const {
75 auto bc = baseCalendars_.find(normalisedName(calname));
76
77 if (bc != baseCalendars_.end()) {
78 return bc->second;
79 } else {
80 static const string empty = "";
81 return empty;
82 }
83}
84string CalendarAdjustmentConfig::normalisedName(const string& c) const { return parseCalendar(c).name(); }
85
87 for (auto it : c.getCalendars()) {
88 for (auto h : c.getHolidays(it)) {
89 addHolidays(it, h);
90 }
91 for (auto b : c.getBusinessDays(it)) {
92 addBusinessDays(it, b);
93 }
94 }
95};
96
98 XMLUtils::checkNode(node, "CalendarAdjustments");
99
100 //we loop once skipping any new calendars
101 for (auto calnode : XMLUtils::getChildrenNodes(node, "Calendar")) {
102 string calname = XMLUtils::getAttribute(calnode, "name");
103 string baseCalendar = XMLUtils::getChildValue(calnode, "BaseCalendar", false);
104 if (baseCalendar != "") {
105 // we check here if the baseCalendar is in the map before the new calendars are added
106 // this is because we don't want any new calendars to be defined in terms of other new calendars
107 parseCalendar(baseCalendar);
108 continue;
109 }
110 Calendar cal = parseCalendar(calname);
111
112 vector<string> holidayDates = XMLUtils::getChildrenValues(calnode, "AdditionalHolidays", "Date");
113 for (auto holiday : holidayDates) {
114 try {
115 Date h = parseDate(holiday);
116 addHolidays(calname, h);
117 cal.addHoliday(h);
118 } catch(std::exception&) {
119 ALOG("error parsing holiday " << holiday << " for calendar " << calname);
120 }
121 }
122 vector<string> businessDates = XMLUtils::getChildrenValues(calnode, "AdditionalBusinessDays", "Date");
123 for (auto businessDay : businessDates) {
124 try {
125 Date b = parseDate(businessDay);
126 addBusinessDays(calname, b);
127 cal.removeHoliday(b);
128 } catch(std::exception&) {
129 ALOG("error parsing business day " << businessDay << " for calendar " << calname);
130 }
131 }
132 }
133 //then loop again adding the new calendars
134 for (auto calnode : XMLUtils::getChildrenNodes(node, "Calendar")) {
135 string calname = XMLUtils::getAttribute(calnode, "name");
136 string baseCalendar = XMLUtils::getChildValue(calnode, "BaseCalendar", false);
137 if (baseCalendar == "")
138 continue;
139 Calendar cal = CalendarParser::instance().addCalendar(baseCalendar, calname);
140
141 vector<string> holidayDates = XMLUtils::getChildrenValues(calnode, "AdditionalHolidays", "Date");
142 for (auto holiday : holidayDates) {
143 try {
144 Date h = parseDate(holiday);
145 addHolidays(calname, h);
146 cal.addHoliday(h);
147 } catch(std::exception&) {
148 ALOG("error parsing business day " << holiday << " for calendar " << calname);
149 }
150
151 }
152 vector<string> businessDates = XMLUtils::getChildrenValues(calnode, "AdditionalBusinessDays", "Date");
153 for (auto businessDay : businessDates) {
154 try {
155 Date b = parseDate(businessDay);
156 addBusinessDays(calname, b);
157 cal.removeHoliday(b);
158 } catch(std::exception&) {
159 ALOG("error parsing business day " << businessDay << " for calendar " << calname);
160 }
161 }
162
163 addBaseCalendar(calname, baseCalendar);
164 }
165
166}
167
169 XMLNode* node = doc.allocNode("CalendarAdjustments");
170 for (auto cal : getCalendars()) {
171 XMLNode* calendarNode = XMLUtils::addChild(doc, node, "Calendar");
172 XMLUtils::addAttribute(doc, calendarNode, "name", cal);
173 if (getBaseCalendar(cal) != "")
174 XMLUtils::addChild(doc, calendarNode, "BaseCalendar", getBaseCalendar(cal));
175 XMLNode* ahd = XMLUtils::addChild(doc, calendarNode, "AdditionalHolidays");
176 for (auto hol : getHolidays(cal)) {
177 XMLUtils::addChild(doc, ahd, "Date", to_string(hol));
178 }
179 XMLNode* abd = XMLUtils::addChild(doc, calendarNode, "AdditionalBusinessDays");
180 for (auto bd : getBusinessDays(cal)) {
181 XMLUtils::addChild(doc, abd, "Date", to_string(bd));
182 }
183 }
184 return node;
185}
186
187} // namespace data
188} // namespace ore
Interface for calendar modifications, additional holidays and business days.
calendar parser singleton class
const set< Date > & getHolidays(const string &calname) const
Returns all the holidays for a given cal name.
const string & getBaseCalendar(const string &calname) const
void addBusinessDays(const string &calname, const Date &d)
This method adds d to the list of business days for cal name.
string normalisedName(const string &) const
XMLNode * toXML(XMLDocument &doc) const override
void addBaseCalendar(const string &calname, const string &d)
This method adds s as a base calendar for cal name.
const set< Date > & getBusinessDays(const string &calname) const
Returns all the business days for a given calname.
map< string, set< Date > > additionalHolidays_
void addHolidays(const string &calname, const Date &d)
This method adds d to the list of holidays for cal name.
void append(const CalendarAdjustmentConfig &c)
add all holidays and business days from c to this instance
map< string, set< Date > > additionalBusinessDays_
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void addAttribute(XMLDocument &doc, XMLNode *node, const string &attrName, const string &attrValue)
Definition: xmlutils.cpp:412
static string getAttribute(XMLNode *node, const string &attrName)
Definition: xmlutils.cpp:419
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:306
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
@ data
Definition: log.hpp:77
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities