rustrade-execution 0.20.0

Stream private account data from financial venues, and execute (live or mock) orders.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
//! Liquidation of a spot margin account, and the margin-call notices before it.

use super::*;
use crate::{
    margin::{SpotMarginConfigError, SpotMarginLiquidation},
    notice::{AccountNotice, NoticeKind},
};

/// Liquidation at a margin level of `level` or below, charging `fee_rate` of the debt repaid.
fn liquidation(level: &str, fee_rate: &str) -> SpotMarginLiquidation {
    SpotMarginLiquidation::new(d(level), d(fee_rate)).unwrap()
}

/// A margin account valued in USDT, lending up to 3x, liquidated as `liquidation` says.
fn liquidating_config(
    balances: Vec<AssetBalance<AssetNameExchange>>,
    liquidation: SpotMarginLiquidation,
) -> MockExecutionConfig {
    MockExecutionConfig {
        spot_margin: Some(
            SpotMarginConfig::new(quote(), d("3"), MarginEffect::Borrow)
                .unwrap()
                .with_liquidation(liquidation)
                .unwrap(),
        ),
        ..config_from_balances(balances, FeeModelConfig::default())
    }
}

/// A fed venue trading BTC at `price` from `time(1)`, holding `balances`.
fn liquidating_venue(
    balances: Vec<AssetBalance<AssetNameExchange>>,
    liquidation: SpotMarginLiquidation,
    price: &str,
) -> SimulatedVenue {
    let config = liquidating_config(balances, liquidation);
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, price, 1).is_empty());
    venue
}

/// A venue holding 10,000 USDT that has sold 1 BTC it borrowed at 10,000: 20,000 USDT held
/// against 1 BTC owed, a margin level of 2 at that price.
fn short_one_btc(liquidation: SpotMarginLiquidation) -> SimulatedVenue {
    let mut venue = liquidating_venue(
        vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
        liquidation,
        "10000",
    );
    let outcome = venue.open_order(sell_request("1", None));
    assert_eq!(filled_trade(&outcome).quantity, d("1"));
    venue
}

/// A venue holding 10,000 USDT that has bought 2 BTC at 10,000, borrowing 10,000 USDT: 2 BTC held
/// against 10,000 USDT owed, a margin level of 2 at that price.
fn long_two_btc(liquidation: SpotMarginLiquidation) -> SimulatedVenue {
    let mut venue = liquidating_venue(
        vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
        liquidation,
        "10000",
    );
    let outcome = venue.open_order(buy_request("2", None));
    assert_eq!(filled_trade(&outcome).quantity, d("2"));
    venue
}

/// A trade at `price`, and nothing else, observed at `time(seconds)`.
fn tick(venue: &mut SimulatedVenue, price: &str, seconds: i64) -> Vec<UnindexedAccountEvent> {
    venue.apply_market(
        &instrument_name(),
        MarketObservation::new(traded(price)),
        time(seconds),
    )
}

/// What each event is, in a word and the asset or kind it names, so a test can pin the order.
fn shape(events: &[UnindexedAccountEvent]) -> Vec<String> {
    events
        .iter()
        .map(|event| match &event.kind {
            AccountEventKind::BalanceSnapshot(balance) => format!("balance {}", balance.0.asset),
            AccountEventKind::MarginDebt(update) => format!("debt {}", update.asset),
            AccountEventKind::Trade(trade) => format!("trade {}", trade.side),
            AccountEventKind::OrderSnapshot(order) => format!("order {}", order.0.key.cid),
            AccountEventKind::Notice(notice) => format!("notice {}", notice.kind),
            AccountEventKind::CashFlow(_) => "cash flow".to_owned(),
            other => format!("{other:?}"),
        })
        .collect()
}

fn notice_of(event: &UnindexedAccountEvent) -> &AccountNotice<InstrumentNameExchange> {
    match &event.kind {
        AccountEventKind::Notice(notice) => notice,
        other => panic!("expected a notice, got: {other:?}"),
    }
}

fn notices(events: &[UnindexedAccountEvent]) -> Vec<NoticeKind> {
    events
        .iter()
        .filter_map(|event| match &event.kind {
            AccountEventKind::Notice(notice) => Some(notice.kind),
            _ => None,
        })
        .collect()
}

fn trades(
    events: &[UnindexedAccountEvent],
) -> Vec<&Trade<AssetNameExchange, InstrumentNameExchange>> {
    events
        .iter()
        .filter_map(|event| match &event.kind {
            AccountEventKind::Trade(trade) => Some(trade),
            _ => None,
        })
        .collect()
}

/// The snapshot `events` report of the order `cid`.
fn order_named<'a>(
    events: &'a [UnindexedAccountEvent],
    cid: &str,
) -> &'a Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState> {
    events
        .iter()
        .find_map(|event| match &event.kind {
            AccountEventKind::OrderSnapshot(order)
                if order.0.key.cid == ClientOrderId::new(cid) =>
            {
                Some(&order.0)
            }
            _ => None,
        })
        .unwrap_or_else(|| panic!("no snapshot of order {cid} in {:?}", shape(events)))
}

fn assert_cancelled(order: &Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>) {
    assert!(
        matches!(
            order.state,
            OrderState::Inactive(InactiveOrderState::Cancelled(_))
        ),
        "{}: {:?}",
        order.key.cid,
        order.state
    );
}

/// Asserts `outcome` was refused for want of `asset`, with nothing borrowable.
fn assert_refused_a_loan(outcome: &OpenOutcome, asset: AssetNameExchange) {
    match &outcome.response.state {
        OrderState::Inactive(InactiveOrderState::OpenFailed(OrderError::Rejected(
            ApiError::BalanceInsufficient(Some(refused), reason),
        ))) => {
            assert_eq!(*refused, asset);
            assert!(reason.contains("Borrowable: 0"), "{reason}");
        }
        other => panic!("expected BalanceInsufficient, got: {other:?}"),
    }
}

fn liquidation_fee(
    event: &UnindexedAccountEvent,
) -> &CashFlow<AssetNameExchange, InstrumentNameExchange> {
    match &event.kind {
        AccountEventKind::CashFlow(flow) => flow,
        other => panic!("expected a cash flow, got: {other:?}"),
    }
}

#[test]
fn an_account_above_its_liquidation_level_is_left_alone() {
    let mut venue = short_one_btc(liquidation("1.1", "0.02"));

    // 20,000 held against 18,000 owed: a margin level of 1.11.
    assert!(tick(&mut venue, "18000", 2).is_empty());
    assert_eq!(balance_now(&venue, base()).margin.unwrap().borrowed, d("1"));
}

#[test]
fn a_short_at_its_liquidation_level_is_bought_back_and_charged_the_fee_on_what_it_owed() {
    let mut venue = short_one_btc(liquidation("1.1", "0.02"));

    // 20,000 held against 18,200 owed: a margin level of 1.0989.
    let events = tick(&mut venue, "18200", 2);

    assert_eq!(
        shape(&events),
        [
            "notice liquidation",
            "balance USDT",
            "balance BTC",
            "debt BTC",
            "trade buy",
            "balance USDT",
            "cash flow",
        ]
    );
    let notice = notice_of(&events[0]);
    assert_eq!(notice.margin_level, Some(d("20000") / d("18200")));
    assert_eq!(notice.instrument, None);
    assert_eq!(notice.status, "liquidation");
    assert_eq!(notice.time_exchange, time(2));

    let trade = trade_of(&events[4]);
    assert_eq!(
        (trade.side, trade.price, trade.quantity),
        (Side::Buy, d("18200"), d("1"))
    );
    assert_eq!(trade.origin, TradeOrigin::Liquidation);
    assert_eq!(trade.client_order_id, None);
    assert_eq!(trade.strategy, StrategyId::unknown());
    assert_eq!(trade.time_exchange, time(2));

    let fee = liquidation_fee(&events[6]);
    assert_eq!(
        fee.kind,
        CashFlowKind::LiquidationFee {
            rate: Some(d("0.02")),
            repaid: Some(d("18200")),
        }
    );
    assert_eq!((fee.asset.clone(), fee.amount), (quote(), d("-364")));
    assert_eq!(fee.instrument, None);

    assert_eq!(
        balance_now(&venue, base()),
        Balance::new_margin(Decimal::ZERO, Decimal::ZERO, Decimal::ZERO, Decimal::ZERO)
    );
    // 20,000 less the 18,200 buy and the 364 fee.
    assert_eq!(
        balance_now(&venue, quote()),
        Balance::new_margin(d("1436"), d("1436"), Decimal::ZERO, Decimal::ZERO)
    );
    assert!(
        tick(&mut venue, "30000", 3).is_empty(),
        "an account that owes nothing is never liquidated again"
    );
}

#[test]
fn a_long_borrowing_the_valuation_asset_sells_only_what_repays_the_loan_and_the_fee() {
    let mut venue = long_two_btc(liquidation("1.1", "0.02"));

    // 2 BTC at 5,500 against 10,000 USDT owed: a margin level of 1.1.
    let events = tick(&mut venue, "5500", 2);

    let sold = trades(&events);
    assert_eq!(
        sold.len(),
        2,
        "one sale repays the loan, another raises the fee"
    );
    assert!(
        sold.iter()
            .all(|trade| trade.side == Side::Sell && trade.origin == TradeOrigin::Liquidation)
    );
    assert_eq!(sold[0].quantity, d("10000") / d("5500"));
    assert_eq!(sold[1].quantity, d("200") / d("5500"));

    let usdt = balance_now(&venue, quote());
    assert_eq!(usdt.margin.unwrap(), MarginDetails::default());
    assert!(
        usdt.free >= Decimal::ZERO && usdt.free < d("0.000000000000000000001"),
        "the sales raise the loan and the fee and no more: {usdt:?}"
    );
    let btc = balance_now(&venue, base()).total;
    assert!(
        (btc - (d("2") - d("10200") / d("5500"))).abs() < d("0.000000000000000000001"),
        "{btc}"
    );
}

#[test]
fn every_open_order_is_cancelled_before_the_forced_trade() {
    let mut venue = short_one_btc(liquidation("1.1", "0"));
    let rested = venue.open_order(gtc_limit("resting", Side::Buy, "0.1", "9000"));
    rested_open(&rested);

    let events = tick(&mut venue, "18200", 2);

    assert_eq!(
        shape(&events),
        [
            "notice liquidation",
            "balance USDT",
            "order resting",
            "balance USDT",
            "balance BTC",
            "debt BTC",
            "trade buy",
        ],
        "the released hold, then the cancel, then the buy; a zero fee charges nothing"
    );
    assert!(venue.account.orders().is_empty());
}

