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

ENH: Session type, parse_session, new ExchangeCalendar methods #29

Merged
merged 6 commits into from
Jun 22, 2021

Conversation

maread99
Copy link
Collaborator

@maread99 maread99 commented Jun 7, 2021

Introduces:

  • Session type for parameters requiring a session label.
  • parse_session function for verifying input.
  • NotSessionError raised by parse_session when session parses to UTC midnight but does not represent a session (only raised if parse_session strict parameter is True).

Only implemented on public methods that would have otherwise raised an error for the given input. Note: Error type raised may differ from error type previously raised.

To avoid breaking changes, not implemented on public methods that return regardless of receiving a session_label as input that does not represent a session, for example, sessions_in_range('2021-06-07', '2021-06-16 23:55'). Note: such input is ambiguous - in this example, if the 2021-06-17 session opened 2021-06-16 23:00 then should it be included?

On any breaking release might be worth losing this ambiguity by verifying session inputs to ensure no time component, although allowing non-sessions so that the likes of sessions_in_range('2021-01-01', '2021-12-31') is valid. This can be handled by the strict parameter of parse_session(calendar, session, param_name, strict=False).

PR also:

  • moves 'ExchangeCalendar._trading_minutes_nanos' to 'ExchangeCalendar.all_minutes_nanos' property. Exposes nanos and allows for benefit of lazyval decoration of 'ExchangeCalendar.all_minutes' (was being called by the constructor to evaluate ExchangeCalendar._trading_minutes_nanos)

  • type annotations to some ExchangeCalendar methods (for documentation purposes)

  • new ExchangeCalendar methods:

    • has_breaks -> bool. Does any session of calendar have a break.
    • session_has_break -> bool. Does a given session have a break.

@github-actions github-actions bot added the enhancement New feature or request label Jun 7, 2021
@maread99 maread99 marked this pull request as ready for review June 7, 2021 16:07
"""
try:
ts = pd.Timestamp(session)
except Exception:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the a more specific exception you can catch here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try / except statement here is to identify when a user has passed a session as an object that is not a valid single-argument input to Timestamp. There are at least three errors that Timestamp can raise in this case (ValueError, TypeError and OutOfBoundsDatetime). Given the try clause contains only the command to create a Timestamp I felt it reasonable to leave the captured Exception bare in order to cover all circumstances(?).

Separately, I've revised the except clause to include the original error within the traceback so the user has visibility as to why the input failed.

@@ -71,3 +82,96 @@ def compute_all_minutes(
)
out = np.concatenate(pieces).view("datetime64[ns]")
return out


def parse_session(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the a reason not to put this as a function on the ExchangeCalendar class? Seems like you're only using it there and always passing self.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I envisaged using it to parse start and end parameters received to ExchangeCalendarDispatcher.get_calendar (#31). To this end I've made revisions to allow calendar to take None if strict is False.

@gerrymanoim
Copy link
Owner

@maread99 Sorry for the delay here. Overall - looks good to me, but two small questions.

@maread99
Copy link
Collaborator Author

maread99 commented Jun 21, 2021

The two conflicts seem to be the result of formatting in the merged #19. In each case the first version is as required.

@gerrymanoim
Copy link
Owner

Ack - I seem to have just messed up the conflict fix rebase here - apologies.

Would it be easier to close this, rebase #32 and I'll just approve + merge that (since IIRC it has all the same commits as this)? Sorry for the trouble.

@maread99
Copy link
Collaborator Author

No worries, the merge was always going to fail on a test that I introduced test_has_breaks. The recently merged revisions to XKRX (nice PR!) included breaks although, naturally, didn't provide for the test. Whichever PR were to be have merged first, the other would have failed. I've added a single-line commit to resolve this.

Separately I've popped my 'Update XSES holidays' commit onto the end here and closed #32.

I believe the commits under this PR are now clean and that it should all merge without an issue.

References for the XSES holidays update:
https://www.sgx.com/securities/trading#Trading%20Hours%20&%20Opening/Closing%20Routine
https://www.mom.gov.sg/employment-practices/public-holidays

@gerrymanoim gerrymanoim merged commit af23a89 into gerrymanoim:master Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants