Skip to content

Silent order cancellation for absolute-sized orders with insufficient margin (no warning emitted) #1360

@ZaToJa

Description

@ZaToJa

Expected behavior

A UserWarning should be emitted (matching the fractional-size path), e.g.:

UserWarning: time=N: Broker canceled the order (size=4) due to insufficient margin.

Code sample

Actual behavior

Silent order cancellation

Additional info, steps to reproduce, full crash traceback, screenshots

Description

When an order with absolute size (integer or float ≥ 1) is cancelled due to insufficient margin, the broker silently removes it with no feedback to the user. This is inconsistent with the behavior for fractional sizes (-1 < size < 1), which correctly emit a UserWarning.

Affected code (_Broker._process_orders):

If we don't have enough liquidity to cover for the order, the broker CANCELS it

if abs(need_size) * adjusted_price_plus_commission > self.margin_available * self._leverage:
self.orders.remove(order)
continue # no warning here
Compare with the fractional-size path, which does warn:

if not size:
warnings.warn(
f'time={self._i}: Broker canceled the relative-sized '
f'order due to insufficient margin.', category=UserWarning)
self.orders.remove(order)
continue
How to reproduce

Run a backtest where the absolute position size × price exceeds available cash. For example, cash=100_000 with buy(size=4) at a price of ~27,000 (NQ futures-like data). The order is silently dropped every bar — no exception, no warning, no indication in stats.

Expected behavior

A UserWarning should be emitted (matching the fractional-size path), e.g.:

UserWarning: time=N: Broker canceled the order (size=4) due to insufficient margin.
Suggested fix

if abs(need_size) * adjusted_price_plus_commission > self.margin_available * self._leverage:
warnings.warn(
f'time={self._i}: Broker canceled the order (size={need_size}) '
f'due to insufficient margin.', category=UserWarning)
self.orders.remove(order)
continue

Software versions

  • backtesting.__version__:
  • pandas.__version__:
  • numpy.__version__:
  • bokeh.__version__:
  • OS:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions