quantopian / trading_calendars

Calendars for various securities exchanges.
Apache License 2.0
615 stars 381 forks source link

Data Differences Between `trading_calendars` and `pandas_market_calendars` #152

Closed gerrymanoim closed 3 years ago

gerrymanoim commented 4 years ago

re https://github.com/rsheftel/pandas_market_calendars/issues/41

Summary status

Extras not covered by diff below:

Details

code generating the table below

from collections import namedtuple 

import pandas_market_calendars as pmc
import trading_calendars as tc

# todo port this back to trading calendars
def get_tc_calendar_names():
  return list(tc.calendar_utils._default_calendar_factories.keys())+list(tc.calendar_utils._default_calendar_aliases.keys())

tc.get_calendar_names = get_tc_calendar_names
# end todo

pmc_calendars = set(pmc.get_calendar_names())
tc_calendars = set(tc.get_calendar_names())
tc_to_pmc_mapping = {
    'XASX': 'ASX',
    'XHKG': 'HKEX',
    'XTKS': 'JPX',
    'XOSL': 'OSE',
    'XSWX': 'SIX',
    'XSHG': 'SSE',
}

in_both = (tc_calendars & pmc_calendars) | set(tc_to_pmc_mapping.keys())

CompareResult = namedtuple("Result", ["only_tc", "only_pmc"])

def compare_calendars(cal: str):
  tc_cal = tc.get_calendar(cal).all_sessions
  pmc_cal_name = tc_to_pmc_mapping.get(cal, cal)
  pmc_cal = pmc.get_calendar(pmc_cal_name).valid_days(min(tc_cal), max(tc_cal))
  if not (tc_cal ^ pmc_cal).empty:
    return CompareResult

out = {}
for cal in in_both:
  diff = compare_calendars(cal)
  if diff:
    out[cal] = diff

# everything below this is markdown formatting
out_str = []
for cal, result in out.items():
  out_str.append(f"## {cal} \n")
  out_str.append("Trading Days only in `trading_calendars` \n")
  out_str.extend([f"- {item} \n" for item in result.only_tc])
  out_str.append("\n")
  out_str.append("Trading Days only in `pandas_market_calendars` \n")
  out_str.extend([f"- {item} \n" for item in result.only_pmc])
  out_str.append("\n")
md = "".join(out_str); print(md)

XHKG

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

CFE

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

XSWX

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

LSE

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

XSHG

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

XASX

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

TSX

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

BMF

Trading Days only in trading_calendars

Trading Days only in pandas_market_calendars

gerrymanoim commented 4 years ago

cc @jmccorriston (FYI - @rsheftel )

jmccorriston commented 4 years ago

For TSX:

I can't find any sources saying that Christmas Eve was a holiday, but I found this source suggesting that the market was open on 2010-12-24 (but closed by 2:41pm ET, suggesting it closed early): https://toronto.ctvnews.ca/modest-tsx-gain-as-trading-takes-a-christmas-break-1.589362

There's also this wiki page on the 9/11 closes around the world (includes TSX): https://en.wikipedia.org/wiki/Closings_and_cancellations_following_the_September_11_attacks

So I think the Toronto Stock exchange implementation in trading_calendars is correct.

gerrymanoim commented 4 years ago

FWIW https://finance.yahoo.com/quote/RY.TO/history?period1=1292112000&period2=1293753600&interval=1d&filter=history&frequency=1d shows volume on TSX on 2010-12-24.

jmccorriston commented 4 years ago

For HKG:

It looks like Hong Kong only started observing Labour Day in 1999 (https://en.wikipedia.org/wiki/International_Workers%27_Day, "In Hong Kong, 1 May is known as Labour Day and has been considered a public holiday since 1999."). pmc is correct here, tc needs to be updated.

Establishment day started in 1997: https://www.scmp.com/news/hong-kong/education-community/article/1983718/everything-you-need-know-about-hong-kongs-return. pmc is correct here, tc needs to be updated.

National Day also first observed in 1997: http://www.cnn.com/WORLD/9710/01/china.bday/. pmc is correct, tc needs to be updated.

Buddha's birthday also only started as a public holiday in 1998: https://www.cantoneseclass101.com/blog/2020/04/24/celebrating-buddhas-birthday-in-hong-kong/, https://www.officeholidays.com/holidays/hong-kong/the-buddhas-birthday. We should trim the calendar in tc to reflect this. However, it looks like there's still a discrepancy in 1998. pmc suggests that 1998-05-04 should be a trading day while tc suggests it should be a holiday (1998-05-03 actual holiday, observed on the Monday).

There are several British holidays that were observed during British rule pre-1997 as well:

It looks like there are a lot of details in the pmc Hong Kong calendar implementation that we are missing in tc. I pinged the person who added them to pmc to see if we can get more info, but my inclination is to pull in the historical calendars that they added to pmc. From what I can tell so far, they are correct.

One exception: 2021-04-06 was a public holiday per https://www.hkex.com.hk/News/HKEX-Calendar?sc_lang=en (Ching Ming festival). This is correct in tc.

jmccorriston commented 4 years ago

For CFE:

I can't seem to find any references for Nixon's Day of Mourning on 1994-04-27... Not sure what to do about this one.

rsheftel commented 4 years ago

Discussion of pre-1990 NYSE (XNYS) calendar here: #162

rsheftel commented 4 years ago

Discussion of LSE differences here: #163

rsheftel commented 4 years ago

Discussion of XKTS (JPX) differences here: #164