#[test]
fn a_margin_call_is_sent_once_per_crossing_and_ends_without_a_notice_at_a_liquidation() {
    let mut venue = short_one_btc(
        liquidation("1.1", "0.02")
            .with_margin_call(d("1.3"))
            .unwrap(),
    );

    // 20,000 against 15,500: 1.29, under the margin-call level.
    let called = tick(&mut venue, "15500", 2);
    assert_eq!(notices(&called), [NoticeKind::MarginCall]);
    assert_eq!(
        notice_of(&called[0]).margin_level,
        Some(d("20000") / d("15500"))
    );
    assert_eq!(notice_of(&called[0]).status, "margin call");
    assert!(
        tick(&mut venue, "15600", 3).is_empty(),
        "still called: no second notice"
    );

    // 20,000 against 15,000: 1.33, back above it.
    let restored = tick(&mut venue, "15000", 4);
    assert_eq!(notices(&restored), [NoticeKind::MarginRestored]);
    assert!(tick(&mut venue, "14000", 5).is_empty());

    assert_eq!(
        notices(&tick(&mut venue, "15500", 6)),
        [NoticeKind::MarginCall]
    );
    assert_eq!(
        notices(&tick(&mut venue, "18200", 7)),
        [NoticeKind::Liquidation],
        "a liquidation ends the margin call without restoring it"
    );
    assert!(
        notices(&tick(&mut venue, "1000", 8)).is_empty(),
        "an account that owes nothing after its liquidation is not restored either"
    );
}

#[test]
fn a_margin_call_is_restored_when_the_account_repays_everything_it_owes() {
    let mut venue = short_one_btc(
        liquidation("1.1", "0.02")
            .with_margin_call(d("1.3"))
            .unwrap(),
    );
    assert_eq!(
        notices(&tick(&mut venue, "15500", 2)),
        [NoticeKind::MarginCall]
    );

    let repaid = venue.open_order(with_effect(buy_request("1", None), MarginEffect::Repay));
    assert_eq!(filled_trade(&repaid).quantity, d("1"));

    let events = venue.advance_time(time(3));
    assert_eq!(notices(&events), [NoticeKind::MarginRestored]);
    assert_eq!(notice_of(&events[0]).margin_level, None);
}

#[test]
fn a_debt_the_account_cannot_cover_is_left_owed_and_the_liquidation_continues_without_a_notice() {
    let mut venue = short_one_btc(liquidation("1.1", "0.02"));

    // 20,000 buys back only 0.8 of the BTC owed at 25,000.
    let (events, warned) = warnings(|| tick(&mut venue, "25000", 2));

    assert_eq!(trades(&events)[0].quantity, d("0.8"));
    assert_eq!(
        balance_now(&venue, base()).margin.unwrap().borrowed,
        d("0.2")
    );
    // The fee on the 20,000 repaid finds nothing left to pay it with.
    assert_eq!(
        balance_now(&venue, quote()),
        Balance::new_margin(d("-400"), d("-400"), Decimal::ZERO, Decimal::ZERO)
    );

    let shortfall = |warned: &[String]| {
        warned
            .iter()
            .filter(|message| message.contains("could not repay every debt"))
            .count()
    };
    assert_eq!(shortfall(&warned), 1, "{warned:?}");

    let (events, warned) = warnings(|| tick(&mut venue, "25000", 3));
    assert!(
        events.is_empty(),
        "nothing more can be repaid, and the liquidation under way sends no second notice"
    );
    assert_eq!(
        shortfall(&warned),
        0,
        "warned once per liquidation: {warned:?}"
    );
}

