|
75 | 75 | ) |
76 | 76 | from app.services.pending_order_position_sync import PendingOrderPositionSyncMixin |
77 | 77 | from app.services.pending_orders.sent_order_recovery import ( |
78 | | - normalize_live_order_status, |
| 78 | + is_final_fill, normalize_live_order_status, |
79 | 79 | tracked_fill_baseline, |
80 | 80 | ) |
81 | 81 | from app.services.live_trading.binance import BinanceFuturesClient |
|
110 | 110 | ALPACA_FILL_DELTA_EPSILON = 1e-8 |
111 | 111 |
|
112 | 112 |
|
113 | | -def _is_final_fill( |
114 | | - *, |
115 | | - requested: float, |
116 | | - filled: float, |
117 | | - avg_price: float, |
118 | | - status: Any = "", |
119 | | -) -> bool: |
120 | | - requested_qty = max(0.0, float(requested or 0.0)) |
121 | | - filled_qty = max(0.0, float(filled or 0.0)) |
122 | | - if requested_qty <= 0 or filled_qty <= 0 or float(avg_price or 0.0) <= 0: |
123 | | - return False |
124 | | - if normalize_live_order_status(status) == "filled": |
125 | | - return True |
126 | | - return filled_qty >= requested_qty * 0.999999 |
127 | | - |
128 | | - |
129 | 113 | def _broker_order_type(payload: Dict[str, Any], ref_price: float) -> Tuple[str, float]: |
130 | 114 | order_type = str(payload.get("order_type") or "market").strip().lower() |
131 | 115 | if order_type == "maker_then_market": |
@@ -2190,12 +2174,7 @@ def _execute_live_order(self, *, order_id: int, order_row: Dict[str, Any], paylo |
2190 | 2174 | filled=filled, |
2191 | 2175 | avg_price=avg_price, |
2192 | 2176 | executed_at=executed_at if filled > 0 else None, |
2193 | | - final_filled=_is_final_fill( |
2194 | | - requested=amount, |
2195 | | - filled=filled, |
2196 | | - avg_price=avg_price, |
2197 | | - status=execution_result.status, |
2198 | | - ), |
| 2177 | + final_filled=is_final_fill(amount, filled, avg_price, execution_result.status), |
2199 | 2178 | ) |
2200 | 2179 | _console_print(f"[worker] order sent: strategy_id={strategy_id} pending_id={order_id} exchange={res.exchange_id} order_id={res.exchange_order_id} filled={filled} avg={avg_price}") |
2201 | 2180 | except Exception as e: |
@@ -2392,12 +2371,7 @@ def _execute_ibkr_order( |
2392 | 2371 | filled=filled, |
2393 | 2372 | avg_price=avg_price, |
2394 | 2373 | executed_at=executed_at if filled > 0 else None, |
2395 | | - final_filled=_is_final_fill( |
2396 | | - requested=amount, |
2397 | | - filled=filled, |
2398 | | - avg_price=avg_price, |
2399 | | - status=result.status, |
2400 | | - ), |
| 2374 | + final_filled=is_final_fill(amount, filled, avg_price, result.status), |
2401 | 2375 | ) |
2402 | 2376 | _console_print(f"[worker] IBKR order sent: strategy_id={strategy_id} pending_id={order_id} order_id={exchange_order_id} filled={filled} avg={avg_price}") |
2403 | 2377 |
|
@@ -2575,12 +2549,7 @@ def _execute_alpaca_order( |
2575 | 2549 | filled=filled, |
2576 | 2550 | avg_price=avg_price, |
2577 | 2551 | executed_at=executed_at if filled > 0 else None, |
2578 | | - final_filled=_is_final_fill( |
2579 | | - requested=amount, |
2580 | | - filled=filled, |
2581 | | - avg_price=avg_price, |
2582 | | - status=result.status, |
2583 | | - ), |
| 2552 | + final_filled=is_final_fill(amount, filled, avg_price, result.status), |
2584 | 2553 | ) |
2585 | 2554 | _console_print( |
2586 | 2555 | f"[worker] Alpaca order sent: strategy_id={strategy_id} pending_id={order_id} " |
|
0 commit comments