Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
DateGrid Class Reference

Simulation Date Grid. More...

#include <ored/utilities/dategrid.hpp>

+ Collaboration diagram for DateGrid:

Public Member Functions

 DateGrid ()
 Build a date grid with a single date equal to Settings::instance().evaluationDate() More...
 
 DateGrid (const std::string &grid, const QuantLib::Calendar &gridCalendar=QuantLib::TARGET(), const QuantLib::DayCounter &dayCounter=QuantLib::ActualActual(QuantLib::ActualActual::ISDA))
 
 DateGrid (const std::vector< QuantLib::Period > &tenors, const QuantLib::Calendar &gridCalendar=QuantLib::TARGET(), const QuantLib::DayCounter &dayCounter=QuantLib::ActualActual(QuantLib::ActualActual::ISDA))
 Build a date grid from the given vector of tenors. More...
 
 DateGrid (const std::vector< QuantLib::Date > &dates, const QuantLib::Calendar &gridCalendar=QuantLib::TARGET(), const QuantLib::DayCounter &dayCounter=QuantLib::ActualActual(QuantLib::ActualActual::ISDA))
 Build a date grid from an explicit set of dates, sorted in ascending order. More...
 
QuantLib::Size size () const
 The size of the date grid. More...
 
void truncate (const QuantLib::Date &d, bool overrun=true)
 Truncate the grid up to the given date. More...
 
void truncate (QuantLib::Size length)
 Truncate the grid to the given length. More...
 
void addCloseOutDates (const QuantLib::Period &p=QuantLib::Period(2, QuantLib::Weeks))
 

Inspectors

QuantLib::Calendar calendar_
 
QuantLib::DayCounter dayCounter_
 
std::vector< QuantLib::Date > dates_
 
std::map< QuantLib::Date, QuantLib::Date > valuationCloseOutMap_
 
std::vector< QuantLib::Period > tenors_
 
std::vector< QuantLib::Time > times_
 
QuantLib::TimeGrid timeGrid_
 
std::vector< boolisValuationDate_
 
std::vector< boolisCloseOutDate_
 
const std::vector< QuantLib::Period > & tenors () const
 
const std::vector< QuantLib::Date > & dates () const
 
const std::vector< bool > & isValuationDate () const
 
const std::vector< bool > & isCloseOutDate () const
 
std::vector< QuantLib::Date > valuationDates () const
 
std::vector< QuantLib::Date > closeOutDates () const
 
const QuantLib::Calendar & calendar () const
 
const QuantLib::DayCounter & dayCounter () const
 
const std::vector< QuantLib::Time > & times () const
 Returns the times from Settings::instance().evaluationDate to each Date using the day counter. More...
 
const QuantLib::TimeGrid & timeGrid () const
 Returns the time grid associated with the vector of times (plus t=0) More...
 
QuantLib::TimeGrid valuationTimeGrid () const
 Returns the time grid associated with the vector of valuation times (plus t=0) More...
 
QuantLib::TimeGrid closeOutTimeGrid () const
 Returns the time grid associated with the vector of close-out times (plus t=0) More...
 
QuantLib::Date closeOutDateFromValuationDate (const QuantLib::Date &d) const
 
const QuantLib::Date & operator[] (QuantLib::Size i) const
 Accessor methods. More...
 
void buildDates (const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc)
 
void log ()
 

Detailed Description

Simulation Date Grid.

Utility for building a simulation date grid.

Definition at line 38 of file dategrid.hpp.

Constructor & Destructor Documentation

◆ DateGrid() [1/4]

DateGrid ( )

Build a date grid with a single date equal to Settings::instance().evaluationDate()

