@AlexAbrahams's recommended resource is a decent one but here's another (I think better) approach which solves both (1) and (2):
#!/usr/bin/env python
# Call from the `Data` directory
import glob
import pandas as pd
import datetime
# Get all traded dates, with possible repetition
dates = []
for f in glob.glob('*/*.us.txt'):
dates += df['Date'].tolist()
# Remove repetitions
dates = set(dates)
# Optional: Convert to `datetime`
dates = map(lambda s: datetime.datetime.strptime(s, '%Y-%m-%d').date(),
dates)
# Optional: Sort
dates = sorted(dates)
Two reasons to consider doing it this way instead:
You don't need an external, and very large, dependency on QuantLib
.
In production, there may be practical, structural or systematic reasons why you can't trade on the days that are in Quantlib
but not in your data. For example, the network that your servers are on may be down, causing you to lose data on those dates. Sometimes this loss is connected with events of significant volatility, e.g. a circuit breaker tripping on the exchange causing a glitch in your own software. It doesn't make sense to impute data on the trading schedule "as though" you would've been able to trade on those dates because there's a very repeatable reason why you wouldn't have been able to. Your own data captures these nuances the best as opposed to a third party library.