rsheftel / pandas_market_calendars

Exchange calendars to use with pandas for trading applications
MIT License
803 stars 173 forks source link

CME Exchange Times #180

Closed glossner closed 2 years ago

glossner commented 2 years ago

@rsheftel I started looking into the CME calendars. Times are coded with a clever offset of -1 day so that Sunday starts are observed. A problem with this approach is that if you query valid_days(), Sundays are not included. This suggests the weekmask should be changed like in NYSE to include Sundays.

Another complication is that Sunday and Friday do not have a break but M-Thu do. That can be handled in code but maybe there is a more general solution?

During holidays there are multiple pre-open/open/halt/close times. See this link for 2021 holiday trading times. Should we code multiple times on the same day when it is a holiday e.g. special_opens() + special_closes()? Or something more general?

If you, @Stryder-Git, or others have any thoughts or suggestions it would be appreciated.

Thanks.

Stryder-Git commented 2 years ago

I will have a look in the next couple of days. Although, I am not sure whether I understand your point regarding holidays correctly. Would you mind elaborating on it, and maybe giving an example? Thanks.

glossner commented 2 years ago

I've attached just the MLK holiday schedule in full and compact form. Thanks 2021-mlk-day-holiday-schedule.xls 2021-mlk-day-schedule-compact.xls .

glossner commented 2 years ago

@Stryder-Git Also, if you want to provide the framework, I'll input all the dates. I say that because the changes you made to MarketCalendar were amazing and greatly simplified exchange_calendar_nyse. If nothing appears obvious to you, then I'll just brute-force it in code. Also, no rush. I'm crazy busy right now but will try to upgrade the CME calendars on weekends when I have availability. Thanks!

Stryder-Git commented 2 years ago

@glossner, Thanks for the kind words.

My first thoughts regarding the points you raised:

  1. From my understanding, Sunday is not a 'normal' valid day like Mon-Fri, Sunday 23:00 UTC is just when Monday starts. Simply changing the weekmask to include Sundays would lead to a an extra trading day, starting on Saturdays at 23:00. Unfortunately, with a lot of the products on the CME exchanges, open times are a day before the "TRADE DATE" (https://www.cmegroup.com/trading-hours.html#foi=O&tradeDate=2022-02-22&pageNumber=2&exchanges=CME), so I am not sure if we can or should change this...

    • The simplest way to get all dates on which a market opens, is by normalizing the market_open column of a schedule but this would not include Fridays. Possibly, a method returning 'active dates', including all dates during which a market is tradeable at any point (Sun-Fri, in this case) could work. Then we should also specify in the docs that valid_days returns the "Trade Dates" (as they are called by the CME Group), rather than all dates on which a market is open. This would actually be useful for all calendars that have this kind of an offset.
  2. Weekday dependent breaks could be implemented by adding an optional weekmask field to the regular_market_times dictionary (like the offset), defining on what weekdays a market_time occurs. It could be handled in days_at_time, by replacing timestamps with NaNs when they shouldn't exist.

    • I am just concerned that it may open a can of worms. If we are going to update the framework, here is some food for thought: what if some weekdays have a different break_start/break_end than other weekdays, does this exist in an exchange you know of? I think in an earlier PR we briefly discussed whether the functionality to have multiple breaks/halts in one day would be useful to have, should that be integrated, as well? We would also have to see how that affects functionality that uses a schedule, which never has NaNs in the current version.. -> I might be overthinking this (I have that tendency), so if anyone else has thoughts on this, I would appreciate it.
  3. Regarding holidays, to be sure I understand the issue correctly, see the following example:

    sched = cme.schedule("2021-01-15", "2021-01-20") # dates around MLK day 2021
    print(sched.to_string())
                         market_open               break_start                 break_end              market_close
    2021-01-15 2021-01-14 23:00:00+00:00 2021-01-15 21:15:00+00:00 2021-01-15 21:30:00+00:00 2021-01-15 22:00:00+00:00
    2021-01-18 2021-01-17 23:00:00+00:00 2021-01-18 18:00:00+00:00 2021-01-18 18:00:00+00:00 2021-01-18 18:00:00+00:00
    2021-01-19 2021-01-18 23:00:00+00:00 2021-01-19 21:15:00+00:00 2021-01-19 21:30:00+00:00 2021-01-19 22:00:00+00:00
    2021-01-20 2021-01-19 23:00:00+00:00 2021-01-20 21:15:00+00:00 2021-01-20 21:30:00+00:00 2021-01-20 22:00:00+00:00

I also have quite the to-do list for the next couple of months but I would be willing to make the time to update the framework, if we work this out more. (Taking our time and splitting this into multiple PR's might also be a good idea then)

@rsheftel, and anyone who is reading this, your thoughts would be appreciated, Thanks!

glossner commented 2 years ago

@Stryder-Git I agree we should split this. I'll leave this thread open for a bit to see if others have comments. I will open new enhancements to discuss each feature. Thanks.

rsheftel commented 2 years ago

@Stryder-Git I agree that Sunday is just when Monday starts. The standard in the futures market is to say that the markets are open Mon-Sat. Agree that we should not consider Sunday a trading day.

Futures are certainly more complex than equities with regards to open/close/break, but I feel we could add a lot to the community but just starting with the existing framework and getting the holidays correct. If we want to update the framework for the futures complexity, that would be great, but I think not needed first pass

glossner commented 2 years ago

I'm closing this out and will re-open on a feature-by-feature basis.