Definition at line 32 of file dategrid.cpp.

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) {}
std::vector< QuantLib::Date > dates_
Definition: dategrid.hpp:116
std::vector< bool > isValuationDate_
Definition: dategrid.hpp:121
std::vector< QuantLib::Period > tenors_
Definition: dategrid.hpp:118
std::vector< QuantLib::Time > times_
Definition: dategrid.hpp:119
std::vector< bool > isCloseOutDate_
Definition: dategrid.hpp:121
QuantLib::TimeGrid timeGrid_
Definition: dategrid.hpp:120

◆ DateGrid() [2/4]

DateGrid ( const std::string &  grid,
const QuantLib::Calendar &  gridCalendar = QuantLib::TARGET(),
const QuantLib::DayCounter &  dayCounter = QuantLib::ActualActual(QuantLib::ActualActual::ISDA) 
)

Build a date grid from a string which can be of the form 40,1M or 1D,2D,1W,2W,3Y,5Y or a fixed name (ALPHA, BETA) indicating a hard coded grid structure

Definition at line 36 of file dategrid.cpp.

37 : calendar_(gridCalendar), dayCounter_(dayCounter) {
38
39 if (grid == "ALPHA") {
40 // ALPHA is
41 // quarterly up to 10Y,
42 // annual up to 30Y,
43 // quinquennial up to 100Y
44 for (Size i = 1; i < 40; i++) { // 3M up to 39*3M = 117M = 9Y9M
45 Period p(i * 3, Months);
46 p.normalize();
47 tenors_.push_back(p);
48 }
49 for (Size i = 10; i < 30; i++) // 10Y up to 29Y
50 tenors_.push_back(Period(i, Years));
51 for (Size i = 30; i < 105; i += 5) // 30Y up to 100Y
52 tenors_.push_back(Period(i, Years));
53 } else if (grid == "BETA") {
54 // BETA is
55 // monthly up to 10Y,
56 // quarterly up to 20Y,
57 // annually up to 50Y,
58 // quinquennial up to 100Y
59 for (Size i = 1; i < 119; i++) {
60 Period p = i * Months;
61 p.normalize();
62 tenors_.push_back(p);
63 }
64 for (Size i = 40; i < 80; i++) {
65 Period p = i * 3 * Months;
66 p.normalize();
67 tenors_.push_back(p);
68 }
69 for (Size i = 20; i < 50; i++)
70 tenors_.push_back(i * Years);
71 for (Size i = 50; i <= 100; i += 5)
72 tenors_.push_back(i * Years);
73 } else { // uniform grid of format "numPillars,spacing" (e.g. 40,1M)
74 vector<string> tokens;
75 boost::split(tokens, grid, boost::is_any_of(","));
76 if (tokens.size() <= 2) {
77 // uniform grid of format "numPillars,spacing" (e.g. 40,1M)
78 Period gridTenor = 1 * Years; // default
79 Size gridSize = atoi(tokens[0].c_str());
80 QL_REQUIRE(gridSize > 0, "Invalid DateGrid string " << grid);
81 if (tokens.size() == 2)
82 gridTenor = data::parsePeriod(tokens[1]);
83 if (gridTenor == Period(1, Days)) {
84 // we have a daily grid. Period and Calendar are not consistent with
85 // working & actual days, so we set the tenor grid
86 Date today = Settings::instance().evaluationDate();
87 Date d = today;
88 for (Size i = 0; i < gridSize; i++) {
89 d = gridCalendar.advance(d, Period(1, Days), Following); // next working day
90 Size n = d - today;
91 tenors_.push_back(Period(n, Days));
92 }
93 } else {
94 for (Size i = 0; i < gridSize; i++)
95 tenors_.push_back((i + 1) * gridTenor);
96 }
97 } else {
98 // New style : 1D,2D,1W,2W,3Y,5Y,....
99 for (Size i = 0; i < tokens.size(); i++)
100 tenors_.push_back(data::parsePeriod(tokens[i]));
101 }
102 }
103 buildDates(gridCalendar, dayCounter);
104}
const QuantLib::DayCounter & dayCounter() const
Definition: dategrid.hpp:87
QuantLib::Calendar calendar_
Definition: dategrid.hpp:114
QuantLib::DayCounter dayCounter_
Definition: dategrid.hpp:115
void buildDates(const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc)
Definition: dategrid.cpp:139
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Definition: parsers.cpp:171
+ Here is the call graph for this function:

