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

Add trading_index function #100

Merged
merged 2 commits into from
Oct 25, 2021
Merged

Add trading_index function #100

merged 2 commits into from
Oct 25, 2021

Conversation

maread99
Copy link
Collaborator

@maread99 maread99 commented Oct 15, 2021

Supersedes #89.

Adds ExchangeCalendar.trading_index to create a trading index over a given range of sessions at a given period. Similar to pandas_market_calendars.date_range with additional functionality:

  • index can be created as a pd.DatetimeIndex of indices or pd.IntervalIndex of intervals.
  • provides for independent force_close and force_break_close options.
  • takes advantage of exchange_calendars use of nanos for faster execution (evaluates trading index for XHKG (a calendar with breaks) over a year at 30 minute periods in <2ms).

Credit to @Stryder-Git at pandas_market_calendars for showing the way with a vectorised implementation.

Given the extent of the implementation, I've placed the guts of it as a class (_TradingIndex) on calendar_helpers.py. This also fits neatly with the substantial test class being placed on test_calendar_helpers.py rather than overwhelming ExchangeCalendarTestBase.

I'll add/update tutorials on the readme branch to include example usage.

Tests

The functionality provides for a high degree of customisation, with the intervals, closed, force_close, force_break_close and curtail_overlaps options all providing for different behaviour. Such variablilty precludes comprehensively testing against concrete cases. The hypothesis library (new dev. dependency) is instead employed to define fuzz tests.

Testing is split between:

  • ExchangeCalendarTestBase.test_trading_index. Tests, for all calendars, a multitude of concrete cases covering the product of various periods and all answers session blocks. Options assume default values.
  • test_calendar_helpers.TestTradingIndex. Class subjects select calendars (each of a unique behaviour) to:
    • fuzz tests verifying expected behaviour / no unexpected errors. These cover all date ranges, periods and options.
    • parsing tests.

This was referenced Oct 15, 2021
@maread99 maread99 marked this pull request as ready for review October 15, 2021 23:16
@maread99 maread99 added the enhancement New feature or request label Oct 15, 2021
@maread99 maread99 requested a review from gerrymanoim October 16, 2021 13:52
@Stryder-Git
Copy link

@maread99, Thanks for the credits and kind words. I also really like what you are doing with nanos over here and I will definitely draw inspiration from it!

In fact, my work on index calculators had not stopped after re-creating the date_range function. I have kept a type of side project the last few months, which I have now moved here.

It is basically an IndexCalculator that allows even more flexibility but still needs some stress testing. When I find some more free time, I will do so by following your suggestion and learning more about the hypothesis package. I apologize, but I have been taking my time with writing docs, so the most information can be gained by looking at the tests.

If you have any thoughts on it, feel free to open an issue over there, I am looking forward to hearing them!

Thanks

@maread99
Copy link
Collaborator Author

Hi @Stryder-Git, thanks for letting us know about the IndexCalculator. I'll certainly keep an eye on what you're doing over there.

Off topic for this PR, but I know you've been working on some improvements to the pandas_market_calendars implementation, including on the exchange_calendars mirrors. If in the course of that work anything occurs to you that could improve exchange_calendars, or help compatibilty between the two packages, then please do let us know via an issue here, or of course a PR!

Cheers

Adds:
- `ExchangeCalendar.trading_index`.
- `_TradingIndex` to `calendar_helpers.py`.
- `TestTradingIndex` to `test_calendar_helpers.py`
- `ExchangeCalendarTestBase.test_trading_index`.

Adds following classes to `errors.py`:
- `IndexOverlapError`
- `IntervalsOverlapError`
- `IndicesOverlapError`

Also:
- Changes `ExchangeCalendar` properties `default_side` and
`valid_sides` to class methods.
- Adds `Answers.get_session_minutes`.
Adds hypothesis to extras_require.
@maread99
Copy link
Collaborator Author

maread99 commented Oct 23, 2021

Tutorial's here.

@gerrymanoim gerrymanoim merged commit fe5ee00 into master Oct 25, 2021
@gerrymanoim gerrymanoim deleted the trading-index-3 branch October 25, 2021 01:17
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.

3 participants