The 88-second phantom
The bot booked a winning exit that the broker hadn't filled yet.
- Trigger fires on WETO. Long entry submitted.
- Filled: 242 shares at 8.7731. Stop 8.34, target 9.45 resting.
- A print at 9.47 crosses the target. The bot books +$168.66 and drops the position from its book.
reconcile_alarm: "broker holds 242 WETO with NO position in the book". Thirteen times.- The resting target finally fills. For 88 seconds a live position sat outside the book: no synthetic stop, no crash adoption, and a resting order nobody would have cancelled.
The fix: decide on the touch, settle on the broker.
The exit decision still happens on the first touch, so parity with the backtest is unchanged. Only the bookkeeping now waits for the broker's fill. Fill quantities are read, never assumed, and every session ends with an audit of IBKR's own execution records against the bot's ledger.
def _arm_target_confirm(self, pos: Position, price: float) -> None:
"""The price touched the target. Record the decision -- but do NOT book
the exit, because the resting limit is a real order at the exchange and
only IT can say whether we are flat."""
self._confirming[pos.key()] = (price, systime.monotonic())
self.rec.event("target_touched", ticker=pos.ticker, touch=price,
target=pos.target_price, shares=pos.remaining)
From live_bot/order_manager.py.