Calculating Trading Time with Exchange Calendars
Summary
The document explains how to measure elapsed time during which an exchange is open between two timestamps. Its accepted answer uses an exchange calendar to retrieve scheduled market openings and closings, then sums the overlap between each session and the requested interval. The first and last sessions may contribute only partial trading time, while intervening sessions contribute their scheduled durations.
The examples show that session hours can vary, including an early close, so assuming identical daily hours can produce errors. The approach depends on having an accurate calendar for the relevant instrument and exchange; instruments without a supported calendar may require a custom one. A second answer emphasizes that maintaining exchange schedules and their historical changes is a substantial data task, and it doubts that a general ready-made package would cover every product. The discussion gives a practical method but no worked total for the sample timestamps, and does not address intraday breaks or other instrument-specific schedule details.
Key ideas
- Use an exchange calendar to obtain scheduled opening and closing times across the timestamp interval.
- Sum only the portions of sessions that overlap the requested start and end timestamps.
- Account for partial first and last sessions and for irregular hours such as early closes.
- Choose a calendar that matches the instrument, and provide a custom schedule if needed.
Tags
Full text
# Is there a python package/function that returns the trading time between two timestamps?
# Is there a python package/function that returns the trading time between two timestamps?
I have two timestamps, e.g. 2017-04-11 and 2017-07-08, or 2017-04-11 21:00:57 and 2017-07-08 12:41:54 and I am looking for a Python package/function that returns the total trading time between these two timestamps (i.e. excluding the time during which market was closed).
The higher the precision of the timestamps, the better. I am aware that the trading time depends on the financial instrument.
## Answer by mementum (score 3, accepted)
https://quant.stackexchange.com/a/35275
Yes you can! With `pandas_market_calendars`. See: GitHub - Repo. From the docs
```
import pandas_market_calendars as mcal
nyse = mcal.get_calendar('NYSE')
schedule = nyse.schedule(start_date='2016-12-30', end_date='2017-01-10')
market_open market_close
2016-12-30 2016-12-30 14:30:00+00:00 2016-12-30 21:00:00+00:00
2017-01-03 2017-01-03 14:30:00+00:00 2017-01-03 21:00:00+00:00
2017-01-04 2017-01-04 14:30:00+00:00 2017-01-04 21:00:00+00:00
2017-01-05 2017-01-05 14:30:00+00:00 2017-01-05 21:00:00+00:00
2017-01-06 2017-01-06 14:30:00+00:00 2017-01-06 21:00:00+00:00
2017-01-09 2017-01-09 14:30:00+00:00 2017-01-09 21:00:00+00:00
2017-01-10 2017-01-10 14:30:00+00:00 2017-01-10 21:00:00+00:00
```
For your starting timestamp, you can calculate the difference until the end of the 1st trading day.
For your ending timestamp, you can calculate the difference from the start of the last trading day.
For the rest of the days, the time to accumulate is obviously the ending time minus the starting time. Even if the output above has the same starting/ending times, it will not always be the case as in:
```
early = nyse.schedule(start_date='2012-07-01', end_date='2012-07-10')
market_open market_close
2012-07-02 2012-07-02 13:30:00+00:00 2012-07-02 20:00:00+00:00
2012-07-03 2012-07-03 13:30:00+00:00 2012-07-03 17:00:00+00:00
2012-07-05 2012-07-05 13:30:00+00:00 2012-07-05 20:00:00+00:00
2012-07-06 2012-07-06 13:30:00+00:00 2012-07-06 20:00:00+00:00
2012-07-09 2012-07-09 13:30:00+00:00 2012-07-09 20:00:00+00:00
2012-07-10 2012-07-10 13:30:00+00:00 2012-07-10 20:00:00+00:00
```
So you actually need to iterate over the results and calculate the accumulated time for each trading day.
Of course: your instrument has to be in one of the supported calendars (or else you can create your own)
## Answer by SRKX (score 1)
https://quant.stackexchange.com/a/35185
I am not aware of such package to exist, and I would be surprised if it does.
The reason behind this is because the amount of "meta" data (exchange closing times per product etc...) is quite substantial and usually not readily available in API. Not that you would also probably have to have a time series of closing times, as some might evolve through time.
There is a chance that some major banks or hedge funds keep track of such datasets, but they probably spent a good amount of resource on building it and won't be willing to give it away for free.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.