|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
6 | | -from typing import Any, Dict, Tuple |
| 6 | +from typing import Any, Dict, List, Tuple |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def normalize_live_order_status(status: str) -> str: |
@@ -36,27 +36,47 @@ def tracked_fill_baseline( |
36 | 36 | previous_filled: float, |
37 | 37 | previous_avg: float, |
38 | 38 | ) -> Tuple[float, float]: |
39 | | - """Return the cumulative baseline for the currently tracked exchange leg.""" |
40 | | - tracked_filled = max(0.0, float(previous_filled or 0.0)) |
41 | | - tracked_avg = max(0.0, float(previous_avg or 0.0)) |
| 39 | + """Return the freshest cumulative baseline for the tracked exchange leg. |
| 40 | +
|
| 41 | + ``live_fill_sync`` is a progress snapshot, not an authoritative source: a |
| 42 | + sync can write zero before the executor records the fill in the row. Keep |
| 43 | + it as a candidate instead of allowing it to hide newer row or executor |
| 44 | + state after a worker restart. |
| 45 | +
|
| 46 | + When the executor summary names this exchange order, the pending-order row |
| 47 | + may aggregate multiple legs. In that case only the executor summary and |
| 48 | + sync marker compete; otherwise the row aggregate is a valid candidate. |
| 49 | + """ |
| 50 | + candidates: List[Tuple[float, float]] = [] |
| 51 | + executor_baseline: Tuple[float, float] | None = None |
42 | 52 | try: |
43 | 53 | previous_response = json.loads(str(row.get("exchange_response_json") or "{}")) or {} |
44 | 54 | sync_state = previous_response.get("live_fill_sync") or {} |
45 | 55 | if isinstance(sync_state, dict) and "tracked_filled" in sync_state: |
46 | | - return ( |
47 | | - max(0.0, float(sync_state.get("tracked_filled") or 0.0)), |
48 | | - max(0.0, float(sync_state.get("tracked_avg_price") or 0.0)), |
| 56 | + candidates.append( |
| 57 | + ( |
| 58 | + max(0.0, float(sync_state.get("tracked_filled") or 0.0)), |
| 59 | + max(0.0, float(sync_state.get("tracked_avg_price") or 0.0)), |
| 60 | + ) |
49 | 61 | ) |
50 | 62 | executor_raw = ((previous_response.get("phases") or {}).get("executor") or {}) |
51 | 63 | market_summary = executor_raw.get("market_summary") or {} |
52 | 64 | if ( |
53 | 65 | isinstance(market_summary, dict) |
54 | 66 | and str(market_summary.get("exchange_order_id") or "") == str(exchange_order_id or "") |
55 | 67 | ): |
56 | | - return ( |
| 68 | + executor_baseline = ( |
57 | 69 | max(0.0, float(market_summary.get("filled_qty") or 0.0)), |
58 | 70 | max(0.0, float(market_summary.get("avg_price") or 0.0)), |
59 | 71 | ) |
| 72 | + candidates.append(executor_baseline) |
60 | 73 | except Exception: |
61 | 74 | pass |
62 | | - return tracked_filled, tracked_avg |
| 75 | + if executor_baseline is None: |
| 76 | + candidates.append( |
| 77 | + ( |
| 78 | + max(0.0, float(previous_filled or 0.0)), |
| 79 | + max(0.0, float(previous_avg or 0.0)), |
| 80 | + ) |
| 81 | + ) |
| 82 | + return max(candidates, key=lambda item: item[0]) |
0 commit comments