30 : maxIterations_(maxIterations) {
31 auto p = QuantLib::ext::dynamic_pointer_cast<CommodityFutureConvention>(
32 InstrumentConventions::instance().conventions()->get(commName));
33 QL_REQUIRE(p,
"ConventionsBasedFutureExpiry: could not cast to CommodityFutureConvention for '"
34 << commName <<
"', this is an internal error. Contact support.");
39 QuantLib::Size maxIterations)
40 : convention_(convention), maxIterations_(maxIterations) {}
52 if (
expiryDate == today && !includeExpiry && offset == 0) {
74 if (includeExpiry &&
expiry == today)
84 while (
expiry >= today && counter > 0) {
89 QL_REQUIRE(
expiry < today,
"Expected that expiry " << io::iso_date(
expiry) <<
" would be less than reference date "
90 << io::iso_date(today) <<
".");
110 for (Size m = 0; m < 120; ++m) {
113 if (
expiry(tmp.dayOfMonth(), tmp.month(), tmp.year(), 0,
false) ==
expiryDate)
119 if (
expiry(tmp.dayOfMonth(), tmp.month(), tmp.year(), 0,
false) ==
expiryDate)
132 QL_FAIL(
"ConventionsBasedFutureExpiry::contractDate(" <<
expiryDate <<
"): could not imply contract date. This is an internal error. Contact support.");
136 Natural futureMonthOffset) {
148 bool forOption)
const {
153 "Please change anchorType to WeeklyDayOfTheWeek for weekly contract expiries");
154 Date d(dayOfMonth, contractMonth, contractYear);
159 if (monthOffset > 0) {
160 Date newDate = Date(15, contractMonth, contractYear) + monthOffset * Months;
161 contractMonth = newDate.month();
162 contractYear = newDate.year();
168 contractMonth = newDate.month();
169 contractYear = newDate.year();
180 Date last = Date::endOfMonth(Date(1, contractMonth, contractYear));
201 QL_FAIL(
"Did not recognise the commodity future convention's anchor type");
215 "Please change anchorType to WeeklyDayOfTheWeek for weekly contract expiries");
220 }
else if (forOption) {
222 auto optionMonth =
expiry.month();
223 auto optionYear =
expiry.year();
227 optionMonth = newDate.month();
228 optionYear = newDate.year();
232 Date last = Date::endOfMonth(Date(1, optionMonth, optionYear));
233 if (d >
static_cast<Natural
>(last.dayOfMonth())) {
236 expiry = Date(d, optionMonth, optionYear);
242 "The expiry month lag "
243 <<
"and the option expiry month lag should be the same if using option expiry offset days.");
248 auto optionMonth =
expiry.month();
249 auto optionYear =
expiry.year();
253 optionMonth = newDate.month();
254 optionYear = newDate.year();
259 auto optionMonth =
expiry.month();
260 auto optionYear =
expiry.year();
264 optionMonth = newDate.month();
265 optionYear = newDate.year();
295 QL_REQUIRE(
expiryDate <
referenceDate,
"Expected the expiry date in the previous year to be before reference");
302 expiryDate =
expiry(guideDate.dayOfMonth(), guideDate.month(), guideDate.year(), 0, forOption);
318 auto it = pes.find(PE(result));
320 while (it != pes.end()) {
323 if ((!forOption && !it->forFuture()) || (forOption && !it->forOption()))
326 auto bdc = !forOption ? it->futureBdc() : it->optionBdc();
328 if (bdc == Preceding || bdc == ModifiedPreceding) {
330 }
else if (bdc == Following || bdc == ModifiedFollowing) {
333 QL_FAIL(
"Convention " << bdc <<
" associated with prohibited expiry " << io::iso_date(result)
334 <<
" is not supported.");
337 it = pes.find(PE(result));
Class to hold prohibited expiry information.
QuantLib::BusinessDayConvention optionBusinessDayConvention() const
QuantLib::Natural optionExpiryOffset() const
bool adjustBeforeOffset() const
QuantLib::Weekday weekday() const
AnchorType anchorType() const
const QuantLib::Calendar & expiryCalendar() const
QuantLib::BusinessDayConvention businessDayConvention() const
QuantLib::Integer businessDaysAfter() const
QuantLib::Frequency contractFrequency() const
QuantLib::Frequency optionContractFrequency() const
OptionAnchorType optionAnchorType() const
QuantLib::Natural optionNth() const
QuantLib::Size optionExpiryMonthLag() const
const QuantLib::Calendar & calendar() const
const std::set< ProhibitedExpiry > & prohibitedExpiries() const
QuantLib::Natural optionExpiryDay() const
QuantLib::Natural dayOfMonth() const
const std::set< QuantLib::Month > & validContractMonths() const
QuantLib::Integer offsetDays() const
QuantLib::Weekday optionWeekday() const
QuantLib::Natural nth() const
QuantLib::Month oneContractMonth() const
QuantLib::Natural calendarDaysBefore() const
QuantLib::Size expiryMonthLag() const
QuantLib::Date nextExpiry(bool includeExpiry=true, const QuantLib::Date &referenceDate=QuantLib::Date(), QuantLib::Natural offset=0, bool forOption=false) override
QuantLib::Date avoidProhibited(const QuantLib::Date &expiry, bool forOption) const
Account for prohibited expiries.
QuantLib::Date expiry(QuantLib::Day dayOfMonth, QuantLib::Month contractMonth, QuantLib::Year contractYear, QuantLib::Natural monthOffset, bool forOption) const
Given a contractMonth, a contractYear and conventions, calculate the contract expiry date.
ConventionsBasedFutureExpiry(const std::string &commName, QuantLib::Size maxIterations=10)
QuantLib::Date priorExpiry(bool includeExpiry=true, const QuantLib::Date &referenceDate=QuantLib::Date(), bool forOption=false) override
QuantLib::Date applyFutureMonthOffset(const QuantLib::Date &contractDate, Natural futureMonthOffset) override
QuantLib::Date expiryDate(const QuantLib::Date &contractDate, QuantLib::Natural monthOffset=0, bool forOption=false) override
CommodityFutureConvention convention_
QuantLib::Size maxIterations() const
Return the maximum iterations parameter.
QuantLib::Date contractDate(const QuantLib::Date &expiryDate) override
QuantLib::Size maxIterations_
const CommodityFutureConvention & commodityFutureConvention() const
Return the commodity future convention.
Base class for classes that perform date calculations for future contracts.
Classes and functions for log message handling.
Date lastWeekday(Weekday dayOfWeek, Month m, Year y)
Serializable Credit Default Swap.