This repository was archived by the owner on May 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoms.py
More file actions
1240 lines (1166 loc) · 49 KB
/
oms.py
File metadata and controls
1240 lines (1166 loc) · 49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: LicenseRef-TradePulse-Proprietary
"""Order management system with persistence and idempotent queues."""
from __future__ import annotations
import concurrent.futures
import json
import time
from collections import deque
from concurrent.futures import TimeoutError as FuturesTimeoutError
from dataclasses import dataclass, replace
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable, Deque, Dict, Iterable, Mapping, MutableMapping
from core.utils.metrics import get_metrics_collector
from domain import Order, OrderSide, OrderStatus
from interfaces.execution import RiskController
from .audit import ExecutionAuditLogger, get_execution_audit_logger
from .compliance import ComplianceMonitor, ComplianceReport, ComplianceViolation
from .connectors import ExecutionConnector, OrderError, TransientOrderError
from .order_ledger import OrderLedger
from .order_lifecycle import OrderEvent, OrderLifecycle
DEFAULT_LEDGER_PATH = Path("observability/audit/order-ledger.jsonl")
@dataclass(slots=True)
class QueuedOrder:
"""Order request paired with its idempotency key."""
correlation_id: str
order: Order
attempts: int = 0
last_error: str | None = None
@dataclass(slots=True)
class OMSConfig:
"""Configuration for :class:`OrderManagementSystem`."""
state_path: Path
auto_persist: bool = True
max_retries: int = 3
backoff_seconds: float = 0.0
ledger_path: Path | None = DEFAULT_LEDGER_PATH
request_timeout: float | None = None
pre_trade_timeout: float | None = 0.25
def __post_init__(self) -> None:
if not isinstance(self.state_path, Path):
object.__setattr__(self, "state_path", Path(self.state_path))
if self.max_retries < 1:
object.__setattr__(self, "max_retries", 1)
if self.backoff_seconds < 0.0:
object.__setattr__(self, "backoff_seconds", 0.0)
if self.request_timeout is not None and self.request_timeout <= 0:
object.__setattr__(self, "request_timeout", None)
if self.pre_trade_timeout is not None and self.pre_trade_timeout <= 0:
object.__setattr__(self, "pre_trade_timeout", None)
ledger_path = self.ledger_path
if ledger_path is not None and not isinstance(ledger_path, Path):
ledger_path = Path(ledger_path)
if ledger_path == DEFAULT_LEDGER_PATH:
ledger_path = (
self.state_path.parent / f"{self.state_path.stem}_ledger.jsonl"
)
if ledger_path is not None:
ledger_path.parent.mkdir(parents=True, exist_ok=True)
object.__setattr__(self, "ledger_path", ledger_path)
class OrderManagementSystem:
"""Queue-based OMS with state recovery."""
def __init__(
self,
connector: ExecutionConnector,
risk_controller: RiskController,
config: OMSConfig,
*,
compliance_monitor: ComplianceMonitor | None = None,
audit_logger: ExecutionAuditLogger | None = None,
lifecycle: OrderLifecycle | None = None,
risk_compliance: object | None = None,
circuit_breaker: object | None = None,
) -> None:
self.connector = connector
self.risk = risk_controller
self.config = config
self._compliance = compliance_monitor
self._risk_compliance = risk_compliance
self._circuit_breaker = circuit_breaker
self._queue: Deque[QueuedOrder] = deque()
self._orders: MutableMapping[str, Order] = {}
self._processed: Dict[str, str] = {}
self._correlations: Dict[str, str] = {}
self._metrics = get_metrics_collector()
self._ack_timestamps: Dict[str, datetime] = {}
self._pending: Dict[str, Order] = {}
self._audit = audit_logger or get_execution_audit_logger()
self._active_orders: Dict[str, Order] = {}
self._active_cache: tuple[Order, ...] = ()
self._active_cache_dirty = False
self._fingerprints: Dict[str, str] = {}
self._broker_lookup: Dict[str, str] = {}
self._lifecycle = lifecycle
self._lifecycle_sequences: Dict[str, int] = {}
ledger_path = self.config.ledger_path
self._ledger = OrderLedger(ledger_path) if ledger_path is not None else None
self._load_state()
# ------------------------------------------------------------------
# Persistence helpers
def _state_payload(self) -> dict:
return {
"orders": [self._serialize_order(order) for order in self._orders.values()],
"queue": [
{
"correlation_id": item.correlation_id,
"order": self._serialize_order(item.order),
"attempts": item.attempts,
"last_error": item.last_error,
}
for item in self._queue
],
"processed": self._processed,
"correlations": self._correlations,
"fingerprints": self._fingerprints,
"lifecycle_sequences": self._lifecycle_sequences,
}
def _persist_state(self) -> None:
if not self.config.auto_persist:
return
path = self.config.state_path
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(self._state_payload(), indent=2, sort_keys=True))
def _load_state(self) -> None:
path = self.config.state_path
payload: Mapping[str, object] | None = None
source = "state_file"
if path.exists():
try:
payload = json.loads(path.read_text())
except json.JSONDecodeError:
payload = None
if payload is None and self._ledger is not None:
payload = self._ledger.latest_state()
source = "ledger"
if payload is None:
return
self._apply_state_snapshot(payload, source=source)
def _apply_state_snapshot(
self, payload: Mapping[str, object], *, source: str = "manual"
) -> None:
self._orders = {
order.order_id: order
for order in self._restore_orders(payload.get("orders", []))
if order.order_id
}
self._queue = deque(
QueuedOrder(
item["correlation_id"],
self._restore_order(item["order"]),
int(item.get("attempts", 0)),
(
str(item.get("last_error"))
if item.get("last_error") is not None
else None
),
)
for item in payload.get("queue", [])
)
self._processed = {
str(k): str(v) for k, v in payload.get("processed", {}).items()
}
self._correlations = {
str(k): str(v) for k, v in payload.get("correlations", {}).items()
}
self._pending = {item.correlation_id: item.order for item in self._queue}
raw_fingerprints = payload.get("fingerprints")
if isinstance(raw_fingerprints, Mapping):
self._fingerprints = {
str(key): str(value) for key, value in raw_fingerprints.items()
}
else:
self._fingerprints = {}
if not self._fingerprints:
for item in self._queue:
self._fingerprints[item.correlation_id] = self._fingerprint(item.order)
for correlation, order_id in self._processed.items():
order = self._orders.get(order_id)
if order is not None:
self._fingerprints[correlation] = self._fingerprint(order)
raw_sequences = payload.get("lifecycle_sequences")
if isinstance(raw_sequences, Mapping):
self._lifecycle_sequences = {
str(key): int(value) for key, value in raw_sequences.items()
}
else:
self._lifecycle_sequences = {}
self._active_orders = {
order_id: order
for order_id, order in self._orders.items()
if order.is_active
}
self._active_cache = tuple(self._active_orders.values())
self._active_cache_dirty = False
self._broker_lookup = {
order.broker_order_id: order_id
for order_id, order in self._orders.items()
if order.broker_order_id
}
if self._ledger is not None:
self._record_ledger_event("state_restored", metadata={"source": source})
# ------------------------------------------------------------------
# Lifecycle helpers
def _ensure_lifecycle_sequence(self, order_id: str) -> None:
if not order_id or order_id in self._lifecycle_sequences:
return
sequence = 0
lifecycle = self._lifecycle
if lifecycle is not None:
try:
history = lifecycle.history(order_id)
except Exception: # pragma: no cover - defensive guard
history = []
for transition in history:
if transition.event not in (
OrderEvent.FILL_PARTIAL,
OrderEvent.FILL_FINAL,
):
continue
parts = transition.correlation_id.rsplit(":", 1)
if len(parts) != 2:
continue
try:
candidate = int(parts[1])
except ValueError:
continue
if candidate > sequence:
sequence = candidate
self._lifecycle_sequences[order_id] = sequence
def _next_fill_sequence(self, order_id: str) -> int:
self._ensure_lifecycle_sequence(order_id)
sequence = self._lifecycle_sequences.get(order_id, 0) + 1
self._lifecycle_sequences[order_id] = sequence
return sequence
def _record_lifecycle_event(
self,
order: Order,
event: OrderEvent,
*,
base_correlation: str | None,
metadata: Mapping[str, object] | None = None,
correlation_override: str | None = None,
sequence_hint: int | None = None,
) -> None:
lifecycle = self._lifecycle
if lifecycle is None:
return
order_id = order.order_id or base_correlation or order.broker_order_id
if order_id is None:
return
base = base_correlation or order_id
correlation = correlation_override
if correlation is None:
suffix = event.value
if sequence_hint is not None:
suffix = f"{suffix}:{sequence_hint}"
correlation = f"{base}:{suffix}"
payload: Dict[str, object] = {
"symbol": order.symbol,
"side": order.side.value,
"order_type": order.order_type.value,
"quantity": float(order.quantity),
"filled_quantity": float(order.filled_quantity),
"status": order.status.value,
}
if order.iceberg_visible is not None:
payload["iceberg_visible"] = float(order.iceberg_visible)
if order.broker_order_id:
payload["broker_order_id"] = order.broker_order_id
if order.average_price is not None:
payload["average_price"] = float(order.average_price)
if metadata:
payload.update(metadata)
lifecycle.apply(order_id, event, correlation_id=correlation, metadata=payload)
# ------------------------------------------------------------------
# Serialization helpers
def _fingerprint(self, order: Order) -> str:
payload: Dict[str, Any] = {
"symbol": order.symbol,
"side": order.side.value,
"quantity": float(order.quantity),
"order_type": order.order_type.value,
"price": None if order.price is None else float(order.price),
"stop_price": None if order.stop_price is None else float(order.stop_price),
"iceberg_visible": (
None if order.iceberg_visible is None else float(order.iceberg_visible)
),
}
metadata = getattr(order, "metadata", None)
if metadata is not None:
payload["metadata"] = metadata
return json.dumps(payload, sort_keys=True, separators=(",", ":"), default=str)
@staticmethod
def _serialize_order(order: Order) -> dict:
data = order.to_dict()
data["side"] = order.side.value
data["order_type"] = order.order_type.value
data["status"] = order.status.value
return data
@staticmethod
def _restore_orders(serialized: Iterable[dict]) -> Iterable[Order]:
return [OrderManagementSystem._restore_order(item) for item in serialized]
@staticmethod
def _restore_order(data: MutableMapping[str, object]) -> Order:
created_at = datetime.fromisoformat(str(data["created_at"]))
order = Order(
symbol=str(data["symbol"]),
side=str(data["side"]),
quantity=float(data["quantity"]),
price=float(data["price"]) if data.get("price") is not None else None,
order_type=str(data.get("order_type", "market")),
stop_price=(
float(data["stop_price"])
if data.get("stop_price") is not None
else None
),
iceberg_visible=(
float(data["iceberg_visible"])
if data.get("iceberg_visible") is not None
else None
),
order_id=str(data.get("order_id")) if data.get("order_id") else None,
broker_order_id=(
str(data.get("broker_order_id"))
if data.get("broker_order_id")
else None
),
status=str(data.get("status", "pending")),
filled_quantity=float(data.get("filled_quantity", 0.0)),
average_price=(
float(data["average_price"])
if data.get("average_price") is not None
else None
),
rejection_reason=(
str(data.get("rejection_reason"))
if data.get("rejection_reason")
else None
),
created_at=created_at,
)
if data.get("updated_at"):
object.__setattr__(
order, "updated_at", datetime.fromisoformat(str(data["updated_at"]))
)
return order
# ------------------------------------------------------------------
# Queue operations
def submit(self, order: Order, *, correlation_id: str) -> Order:
"""Submit an order, enforcing idempotency with correlation IDs."""
fingerprint = self._fingerprint(order)
existing_fp = self._fingerprints.get(correlation_id)
if existing_fp is not None and existing_fp != fingerprint:
raise ValueError("Correlation ID reused with a different order payload")
if correlation_id in self._processed:
order_id = self._processed[correlation_id]
stored = self._orders[order_id]
if existing_fp is None:
self._fingerprints[correlation_id] = self._fingerprint(stored)
return stored
if correlation_id in self._pending:
if existing_fp is None:
self._fingerprints[correlation_id] = fingerprint
return self._pending[correlation_id]
self._fingerprints[correlation_id] = fingerprint
try:
if self._compliance is not None:
report = None
try:
report = self._run_pre_trade_check(
"compliance",
self._compliance.check,
order.symbol,
order.quantity,
order.price,
)
except ComplianceViolation as exc:
report = exc.report
self._metrics.record_compliance_check(
order.symbol,
"blocked",
() if report is None else report.violations,
)
self._emit_compliance_audit(order, correlation_id, report, str(exc))
self._record_ledger_event(
"compliance_blocked",
order=order,
correlation_id=correlation_id,
metadata={
"violations": [] if report is None else report.violations,
"error": str(exc),
},
)
raise
except TimeoutError as exc:
order.reject("PRE_TRADE_TIMEOUT:compliance")
self._metrics.record_compliance_check(
order.symbol, "timeout", ()
)
self._emit_compliance_audit(order, correlation_id, None, str(exc))
self._record_ledger_event(
"compliance_timeout",
order=order,
correlation_id=correlation_id,
metadata={"error": str(exc)},
)
raise ComplianceViolation("Compliance check timed out") from exc
status = "passed" if report is None or report.is_clean() else "warning"
if report is not None:
self._metrics.record_compliance_check(
order.symbol,
"blocked" if report.blocked else status,
report.violations,
)
self._emit_compliance_audit(order, correlation_id, report, None)
if report.blocked:
self._record_ledger_event(
"compliance_blocked",
order=order,
correlation_id=correlation_id,
metadata={
"violations": report.violations,
"blocked": True,
},
)
raise ComplianceViolation(
"Compliance check blocked order", report=report
)
if self._circuit_breaker is not None:
if not self._circuit_breaker.can_execute():
reason = "Circuit breaker is OPEN"
last_trip = self._circuit_breaker.get_last_trip_reason()
if last_trip:
reason = f"{reason}: {last_trip}"
ttl = self._circuit_breaker.get_time_until_recovery()
order.reject(reason)
self._record_ledger_event(
"risk_blocked",
order=order,
correlation_id=correlation_id,
metadata={
"reason": reason,
"circuit_state": "open",
"ttl_seconds": ttl,
},
)
self._emit_risk_audit(
order, correlation_id, reason, {"ttl_seconds": ttl}
)
raise ComplianceViolation(reason)
if self._risk_compliance is not None:
reference_price = (
order.price
if order.price is not None
else max(order.average_price or 0.0, 1.0)
)
positions = {}
gross_exposure = 0.0
equity = 0.0
peak_equity = 0.0
if hasattr(self.risk, "current_position"):
try:
positions = {
symbol: self.risk.current_position(symbol)
for symbol in [order.symbol]
}
except Exception:
pass
if hasattr(self.risk, "_gross_notional"):
try:
gross_exposure = self.risk._gross_notional
except Exception:
pass
if hasattr(self.risk, "_balance"):
try:
equity = self.risk._balance
peak_equity = getattr(self.risk, "_peak_equity", equity)
except Exception:
pass
portfolio_state = {
"positions": positions,
"gross_exposure": gross_exposure,
"equity": equity,
"peak_equity": peak_equity,
}
market_data = {"price": reference_price}
try:
risk_decision = self._run_pre_trade_check(
"risk_compliance",
self._risk_compliance.check_order,
order,
market_data,
portfolio_state,
)
except TimeoutError as exc:
order.reject("PRE_TRADE_TIMEOUT:risk_compliance")
self._record_ledger_event(
"risk_blocked",
order=order,
correlation_id=correlation_id,
metadata={
"reason": str(exc),
"timeout": True,
},
)
self._emit_risk_audit(
order, correlation_id, str(exc), {"timeout": True}
)
raise ComplianceViolation("Risk compliance timed out") from exc
if not risk_decision.allowed:
order.reject("RISK_LIMIT_BREACH")
self._record_ledger_event(
"risk_blocked",
order=order,
correlation_id=correlation_id,
metadata={
"reasons": risk_decision.reasons,
"breached_limits": risk_decision.breached_limits,
},
)
self._emit_risk_audit(
order,
correlation_id,
"; ".join(risk_decision.reasons),
risk_decision.breached_limits,
)
if self._circuit_breaker is not None:
for reason in risk_decision.reasons:
self._circuit_breaker.record_risk_breach(reason)
raise ComplianceViolation(
f"Risk limit breach: {'; '.join(risk_decision.reasons)}"
)
reference_price = (
order.price
if order.price is not None
else max(order.average_price or 0.0, 1.0)
)
try:
self._run_pre_trade_check(
"risk_validation",
self.risk.validate_order,
order.symbol,
order.side.value,
order.quantity,
reference_price,
)
except TimeoutError as exc:
order.reject("PRE_TRADE_TIMEOUT:risk_validation")
self._record_ledger_event(
"risk_blocked",
order=order,
correlation_id=correlation_id,
metadata={"reason": str(exc), "timeout": True},
)
self._emit_risk_audit(
order, correlation_id, str(exc), {"timeout": True}
)
raise ComplianceViolation("Risk validation timed out") from exc
except Exception:
self._fingerprints.pop(correlation_id, None)
raise
queued_order = QueuedOrder(correlation_id, order)
self._queue.append(queued_order)
self._pending[correlation_id] = order
self._persist_state()
self._record_ledger_event(
"order_queued",
order=order,
correlation_id=correlation_id,
)
return order
def _emit_compliance_audit(
self,
order: Order,
correlation_id: str,
report: ComplianceReport | None,
error: str | None,
) -> None:
payload = {
"event": "compliance_check",
"symbol": order.symbol,
"side": order.side.value,
"quantity": float(order.quantity),
"price": None if order.price is None else float(order.price),
"correlation_id": correlation_id,
"error": error,
}
if report is not None:
payload["report"] = report.to_dict()
if report.blocked:
status = "blocked"
elif report.is_clean():
status = "passed"
else:
status = "warning"
else:
payload["report"] = None
status = "blocked" if error else "passed"
payload["status"] = status
self._audit.emit(payload)
def _emit_risk_audit(
self,
order: Order,
correlation_id: str,
reason: str,
breached_limits: dict,
) -> None:
"""Emit structured audit log for risk rejection."""
payload = {
"event": "risk_check",
"symbol": order.symbol,
"side": order.side.value,
"quantity": float(order.quantity),
"price": None if order.price is None else float(order.price),
"order_id": order.order_id,
"correlation_id": correlation_id,
"status": "blocked",
"reason": reason,
"breached_limits": breached_limits,
"timestamp": datetime.now(timezone.utc).isoformat(),
}
self._audit.emit(payload)
def _run_pre_trade_check(
self, label: str, func: Callable[..., object], *args: object, **kwargs: object
) -> object:
timeout = self.config.pre_trade_timeout
if not timeout:
return func(*args, **kwargs)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(func, *args, **kwargs)
try:
return future.result(timeout=timeout)
except FuturesTimeoutError as exc:
future.cancel()
raise TimeoutError(
f"{label} check exceeded {timeout:.3f}s timeout"
) from exc
def _place_order_with_timeout(self, order: Order, correlation_id: str) -> Order:
timeout = self.config.request_timeout
if not timeout:
return self.connector.place_order(order, idempotency_key=correlation_id)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(
self.connector.place_order,
order,
idempotency_key=correlation_id,
)
try:
return future.result(timeout=timeout)
except FuturesTimeoutError as exc:
future.cancel()
raise TimeoutError(
f"Connector request exceeded {timeout:.3f}s timeout"
) from exc
def process_next(self) -> Order:
if not self._queue:
raise LookupError("No orders pending")
item = self._queue[0]
retryable: tuple[type[Exception], ...] = (
TransientOrderError,
TimeoutError,
ConnectionError,
)
max_retries = max(1, int(self.config.max_retries))
while True:
item.attempts += 1
try:
start = time.perf_counter()
submitted = self._place_order_with_timeout(
item.order, item.correlation_id
)
ack_latency = time.perf_counter() - start
except retryable as exc:
item.last_error = str(exc)
if item.attempts >= max_retries:
self._queue.popleft()
self._pending.pop(item.correlation_id, None)
item.order.reject(str(exc))
self._record_lifecycle_event(
item.order,
OrderEvent.REJECT,
base_correlation=item.correlation_id,
metadata={
"reason": str(exc),
"attempts": item.attempts,
"transient": True,
},
)
self._persist_state()
self._record_ledger_event(
"order_rejected",
order=item.order,
correlation_id=item.correlation_id,
metadata={
"reason": str(exc),
"attempts": item.attempts,
"transient": True,
},
)
return item.order
backoff = max(0.0, float(self.config.backoff_seconds))
self._persist_state()
self._record_ledger_event(
"order_retry_scheduled",
order=item.order,
correlation_id=item.correlation_id,
metadata={
"attempts": item.attempts,
"error": str(exc),
"backoff_seconds": backoff * item.attempts,
},
)
if backoff:
time.sleep(backoff * item.attempts)
continue
except OrderError as exc:
self._queue.popleft()
self._pending.pop(item.correlation_id, None)
item.order.reject(str(exc))
self._record_lifecycle_event(
item.order,
OrderEvent.REJECT,
base_correlation=item.correlation_id,
metadata={"reason": str(exc)},
)
self._persist_state()
self._record_ledger_event(
"order_rejected",
order=item.order,
correlation_id=item.correlation_id,
metadata={"reason": str(exc)},
)
return item.order
break
self._queue.popleft()
self._pending.pop(item.correlation_id, None)
if submitted.order_id is None:
raise RuntimeError("Connector returned order without ID")
self._orders[submitted.order_id] = submitted
self._processed[item.correlation_id] = submitted.order_id
self._correlations[submitted.order_id] = item.correlation_id
if submitted.broker_order_id:
self._broker_lookup[submitted.broker_order_id] = submitted.order_id
self._ensure_lifecycle_sequence(submitted.order_id)
base_correlation = item.correlation_id
self._record_lifecycle_event(
submitted,
OrderEvent.SUBMIT,
base_correlation=base_correlation,
metadata={"attempts": item.attempts},
)
self._record_lifecycle_event(
submitted,
OrderEvent.ACK,
base_correlation=base_correlation,
metadata={"attempts": item.attempts, "ack_latency": ack_latency},
)
fingerprint = self._fingerprints.get(item.correlation_id)
updated_fingerprint = self._fingerprint(submitted)
if fingerprint != updated_fingerprint:
self._fingerprints[item.correlation_id] = updated_fingerprint
self._update_active_order(submitted.order_id, submitted)
if self._metrics.enabled:
exchange = getattr(
self.connector, "name", self.connector.__class__.__name__.lower()
)
self._metrics.record_order_ack_latency(
exchange, submitted.symbol, max(0.0, ack_latency)
)
self._ack_timestamps[submitted.order_id] = datetime.now(timezone.utc)
self._persist_state()
self._record_ledger_event(
"order_acknowledged",
order=submitted,
correlation_id=item.correlation_id,
metadata={"attempts": item.attempts, "ack_latency": ack_latency},
)
return submitted
def process_all(self) -> None:
while self._queue:
self.process_next()
# ------------------------------------------------------------------
# Lifecycle helpers
def cancel(self, order_id: str) -> bool:
if order_id not in self._orders:
return False
cancelled = self.connector.cancel_order(order_id)
if cancelled:
order = self._orders[order_id]
order.cancel()
self._ack_timestamps.pop(order_id, None)
self._update_active_order(order_id, order)
self._record_lifecycle_event(
order,
OrderEvent.CANCEL,
base_correlation=self._correlations.get(order_id),
)
self._lifecycle_sequences.pop(order_id, None)
self._persist_state()
self._record_ledger_event(
"order_cancelled",
order=self._orders[order_id],
correlation_id=self._correlations.get(order_id),
)
return cancelled
def register_fill(
self,
order_id: str,
quantity: float,
price: float,
correlation_id: str | None = None,
) -> Order:
order = self._orders[order_id]
previous_status = order.status
previous_filled = order.filled_quantity
order.record_fill(quantity, price)
self._ensure_lifecycle_sequence(order_id)
self._update_active_order(order_id, order)
self.risk.register_fill(order.symbol, order.side.value, quantity, price)
if self._metrics.enabled:
exchange = getattr(
self.connector, "name", self.connector.__class__.__name__.lower()
)
now = datetime.now(timezone.utc)
ack_ts = self._ack_timestamps.get(order_id)
if ack_ts is not None:
latency = max(0.0, (now - ack_ts).total_seconds())
self._metrics.record_order_fill_latency(exchange, order.symbol, latency)
signal_origin = getattr(order, "created_at", None)
signal_latency = None
if isinstance(signal_origin, datetime):
if signal_origin.tzinfo is None:
signal_origin = signal_origin.replace(tzinfo=timezone.utc)
signal_latency = max(0.0, (now - signal_origin).total_seconds())
if signal_latency is not None:
metadata = getattr(order, "metadata", None)
strategy = "unspecified"
if isinstance(metadata, dict):
strategy = str(metadata.get("strategy") or strategy)
else:
strategy = str(getattr(order, "strategy", strategy))
self._metrics.record_signal_to_fill_latency(
strategy,
exchange,
order.symbol,
signal_latency,
)
self._ack_timestamps.pop(order_id, None)
base_correlation = self._correlations.get(order_id)
sequence = self._next_fill_sequence(order_id)
sequence_hint = None if correlation_id is not None else sequence
event = (
OrderEvent.FILL_FINAL
if order.status is OrderStatus.FILLED
else OrderEvent.FILL_PARTIAL
)
metadata: Dict[str, object] = {
"fill_quantity": float(quantity),
"fill_price": float(price),
"cumulative_filled": float(order.filled_quantity),
}
if previous_status is not order.status:
metadata["previous_status"] = previous_status.value
if order.status is OrderStatus.FILLED:
metadata["completed"] = True
if order.filled_quantity > previous_filled + 1e-9:
metadata["delta_filled"] = float(order.filled_quantity - previous_filled)
self._record_lifecycle_event(
order,
event,
base_correlation=base_correlation,
metadata=metadata,
correlation_override=correlation_id,
sequence_hint=sequence_hint,
)
if order.status is OrderStatus.FILLED:
self._lifecycle_sequences.pop(order_id, None)
self._persist_state()
self._record_ledger_event(
"order_fill_recorded",
order=order,
correlation_id=self._correlations.get(order_id),
metadata={"fill_quantity": quantity, "fill_price": price},
)
return order
def sync_remote_state(self, order: Order) -> Order:
"""Synchronize terminal state reported by the venue without reissuing API calls."""
if order.order_id is None:
raise ValueError("order must include an order_id to sync state")
stored = self._orders.get(order.order_id)
if stored is None:
raise LookupError(f"Unknown order_id: {order.order_id}")
previous_status = stored.status
previous_filled = stored.filled_quantity
stored.status = OrderStatus(order.status)
stored.filled_quantity = float(order.filled_quantity)
stored.average_price = (
float(order.average_price) if order.average_price is not None else None
)
stored.rejection_reason = order.rejection_reason
stored.updated_at = getattr(order, "updated_at", stored.updated_at)
if not stored.is_active:
self._ack_timestamps.pop(order.order_id, None)
self._update_active_order(order.order_id, stored)
if stored.broker_order_id:
self._broker_lookup[stored.broker_order_id] = stored.order_id
base_correlation = self._correlations.get(order.order_id)
if (
stored.status is OrderStatus.CANCELLED
and previous_status is not OrderStatus.CANCELLED
):
self._record_lifecycle_event(
stored,
OrderEvent.CANCEL,
base_correlation=base_correlation,
metadata={"source": "sync_remote_state"},
)
self._lifecycle_sequences.pop(order.order_id, None)
elif (
stored.status is OrderStatus.REJECTED
and previous_status is not OrderStatus.REJECTED
):
metadata: Dict[str, object] = {"source": "sync_remote_state"}
if stored.rejection_reason:
metadata["reason"] = stored.rejection_reason
self._record_lifecycle_event(
stored,
OrderEvent.REJECT,
base_correlation=base_correlation,
metadata=metadata,
)
self._lifecycle_sequences.pop(order.order_id, None)
else:
if stored.status in {OrderStatus.PARTIALLY_FILLED, OrderStatus.FILLED}:
if stored.filled_quantity > previous_filled + 1e-9 or (
stored.status is OrderStatus.FILLED
and previous_status is not OrderStatus.FILLED
):
self._ensure_lifecycle_sequence(order.order_id)
sequence = self._next_fill_sequence(order.order_id)
fill_event = (
OrderEvent.FILL_FINAL
if stored.status is OrderStatus.FILLED
else OrderEvent.FILL_PARTIAL
)