2

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.

SRKX
  • 11,126
  • 4
  • 42
  • 83
Franck Dernoncourt
  • 507
  • 1
  • 7
  • 19
  • 3
    I'm voting to close this question as off-topic because this question belongs on stack overflow. – pyCthon Jul 09 '17 at 04:29
  • 1
    @pyCthon it isn't on topic on SO. Also even if it was, it isn't a reason to vote to close. – Franck Dernoncourt Jul 09 '17 at 16:44
  • 1
    https://stackoverflow.com/questions/4362491/how-do-i-check-the-difference-in-seconds-between-two-dates – pyCthon Jul 09 '17 at 16:49
  • @pyCthon I am looking for the trading time. – Franck Dernoncourt Jul 09 '17 at 18:10
  • What do you mean by trading time? Could you provide us with an example of the expected output? – JejeBelfort Jul 10 '17 at 09:20
  • 2
    For example: for stocks in the USA the trading time between 2017-04-11 21:00:57 (local time) and 2017-07-08 12:41:54 is 1212425345347 seconds and the nontrading time (due to exchange closing at nite, weekends and holidays) is 423423423 seconds. For Bunds in Germany it is 121200004343 seconds because the trading hours there are different, they don't have the same holidays. And for Paris CAC40 stocks it is ..... (yet another figure), for London Libor swaps ... – Alex C Jul 10 '17 at 10:47
  • How is this not just the time delta between two time stamps? – David Addison Jul 11 '17 at 18:35
  • @DavidAddison some products are not constantly traded – Franck Dernoncourt Jul 11 '17 at 19:50
  • 2
    @FranckDernoncourt. Gotcha. It was not initially clear to me that trading time $\ne$ calendar time. I had to do this a while back when creating continuous futures. I didn't find a package, but found that the required information was readily available in futures contract specifications. – David Addison Jul 12 '17 at 01:37

2 Answers2

3

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)

mementum
  • 628
  • 6
  • 8
1

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.

SRKX
  • 11,126
  • 4
  • 42
  • 83