#[test]
fn a_debt_the_book_is_too_thin_to_close_is_closed_by_later_inputs() {
    let mut venue = short_one_btc(liquidation("1.1", "0.02"));
    let thin = |venue: &mut SimulatedVenue, seconds| {
        venue.apply_market(
            &instrument_name(),
            MarketObservation::new(traded("18200"))
                .with_depth(MarketDepth::new(Some(d("0.6")), Some(d("0.6")))),
            time(seconds),
        )
    };

    let first = thin(&mut venue, 2);
    assert_eq!(notices(&first), [NoticeKind::Liquidation]);
    assert_eq!(trades(&first)[0].quantity, d("0.6"));
    assert_eq!(
        balance_now(&venue, base()).margin.unwrap().borrowed,
        d("0.4")
    );

    assert_eq!(
        venue
            .borrow_capacity(&instrument_name(), Side::Sell)
            .unwrap()
            .borrowable_now,
        Some(Decimal::ZERO),
        "a liquidation under way lends nothing"
    );

    let placed = venue.open_order(gtc_limit("later", Side::Buy, "0.1", "9000"));
    rested_open(&placed);

    // Above the liquidation level now, but a liquidation under way closes every debt, and
    // cancels what the account placed since.
    let second = thin(&mut venue, 3);
    assert!(notices(&second).is_empty());
    assert_eq!(shape(&second)[..2], ["balance USDT", "order later"]);
    assert_eq!(trades(&second)[0].quantity, d("0.4"));
    assert_eq!(
        balance_now(&venue, base()).margin.unwrap(),
        MarginDetails::default()
    );

    let [first_fee, second_fee] = [&first, &second].map(|events| {
        let flows = events
            .iter()
            .filter_map(|event| match &event.kind {
                AccountEventKind::CashFlow(flow) => Some(flow.clone()),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(flows.len(), 1, "{:?}", shape(events));
        flows[0].clone()
    });
    assert_eq!(
        (first_fee.amount, second_fee.amount),
        (d("-218.4"), d("-145.6")),
        "each pass is charged on what it repaid"
    );
    assert!(first_fee.id.is_some());
    assert_ne!(first_fee.id, second_fee.id, "each fee has an id of its own");
    assert!(thin(&mut venue, 4).is_empty());
}

#[test]
fn moving_the_clock_liquidates_an_account_that_owes_the_valuation_asset() {
    // Owes 1,000 USDT and holds 1,000: a margin level of 1, with no market needed to value it.
    let config = liquidating_config(
        vec![AssetBalance::new(
            quote(),
            Balance::new_margin(d("1000"), d("1000"), d("1000"), Decimal::ZERO),
            Utc::now(),
        )],
        liquidation("1.1", "0.02"),
    );
    let mut venue = SimulatedVenue::new(&config, spot_instruments());

    let events = venue.advance_time(time(1));

    assert_eq!(
        shape(&events),
        [
            "notice liquidation",
            "balance USDT",
            "debt USDT",
            "balance USDT",
            "cash flow",
        ],
        "repaid from the account's own balance, then the fee"
    );
    assert_eq!(
        balance_now(&venue, quote()),
        Balance::new_margin(d("-20"), d("-20"), Decimal::ZERO, Decimal::ZERO)
    );
}

#[test]
fn a_debt_the_venue_cannot_value_is_not_checked() {
    // Owes 1 BTC with no market to value it, and holds nothing else.
    let config = liquidating_config(
        vec![
            AssetBalance::new(
                base(),
                Balance::new_margin(Decimal::ZERO, Decimal::ZERO, d("1"), Decimal::ZERO),
                Utc::now(),
            ),
            funded(quote(), d("1")),
        ],
        liquidation("1.1", "0.02"),
    );
    let mut venue = SimulatedVenue::new(&config, spot_instruments());

    let (events, warned) = warnings(|| venue.advance_time(time(1)));
    assert!(events.is_empty());
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(warned[0].contains("cannot value a debt"), "{warned:?}");

    let (events, warned) = warnings(|| venue.advance_time(time(2)));
    assert!(events.is_empty());
    assert!(warned.is_empty(), "warned once per venue: {warned:?}");
    assert_eq!(balance_now(&venue, base()).margin.unwrap().borrowed, d("1"));
}

#[test]
fn a_liquidation_the_leverage_limit_could_reach_is_refused() {
    let config = SpotMarginConfig::new(quote(), d("3"), MarginEffect::Borrow).unwrap();

    // Borrowed to 3x, an account holds 1.5 times what it owes.
    assert_eq!(
        config.clone().with_liquidation(liquidation("1.5", "0")),
        Err(SpotMarginConfigError::LiquidationWithinLeverage {
            level: d("1.5"),
            max_leverage: d("3"),
        })
    );
    assert!(
        config
            .clone()
            .with_liquidation(liquidation("1.49", "0"))
            .is_ok()
    );
    let lends_nothing = SpotMarginConfig::new(quote(), d("1"), MarginEffect::Borrow).unwrap();
    assert!(
        lends_nothing
            .with_liquidation(liquidation("5", "0"))
            .is_ok()
    );
}

#[test]
fn a_fee_taken_in_base_is_grossed_up_so_the_buy_back_clears_the_loan() {
    let config = MockExecutionConfig {
        fee_model: FeeModelConfig::Percentage(PercentageFeeModel::new(d("0.001"))),
        spot_fee_asset: SpotFeeAsset::Received,
        ..liquidating_config(
            vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
            liquidation("1.1", "0.02"),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, "10000", 1).is_empty());
    venue.open_order(sell_request("1", None));

    // 19,990 left of the sale against 18,200 owed: a margin level of 1.098.
    let events = tick(&mut venue, "18200", 2);

    let bought = trades(&events)[0];
    assert_eq!(bought.fees.asset, base());
    assert!(
        bought.quantity > d("1"),
        "grossed up by the fee: {}",
        bought.quantity
    );
    let btc = balance_now(&venue, base());
    assert_eq!(btc.margin.unwrap(), MarginDetails::default());
    assert!(
        btc.free >= Decimal::ZERO && btc.free < d("0.000000000000000000001"),
        "{btc:?}"
    );
}

#[test]
fn a_debt_in_a_pairs_quote_is_bought_back_by_selling_the_valuation_asset_as_its_base() {
    // Valued in BTC, the base of BTCUSDT: 1 BTC held against 9,500 USDT owed, worth 0.95 BTC at
    // 10,000, a margin level of 1.05.
    let config = MockExecutionConfig {
        spot_margin: Some(
            SpotMarginConfig::new(base(), d("3"), MarginEffect::Borrow)
                .unwrap()
                .with_liquidation(liquidation("1.1", "0.02"))
                .unwrap(),
        ),
        ..config_from_balances(
            vec![
                funded(base(), d("1")),
                AssetBalance::new(
                    quote(),
                    Balance::new_margin(Decimal::ZERO, Decimal::ZERO, d("9500"), Decimal::ZERO),
                    Utc::now(),
                ),
            ],
            FeeModelConfig::default(),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());

    let events = tick(&mut venue, "10000", 1);

    let sold = trades(&events);
    assert_eq!(sold.len(), 1);
    assert_eq!((sold[0].side, sold[0].quantity), (Side::Sell, d("0.95")));
    assert_eq!(
        balance_now(&venue, quote()),
        Balance::new_margin(Decimal::ZERO, Decimal::ZERO, Decimal::ZERO, Decimal::ZERO)
    );
    // 1 BTC less the 0.95 sold and the 0.019 fee, on the 0.95 BTC the debt was worth.
    assert_eq!(balance_now(&venue, base()).total, d("0.031"));
}

#[test]
fn collateral_is_sold_in_name_order_and_only_as_much_as_the_debt_and_fee_need() {
    let config = liquidating_config(
        vec![
            funded(base(), d("0.1")),
            funded(eth(), Decimal::ZERO),
            funded(quote(), Decimal::ZERO),
        ],
        liquidation("1.1", "0.02"),
    );
    let mut venue = SimulatedVenue::new_market_driven(&config, btc_and_eth_instruments());
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, "10000", 1).is_empty());
    let eth_tick = |venue: &mut SimulatedVenue, price: &str, seconds| {
        venue.apply_market(
            &eth_instrument_name(),
            MarketObservation::new(traded(price)),
            time(seconds),
        )
    };
    assert!(eth_tick(&mut venue, "1000", 1).is_empty());
    // 2 ETH bought with 2,000 USDT borrowed against the 1,000 of BTC: a margin level of 1.5.
    let bought = venue.open_order(request(eth_instrument_name(), Side::Buy, "2", None));
    assert_eq!(filled_trade(&bought).quantity, d("2"));

    // 1,000 of BTC and 1,200 of ETH against 2,000 owed: a margin level of 1.1.
    let events = eth_tick(&mut venue, "600", 2);

    let sold = trades(&events)
        .into_iter()
        .map(|trade| (trade.instrument.clone(), trade.quantity))
        .collect::<Vec<_>>();
    assert_eq!(
        sold,
        [
            (instrument_name(), d("0.1")),
            (eth_instrument_name(), d("1000") / d("600")),
            (eth_instrument_name(), d("40") / d("600")),
        ],
        "all the BTC first, then the ETH the rest of the loan needs, then the ETH the fee needs"
    );
    assert_eq!(
        balance_now(&venue, quote()).margin.unwrap(),
        MarginDetails::default()
    );
    let eth_left = balance_now(&venue, eth()).total;
    assert!(
        (eth_left - (d("2") - d("1040") / d("600"))).abs() < d("0.000000000000000000001"),
        "{eth_left}"
    );
}

/// A short of 1 ETH sold at 1,000 against `btc` held and `usdt` of the account's own, on a venue
/// trading BTCUSDT at 10,000 and ETHUSDT at 1,000, charging `fee_model`'s fees in
/// `spot_fee_asset`, and liquidating at 1.1 for a fee of 2 %.
fn short_one_eth(
    fee_model: FeeModelConfig,
    spot_fee_asset: SpotFeeAsset,
    btc: &str,
    usdt: &str,
) -> SimulatedVenue {
    let config = MockExecutionConfig {
        fee_model,
        spot_fee_asset,
        ..liquidating_config(
            vec![
                funded(base(), d(btc)),
                funded(eth(), Decimal::ZERO),
                funded(quote(), d(usdt)),
            ],
            liquidation("1.1", "0.02"),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, btc_and_eth_instruments());
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, "10000", 1).is_empty());
    assert!(eth_tick(&mut venue, "1000", 1).is_empty());
    let sold = venue.open_order(request(eth_instrument_name(), Side::Sell, "1", None));
    assert_eq!(filled_trade(&sold).quantity, d("1"));
    venue
}

