2
$\begingroup$

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.

$\endgroup$
9
  • 3
    $\begingroup$ I'm voting to close this question as off-topic because this question belongs on stack overflow. $\endgroup$
    – pyCthon
    Jul 9, 2017 at 4:29
  • 1
    $\begingroup$ @pyCthon it isn't on topic on SO. Also even if it was, it isn't a reason to vote to close. $\endgroup$ Jul 9, 2017 at 16:44
  • 1
    $\begingroup$ stackoverflow.com/questions/4362491/… $\endgroup$
    – pyCthon
    Jul 9, 2017 at 16:49
  • 2
    $\begingroup$ 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 ... $\endgroup$
    – Alex C
    Jul 10, 2017 at 10:47
  • 2
    $\begingroup$ @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. $\endgroup$ Jul 12, 2017 at 1:37

2 Answers 2

3
$\begingroup$

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)

$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.