@@ -254,8 +254,12 @@ def check_order_submitted(report, *, translator):
254254 """Check if order was accepted. DAY orders auto-expire at close if not filled."""
255255 order_id = report .broker_order_id
256256 status = report .status
257+ order_state = normalize_ibkr_order_state (
258+ status ,
259+ filled_quantity = getattr (report , "filled_quantity" , 0.0 ),
260+ )
257261
258- if status == "Filled " :
262+ if order_state == "filled " :
259263 return (
260264 True ,
261265 translator (
@@ -267,7 +271,7 @@ def check_order_submitted(report, *, translator):
267271 order_id = order_id ,
268272 ),
269273 )
270- if status in { "PartiallyFilled" , "Partial" } :
274+ if order_state == "partially_filled" :
271275 return (
272276 True ,
273277 translator (
@@ -280,18 +284,11 @@ def check_order_submitted(report, *, translator):
280284 order_id = order_id ,
281285 ),
282286 )
283- if status in {"PendingSubmit " , "ApiPending" , "ApiPendingSubmit" , "Submitted" , "PreSubmitted " }:
287+ if order_state in {"submitted " , "pending_cancel " }:
284288 return True , f"✅ { translator ('submitted' , order_id = order_id , status = status )} "
285289 return False , f"❌ { translator ('failed' , reason = status )} "
286290
287291
288- _FILLED_ORDER_STATUSES = frozenset ({"Filled" })
289- _PARTIALLY_FILLED_ORDER_STATUSES = frozenset ({"PartiallyFilled" , "Partial" })
290- _PENDING_ORDER_STATUSES = frozenset (
291- {"PendingSubmit" , "ApiPending" , "ApiPendingSubmit" , "Submitted" , "PreSubmitted" }
292- )
293-
294-
295292def _ibkr_client_id (ib : Any ) -> object :
296293 wrapper = getattr (ib , "wrapper" , None )
297294 if wrapper is not None and getattr (wrapper , "clientId" , None ) is not None :
@@ -318,7 +315,13 @@ def _build_order_event_payload(
318315 "broker_order_id" : order_id ,
319316 "cumulative_filled_quantity" : float (getattr (report , "filled_quantity" , 0.0 ) or 0.0 ),
320317 "status_transitions" : [
321- {"from" : "created" , "to" : normalize_ibkr_order_state (status )},
318+ {
319+ "from" : "created" ,
320+ "to" : normalize_ibkr_order_state (
321+ status ,
322+ filled_quantity = getattr (report , "filled_quantity" , 0.0 ),
323+ ),
324+ },
322325 ],
323326 }
324327 try :
@@ -353,14 +356,18 @@ def _record_order_outcome(
353356 make the rebalance appear complete.
354357 """
355358 normalized_status = str (status or "" ).strip ()
359+ order_state = normalize_ibkr_order_state (
360+ normalized_status ,
361+ filled_quantity = order_payload .get ("cumulative_filled_quantity" , 0.0 ),
362+ )
356363 prefix = "option_orders" if option_order else "orders"
357- if normalized_status in _FILLED_ORDER_STATUSES :
364+ if order_state == "filled" :
358365 execution_summary [f"{ prefix } _filled" ].append (order_payload )
359366 return "filled"
360- if normalized_status in _PARTIALLY_FILLED_ORDER_STATUSES :
367+ if order_state == "partially_filled" :
361368 execution_summary [f"{ prefix } _partially_filled" ].append (order_payload )
362369 return "partially_filled"
363- if normalized_status in _PENDING_ORDER_STATUSES :
370+ if order_state in { "submitted" , "pending_cancel" } :
364371 execution_summary [f"{ prefix } _pending" ].append (order_payload )
365372 return "pending"
366373 execution_summary [f"{ prefix } _skipped" ].append (
@@ -1378,10 +1385,11 @@ def _planned_buy_order_quantity(
13781385
13791386def _projected_sell_release_value_for_report (report , * , fallback_price = 0.0 , fallback_quantity = 0.0 ) -> float :
13801387 status = str (getattr (report , "status" , "" ) or "" )
1381- if status not in {"Filled" , "PartiallyFilled" , "Partial" }:
1382- return 0.0
13831388 filled_quantity = float (getattr (report , "filled_quantity" , 0.0 ) or 0.0 )
1384- if status == "Filled" and filled_quantity <= 0.0 :
1389+ order_state = normalize_ibkr_order_state (status , filled_quantity = filled_quantity )
1390+ if order_state not in {"filled" , "partially_filled" }:
1391+ return 0.0
1392+ if order_state == "filled" and filled_quantity <= 0.0 :
13851393 filled_quantity = float (getattr (report , "quantity" , 0.0 ) or fallback_quantity or 0.0 )
13861394 if filled_quantity <= 0.0 :
13871395 return 0.0
0 commit comments