/// A trade on ETHUSDT at `price`, and nothing else, observed at `time(seconds)`.
fn eth_tick(venue: &mut SimulatedVenue, price: &str, seconds: i64) -> Vec<UnindexedAccountEvent> {
    venue.apply_market(
        &eth_instrument_name(),
        MarketObservation::new(traded(price)),
        time(seconds),
    )
}

/// Each trade's instrument, side and quantity.
fn traded_legs(events: &[UnindexedAccountEvent]) -> Vec<(InstrumentNameExchange, Side, Decimal)> {
    trades(events)
        .into_iter()
        .map(|trade| (trade.instrument.clone(), trade.side, trade.quantity))
        .collect()
}

#[test]
fn a_fee_the_free_balance_covers_in_part_sells_collateral_for_only_the_rest() {
    let mut venue = short_one_eth(FeeModelConfig::default(), SpotFeeAsset::Quote, "0.1", "10");

    // 80 of BTC and 1,010 USDT against 1,000 of ETH owed: a margin level of 1.09.
    let events = tick(&mut venue, "800", 2);

    assert_eq!(
        traded_legs(&events),
        [
            (eth_instrument_name(), Side::Buy, d("1")),
            (instrument_name(), Side::Sell, d("0.0125")),
        ],
        "the ETH bought back on its own pair, not on BTCUSDT, which is also quoted in USDT; then \
         10 of the 20 fee raised from BTC, since 10 USDT is left free"
    );
    assert_eq!(
        balance_now(&venue, eth()),
        Balance::new_margin(Decimal::ZERO, Decimal::ZERO, Decimal::ZERO, Decimal::ZERO)
    );
    assert_eq!(balance_now(&venue, quote()).total, Decimal::ZERO);
    assert_eq!(balance_now(&venue, base()).total, d("0.0875"));
}

