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 adaptive_bar_ordering to BacktestVenueConfig #2188

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

faysou
Copy link
Collaborator

@faysou faysou commented Jan 7, 2025

Pull Request

Add adaptive_bar_ordering to BacktestVenueConfig

Executes Low before High if Low is closer to Open, and vice versa

Type of change

  • New feature (non-breaking change which adds functionality)

How has this change been tested?

Attempted to add a test in test_matching_engine.py, left it as comment for further work

@faysou faysou force-pushed the dynamicbarexecution branch 2 times, most recently from 1efba17 to 81bfc14 Compare January 7, 2025 09:53
@faysou faysou changed the title Add dynamic_bar_execution to BacktestEngineConfig Add dynamic_bar_execution to BacktestVenueConfig Jan 7, 2025
@faysou faysou force-pushed the dynamicbarexecution branch from 81bfc14 to 2e358b0 Compare January 7, 2025 10:02
@stefansimik
Copy link
Contributor

Hi @faysou ,

First, thank you again for implementing this feature so quickly! While testing it, I had a thought about the configuration name dynamic_bar_execution that I wanted to run by you.

Would you consider renaming it to adaptive_bar_ordering? Here's my reasoning:

  1. "Ordering" more precisely describes what we're modifying - it's specifically about the sequence of OHLC price points within a bar, rather than the broader concept of "execution"
  2. "Adaptive" better communicates that the ordering is based on a specific condition (price proximity) rather than just being dynamic/random

The new name would make the feature's purpose more immediately clear to other users who might benefit from this improvement in backtesting accuracy.

What are your thoughts on this? :-)

@faysou
Copy link
Collaborator Author

faysou commented Jan 7, 2025

Yes no problem, I'll make the change later today

@faysou faysou force-pushed the dynamicbarexecution branch from 2e358b0 to 8bd7c60 Compare January 7, 2025 11:36
@stefansimik
Copy link
Contributor

I have tested this new behavior for basic scenario and I can confirm it works great 👍

CleanShot 2025-01-07 at 14 47 53@2x

@faysou faysou force-pushed the dynamicbarexecution branch from 8bd7c60 to 4ae173a Compare January 7, 2025 14:53
@faysou
Copy link
Collaborator Author

faysou commented Jan 7, 2025

ok thank you. I've attempted to add a test, but it's quite difficult, I've left the current status of the code for the test as comment.

@stefansimik
Copy link
Contributor

stefansimik commented Jan 7, 2025

I was also checking, how to debug this (using some Python breakpoint) and found out, that the only way for me was to check the DEBUG logs. Probably it is hard to test easily using Python, as the ticks from bars are generated outside of Python.

As an alternative (for missing test coverage), I will be using this feature extensively - so I will guarantee correct working.
Let's use human power here, as in the old days 😄

@faysou
Copy link
Collaborator Author

faysou commented Jan 7, 2025

yes ticks from bars are in cython. you can add log or print statements then recompile with make build-debug

@stefansimik
Copy link
Contributor

yes ticks from bars are in cython. you can add log or print statements then recompile with make build-debug

Yeah, I am really happy I learnt this from you today - how to build / compile this locally. That makes a huge difference in testing /dev and brings new options how to test everything. Very happy for this 👍

@faysou
Copy link
Collaborator Author

faysou commented Jan 7, 2025

Yes, I've saved you some time, but you have good ideas as well, I like that. Most people never have any idea 😂

@faysou faysou changed the title Add dynamic_bar_execution to BacktestVenueConfig Add adaptive_bar_ordering to BacktestVenueConfig Jan 7, 2025
@faysou faysou closed this Jan 7, 2025
@faysou faysou deleted the dynamicbarexecution branch January 7, 2025 19:46
@faysou faysou restored the dynamicbarexecution branch January 7, 2025 19:46
@faysou faysou reopened this Jan 7, 2025
@faysou faysou force-pushed the dynamicbarexecution branch from 4ae173a to 7104aea Compare January 7, 2025 20:17
Copy link
Member

@cjdsellers cjdsellers left a comment

Choose a reason for hiding this comment

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

Noted that the test is WIP and being actively worked on 👌

@cjdsellers cjdsellers merged commit a70e9b0 into nautechsystems:develop Jan 7, 2025
12 checks passed
@stefansimik
Copy link
Contributor

stefansimik commented Jan 7, 2025

Noted that the test is WIP and being actively worked on 👌

I was thinking about test - it probably belongs here:
tests/unit_tests/backtest/test_matching_engine.py,

but have no idea, how could one technically verify from Python, that Bar was splitted into 4 prices as this is done in Cython code. I can't simply find any link to internal data, that could be checked in the test to verify.

Maybe one option would be to verify it by the external effects -> for examle when we fire 2 limit order (1st near top of the bar, 2nd near bottom of the bar) and we can check in which order they are filled. By this approach could be confusing and too far from original simple point we need to test.

@cjdsellers
Copy link
Member

@stefansimik I also think thats a good location for the test, and @faysou is currently working on one.

I think testing the external effects of setting the config option True and then processing bars and checking order fills would be enough.

The OrderMatchingEngine is indirectly tested via the SimulatedExchange which contains it, in many of those test_exchange* modules. Thats why there aren't many tests in test_matching_engine.py. (There was a refactoring years ago which extracted the matching engine out of the exchange, but test modules weren't renamed.)

@filipmacek is currently working on the Rust port for OrderMatchingEngine, and we'll make sure this time around its more thoroughly and directly tested.

I also just pushed this refactoring.

  • Renamed to bar_adaptive_high_low_ordering to more clearly express the behavior.
  • Reordered the params to group the bar config options and adjust the hierarchy of settings.

@stefansimik
Copy link
Contributor

Like the new name even more = 100% clear and self-descriptive. 👌👌

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 this pull request may close these issues.

3 participants