qx-zhenlu 0.4.2

针路:执行与路由,风控门禁、订单管理、路由决策
Documentation
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
//! 牵星订单管理系统(OMS)。原 `qx-oms` crate,V10 P2a 按"订单管理归针路"并入。
//!
//! OMS 只拥有本地订单状态和状态迁移,不调用 Venue、不写 Ledger;Venue 回报由
//! `apply_fill` 归约进来。Paper、CCXT 和其他执行器都通过同一个订单状态机。

use qx_core::{Fill, Order, OrderStatus, Quantity, QxError, QxResult};
use std::collections::BTreeMap;

/// 可重放的本地订单管理器。
///
/// 使用 `BTreeMap` 而非 `HashMap`,保证快照和回放的遍历顺序确定;OMS 不接受
/// 已完成订单的重复注册,也不允许成交绕过状态机直接改变订单状态。
#[derive(Clone)]
pub struct Oms {
    orders: BTreeMap<u64, Order>,
}

impl Default for Oms {
    fn default() -> Self {
        Self::new()
    }
}

impl Oms {
    pub fn new() -> Self {
        Self {
            orders: BTreeMap::new(),
        }
    }

    pub fn submit(&mut self, order: Order) -> QxResult<()> {
        order.validate().map_err(QxError::BusinessViolation)?;
        if !matches!(
            order.status,
            OrderStatus::PendingSubmit | OrderStatus::Submitted
        ) {
            return Err(QxError::BusinessViolation(
                "OMS 只接受待提交或已提交订单".into(),
            ));
        }
        if self.orders.contains_key(&order.client_id) {
            return Err(QxError::Invariant(format!(
                "重复的 client_order_id: {}",
                order.client_id
            )));
        }
        self.orders.insert(order.client_id, order);
        Ok(())
    }

    /// Venue 确认——`Submitted` 只是意图,`Accepted` 才是事实。
    pub fn accept(&mut self, client_order_id: u64) -> QxResult<()> {
        let order = self
            .orders
            .get_mut(&client_order_id)
            .ok_or_else(|| QxError::Permanent("订单不存在".into()))?;
        order
            .status
            .transition(OrderStatus::Accepted)
            .map_err(QxError::Invariant)?;
        Ok(())
    }

    /// Confirm a local simulated cancel through the same order state machine used by venues.
    pub fn cancel(&mut self, client_order_id: u64) -> QxResult<()> {
        let order = self
            .orders
            .get_mut(&client_order_id)
            .ok_or_else(|| QxError::Permanent("订单不存在".into()))?;
        if order.status.is_terminal() {
            return Err(QxError::BusinessViolation("终态订单不能撤销".into()));
        }
        if order.status != OrderStatus::CancelPending {
            order
                .status
                .transition(OrderStatus::CancelPending)
                .map_err(QxError::Invariant)?;
        }
        order
            .status
            .transition(OrderStatus::Cancelled)
            .map_err(QxError::Invariant)?;
        Ok(())
    }

    /// 只接受带账户身份的成交,成交数量必须落在订单剩余数量内。
    pub fn apply_fill(&mut self, fill: &Fill) -> QxResult<()> {
        let next = self.reduce_fill(fill)?;
        self.commit_fill(next);
        Ok(())
    }

