|
| 1 | +"""Bounded execution-receipt facts derived from Firstrade cycle results.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Mapping, Sequence |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from quant_platform_kit.common.execution_receipts import ( |
| 9 | + attach_runtime_execution_receipt, |
| 10 | + resolve_execution_receipt_fact, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +_RECONCILIATION_STAGES = frozenset({"PENDING_RECONCILIATION", "PENDING_SUBMISSION"}) |
| 15 | +_RISK_BLOCKED_STAGES = frozenset({"EXECUTION_BLOCKED", "FUNDING_BLOCKED"}) |
| 16 | + |
| 17 | + |
| 18 | +def attach_strategy_result_execution_receipt( |
| 19 | + report: dict[str, Any], |
| 20 | + result: Mapping[str, Any], |
| 21 | + *, |
| 22 | + dry_run: bool, |
| 23 | +) -> dict[str, Any]: |
| 24 | + """Attach only the highest broker fact already present in a cycle result. |
| 25 | +
|
| 26 | + A locally recorded submission is deliberately reported as ``submitted``; |
| 27 | + Firstrade's cycle result has no fill-confirmation field, so this adapter |
| 28 | + never infers an acknowledgement or fill from it. |
| 29 | + """ |
| 30 | + |
| 31 | + stage = str(result.get("strategy_run_stage") or "").strip().upper() |
| 32 | + submitted_orders = _as_sequence(result.get("submitted_orders")) |
| 33 | + submission_attempted = bool( |
| 34 | + result.get("action_done") |
| 35 | + or result.get("broker_submission_done") |
| 36 | + or submitted_orders |
| 37 | + ) |
| 38 | + reconciliation_required = stage in _RECONCILIATION_STAGES or bool( |
| 39 | + result.get("execution_status") == "pending_reconciliation" |
| 40 | + or result.get("orders_pending_count") |
| 41 | + ) |
| 42 | + risk_blocked = not submission_attempted and ( |
| 43 | + stage in _RISK_BLOCKED_STAGES |
| 44 | + or bool(result.get("execution_blocked")) |
| 45 | + or bool(result.get("funding_blocked")) |
| 46 | + ) |
| 47 | + failed = ( |
| 48 | + result.get("ok", True) is False |
| 49 | + and not submission_attempted |
| 50 | + and not reconciliation_required |
| 51 | + and not risk_blocked |
| 52 | + ) |
| 53 | + outcome, confirmation = resolve_execution_receipt_fact( |
| 54 | + dry_run=dry_run, |
| 55 | + submission_attempted=submission_attempted, |
| 56 | + reconciliation_required=reconciliation_required, |
| 57 | + risk_blocked=risk_blocked, |
| 58 | + failed=failed, |
| 59 | + ) |
| 60 | + return attach_runtime_execution_receipt( |
| 61 | + report, |
| 62 | + outcome=outcome, |
| 63 | + broker_confirmation=confirmation, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def attach_unknown_failure_execution_receipt(report: dict[str, Any]) -> dict[str, Any]: |
| 68 | + """Preserve uncertainty when an exception escapes the strategy cycle.""" |
| 69 | + |
| 70 | + outcome, confirmation = resolve_execution_receipt_fact( |
| 71 | + dry_run=bool(report.get("dry_run")), |
| 72 | + submission_attempted=True, |
| 73 | + failed=True, |
| 74 | + ) |
| 75 | + return attach_runtime_execution_receipt( |
| 76 | + report, |
| 77 | + outcome=outcome, |
| 78 | + broker_confirmation=confirmation, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def _as_sequence(value: object) -> Sequence[object]: |
| 83 | + return value if isinstance(value, (list, tuple)) else () |
0 commit comments