Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Trading calendar with end date treats last business day as regular even if it is a special close/open day. #8

Closed
jenskeiner opened this issue Jan 28, 2021 · 2 comments · Fixed by #88

Comments

@jenskeiner
Copy link

While working on #4, I discovered what is likely a bug in the TradingCalendar base class.

When a calendar, e.g. for XETR or XWBO, is created with end date, say, 2020-12-30, then that day itself always seems to be treated as a regular business day if the exchange is open at all that day.

However, XETR and XWBO have rules in that make the last trading day of a year a special close day. This seems to be ignored in this case. If the end date is chosen to be after the special close day in question, then the special close is again reflected accurately.

The bug seems to manifest itself only in the edge case where the end date is a special open/close day.

@gerrymanoim Can you have look? You're probably quicker at finding the root cause. Let me know if I can help.

@maread99
Copy link
Collaborator

maread99 commented Jul 14, 2021

I just came across this as well. Example for reference:

from exchange_calendars.exchange_calendar_xlon import XLONExchangeCalendar

start =pd.Timestamp("2020-12-30", tz="UTC")
end = pd.Timestamp("2021-01-04", tz="UTC")
XLONExchangeCalendar(start, end).closes

returns...

2020-12-30 00:00:00+00:00   2020-12-30 16:30:00
2020-12-31 00:00:00+00:00   2020-12-31 12:30:00
2021-01-04 00:00:00+00:00   2021-01-04 16:30:00
Freq: C, Name: market_close, dtype: datetime64[ns]

i.e. close on 2020-12-31 is correct (12.30)

although...

end = pd.Timestamp("2020-12-31", tz="UTC")
XLONExchangeCalendar(start, end).closes

returns...

2020-12-30 00:00:00+00:00   2020-12-30 16:30:00
2020-12-31 00:00:00+00:00   2020-12-31 16:30:00
Freq: C, Name: market_close, dtype: datetime64[ns]

Worth noting that cases when this bug appears include whenever the end date is the last day of a year (I imagine not uncommon usage) and that last day of the year (i.e. new year's eve) is a special close (certainly not uncommon!).

maread99 added a commit that referenced this issue Sep 23, 2021
@maread99
Copy link
Collaborator

The bug's here:

result = result.loc[(result >= start_date) & (result <= end_date)]

Using the XLON example above, what's happening is end_date is "2020-12-31 00:00" whilst the value in result being subjected to the query is "2020-12-31 12:30", i.e. it fails the query because if falls after end_date.

Fix:

end_rng = end_date + pd.Timedelta(1, "D")
result = result.loc[(result >= start_date) & (result < end_rng)]

Fix in PR #88.

@maread99 maread99 mentioned this issue Sep 23, 2021
maread99 added a commit that referenced this issue Sep 23, 2021
maread99 added a commit that referenced this issue Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants