Skip to content
All library documents

Listing QuantLib Holidays in Python with Calendar Checks

Article Quant Q&A · Author: Bernd

Summary

The document explains that QuantLib’s C++ calendar interface provides a holiday-list function, but the Python wrapper does not expose it. Calling that method from a Python calendar therefore raises an attribute error, and calling it as a standalone function also fails. The accepted answer points to the wrapper interface as the reason.

As a workaround, it shows how to iterate through dates in a chosen range, test each date with the calendar’s holiday check, and exclude weekends with a separate weekend check. This produces a list of weekday holidays for the selected calendar. The method depends on the calendar definition and date range supplied, and the document does not discuss additional calendar conventions or validation. A second answer briefly mentions a separate date-roll package as another option.

Key ideas

  • The QuantLib Python wrapper does not expose the C++ calendar holiday-list function described here.
  • A date-by-date loop can collect holidays by checking each date against the calendar.
  • A weekend check can exclude Saturdays and Sundays from the resulting list.
  • The workaround relies on the selected calendar’s holiday rules and the requested date interval.

Tags

Full text
# QuantLib-Python: Getting a list of all holidays between D1 & D2 with function "holidayList"


# QuantLib-Python: Getting a list of all holidays between D1 & D2 with function "holidayList"












I figured out that the C++ version of QuantLib offers a function called holidayList in the calendar class.

```
//! Returns the holidays between two dates
static std::vector<Date> holidayList(const Calendar& calendar,
                                     const Date& from,
                                     const Date& to,
                                     bool includeWeekEnds = false);
```

I tried to get all holidays between two dates by using this call:

```
TARGET_calendar = TARGET()
TARGET_calendar.holidayList(TARGET_calendar, Date(1,1,2015), Date(1,1,2016))

AttributeError: 'TARGET' object has no attribute 'holidayList'
```

Since I am not so familliar with converting C++ to Python, I also tried:

```
holidayList(TARGET_calendar, Date(1,1,2015), Date(1,1,2016))

NameError: name 'holidayList' is not defined
```

Can anybody help me out, please?

Thank you very much!

## Answer by byouness (score 3, accepted)

https://quant.stackexchange.com/a/39788

The `holidayList()` function is not exposed in Python: https://github.com/lballabio/QuantLib-SWIG/blob/master/SWIG/calendars.i

You can submit an issue asking for it to be added, or do it yourself via a pull request or just locally but in this last case you'd have to rebuild the QuantLib Python library, etc.

As a workaround, you can use the `isHoliday()` function in order to get this list, combined with `isWeekend()` if you want want to exclude the weekends from the holidays list:

```
TARGET_calendar = TARGET()
date = Date(1,1,2015)
holiday_list = []
while date < Date(1,1,2016):
    if(TARGET_calendar.isHoliday(date) and not
       TARGET_calendar.isWeekend(date.weekday())):
        holiday_list.append(date)
    date = date + Period(1, Days)
```

## Answer by Mike (score 1)

https://quant.stackexchange.com/a/78932

not quantlib, but with dateroll you can do ddh.hols.NY['7/1/93':'7/1/94']

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.