Skip to content

fix(futures): release the margin a gap-filled entry reserved - #657

Open
QuantIntellect wants to merge 1 commit into
jesse-ai:masterfrom
QuantIntellect:fix/futures-gap-entry-margin
Open

QuantIntellect wants to merge 1 commit into
jesse-ai:masterfrom
QuantIntellect:fix/futures-gap-entry-margin

Conversation

@QuantIntellect

Copy link
Copy Markdown

A resting futures entry that an opening gap fills never releases the margin it reserved, so available_margin stays too low for the rest of the backtest.

on_order_submission reserves [qty, price] in buy_orders / sell_orders at the submitted price. _execute_opening_gap_orders then moves order.price to the open and keeps the original in order.vars['submitted_price']. So on_order_execution looks for [qty, open], doesn't find it, and the row is never removed. This change makes FuturesExchange release the row at submitted_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 before execute_order, so an order with a rewritten price is never cancelled.

The wallet and PnL were already right. Only available_margin was wrong, but order submission checks it and strategies size from it. On master (ddb5a66), with no fees:

  • The backtest aborts. 1x cross, 10,000 balance: five limit buys of 40 @ 95 in a row, each filled at 90 by a gap and closed by a take-profit at 100. Each one leaves its 3,800 reserved, so available margin goes 10,000 → 6,600 → 3,200 while the wallet goes 10,000 → 10,400 → 10,800. The third entry raises InsufficientMargin ("available margin is $3200") and the backtest stops. With this change all five trades run, for +2,000.
  • Positions shrink. The same trades at 2x cross, each entry sized at 50% of 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.
  • Margin stays reserved. At 2x, after one gap-filled trade with nothing left resting: a limit buy 2 @ 95 filled at 90 leaves 95 reserved, and a stop buy or limit sell 2 @ 105 filled at 110 leaves 105.

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_margin in tests/test_price_gap_backtest.py covers 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 that available_margin equals 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.

  • Live trading returns early from these methods.
  • I didn't measure how often a gap fills a resting entry. It needs a candle that opens away from the previous close, so markets that close are the main case.

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.
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.

1 participant