    /// 在订单副本上校验成交并推进状态机,不改动任何持久状态。
    pub(crate) fn reduce_fill(&self, fill: &Fill) -> QxResult<Order> {
        let order = self
            .orders
            .get(&fill.order_id)
            .ok_or_else(|| QxError::Invariant("成交对应的订单不存在".into()))?
            .clone();
        if !fill.account_id.is_empty() && fill.account_id != order.account_id {
            return Err(QxError::Invariant("成交账户与 OMS 订单账户不一致".into()));
        }
        fill.validate_exact_amounts().map_err(QxError::Invariant)?;
        let mut next = order;
        if next.exact_amounts.is_some() || fill.exact_amounts.is_some() {
            let exact_fill = fill
                .exact_amounts
                .ok_or_else(|| QxError::Invariant("精确订单收到缺少精确数量/价格的成交".into()))?;
            let exact_order = next
                .exact_amounts
                .as_mut()
                .ok_or_else(|| QxError::Invariant("旧精度订单收到精确成交".into()))?;
            let quote_budget = exact_order.quote_budget;
            if exact_fill.quote_budget_complete && quote_budget.is_none() {
                return Err(QxError::Invariant(
                    "非 quote_budget 订单收到预算完成标记".into(),
                ));
            }
            if let Some(budget) = quote_budget {
                let cost = exact_fill
                    .quote_cost
                    .ok_or_else(|| QxError::Invariant("计价币预算订单成交缺少精确花费".into()))?;
                let remaining_budget = budget
                    .checked_sub(exact_order.quote_filled)
                    .ok_or_else(|| QxError::Invariant("计价币预算剩余量计算溢出".into()))?;
                if cost
                    .checked_cmp(remaining_budget)
                    .is_none_or(|ordering| ordering.is_gt())
                {
                    return Err(QxError::Invariant("成交花费超过计价币预算".into()));
                }
                exact_order.quote_filled = exact_order
                    .quote_filled
                    .checked_add(cost)
                    .ok_or_else(|| QxError::Invariant("计价币累计成交花费溢出".into()))?;
            } else {
                let remaining = exact_order
                    .quantity
                    .checked_sub(exact_order.filled)
                    .ok_or_else(|| QxError::Invariant("精确订单剩余数量计算溢出".into()))?;
                if exact_fill
                    .quantity
                    .checked_cmp(remaining)
                    .is_none_or(|ordering| ordering.is_gt())
                {
                    return Err(QxError::Invariant("成交数量超过订单剩余数量".into()));
                }
            }
            exact_order.filled = exact_order
                .filled
                .checked_add(exact_fill.quantity)
                .ok_or_else(|| QxError::Invariant("精确订单累计成交数量溢出".into()))?;
            if matches!(next.status, OrderStatus::Submitted | OrderStatus::Accepted) {
                next.status
                    .transition(OrderStatus::Working)
                    .map_err(QxError::Invariant)?;
            }
            let is_filled = quote_budget.map_or_else(
                || {
                    exact_order
                        .filled
                        .checked_cmp(exact_order.quantity)
                        .is_some_and(|ordering| ordering.is_ge())
                },
                |budget| {
                    exact_fill.quote_budget_complete
                        || exact_order
                            .quote_filled
                            .checked_cmp(budget)
                            .is_some_and(|ordering| ordering.is_ge())
                },
            );
            let status = if is_filled {
                OrderStatus::Filled
            } else if next.status == OrderStatus::CancelPending {
                // A fill can race a venue cancel acknowledgement. Keep the cancel
                // operation in flight until the venue confirms or rejects it.
                OrderStatus::CancelPending
            } else {
                OrderStatus::PartiallyFilled
            };
            next.status.transition(status).map_err(QxError::Invariant)?;
            return Ok(next);
        }
        if fill.qty.raw() <= 0 || fill.qty.raw() > next.remaining().raw() {
            return Err(QxError::Invariant("成交数量超过订单剩余数量".into()));
        }
        if matches!(next.status, OrderStatus::Submitted | OrderStatus::Accepted) {
            next.status
                .transition(OrderStatus::Working)
                .map_err(QxError::Invariant)?;
        }
        next.filled = Quantity::from_raw(next.filled.raw() + fill.qty.raw());
        let status = if next.filled.raw() >= next.qty.raw() {
            OrderStatus::Filled
        } else if next.status == OrderStatus::CancelPending {
            // Do not lose the durable cancel-in-flight state on a partial fill.
            OrderStatus::CancelPending
        } else {
            OrderStatus::PartiallyFilled
        };
        next.status.transition(status).map_err(QxError::Invariant)?;
        Ok(next)
    }

    /// 写回 `reduce_fill` 的产物。除内核成交归约接缝外,调用方应走 `apply_fill`。
    pub(crate) fn commit_fill(&mut self, order: Order) {
        self.orders.insert(order.client_id, order);
    }

