Skip to content

Streaming backtest does not stop immediately when margin exceeded #2546

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

Closed
stastnypremysl opened this issue Apr 21, 2025 · 4 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@stastnypremysl
Copy link
Collaborator

stastnypremysl commented Apr 21, 2025

Bug Report

In given files margin-exceeded-nontermination_bug.py and margin-exceeded-nontermination_bug.out are a strategy script and its output. The strategy exceeds its margin.

Expected Behavior

Strategy immediately terminates after margin exceed is detected.

Actual Behavior

Strategy continues to run for some time.

Comment on severity

If margin exceed happens on the end of the backtesting, there might be a possibility, that it is not terminated correctly with an exception. If not solved, it is needed in my opinion to be sure, that once margin exceeded, the backtest can't finish without an exception raised.

Steps to Reproduce the Problem

  1. Download the data
  2. Extract the zip file and change extracted catalog path in margin-exceeded-nontermination_bug.py to the catalog
  3. ./margin-exceeded-nontermination_bug.py >./margin-exceeded-nontermination_bug.out 2>&1
  4. less ./margin-exceeded-nontermination_bug.out

Specifications

  • OS platform: Gentoo Linux (native build, amd64)
  • Python version: Python 3.12.9 (native build)
  • nautilus_trader version: 1.217.0 (revision 37b4a6f; make build-wheel)

Additional notes

  • The simulation is running on aggregated trade ticks from Binance data collection
  • The instrument information was downloaded using the NT Binance client
  • Ignore the 1.217.2 version in the log - no changes in the source codes was made other then change of version
@stastnypremysl stastnypremysl added the bug Something isn't working label Apr 21, 2025
@cjdsellers cjdsellers changed the title Simulation don't stop immediately, when margin exceeded Simulation doesn't stop immediately when margin exceeded Apr 22, 2025
@cjdsellers
Copy link
Member

Hi @stastnypremysl

Thanks for the report.

In the provided output I see the following error (related to balance), which stops the backtest:

ValueError(`free` amount was negative)

My understanding is you're concerned that when margin is exceeded on the last iteration of the engine, an exception may not be raised and so this condition would go undetected?

I agree with you that this is not preferable -- the user should always be notified of this in all circumstances.

If this is the expectation, are you able to provide a Python script which clearly reproduces that scenario. This would help us to target a change for the expected behavior efficiently.

@stastnypremysl
Copy link
Collaborator Author

Hi @cjdsellers,

As I wrote, this is only a possibility. This is a bug either way, but with different severity.

I will try to provide an example, where it exceeds margin without a fail or confirm, it always failed in my tests, so we know more and the bug is handled appropriately with correct severity.

@stastnypremysl
Copy link
Collaborator Author

Hi @cjdsellers,

Here is a strategy and its output, in which an exception is not raised, but the margin is exceed. The data are still the same.

@cjdsellers cjdsellers self-assigned this Apr 30, 2025
@cjdsellers cjdsellers moved this to In progress in NautilusTrader Kanban Board Apr 30, 2025
@cjdsellers cjdsellers changed the title Simulation doesn't stop immediately when margin exceeded Improve backtest behavior on stop due margin exceeded May 2, 2025
@cjdsellers cjdsellers changed the title Improve backtest behavior on stop due margin exceeded Streaming backtest does not stop immediately when margin exceeded May 2, 2025
@cjdsellers
Copy link
Member

Hi @stastnypremysl

Thanks for the report.

The issue here was that the AccountError producing those margin exceeded logs was being handled by the backtest engine (to produce the log), but not reraised to interrupt the streaming of chunks here.

Now fixed from commit 442a95b, the backtest now stops immediately on the first occurrence of margin being exceeded.

@cjdsellers cjdsellers moved this from In progress to Done in NautilusTrader Kanban Board May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants