Skip to content

Conversation

ms32035
Copy link
Contributor

@ms32035 ms32035 commented Jun 21, 2025

Pull Request

NautilusTrader prioritizes correctness and reliability, please follow existing patterns for validation and testing.

Summary

Under certain scenarios when running backtests with a margin account I would end up with a negative balance, which was then unhandled and caused:

  File "nautilus_trader/portfolio/portfolio.pyx", line 504, in nautilus_trader.portfolio.portfolio.Portfolio.update_order
  File "nautilus_trader/accounting/manager.pyx", line 145, in nautilus_trader.accounting.manager.AccountsManager.update_balances
  File "nautilus_trader/accounting/manager.pyx", line 473, in nautilus_trader.accounting.manager.AccountsManager._update_balance_single_currency
  File "nautilus_trader/model/objects.pyx", line 1631, in nautilus_trader.model.objects.AccountBalance.__init__
  File "nautilus_trader/core/correctness.pyx", line 57, in nautilus_trader.core.correctness.Condition.is_true
ValueError: `total` amount was negative

This PR handles the problems by properly raising AccountBalanceNegative

Related Issues/PRs

Type of change

  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change (impacts existing behavior)
  • Documentation update
  • Maintenance / chore

Breaking change details (if applicable)

Release notes

  • I added a concise entry to RELEASES.md that follows the existing conventions (when applicable)

Testing

Ensure new or changed logic is covered by tests.

  • Affected code paths are already covered by the test suite
  • I added/updated tests to cover new or changed logic

@CLAassistant
Copy link

CLAassistant commented Jun 21, 2025

CLA assistant check
All committers have signed the CLA.

@cjdsellers
Copy link
Member

Hi @ms32035

Thanks for the contribution! I think this makes the negative balance condition much clearer.

@cjdsellers cjdsellers merged commit eadb901 into nautechsystems:develop Jun 22, 2025
13 checks passed
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