◆ DateGrid() [3/4]

DateGrid ( const std::vector< QuantLib::Period > &  tenors,
const QuantLib::Calendar &  gridCalendar = QuantLib::TARGET(),
const QuantLib::DayCounter &  dayCounter = QuantLib::ActualActual(QuantLib::ActualActual::ISDA) 
)

Build a date grid from the given vector of tenors.

◆ DateGrid() [4/4]

DateGrid ( const std::vector< QuantLib::Date > &  dates,
const QuantLib::Calendar &  gridCalendar = QuantLib::TARGET(),
const QuantLib::DayCounter &  dayCounter = QuantLib::ActualActual(QuantLib::ActualActual::ISDA) 
)

Build a date grid from an explicit set of dates, sorted in ascending order.

Member Function Documentation

◆ size()

QuantLib::Size size ( ) const

The size of the date grid.

Definition at line 58 of file dategrid.hpp.

58{ return dates_.size(); }
+ Here is the caller graph for this function:

◆ truncate() [1/2]

void truncate ( const QuantLib::Date &  d,
bool  overrun = true 
)

Truncate the grid up to the given date.

If overrun is true, we make sure the last date in the grid is greater than the portfolio maturity, even though every scenario portfolio NPV will be 0 at this point we may need the market data. If overrun is false, the last date in the grid is the last date where the portfolio is live.

◆ truncate() [2/2]

void truncate ( QuantLib::Size  length)

Truncate the grid to the given length.

◆ addCloseOutDates()

void addCloseOutDates ( const QuantLib::Period &  p = QuantLib::Period(2, QuantLib::Weeks))

Add close out dates. If 0D is given, the valuation dates itself are treated as close out dates. The first date is a valuation date only and the last date is a close out date only then, all other dates are both valuation and close out dates.

Definition at line 207 of file dategrid.cpp.

207 {
208 valuationCloseOutMap_.clear();
209 if (p == QuantLib::Period(0, QuantLib::Days)) {
210 for (Size i = 0; i < dates_.size(); ++i) {
211 if (i == 0) {
212 isCloseOutDate_.front() = false;
213 isValuationDate_.front() = true;
214 } else if (i == dates_.size() - 1) {
215 isCloseOutDate_.back() = true;
216 isValuationDate_.back() = false;
217 } else {
218 isCloseOutDate_[i] = true;
219 isValuationDate_[i] = true;
220 }
221 if (isCloseOutDate_[i] && i > 0)
223 }
224 } else {
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) {
229 Date c;
230 if (p.units() == Days)
231 c = calendar_.adjust(dates_[i] + p);
232 else
233 c = calendar_.advance(dates_[i], p, Following, false);
234 tmpCloseOutDates.insert(c);
236 tmpDates.insert(dates_[i]);
237 tmpDates.insert(c);
238 tmpValueDates.insert(dates_[i]);
239 }
240 dates_.clear();
241 dates_.assign(tmpDates.begin(), tmpDates.end());
242 isCloseOutDate_ = std::vector<bool>(dates_.size(), false);
243 isValuationDate_ = std::vector<bool>(dates_.size(), true);
244 for(size_t i = 0; i < dates_.size(); ++i){
245 Date d = dates_[i];
246 if (tmpCloseOutDates.count(d) == 1) {
247 isCloseOutDate_[i] = true;
248 }
249 if(tmpValueDates.count(d) == 0){
250 isValuationDate_[i] = false;
251 }
252 }
253 // FIXME ... (is that needed anywhere ?)
254 tenors_ = std::vector<QuantLib::Period>(dates_.size(), 0 * Days);
255 times_.resize(dates_.size());
256 Date today = Settings::instance().evaluationDate();
257 for (Size i = 0; i < dates_.size(); i++)
258 times_[i] = dayCounter_.yearFraction(today, dates_[i]);
259 timeGrid_ = TimeGrid(times_.begin(), times_.end());
260 }
261 // Log Grid
262 DLOG("Added Close Out Dates to DateGrid , size = " << size());
263 log();
264}
std::map< QuantLib::Date, QuantLib::Date > valuationCloseOutMap_
Definition: dategrid.hpp:117
QuantLib::Size size() const
The size of the date grid.
Definition: dategrid.hpp:58
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
+ Here is the call graph for this function:

◆ tenors()

const std::vector< QuantLib::Period > & tenors ( ) const

Definition at line 80 of file dategrid.hpp.

80{ return tenors_; }

◆ dates()

const std::vector< QuantLib::Date > & dates ( ) const

Definition at line 81 of file dategrid.hpp.

81{ return dates_; }
+ Here is the caller graph for this function:

◆ isValuationDate()

const std::vector< bool > & isValuationDate ( ) const

Definition at line 82 of file dategrid.hpp.

82{ return isValuationDate_; }

◆ isCloseOutDate()

const std::vector< bool > & isCloseOutDate ( ) const

Definition at line 83 of file dategrid.hpp.

83{ return isCloseOutDate_; }

◆ valuationDates()

std::vector< QuantLib::Date > valuationDates ( ) const

Definition at line 266 of file dategrid.cpp.

266 {
267 std::vector<Date> res;
268 for (Size i = 0; i < dates_.size(); ++i) {
269 if (isValuationDate_[i])
270 res.push_back(dates_[i]);
271 }
272 return res;
273}

◆ closeOutDates()

std::vector< QuantLib::Date > closeOutDates ( ) const

Definition at line 275 of file dategrid.cpp.

275 {
276 std::vector<Date> res;
277 for (Size i = 0; i < dates_.size(); ++i) {
278 if (isCloseOutDate_[i])
279 res.push_back(dates_[i]);
280 }
281 return res;
282}

◆ calendar()

const QuantLib::Calendar & calendar ( ) const

Definition at line 86 of file dategrid.hpp.

86{ return calendar_; }

◆ dayCounter()

const QuantLib::DayCounter & dayCounter ( ) const

Definition at line 87 of file dategrid.hpp.

87{ return dayCounter_; }
+ Here is the caller graph for this function:

◆ times()

const std::vector< QuantLib::Time > & times ( ) const

Returns the times from Settings::instance().evaluationDate to each Date using the day counter.

Definition at line 90 of file dategrid.hpp.

90{ return times_; }
+ Here is the caller graph for this function:

◆ timeGrid()

const QuantLib::TimeGrid & timeGrid ( ) const

Returns the time grid associated with the vector of times (plus t=0)

Definition at line 93 of file dategrid.hpp.

93{ return timeGrid_; }

◆ valuationTimeGrid()

QuantLib::TimeGrid valuationTimeGrid ( ) const

Returns the time grid associated with the vector of valuation times (plus t=0)

Definition at line 284 of file dategrid.cpp.

284 {
285 std::vector<Real> times;
286 Date today = Settings::instance().evaluationDate();
287 for (Size i = 0; i < dates_.size(); ++i) {
288 if (isValuationDate_[i])
289 times.push_back(dayCounter_.yearFraction(today, dates_[i]));
290 }
291 return TimeGrid(times.begin(), times.end());
292}
const std::vector< QuantLib::Time > & times() const
Returns the times from Settings::instance().evaluationDate to each Date using the day counter.
Definition: dategrid.hpp:90
+ Here is the call graph for this function:

◆ closeOutTimeGrid()

QuantLib::TimeGrid closeOutTimeGrid ( ) const

Returns the time grid associated with the vector of close-out times (plus t=0)

Definition at line 294 of file dategrid.cpp.

294 {
295 std::vector<Real> times;
296 Date today = Settings::instance().evaluationDate();
297 for (Size i = 0; i < dates_.size(); ++i) {
298 if (isCloseOutDate_[i])
299 times.push_back(dayCounter_.yearFraction(today, dates_[i]));
300 }
301 return TimeGrid(times.begin(), times.end());
302}
+ Here is the call graph for this function:

◆ closeOutDateFromValuationDate()

QuantLib::Date closeOutDateFromValuationDate ( const QuantLib::Date &  d) const

Definition at line 304 of file dategrid.cpp.

304 {
305 auto it = valuationCloseOutMap_.find(d);
306 if(it == valuationCloseOutMap_.end()){
307 return Date();
308 }
309 return it->second;
310}

◆ operator[]()

const QuantLib::Date & operator[] ( QuantLib::Size  i) const

Accessor methods.

Definition at line 107 of file dategrid.hpp.

107{ return dates_[i]; };

◆ buildDates()

void buildDates ( const QuantLib::Calendar &  cal,
const QuantLib::DayCounter &  dc 
)
private

Definition at line 139 of file dategrid.cpp.

139 {
140 // build dates from tenors
141 // this is called by both constructors
142 dates_.resize(tenors_.size());
143 Date today = Settings::instance().evaluationDate();
144 for (Size i = 0; i < tenors_.size(); i++) {
145 if (tenors_[i].units() == Days)
146 dates_[i] = cal.adjust(today + tenors_[i]);
147 else
148 dates_[i] = cal.advance(today, tenors_[i], Following, false);
149 if (i > 0) {
150 QL_REQUIRE(dates_[i] >= dates_[i - 1], "DateGrid::buildDates(): tenors must be monotonic");
151 if (dates_[i] == dates_[i - 1]) {
152 dates_.erase(std::next(dates_.begin(), i));
153 tenors_.erase(std::next(tenors_.begin(), i));
154 --i;
155 }
156 }
157 }
158
159 // Build times
160 times_.resize(dates_.size());
161 for (Size i = 0; i < dates_.size(); i++)
162 times_[i] = dc.yearFraction(today, dates_[i]);
163
164 timeGrid_ = TimeGrid(times_.begin(), times_.end());
165 isValuationDate_ = std::vector<bool>(dates_.size(), true);
166 isCloseOutDate_ = std::vector<bool>(dates_.size(), false);
167
168 // Log the date grid
169 log();
170}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log()

void log ( )
private

Definition at line 172 of file dategrid.cpp.

172 {
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])
176 << ", Valuation:" << isValuationDate_[i] << ", CloseOut:" << isCloseOutDate_[i]);
177}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ calendar_

QuantLib::Calendar calendar_
private

Definition at line 114 of file dategrid.hpp.

◆ dayCounter_

QuantLib::DayCounter dayCounter_
private

Definition at line 115 of file dategrid.hpp.

◆ dates_

std::vector<QuantLib::Date> dates_
private

Definition at line 116 of file dategrid.hpp.

◆ valuationCloseOutMap_

std::map<QuantLib::Date, QuantLib::Date> valuationCloseOutMap_
private

Definition at line 117 of file dategrid.hpp.

◆ tenors_

std::vector<QuantLib::Period> tenors_
private

Definition at line 118 of file dategrid.hpp.

◆ times_

std::vector<QuantLib::Time> times_
private

Definition at line 119 of file dategrid.hpp.

◆ timeGrid_

QuantLib::TimeGrid timeGrid_
private

Definition at line 120 of file dategrid.hpp.

◆ isValuationDate_

std::vector<bool> isValuationDate_
private

Definition at line 121 of file dategrid.hpp.

◆ isCloseOutDate_

std::vector<bool> isCloseOutDate_
private

Definition at line 121 of file dategrid.hpp.