    pub fn get(&self, client_order_id: u64) -> Option<&Order> {
        self.orders.get(&client_order_id)
    }

    /// Runtime EventLog 归约器需要在保留额外事实检查的同时更新 OMS;订单存储
    /// 仍然只存在于 OMS 内部,调用方不能替换或绕过 client id 索引。
    pub fn get_mut(&mut self, client_order_id: u64) -> Option<&mut Order> {
        self.orders.get_mut(&client_order_id)
    }

    pub fn insert_replayed(&mut self, order: Order) -> QxResult<()> {
        self.submit(order)
    }

    /// Replace the live OMS terms after an authoritative venue amendment fact.
    /// The client order identity, routing, policy, lifecycle state, and cumulative fills
    /// are immutable; total quantity, same-kind limit price, and conditional trigger terms
    /// may change when the venue supports that amendment shape.
    pub fn validate_amendment(&self, amended: &Order) -> QxResult<()> {
        let current = self
            .orders
            .get(&amended.client_id)
            .ok_or_else(|| QxError::ReconcileRequired("改单事实对应未知 OMS 订单".into()))?;
        validate_amended_order(current, amended)
    }

    pub fn amend_replayed(&mut self, amended: Order) -> QxResult<()> {
        self.validate_amendment(&amended)?;
        self.orders.insert(amended.client_id, amended);
        Ok(())
    }

    pub fn all_orders(&self) -> Vec<Order> {
        self.orders.values().cloned().collect()
    }

    /// Borrow all orders without cloning the OMS snapshot.
    pub fn all_orders_ref(&self) -> impl Iterator<Item = &Order> {
        self.orders.values()
    }

    pub fn active_order_ids(&self) -> Vec<u64> {
        self.orders
            .iter()
            .filter(|(_, order)| !order.status.is_terminal())
            .map(|(client_order_id, _)| *client_order_id)
            .collect()
    }

    pub fn len(&self) -> usize {
        self.orders.len()
    }

    pub fn is_empty(&self) -> bool {
        self.orders.is_empty()
    }
}

/// Validate a proposed amendment against the current authoritative OMS snapshot.
/// This pure form is also used by adapters during their no-side-effect preflight.
pub fn validate_amended_order(current: &Order, amended: &Order) -> QxResult<()> {
    amended.validate().map_err(QxError::BusinessViolation)?;
    if !matches!(
        current.status,
        OrderStatus::Accepted | OrderStatus::Working | OrderStatus::PartiallyFilled
    ) || amended.status != current.status
        || amended.instrument != current.instrument
        || amended.side != current.side
        || amended.account_id != current.account_id
        || amended.trace != current.trace
        || amended.policy != current.policy
    {
        return Err(QxError::ReconcileRequired(format!(
            "订单 {} 的改单事实改变了身份/生命周期,或原订单当前不可改单",
            amended.client_id
        )));
    }

    match (current.exact_amounts, amended.exact_amounts) {
        (Some(old), Some(new)) => {
            let conditional_changed = match (old.conditional, new.conditional) {
                (Some(current), Some(amended)) => {
                    current.kind == amended.kind && current != amended
                }
                (None, None) => false,
                _ => false,
            };
            if old.filled != new.filled
                || new
                    .quantity
                    .checked_cmp(new.filled)
                    .is_none_or(|ordering| !ordering.is_gt())
                || old.quote_budget != new.quote_budget
                || old.quote_filled != new.quote_filled
                || old.market_reference_price != new.market_reference_price
                || (old.conditional.is_some() != new.conditional.is_some())
                || (old.conditional != new.conditional && !conditional_changed)
                || old.limit_price.is_some() != new.limit_price.is_some()
                || (old.quantity == new.quantity
                    && old.limit_price == new.limit_price
                    && !conditional_changed)
            {
                return Err(QxError::ReconcileRequired(format!(
                    "精确订单 {} 的改单事实改动了成交、预算、风控参考或订单种类",
                    amended.client_id
                )));
            }
        }
        (None, None) => {
            if current.filled != amended.filled
                || amended.qty.raw() <= amended.filled.raw()
                || current.limit.is_some() != amended.limit.is_some()
                || (current.qty == amended.qty && current.limit == amended.limit)
            {
                return Err(QxError::ReconcileRequired(format!(
                    "订单 {} 的改单事实改动了成交状态、订单种类或没有改变订单条款",
                    amended.client_id
                )));
            }
        }
        _ => {
            return Err(QxError::ReconcileRequired(format!(
                "订单 {} 的改单事实在旧精度与精确金额之间切换",
                amended.client_id
            )))
        }
    }
    Ok(())
}

