rsheftel / pandas_market_calendars

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

CME globex USNewYearDay incorrect when NY is on Sunday #340

Open r3k4mn14r opened 8 months ago

r3k4mn14r commented 8 months ago

Hi,

CME globex NEW years appears to be incorrect. For instance 2011 is a year where that happens. See attached for market schedule.

My understanding is that observance should be sunday_to_monday. I see that it is commented out in cme_globex.py. I can submit a patch but I wanted to ask before to confirm the reason the observance was commented out in the first place. 2011-new-years.pdf

alex-muci commented 7 months ago

Indeed. Same thing happening for say 2023 (since 1-Jan was on Sunday, then Monday 2 of Jan 2023 was an holiday for say WTI)

alex-muci commented 2 months ago

For everyone that needs a quick fixing in the meantime. See below for an example with 'CMEGlobex_EnergyAndMetals':

#########> import the lib as usual
import pandas_market_calendars as mcal

#########> Then do the following to correct the USNewYearsDay
from pandas_market_calendars.holidays.us import USNewYearsDay  # Correct holiday here!
from pandas.tseries.holiday import AbstractHolidayCalendar

#### Subclass the existing calendar
class CustomCMEGlobexEnergyAndMetalsCalendar(mcal.get_calendar('CMEGlobex_EnergyAndMetals').__class__):

    @property
    def regular_holidays(self):
        # Rebuild the holidays with the corrected USNewYearsDay
        return AbstractHolidayCalendar(rules=[
            rule for rule in super().regular_holidays.rules if rule.name != "New Years Day"
        ] + [USNewYearsDay])

##### Dynamically add the subclass to the registry
mcal.MarketCalendar._regmeta_class_registry['CustomCMEGlobexEnergyAndMetals'] = CustomCMEGlobexEnergyAndMetalsCalendar
########################### ALL DONE!

#########################
### Now you can access the modified calendar with the correct USNewYearsDay, e.g.
cme_energy_and_metals_calendar = mcal.get_calendar('CustomCMEGlobexEnergyAndMetals')
cme_energy_and_metals_calendar.regular_holidays.holidays(start="2022-12-31", end="2023-12-31")  # now 2023-01-02 is an holiday (since 2023-01-01 was a Sunday)
rsheftel commented 2 months ago

Do you want to submit a PR and I can add to the package?

alex-muci commented 2 months ago

Sure - I will try and get a PR later today [just need to uncomment the observance=sunday_to_monday in cme_globex.py]

glossner commented 2 months ago

@alex-muci - When we coded the globex rules a few years ago, many of the globex markets had different open/close/holidays. You have referenced the EnergyAndMetals market. Is that the one you wanted?

I guess what this really means is that we don't have the correct tests coded for pandas_market_calendars/tests /test_exchange_calendar_cme_globex_energy_and_metals.py

2022 does not have NewYearsDay but 2021 does. Specifically, many of the globex markets could not use the NYSE rules. That is why they were moved to their own file.

If you can please update the tests so we have the true historical record that would be great. If you can point to the URL that contains the holiday calendar noting the closing, that would also be appreciated.

Thank you!

glossner commented 2 months ago

@alex-muci - sorry, I missed your link in the first post. In fact, there are no tests for EnergyAndMetals prior to 2020. If you have the data as far back as possible (for all holidays), it would be great to post them so that eventually all of them can be coded. Thanks.