19#include <boost/algorithm/string.hpp>
23#include <ql/settings.hpp>
24#include <ql/time/daycounters/actualactual.hpp>
33 : dates_(1, Settings::instance().evaluationDate()), tenors_(1, 0 * Days), times_(1, 0.0),
34 timeGrid_(times_.begin(), times_.end()), isValuationDate_(1, true), isCloseOutDate_(1, false) {}
37 : calendar_(gridCalendar), dayCounter_(dayCounter) {
39 if (grid ==
"ALPHA") {
44 for (Size i = 1; i < 40; i++) {
45 Period p(i * 3, Months);
49 for (Size i = 10; i < 30; i++)
50 tenors_.push_back(Period(i, Years));
51 for (Size i = 30; i < 105; i += 5)
52 tenors_.push_back(Period(i, Years));
53 }
else if (grid ==
"BETA") {
59 for (Size i = 1; i < 119; i++) {
60 Period p = i * Months;
64 for (Size i = 40; i < 80; i++) {
65 Period p = i * 3 * Months;
69 for (Size i = 20; i < 50; i++)
71 for (Size i = 50; i <= 100; i += 5)
74 vector<string> tokens;
75 boost::split(tokens, grid, boost::is_any_of(
","));
76 if (tokens.size() <= 2) {
78 Period gridTenor = 1 * Years;
79 Size gridSize = atoi(tokens[0].c_str());
80 QL_REQUIRE(gridSize > 0,
"Invalid DateGrid string " << grid);
81 if (tokens.size() == 2)
83 if (gridTenor == Period(1, Days)) {
86 Date today = Settings::instance().evaluationDate();
88 for (Size i = 0; i < gridSize; i++) {
89 d = gridCalendar.advance(d, Period(1, Days), Following);
91 tenors_.push_back(Period(n, Days));
94 for (Size i = 0; i < gridSize; i++)
95 tenors_.push_back((i + 1) * gridTenor);
99 for (Size i = 0; i < tokens.size(); i++)
107 const QuantLib::DayCounter& dayCounter)
108 : calendar_(gridCalendar), dayCounter_(dayCounter), tenors_(tenors) {
109 QL_REQUIRE(!
tenors_.empty(),
"DateGrid requires a non-empty vector of tenors");
111 "Construction of DateGrid requires a sorted vector of unique tenors");
116 : calendar_(cal), dayCounter_(dayCounter), dates_(dates) {
117 QL_REQUIRE(!
dates_.empty(),
"Construction of DateGrid requires a non-empty vector of dates");
119 "Construction of DateGrid requires a sorted vector of unique dates");
120 Date today = Settings::instance().evaluationDate();
121 QL_REQUIRE(today <
dates_.front(),
122 "Construction of DateGrid requires first element to be strictly greater than today");
127 for (Size i = 0; i <
dates_.size(); i++) {
143 Date today = Settings::instance().evaluationDate();
144 for (Size i = 0; i <
tenors_.size(); i++) {
145 if (
tenors_[i].units() == Days)
148 dates_[i] = cal.advance(today,
tenors_[i], Following,
false);
150 QL_REQUIRE(
dates_[i] >=
dates_[i - 1],
"DateGrid::buildDates(): tenors must be monotonic");
161 for (Size i = 0; i <
dates_.size(); i++)
173 DLOG(
"DateGrid constructed, size = " <<
size());
174 for (Size i = 0; i <
tenors_.size(); i++)
175 DLOG(
"[" << setw(2) << i <<
"] Tenor:" <<
tenors_[i] <<
", Date:" << io::iso_date(
dates_[i])
182 DLOG(
"Truncating DateGrid beyond " << QuantLib::io::iso_date(d));
183 vector<Date>::iterator it = std::upper_bound(
dates_.begin(),
dates_.end(), d);
195 if (
dates_.size() > len) {
196 DLOG(
"Truncating DateGrid, removing elements " <<
dates_[len] <<
" to " <<
dates_.back());
209 if (p == QuantLib::Period(0, QuantLib::Days)) {
210 for (Size i = 0; i <
dates_.size(); ++i) {
214 }
else if (i ==
dates_.size() - 1) {
225 std::set<QuantLib::Date> tmpCloseOutDates;
226 std::set<QuantLib::Date> tmpDates;
227 std::set<QuantLib::Date> tmpValueDates;
228 for (Size i = 0; i <
dates_.size(); ++i) {
230 if (p.units() == Days)
234 tmpCloseOutDates.insert(c);
236 tmpDates.insert(
dates_[i]);
238 tmpValueDates.insert(
dates_[i]);
241 dates_.assign(tmpDates.begin(), tmpDates.end());
244 for(
size_t i = 0; i <
dates_.size(); ++i){
246 if (tmpCloseOutDates.count(d) == 1) {
249 if(tmpValueDates.count(d) == 0){
254 tenors_ = std::vector<QuantLib::Period>(
dates_.size(), 0 * Days);
256 Date today = Settings::instance().evaluationDate();
257 for (Size i = 0; i <
dates_.size(); i++)
262 DLOG(
"Added Close Out Dates to DateGrid , size = " <<
size());
267 std::vector<Date> res;
268 for (Size i = 0; i <
dates_.size(); ++i) {
276 std::vector<Date> res;
277 for (Size i = 0; i <
dates_.size(); ++i) {
285 std::vector<Real>
times;
286 Date today = Settings::instance().evaluationDate();
287 for (Size i = 0; i <
dates_.size(); ++i) {
295 std::vector<Real>
times;
296 Date today = Settings::instance().evaluationDate();
297 for (Size i = 0; i <
dates_.size(); ++i) {
313 const QuantLib::Period& shift) {
314 DLOG(
"Building shifted date grid with shift of " << shift);
315 vector<Date> defaultDates = dg->dates();
316 vector<Date> closeOutDates;
317 for (
auto d : defaultDates) {
318 Date closeOut = dg->calendar().adjust(d + shift);
319 closeOutDates.push_back(closeOut);
321 QuantLib::ext::shared_ptr<DateGrid> newDg = QuantLib::ext::make_shared<DateGrid>(closeOutDates, dg->calendar(), dg->dayCounter());
325QuantLib::ext::shared_ptr<DateGrid>
combineDateGrids(
const QuantLib::ext::shared_ptr<DateGrid>& dg1,
326 const QuantLib::ext::shared_ptr<DateGrid>& dg2) {
327 DLOG(
"Combining date grids");
328 vector<Date> combinedVec;
329 vector<Date> dates1 = dg1->dates();
330 vector<Date> dates2 = dg2->dates();
331 combinedVec.reserve(dates1.size() + dates2.size());
332 combinedVec.insert(combinedVec.end(), dates1.begin(), dates1.end());
333 combinedVec.insert(combinedVec.end(), dates2.begin(), dates2.end());
334 std::sort(combinedVec.begin(), combinedVec.end());
335 auto last = std::unique(combinedVec.begin(), combinedVec.end());
336 combinedVec.erase(last, combinedVec.end());
338 QuantLib::ext::shared_ptr<DateGrid> newDg = QuantLib::ext::make_shared<DateGrid>(combinedVec, dg1->calendar(), dg1->dayCounter());
const TimeGrid & timeGrid_
void truncate(const QuantLib::Date &d, bool overrun=true)
Truncate the grid up to the given date.
std::map< QuantLib::Date, QuantLib::Date > valuationCloseOutMap_
DateGrid()
Build a date grid with a single date equal to Settings::instance().evaluationDate()
QuantLib::TimeGrid closeOutTimeGrid() const
Returns the time grid associated with the vector of close-out times (plus t=0)
QuantLib::Size size() const
The size of the date grid.
std::vector< QuantLib::Date > closeOutDates() const
std::vector< QuantLib::Date > dates_
std::vector< QuantLib::Date > valuationDates() const
const std::vector< QuantLib::Time > & times() const
Returns the times from Settings::instance().evaluationDate to each Date using the day counter.
std::vector< bool > isValuationDate_
std::vector< QuantLib::Period > tenors_
const QuantLib::DayCounter & dayCounter() const
QuantLib::Date closeOutDateFromValuationDate(const QuantLib::Date &d) const
QuantLib::Calendar calendar_
QuantLib::TimeGrid valuationTimeGrid() const
Returns the time grid associated with the vector of valuation times (plus t=0)
std::vector< QuantLib::Time > times_
void addCloseOutDates(const QuantLib::Period &p=QuantLib::Period(2, QuantLib::Weeks))
QuantLib::DayCounter dayCounter_
std::vector< bool > isCloseOutDate_
QuantLib::TimeGrid timeGrid_
void buildDates(const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc)
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
QuantLib::ext::shared_ptr< DateGrid > combineDateGrids(const QuantLib::ext::shared_ptr< DateGrid > &dg1, const QuantLib::ext::shared_ptr< DateGrid > &dg2)
QuantLib::ext::shared_ptr< DateGrid > generateShiftedDateGrid(const QuantLib::ext::shared_ptr< DateGrid > &dg, const QuantLib::Period &shift)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.