/// 内核的成交归约接缝只通过这三个方法读写订单状态,避免 `qx-core` 反向依赖 OMS。
impl qx_core::OrderFillBook for Oms {
    fn order_state(&self, client_order_id: u64) -> Option<Order> {
        self.get(client_order_id).cloned()
    }

    fn reduce_fill(&self, fill: &Fill) -> QxResult<Order> {
        Oms::reduce_fill(self, fill)
    }

    fn commit_fill(&mut self, order: Order) {
        Oms::commit_fill(self, order);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use qx_core::{
        ConditionalOrderIntent, ConditionalOrderKind, ExactDecimal, ExactFillAmounts,
        ExactOrderAmounts, InstrumentId, OrderTrace, Side, TriggerPriceType,
    };

    fn order() -> Order {
        Order {
            client_id: 1,
            instrument: InstrumentId::parse("BTC/USDT.OKX").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(2),
            limit: None,
            status: OrderStatus::Submitted,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: Some(OrderTrace {
                run_id: None,
                strategy_id: Some("oms-test".into()),
                signal_id: Some(1),
                intent_id: Some(1),
                rule_version: Some("v1".into()),
            }),
            policy: None,

            exact_amounts: None,
        }
    }

    #[test]
    fn amendment_replay_changes_only_order_terms_and_preserves_cumulative_fills() {
        let mut oms = Oms::new();
        let mut current = order();
        current.status = OrderStatus::Working;
        current.filled = Quantity::from_i64(1);
        current.limit = Some(qx_core::Price::from_i64(100));
        oms.submit(Order {
            status: OrderStatus::Submitted,
            filled: Quantity::ZERO,
            ..current.clone()
        })
        .unwrap();
        oms.get_mut(1).unwrap().status = OrderStatus::Working;
        oms.get_mut(1).unwrap().filled = Quantity::from_i64(1);

        let mut amended = current.clone();
        amended.qty = Quantity::from_i64(3);
        amended.limit = Some(qx_core::Price::from_i64(101));
        oms.amend_replayed(amended.clone()).unwrap();
        assert_eq!(oms.get(1), Some(&amended));

        let before_invalid = oms.get(1).unwrap().clone();
        let mut changed_identity = amended.clone();
        changed_identity.side = Side::Sell;
        assert!(oms.amend_replayed(changed_identity).is_err());
        assert_eq!(oms.get(1), Some(&before_invalid));

        let mut no_op = amended.clone();
        assert!(oms.amend_replayed(no_op.clone()).is_err());
        no_op.qty = Quantity::from_i64(1);
        assert!(oms.amend_replayed(no_op).is_err());
        assert_eq!(oms.get(1), Some(&before_invalid));
    }

    #[test]
    fn exact_conditional_amendment_changes_trigger_terms_without_changing_order_kind() {
        let mut current = order();
        current.status = OrderStatus::Working;
        current.qty = Quantity::ZERO;
        current.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::parse("0.01").unwrap(),
            limit_price: None,
            filled: ExactDecimal::ZERO,
            quote_budget: None,
            quote_filled: ExactDecimal::ZERO,
            market_reference_price: None,
            conditional: Some(ConditionalOrderIntent {
                kind: ConditionalOrderKind::StopLoss,
                trigger_price: Some(ExactDecimal::parse("60000").unwrap()),
                trigger_price_type: Some(TriggerPriceType::Mark),
                trailing_percent: None,
                trailing_amount: None,
                trailing_trigger_price: None,
            }),
        });

        let mut amended = current.clone();
        amended
            .exact_amounts
            .as_mut()
            .unwrap()
            .conditional
            .as_mut()
            .unwrap()
            .trigger_price = Some(ExactDecimal::parse("59000").unwrap());
        validate_amended_order(&current, &amended).unwrap();

        let mut changed_kind = amended.clone();
        changed_kind
            .exact_amounts
            .as_mut()
            .unwrap()
            .conditional
            .as_mut()
            .unwrap()
            .kind = ConditionalOrderKind::TakeProfit;
        assert!(validate_amended_order(&current, &changed_kind).is_err());

        let mut changed_contract = amended;
        changed_contract
            .exact_amounts
            .as_mut()
            .unwrap()
            .quote_budget = Some(ExactDecimal::parse("100").unwrap());
        assert!(validate_amended_order(&current, &changed_contract).is_err());
    }

    #[test]
    fn oms_is_idempotence_boundary_for_client_order_and_fills() {
        let mut oms = Oms::new();
        oms.submit(order()).unwrap();
        assert!(oms.submit(order()).is_err());
        oms.accept(1).unwrap();
        let mut fill = Fill {
            order_id: 1,
            qty: Quantity::from_i64(1),
            price: qx_core::Price::from_i64(100),
            account_id: "main".into(),
            ..Fill::default()
        };
        oms.apply_fill(&fill).unwrap();
        assert_eq!(oms.get(1).unwrap().status, OrderStatus::PartiallyFilled);
        fill.qty = Quantity::from_i64(1);
        oms.apply_fill(&fill).unwrap();
        assert_eq!(oms.get(1).unwrap().status, OrderStatus::Filled);
        assert!(oms
            .all_orders()
            .iter()
            .all(|order| order.status.is_terminal()));
    }

    #[test]
    fn partial_fill_during_cancel_pending_preserves_cancel_until_confirmation() {
        let mut oms = Oms::new();
        let mut pending = order();
        pending.status = OrderStatus::Submitted;
        oms.submit(pending).unwrap();
        oms.accept(1).unwrap();
        oms.get_mut(1).unwrap().status = OrderStatus::CancelPending;

        oms.apply_fill(&Fill {
            order_id: 1,
            qty: Quantity::from_i64(1),
            price: qx_core::Price::from_i64(100),
            account_id: "main".into(),
            ..Fill::default()
        })
        .unwrap();

        let after_fill = oms.get(1).unwrap();
        assert_eq!(after_fill.status, OrderStatus::CancelPending);
        assert_eq!(after_fill.filled, Quantity::from_i64(1));

        oms.cancel(1).unwrap();
        let after_cancel = oms.get(1).unwrap();
        assert_eq!(after_cancel.status, OrderStatus::Cancelled);
        assert_eq!(after_cancel.filled, Quantity::from_i64(1));
    }

    #[test]
    fn exact_partial_fill_during_cancel_pending_preserves_cancel_until_confirmation() {
        let mut oms = Oms::new();
        let mut pending = order();
        pending.qty = Quantity::ZERO;
        pending.status = OrderStatus::Submitted;
        pending.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::parse("0.0000000002").unwrap(),
            limit_price: Some(ExactDecimal::parse("0.00000001").unwrap()),
            filled: ExactDecimal::ZERO,
            market_reference_price: None,
            quote_budget: None,
            quote_filled: Default::default(),
            conditional: None,
        });
        oms.submit(pending).unwrap();
        oms.accept(1).unwrap();
        oms.get_mut(1).unwrap().status = OrderStatus::CancelPending;

        oms.apply_fill(&Fill {
            order_id: 1,
            account_id: "main".into(),
            exact_amounts: Some(ExactFillAmounts {
                quantity: ExactDecimal::parse("0.0000000001").unwrap(),
                price: ExactDecimal::parse("0.00000001").unwrap(),
                quote_cost: None,
                quote_budget_complete: false,
            }),
            ..Fill::default()
        })
        .unwrap();

        let after_fill = oms.get(1).unwrap();
        assert_eq!(after_fill.status, OrderStatus::CancelPending);
        assert_eq!(
            after_fill.filled_decimal(),
            ExactDecimal::parse("0.0000000001").unwrap()
        );

        oms.cancel(1).unwrap();
        let after_cancel = oms.get(1).unwrap();
        assert_eq!(after_cancel.status, OrderStatus::Cancelled);
        assert_eq!(
            after_cancel.filled_decimal(),
            ExactDecimal::parse("0.0000000001").unwrap()
        );
    }

    /// 状态机拒下的成交不能把订单留在"已加仓但未迁移状态"的半程上。
    #[test]
    fn rejected_fill_leaves_no_partial_order_state() {
        let mut oms = Oms::new();
        oms.submit(Order {
            status: OrderStatus::PendingSubmit,
            ..order()
        })
        .unwrap();
        let fill = Fill {
            order_id: 1,
            qty: Quantity::from_i64(1),
            price: qx_core::Price::from_i64(100),
            account_id: "main".into(),
            ..Fill::default()
        };
        assert!(oms.apply_fill(&fill).is_err());
        let stored = oms.get(1).unwrap();
        assert_eq!(stored.status, OrderStatus::PendingSubmit);
        assert_eq!(stored.filled, Quantity::ZERO);
    }

    #[test]
    fn oms_accumulates_exact_fill_quantity_without_legacy_scale_loss() {
        let mut oms = Oms::new();
        let mut precise_order = order();
        precise_order.qty = Quantity::ZERO;
        precise_order.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::parse("0.0000000002").unwrap(),
            limit_price: None,
            filled: ExactDecimal::ZERO,
            market_reference_price: None,
            quote_budget: None,
            quote_filled: Default::default(),
            conditional: None,
        });
        oms.submit(precise_order).unwrap();
        oms.accept(1).unwrap();

        for expected_status in [OrderStatus::PartiallyFilled, OrderStatus::Filled] {
            let fill = Fill {
                order_id: 1,
                account_id: "main".into(),
                exact_amounts: Some(ExactFillAmounts {
                    quantity: ExactDecimal::parse("0.0000000001").unwrap(),
                    price: ExactDecimal::parse("0.00000001").unwrap(),
                    quote_cost: None,
                    quote_budget_complete: false,
                }),
                ..Fill::default()
            };
            oms.apply_fill(&fill).unwrap();
            let stored = oms.get(1).unwrap();
            assert_eq!(stored.status, expected_status);
        }
        assert_eq!(
            oms.get(1).unwrap().filled_decimal(),
            ExactDecimal::parse("0.0000000002").unwrap()
        );
    }

    #[test]
    fn oms_tracks_quote_budget_spend_while_accumulating_base_fills() {
        let mut oms = Oms::new();
        let mut budget_order = order();
        budget_order.qty = Quantity::ZERO;
        budget_order.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::ZERO,
            limit_price: None,
            filled: ExactDecimal::ZERO,
            quote_budget: Some(ExactDecimal::parse("10").unwrap()),
            quote_filled: ExactDecimal::ZERO,
            market_reference_price: None,
            conditional: None,
        });
        oms.submit(budget_order).unwrap();
        oms.accept(1).unwrap();

        for (quantity, price, cost, expected_status) in [
            ("0.5", "10", "5", OrderStatus::PartiallyFilled),
            ("0.25", "20", "5", OrderStatus::Filled),
        ] {
            oms.apply_fill(&Fill {
                order_id: 1,
                account_id: "main".into(),
                exact_amounts: Some(ExactFillAmounts {
                    quantity: ExactDecimal::parse(quantity).unwrap(),
                    price: ExactDecimal::parse(price).unwrap(),
                    quote_cost: Some(ExactDecimal::parse(cost).unwrap()),
                    quote_budget_complete: false,
                }),
                ..Fill::default()
            })
            .unwrap();
            assert_eq!(oms.get(1).unwrap().status, expected_status);
        }
        let amounts = oms.get(1).unwrap().exact_amounts.unwrap();
        assert_eq!(amounts.filled, ExactDecimal::parse("0.75").unwrap());
        assert_eq!(amounts.quote_filled, ExactDecimal::parse("10").unwrap());
    }

    #[test]
    fn oms_finishes_quote_budget_order_when_lot_step_leaves_unspent_dust() {
        let mut oms = Oms::new();
        let mut budget_order = order();
        budget_order.qty = Quantity::ZERO;
        budget_order.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::ZERO,
            limit_price: None,
            filled: ExactDecimal::ZERO,
            quote_budget: Some(ExactDecimal::parse("10").unwrap()),
            quote_filled: ExactDecimal::ZERO,
            market_reference_price: None,
            conditional: None,
        });
        oms.submit(budget_order).unwrap();
        oms.accept(1).unwrap();

        oms.apply_fill(&Fill {
            order_id: 1,
            account_id: "main".into(),
            exact_amounts: Some(ExactFillAmounts {
                quantity: ExactDecimal::parse("3.333").unwrap(),
                price: ExactDecimal::parse("3").unwrap(),
                quote_cost: Some(ExactDecimal::parse("9.999").unwrap()),
                quote_budget_complete: true,
            }),
            ..Fill::default()
        })
        .unwrap();

        assert_eq!(oms.get(1).unwrap().status, OrderStatus::Filled);
        assert_eq!(
            oms.get(1).unwrap().quote_filled_decimal(),
            ExactDecimal::parse("9.999").unwrap()
        );
    }

    #[test]
    fn oms_rejects_quote_budget_overrun_without_advancing_order() {
        let mut oms = Oms::new();
        let mut budget_order = order();
        budget_order.qty = Quantity::ZERO;
        budget_order.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::ZERO,
            limit_price: None,
            filled: ExactDecimal::ZERO,
            quote_budget: Some(ExactDecimal::parse("10").unwrap()),
            quote_filled: ExactDecimal::ZERO,
            market_reference_price: None,
            conditional: None,
        });
        oms.submit(budget_order).unwrap();
        oms.accept(1).unwrap();
        let result = oms.apply_fill(&Fill {
            order_id: 1,
            account_id: "main".into(),
            exact_amounts: Some(ExactFillAmounts {
                quantity: ExactDecimal::parse("1").unwrap(),
                price: ExactDecimal::parse("10.01").unwrap(),
                quote_cost: Some(ExactDecimal::parse("10.01").unwrap()),
                quote_budget_complete: false,
            }),
            ..Fill::default()
        });
        assert!(result.is_err());
        assert_eq!(oms.get(1).unwrap().filled_decimal(), ExactDecimal::ZERO);
        assert_eq!(
            oms.get(1).unwrap().quote_filled_decimal(),
            ExactDecimal::ZERO
        );
    }

    #[test]
    fn oms_accumulates_exact_fills_when_raw_alignment_would_overflow_before_normalization() {
        let mut oms = Oms::new();
        let mut precise_order = order();
        precise_order.qty = Quantity::ZERO;
        precise_order.exact_amounts = Some(ExactOrderAmounts {
            quantity: ExactDecimal::parse("340282366920938463463.37460743176821145").unwrap(),
            limit_price: None,
            filled: ExactDecimal::ZERO,
            market_reference_price: None,
            quote_budget: None,
            quote_filled: Default::default(),
            conditional: None,
        });
        oms.submit(precise_order).unwrap();
        oms.accept(1).unwrap();

        for quantity in [
            "170141183460469231731.687303715884105727",
            "170141183460469231731.687303715884105723",
        ] {
            let fill = Fill {
                order_id: 1,
                account_id: "main".into(),
                exact_amounts: Some(ExactFillAmounts {
                    quantity: ExactDecimal::parse(quantity).unwrap(),
                    price: ExactDecimal::parse("0.00000001").unwrap(),
                    quote_cost: None,
                    quote_budget_complete: false,
                }),
                ..Fill::default()
            };
            oms.apply_fill(&fill).unwrap();
        }

        let stored = oms.get(1).unwrap();
        assert_eq!(stored.status, OrderStatus::Filled);
        assert_eq!(
            stored.filled_decimal().to_string(),
            "340282366920938463463.37460743176821145"
        );
    }
}