Repository navigation
fix(futures): release the margin a gap-filled entry reserved - #657
Open
QuantIntellect wants to merge 1 commit into
Open
QuantIntellect wants to merge 1 commit into
QuantIntellect wants to merge 1 commit into
Conversation
An opening gap fills a resting entry at the open and moves order.price there, but the margin row was reserved at the submitted price. Execution looked for the row at the fill price, missed it, and left it reserved for the rest of the backtest, so available_margin kept shrinking. Release the row at order.vars['submitted_price'], the price 021f8ff already uses for spot buys.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A resting futures entry that an opening gap fills never releases the margin it reserved, so
available_marginstays too low for the rest of the backtest.on_order_submissionreserves[qty, price]inbuy_orders/sell_ordersat the submitted price._execute_opening_gap_ordersthen movesorder.priceto the open and keeps the original inorder.vars['submitted_price']. Soon_order_executionlooks for[qty, open], doesn't find it, and the row is never removed. This change makesFuturesExchangerelease the row atsubmitted_price, the price 021f8ff already uses for spot buys.Cancellation still matches on
order.price, which is correct: the gap path rewrites the price only for active orders, right beforeexecute_order, so an order with a rewritten price is never cancelled.The wallet and PnL were already right. Only
available_marginwas wrong, but order submission checks it and strategies size from it. On master (ddb5a66), with no fees:InsufficientMargin("available margin is $3200") and the backtest stops. With this change all five trades run, for +2,000.self.available_margin: the fifth entry is 14.13 units on master instead of 157.09, and the wallet at that point is 12,308.78 instead of 14,923.23.Both scenarios give the same numbers with market-closure (sparse) candles, in step and fast mode. The leak also happens in isolated mode, and when one gap fills an entry split across two limit orders. The change fixes both.
Tests.
test_futures_entry_filled_by_a_price_gap_releases_its_marginintests/test_price_gap_backtest.pycovers limit and stop entries on both sides, in step and fast mode, with a 0.001 fee. Each case ends flat and checks that no reservation row is left and thatavailable_marginequals the wallet. All 8 cases fail on master and pass with the change. The full suite passes: 2627 passed, 102 skipped (Python 3.12).Scope.