#[test]
fn a_buy_back_costing_more_than_the_free_balance_sells_collateral_first() {
    let mut venue = short_one_eth(
        FeeModelConfig::Percentage(PercentageFeeModel::new(d("0.001"))),
        SpotFeeAsset::Received,
        "0.1",
        "0",
    );
    assert_eq!(balance_now(&venue, quote()).total, d("999"));

    // 1,000 of BTC and 999 USDT against 1,900 of ETH owed: a margin level of 1.05.
    let events = eth_tick(&mut venue, "1900", 2);

    let legs = traded_legs(&events);
    assert_eq!(
        legs.iter()
            .map(|(instrument, side, _)| (instrument.clone(), *side))
            .collect::<Vec<_>>(),
        [
            (instrument_name(), Side::Sell),
            (eth_instrument_name(), Side::Buy),
            (instrument_name(), Side::Sell),
        ],
        "BTC sold for what the buy-back lacks, the ETH bought back, then BTC sold for the fee"
    );
    // The buy-back costs 1 / 0.999 ETH at 1,900, of which 999 USDT is free; a BTC sale delivers
    // 9,990 USDT per BTC, net of its fee.
    let lacked = d("1") / d("0.999") * d("1900") - d("999");
    assert!(
        (legs[0].2 - lacked / d("9990")).abs() < d("0.000000000000000000001"),
        "{legs:?}"
    );
    assert_eq!(
        balance_now(&venue, eth()).margin.unwrap(),
        MarginDetails::default(),
        "the buy-back, grossed up for its fee in ETH, repays all of the ETH owed"
    );
}

#[test]
fn a_buy_back_the_balance_cannot_quite_afford_trades_a_digit_less() {
    let mut venue = short_one_btc(liquidation("1.1", "0"));

    // 20,000 buys 2/3 of a BTC at 30,000, which rounds up to more than the account holds.
    let events = tick(&mut venue, "30000", 2);

    let bought = trades(&events);
    assert_eq!(bought.len(), 1, "{:?}", shape(&events));
    assert_eq!(bought[0].quantity, d("0.6666666666666666666666666666"));
    let usdt = balance_now(&venue, quote());
    assert!(
        usdt.free >= Decimal::ZERO && usdt.free < d("0.00000000000000000001"),
        "{usdt:?}"
    );
}

#[test]
fn a_sale_whose_quantity_rounds_down_is_nudged_up_to_raise_the_whole_fee() {
    let mut venue = liquidating_venue(
        vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
        liquidation("1.1", "0.02"),
        "10000",
    );
    let outcome = venue.open_order(buy_request("2.5", None));
    assert_eq!(filled_trade(&outcome).quantity, d("2.5"));

    // 2.5 BTC at 6,300 against 15,000 owed: a margin level of 1.05. The fee is 300, and
    // 300 / 6,300 rounds down, so that quantity sells for a little less than the fee.
    let events = tick(&mut venue, "6300", 2);

    let sold = trades(&events);
    assert_eq!(sold.len(), 2, "{:?}", shape(&events));
    assert!(
        d("300") / d("6300") * d("6300") < d("300"),
        "the case this test is for"
    );
    assert!(sold[1].quantity > d("300") / d("6300"), "{:?}", sold[1]);
    let usdt = balance_now(&venue, quote());
    assert_eq!(usdt.margin.unwrap(), MarginDetails::default());
    assert!(
        usdt.free >= Decimal::ZERO && usdt.free < d("0.00000000000000000001"),
        "the fee is raised in full, so it leaves no negative balance: {usdt:?}"
    );
    assert!(
        tick(&mut venue, "6300", 3).is_empty(),
        "the loan and the fee are repaid in one pass, so no liquidation continues"
    );
}

/// Prices every fill at the last trade, recording what it was asked to price.
#[derive(Debug, Default)]
struct Recording(std::sync::Mutex<Vec<(Side, Decimal, Option<Decimal>)>>);

impl Recording {
    /// The side, quantity and last price of each fill priced since the last call.
    fn take(&self) -> Vec<(Side, Decimal, Option<Decimal>)> {
        std::mem::take(&mut *self.0.lock().unwrap())
    }
}

impl FillModel for Recording {
    fn fill_price(&self, fill: &FillContext<'_>) -> Option<Decimal> {
        self.0
            .lock()
            .unwrap()
            .push((fill.side, fill.quantity, fill.market.last_price));
        fill.market.last_price
    }
}

#[test]
fn a_leg_is_priced_for_the_base_it_trades_and_only_for_an_asset_the_account_holds() {
    let config = liquidating_config(
        vec![
            funded(base(), Decimal::ZERO),
            funded(eth(), Decimal::ZERO),
            funded(quote(), d("1000")),
        ],
        liquidation("1.1", "0.02"),
    );
    let recording = Arc::new(Recording::default());
    let mut venue = SimulatedVenue::new_market_driven(&config, btc_and_eth_instruments())
        .with_fill_model(recording.clone());
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, "10000", 1).is_empty());
    assert!(eth_tick(&mut venue, "1000", 1).is_empty());
    let bought = venue.open_order(request(eth_instrument_name(), Side::Buy, "2.5", None));
    assert_eq!(filled_trade(&bought).quantity, d("2.5"));
    recording.take();

    // 2.5 ETH at 625 against 1,500 USDT owed: a margin level of 1.04.
    let events = eth_tick(&mut venue, "625", 2);

    assert_eq!(
        recording.take(),
        [
            (Side::Sell, d("2.4"), Some(d("625"))),
            (Side::Sell, d("0.048"), Some(d("625"))),
        ],
        "the ETH the loan and then the fee need, in ETH; the BTC the account does not hold is \
         never priced"
    );
    assert_eq!(
        traded_legs(&events),
        [
            (eth_instrument_name(), Side::Sell, d("2.4")),
            (eth_instrument_name(), Side::Sell, d("0.048")),
        ]
    );
}

#[test]
fn a_pair_without_a_market_is_passed_over_for_one_with_a_market() {
    // A second BTC/USDT pair, first by name, that the venue is never fed.
    let unfed = InstrumentNameExchange::new("BTC-USDT");
    let mut instruments = spot_instruments();
    let mut alias = instruments[&instrument_name()].clone();
    alias.name_exchange = unfed.clone();
    alias.name_internal = InstrumentNameInternal::new("btc-usdt");
    instruments.insert(unfed, alias);
    let config = liquidating_config(
        vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
        liquidation("1.1", "0"),
    );
    let mut venue = SimulatedVenue::new_market_driven(&config, instruments);
    advance(&mut venue, time(1));
    assert!(tick(&mut venue, "10000", 1).is_empty());
    assert_eq!(
        filled_trade(&venue.open_order(sell_request("1", None))).quantity,
        d("1")
    );

    let events = tick(&mut venue, "18200", 2);

    assert_eq!(
        traded_legs(&events),
        [(instrument_name(), Side::Buy, d("1"))]
    );
}

#[test]
fn a_leg_whose_fee_takes_all_it_delivers_is_not_traded() {
    // Owes 11,000 USDT against 2 BTC, and a sale's fee is 6,000 per BTC.
    let config = MockExecutionConfig {
        fee_model: FeeModelConfig::PerContract(PerContractFeeModel {
            commission_per_contract: d("6000"),
        }),
        ..liquidating_config(
            vec![
                funded(base(), d("2")),
                AssetBalance::new(
                    quote(),
                    Balance::new_margin(Decimal::ZERO, Decimal::ZERO, d("11000"), Decimal::ZERO),
                    Utc::now(),
                ),
            ],
            liquidation("1.1", "0.02"),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());

    // At 6,000 a margin level of 1.09, and a sale delivers nothing net of its fee.
    let (events, warned) = warnings(|| tick(&mut venue, "6000", 1));

    assert_eq!(shape(&events), ["notice liquidation"]);
    assert_eq!(
        balance_now(&venue, quote()).margin.unwrap().borrowed,
        d("11000")
    );
    assert_eq!(balance_now(&venue, base()).total, d("2"));
    assert!(
        warned
            .iter()
            .any(|message| message.contains("could not repay every debt")),
        "{warned:?}"
    );
}

#[test]
fn held_and_resting_orders_are_all_cancelled_in_client_order_id_order() {
    // Owes 1 BTC and holds 20,000 USDT, on a venue holding market orders for the next bar.
    let config = MockExecutionConfig {
        market_order_timing: MarketOrderTiming::NextBar(NextBarPrice::Open),
        ..liquidating_config(
            vec![
                AssetBalance::new(
                    base(),
                    Balance::new_margin(Decimal::ZERO, Decimal::ZERO, d("1"), Decimal::ZERO),
                    Utc::now(),
                ),
                funded(quote(), d("20000")),
            ],
            liquidation("1.1", "0"),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());
    advance(&mut venue, time(1));
    assert!(feed_bar(&mut venue, 1, ohlc("10000", "10000", "10000", "10000")).is_empty());
    rested_open(&venue.open_order(gtc_limit("c-resting", Side::Buy, "0.1", "9000")));
    let held = venue.open_order(ioc_buy("b-held", "0.1"));
    assert!(
        matches!(held.response.state, OrderState::Active(_)),
        "held for the next bar: {:?}",
        held.response.state
    );
    rested_open(&venue.open_order(gtc_limit("a-resting", Side::Buy, "0.1", "9000")));

    // 20,000 against 18,200 owed, and a trade price is no bar, so the held order is still held.
    let events = tick(&mut venue, "18200", 2);

    assert_eq!(
        shape(&events)[..7],
        [
            "notice liquidation",
            "balance USDT",
            "order a-resting",
            "balance USDT",
            "order b-held",
            "balance USDT",
            "order c-resting",
        ],
        "each releases what it held, by client order id"
    );
    assert!(venue.account.orders().is_empty());
    assert_eq!(
        traded_legs(&events),
        [(instrument_name(), Side::Buy, d("1"))]
    );
}

#[test]
fn a_dust_debt_that_values_at_zero_is_not_liquidated() {
    // Owes 1e-20 BTC, which at 1e-10 USDT is worth less than `Decimal` can hold.
    let config = liquidating_config(
        vec![
            AssetBalance::new(
                base(),
                Balance::new_margin(
                    Decimal::ZERO,
                    Decimal::ZERO,
                    d("0.00000000000000000001"),
                    Decimal::ZERO,
                ),
                Utc::now(),
            ),
            funded(quote(), d("1")),
        ],
        liquidation("1.1", "0"),
    );
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());
    advance(&mut venue, time(1));

    assert!(tick(&mut venue, "0.0000000001", 1).is_empty());
    assert_eq!(
        balance_now(&venue, base()).margin.unwrap().borrowed,
        d("0.00000000000000000001")
    );
}

#[test]
fn while_a_liquidation_is_under_way_the_venue_lends_nothing() {
    let mut venue = short_one_btc(liquidation("1.1", "0.02"));
    let at = |venue: &mut SimulatedVenue, price: &str, ask: &str, seconds| {
        venue.apply_market(
            &instrument_name(),
            MarketObservation::new(traded(price))
                .with_depth(MarketDepth::new(Some(d("1")), Some(d(ask)))),
            time(seconds),
        )
    };
    // 0.6 of the 1 BTC owed bought back at 18,200, leaving 8,861.6 USDT free.
    assert_eq!(
        notices(&at(&mut venue, "18200", "0.6", 2)),
        [NoticeKind::Liquidation]
    );
    // BTC falls to 10,000 with nothing offered, so the liquidation is still under way, though
    // 8,861.6 held against 4,000 owed is a margin level of 2.2, within the leverage limit.
    assert!(trades(&at(&mut venue, "10000", "0", 3)).is_empty());
    assert_eq!(balance_now(&venue, quote()).free, d("8861.6"));

    // 9,000 needs a loan of 138.4, which the leverage limit allows, but nothing is lent.
    let refused = venue.open_order(gtc_limit("borrows", Side::Buy, "1", "9000"));
    assert_refused_a_loan(&refused, quote());
    // Nor is BTC lent to a market sale, which the bid on offer would fill at once.
    let refused = venue.open_order(sell_request("0.1", None));
    assert_refused_a_loan(&refused, base());
    rested_open(&venue.open_order(gtc_limit("pays", Side::Buy, "0.1", "9000")));

    // The rest bought back at 10,000, and the order it could pay for cancelled.
    let closed = at(&mut venue, "10000", "1", 4);
    assert_cancelled(order_named(&closed, "pays"));
    assert_eq!(balance_now(&venue, quote()).free, d("4781.6"));

    // 5,400 needs a loan of 618.4, which the venue lends again now that nothing is owed.
    rested_open(&venue.open_order(gtc_limit("borrows-after", Side::Buy, "0.6", "9000")));
    assert_eq!(
        balance_now(&venue, quote()).margin.unwrap().borrowed,
        d("618.4")
    );
}

#[test]
fn while_a_liquidation_is_under_way_a_held_order_whose_fill_needs_a_loan_is_cancelled() {
    // Owes 1 BTC and holds 20,000 USDT, on a venue holding market orders for the next bar.
    let config = MockExecutionConfig {
        market_order_timing: MarketOrderTiming::NextBar(NextBarPrice::Open),
        ..liquidating_config(
            vec![
                AssetBalance::new(
                    base(),
                    Balance::new_margin(Decimal::ZERO, Decimal::ZERO, d("1"), Decimal::ZERO),
                    Utc::now(),
                ),
                funded(quote(), d("20000")),
            ],
            liquidation("1.1", "0.02"),
        )
    };
    let mut venue = SimulatedVenue::new_market_driven(&config, spot_instruments());
    advance(&mut venue, time(1));
    assert!(feed_bar(&mut venue, 1, ohlc("10000", "10000", "10000", "10000")).is_empty());
    let at = |venue: &mut SimulatedVenue, price: &str, ask: &str, seconds| {
        venue.apply_market(
            &instrument_name(),
            MarketObservation::new(traded(price))
                .with_depth(MarketDepth::new(Some(d("1")), Some(d(ask)))),
            time(seconds),
        )
    };
    // 0.6 of the 1 BTC owed bought back at 18,200, leaving 8,861.6 USDT free; then BTC falls to
    // 10,000 with nothing offered, so the liquidation is still under way.
    assert_eq!(
        notices(&at(&mut venue, "18200", "0.6", 2)),
        [NoticeKind::Liquidation]
    );
    assert!(trades(&at(&mut venue, "10000", "0", 3)).is_empty());
    assert_eq!(balance_now(&venue, quote()).free, d("8861.6"));

    // 0.85 at 10,000 is 8,500, which the account pays for itself, so it is held.
    let held = venue.open_order(ioc_buy("held", "0.85"));
    assert!(
        matches!(held.response.state, OrderState::Active(_)),
        "held for the next bar: {:?}",
        held.response.state
    );

    // The bar opens at 11,000, where the fill costs 9,350: a loan of 488.4 that the leverage
    // limit allows, but nothing is lent, so the order is cancelled. The liquidation then buys
    // back the 0.4 still owed at 11,000, charging 2% of 4,400.
    let events = feed_bar(&mut venue, 4, ohlc("11000", "11000", "11000", "11000"));

    assert_cancelled(order_named(&events, "held"));
    assert_eq!(
        traded_legs(&events),
        [(instrument_name(), Side::Buy, d("0.4"))]
    );
    assert!(venue.account.orders().is_empty());
    assert_eq!(balance_now(&venue, quote()).free, d("4373.6"));
    assert_eq!(
        balance_now(&venue, quote()).margin.unwrap(),
        MarginDetails::default()
    );
}

#[test]
fn an_order_a_liquidation_cancels_reports_the_interest_its_loan_was_charged_when_it_rested() {
    let mut venue = liquidating_venue(
        vec![funded(base(), Decimal::ZERO), funded(quote(), d("10000"))],
        liquidation("1.1", "0"),
        "10000",
    )
    .with_margin_interest(Arc::new(hourly_interest(base())));
    assert_eq!(
        filled_trade(&venue.open_order(sell_request("1", None))).quantity,
        d("1")
    );
    // A sell resting above the market borrows the 0.1 BTC it would deliver, charged 0.00001.
    rested_open(&venue.open_order(gtc_limit("resting", Side::Sell, "0.1", "20000")));

    let events = tick(&mut venue, "18200", 2);

    assert_eq!(
        shape(&events)[..5],
        [
            "notice liquidation",
            "balance BTC",
            "debt BTC",
            "cash flow",
            "order resting"
        ],
        "the released hold and the loan it repaid, the charge it never reported, then the cancel: \
         {:?}",
        shape(&events)
    );
    assert_eq!(
        *liquidation_fee(&events[3]),
        interest_flow(
            base(),
            "0.1",
            "0.00001",
            time(2),
            InterestTiming::OnBorrow,
            "1"
        ),
        "stamped at the cancel; the short's own charge was the first"
    );
    assert_cancelled(order_named(&events, "resting"));
}