rustrade-execution 0.19.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
1192
1193
1194
1195
//! Margin borrowing: the borrow and repay policy a margin account applies to its orders, the
//! simulated venue's spot margin configuration, and the interest it charges on its loans.
//!
//! [`MarginEffect`] is what an order does to a margin account's loans: borrow what it lacks, repay
//! from what it receives, or neither. A live margin client sends it with each order, and the
//! simulated venue applies it when [`SpotMarginConfig`] turns its spot margin on. A
//! [`MarginInterestModel`] prices those loans over time; [`PeriodicMarginInterest`] is the
//! built-in one. A margin client that learns what an order borrowed reports it as a
//! [`MarginBorrow`], in a [`MarginPlacement`].

use crate::holding_cost::{DayCount, RateSeries, next_whole_interval};
use crate::order::{Order, state::UnindexedOrderState};
use chrono::{DateTime, TimeDelta, Utc};
use derive_more::Constructor;
use fnv::FnvHashMap;
use rust_decimal::Decimal;
use rustrade_instrument::{
    asset::name::AssetNameExchange, exchange::ExchangeId, instrument::name::InstrumentNameExchange,
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt::Debug};
use thiserror::Error;

/// What an order does to a margin account's loans: borrow what it lacks, repay from what it
/// receives, or touch neither.
///
/// A margin client has one as its default and a request may carry its own
/// (`RequestOpen::margin_effect`, where `None` takes the client's default). The three values are
/// the ones a margin venue takes per order (Binance's `NO_SIDE_EFFECT`, `MARGIN_BUY` and
/// `AUTO_REPAY`). Whether an order opens or closes a position decides which it should be, and
/// that is position state the library deliberately does not track, so the caller says.
///
/// A client or simulated venue that cannot honour an effect refuses the order unsent with
/// [`OrderError::UnsupportedMarginEffect`](crate::error::OrderError::UnsupportedMarginEffect),
/// rather than place it without the effect.
///
/// The `Serialize`/`Deserialize` wire form is the config-file value (`"no_side_effect"`,
/// `"borrow"`, `"repay"`), which is not any venue's API value.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MarginEffect {
    /// Neither borrow nor repay: an order that would need a loan is refused, and what it receives
    /// stays free, as Binance's `NO_SIDE_EFFECT` does. The default, so that no order takes on
    /// debt unless the caller asks.
    #[default]
    NoSideEffect,
    /// Borrow what the order lacks, as Binance's `MARGIN_BUY` does, in the asset it pays with
    /// (base for a sell, which opens or extends a short).
    ///
    /// Binance lends the shortfall, or the asset's minimum borrow if that is more: **loan =
    /// max(shortfall, minimum borrow)**, where the minimum is the asset's `userMinBorrow` in
    /// `GET /sapi/v1/margin/allAssets`. A sell's shortfall is its quantity less the free base; a
    /// limit buy's is its quantity times its limit price less the free quote. Nothing caps the
    /// loan at the order's size, so a small order on an empty balance can borrow more than it
    /// pays, and what it does not pay stays free balance, owed.
    ///
    /// The loan is taken when the order is placed, not when it fills, and nothing repays it from
    /// the order's proceeds: close it with a [`Repay`](Self::Repay) order. Whether a loan
    /// is repaid when the order is cancelled before it fills is the client's `auto_repay_at_cancel`
    /// option.
    ///
    /// # Warning
    /// A **mis-sized order takes on debt**: one larger than the free balance borrows the shortfall,
    /// or the asset's minimum borrow, without any further opt-in, and interest accrues on it.
    Borrow,
    /// Repay the loan of the asset the order receives from what it receives, interest first, as
    /// Binance's `AUTO_REPAY` does. It borrows nothing: an order the free balance cannot pay for
    /// is refused.
    Repay,
}

/// A loan a margin order took out when it was placed: `amount` of `asset`.
///
/// `amount` is the loan's principal, as the venue reports lending it: a reported zero is kept as
/// zero, not read as no loan, and the amount need not equal what the order lacked. It is not the
/// order's net
/// effect on the balance: a fee the order pays in `asset` is not in it, and interest accrues on
/// it from the moment it is lent. `asset` is the asset the order pays with, which may be the base
/// or the quote asset of the pair.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Constructor)]
pub struct MarginBorrow {
    pub asset: AssetNameExchange,
    pub amount: Decimal,
}

/// The answer to placing a margin order: the order, and what it borrowed.
///
/// Returned by a margin client's own placement method (for example `BinanceMargin`'s
/// `open_margin_order`), whose venue reports the loan in its answer to the order.
/// `ExecutionClient::open_order` returns only the order.
#[derive(Debug, Clone, PartialEq)]
pub struct MarginPlacement {
    /// The order, as [`ExecutionClient::open_order`](crate::client::ExecutionClient::open_order)
    /// returns it.
    pub order: Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>,
    /// What the order borrowed when it was placed.
    ///
    /// - `Some` when the venue's answer reports a loan.
    /// - `None` when the answer reports none: the order borrowed nothing (its
    ///   [`MarginEffect`] is not [`Borrow`](MarginEffect::Borrow), or its free balance covered
    ///   it), or the order was refused.
    /// - When [`order.state`](Order::state) is an unknown outcome
    ///   ([`OrderState::is_outcome_unknown`](crate::order::state::OrderState::is_outcome_unknown)),
    ///   the venue may have placed the order and lent to it without the client learning how
    ///   much. `None` then says nothing about a loan: read the account's balances.
    pub borrowed: Option<MarginBorrow>,
}

/// Spot margin on the simulated venue: what it lends a spot order, how much, and what it repays.
///
/// Off by default: `MockExecutionConfig::spot_margin` is `None`, and the venue is a cash account
/// whose spot orders must be paid for with what it holds. With a configuration, it is a cross
/// margin account, with one pool of balances shared by every instrument, which lends a spot order
/// what it lacks and reports each balance's loan as Binance's margin account does.
///
/// # What it lends, and when
/// Each spot order has a [`MarginEffect`]: its request's `margin_effect`, else this
/// configuration's [`margin_effect`](Self::margin_effect). An order with [`MarginEffect::Borrow`]
/// that the account cannot pay for with its free balance borrows the shortfall of the asset it
/// pays with (base for a sell, quote for a buy), or that asset's
/// [`min_borrow`](Self::min_borrow) if it is more, as Binance does. It borrows when the venue
/// accepts the order, as Binance does, so a resting limit order or a held market order is lent its
/// shortfall at once, and the venue holds it against the order as it holds any other balance. What
/// a minimum borrow lends beyond the shortfall is not held: it is free balance, owed. An asset the
/// account has never held is opened with the loan.
///
/// An order that is cancelled, expires or is refused at its fill before it fills repays what it
/// borrowed, interest first, and a held market order that fills for less than was held repays the
/// part of its loan it no longer needs, if [`auto_repay_at_cancel`](Self::auto_repay_at_cancel)
/// is on (the default, as on Binance). One that fills in part and rests the remainder pays for the
/// part that filled with the account's own balance first, so the loan is held against the
/// remainder and repaid if the remainder is cancelled. What a minimum borrow lent beyond the
/// shortfall is repaid with the rest if the order ends before it fills in full, and stays owed
/// once it does.
///
/// An order with [`MarginEffect::NoSideEffect`] or [`MarginEffect::Repay`] lends nothing: one it
/// cannot pay for is rejected as on a cash account. A loan already in the venue's initial state
/// is left as it is, and balances still report their loans.
///
/// Only spot orders borrow. A CFD's margin is posted from the quote asset's free balance, as
/// without this configuration, and a CFD order carrying [`MarginEffect::Borrow`] or
/// [`MarginEffect::Repay`] is refused with
/// [`OrderError::UnsupportedMarginEffect`](crate::error::OrderError::UnsupportedMarginEffect).
///
/// # How much it lends
/// A borrow is allowed while the account's debt, after it, is at most `max_leverage − 1` times
/// the account's equity. Both are valued in [`valuation_asset`](Self::valuation_asset):
/// - Each asset is valued at the mid of an instrument on the venue that pairs it with the
///   valuation asset, else that market's last price, in either direction (`BTC/USDT`, or the
///   inverse of `USDT/BTC`). The valuation asset is worth one. A market priced at zero values
///   nothing.
/// - **Equity** is the sum over assets of `total − borrowed − interest`, valued that way. An asset
///   the venue cannot value counts as no equity, which understates what the account can borrow
///   rather than overstating it.
/// - **Debt** is the sum over assets of `borrowed + interest`, valued the same way. The venue
///   lends nothing while it cannot value a debt, and never lends an asset it cannot value.
///
/// An order that needs more than that is rejected as unaffordable, with a reason that says how
/// much the venue would lend. So is one whose asset's [`min_borrow`](Self::min_borrow) is more
/// than that, since the venue never lends less than the minimum, and its reason names the minimum
/// too. A `max_leverage` of one lends nothing, but still reports each balance's loan.
///
/// It lends nothing while a liquidation is under way: see [`SpotMarginLiquidation`].
///
/// # What it repays
/// Under [`MarginEffect::Repay`], whatever a fill delivers into an asset the account owes repays
/// that asset's loan, interest first, then principal, and only the rest becomes free balance. What
/// a buy delivers is its quantity less any fee charged in base, so a buy of exactly what is owed,
/// with a base fee, leaves a fee-sized loan. Gross the buy up by the fee to clear it. Under the
/// other two effects a fill repays nothing: as on Binance, a repay is per order, and nothing
/// repays a loan automatically.
///
/// # Liquidation
/// Off by default: an account whose debt grows past its equity keeps trading, and nothing closes
/// its positions. Configured with [`with_liquidation`](Self::with_liquidation), the venue repays
/// every debt once the account's margin level falls to the configured level, selling what the
/// account holds as needed. See [`SpotMarginLiquidation`].
///
/// # What it does not model
/// - **Isolated margin.** One pool of balances backs every instrument.
/// - **Per-asset borrow limits and the venue's lending pool.** Only the leverage limit, and each
///   asset's minimum borrow, apply.
/// - **Interest**, by this configuration alone. A venue given a [`MarginInterestModel`] charges
///   it; see that trait.
///
/// [`new`](Self::new) refuses a `max_leverage` below one, [`with_min_borrow`](Self::with_min_borrow)
/// a negative minimum, [`with_liquidation`](Self::with_liquidation) a liquidation the leverage
/// limit could reach, and so does deserialising any of them, so a configuration that exists is
/// one the venue can apply. Serialised, `max_leverage` and each minimum borrow are decimal strings
/// (`"3"`), as the crate writes decimals elsewhere, so no precision is lost to a float.
/// `min_borrow` is a map from asset to minimum, left out when empty, and `liquidation` is left out
/// when off.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[serde(try_from = "SpotMarginRepr", into = "SpotMarginRepr")]
pub struct SpotMarginConfig(SpotMarginRepr);

/// The serialised form of a [`SpotMarginConfig`], before validation.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
struct SpotMarginRepr {
    valuation_asset: AssetNameExchange,
    #[serde(with = "rust_decimal::serde::str")]
    max_leverage: Decimal,
    margin_effect: MarginEffect,
    #[serde(default = "default_auto_repay_at_cancel")]
    auto_repay_at_cancel: bool,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    min_borrow: BTreeMap<AssetNameExchange, Decimal>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    liquidation: Option<SpotMarginLiquidation>,
}

/// Serde default for `auto_repay_at_cancel`, here and on `BinanceMarginConfig`: on, as Binance's
/// `autoRepayAtCancel` is.
pub(crate) fn default_auto_repay_at_cancel() -> bool {
    true
}

impl SpotMarginConfig {
    /// Spot margin applying `margin_effect` to each order that does not carry its own, up to
    /// `max_leverage` over the account's equity, valued in `valuation_asset`.
    ///
    /// The effect has no default, so that a configuration says whether the venue may take on debt
    /// for an order. Unused loans are repaid when their order ends unfilled; see
    /// [`with_auto_repay_at_cancel`](Self::with_auto_repay_at_cancel).
    ///
    /// # Errors
    /// Returns [`SpotMarginConfigError`] if `max_leverage` is below one.
    pub fn new(
        valuation_asset: AssetNameExchange,
        max_leverage: Decimal,
        margin_effect: MarginEffect,
    ) -> Result<Self, SpotMarginConfigError> {
        Self::try_from(SpotMarginRepr {
            valuation_asset,
            max_leverage,
            margin_effect,
            auto_repay_at_cancel: default_auto_repay_at_cancel(),
            min_borrow: BTreeMap::new(),
            liquidation: None,
        })
    }

    /// This configuration, repaying a loan its order no longer needs (the order was cancelled,
    /// expired or refused at its fill, or a held market order filled for less than was held) when
    /// `auto_repay_at_cancel` is true, and leaving it owed when false.
    ///
    /// On by default, as Binance's `autoRepayAtCancel` is. With it off, an unused loan stays owed
    /// until a [`MarginEffect::Repay`] order repays it.
    #[must_use]
    pub fn with_auto_repay_at_cancel(mut self, auto_repay_at_cancel: bool) -> Self {
        self.0.auto_repay_at_cancel = auto_repay_at_cancel;
        self
    }

    /// The asset equity and debt are valued in.
    pub fn valuation_asset(&self) -> &AssetNameExchange {
        &self.0.valuation_asset
    }

    /// The most the account may hold, as a multiple of its equity: its debt may reach
    /// `max_leverage − 1` times its equity.
    pub fn max_leverage(&self) -> Decimal {
        self.0.max_leverage
    }

    /// The effect of an order whose request carries none.
    pub fn margin_effect(&self) -> MarginEffect {
        self.0.margin_effect
    }

    /// Whether a loan its order no longer needs is repaid. See
    /// [`with_auto_repay_at_cancel`](Self::with_auto_repay_at_cancel).
    pub fn auto_repay_at_cancel(&self) -> bool {
        self.0.auto_repay_at_cancel
    }

    /// This configuration, never lending less than `min_borrow` of `asset`: a loan of `asset`
    /// below it is raised to it, as Binance raises a `MARGIN_BUY` loan to the asset's
    /// `userMinBorrow`. A minimum of zero removes it. Replaces any minimum `asset` had.
    ///
    /// No asset has one by default, so each loan is exactly its order's shortfall. An order its
    /// free balance covers borrows nothing, whatever the minimum.
    ///
    /// # Errors
    /// Returns [`SpotMarginConfigError::NegativeMinBorrow`] if `min_borrow` is negative.
    pub fn with_min_borrow(
        mut self,
        asset: AssetNameExchange,
        min_borrow: Decimal,
    ) -> Result<Self, SpotMarginConfigError> {
        self.0.min_borrow.insert(asset, min_borrow);
        Self::try_from(self.0)
    }

    /// The least the venue lends of `asset` in one loan, zero if it has no minimum. See
    /// [`with_min_borrow`](Self::with_min_borrow).
    pub fn min_borrow(&self, asset: &AssetNameExchange) -> Decimal {
        self.0
            .min_borrow
            .get(asset)
            .copied()
            .unwrap_or(Decimal::ZERO)
    }

    /// This configuration, liquidating the account as `liquidation` says. Replaces any
    /// liquidation it had.
    ///
    /// # Errors
    /// Returns [`SpotMarginConfigError::LiquidationWithinLeverage`] if a borrow the leverage limit
    /// allows could leave the account at the liquidation level: if `max_leverage` is above one
    /// and the level is at least `max_leverage / (max_leverage − 1)`, the margin level of an
    /// account borrowed to the limit.
    pub fn with_liquidation(
        mut self,
        liquidation: SpotMarginLiquidation,
    ) -> Result<Self, SpotMarginConfigError> {
        self.0.liquidation = Some(liquidation);
        Self::try_from(self.0)
    }

    /// When the venue liquidates the account, or `None` if it never does. See
    /// [`with_liquidation`](Self::with_liquidation).
    pub fn liquidation(&self) -> Option<SpotMarginLiquidation> {
        self.0.liquidation
    }
}

impl TryFrom<SpotMarginRepr> for SpotMarginConfig {
    type Error = SpotMarginConfigError;

    fn try_from(mut repr: SpotMarginRepr) -> Result<Self, Self::Error> {
        if repr.max_leverage < Decimal::ONE {
            return Err(SpotMarginConfigError::LeverageBelowOne(repr.max_leverage));
        }
        if let Some((asset, min_borrow)) = repr
            .min_borrow
            .iter()
            .find(|(_, min_borrow)| **min_borrow < Decimal::ZERO)
        {
            return Err(SpotMarginConfigError::NegativeMinBorrow {
                asset: asset.clone(),
                min_borrow: *min_borrow,
            });
        }
        if let Some(liquidation) = &repr.liquidation {
            // An account borrowed to the limit has `debt = (max_leverage − 1) × equity`, so a
            // margin level of `max_leverage / (max_leverage − 1)`. One lending nothing, at a
            // `max_leverage` of one, has no limit to reach, and passes: the product is zero.
            if liquidation.level() * (repr.max_leverage - Decimal::ONE) >= repr.max_leverage {
                return Err(SpotMarginConfigError::LiquidationWithinLeverage {
                    level: liquidation.level(),
                    max_leverage: repr.max_leverage,
                });
            }
        }
        // A zero minimum is no minimum, so it is not kept: two configurations that lend alike
        // compare equal.
        repr.min_borrow
            .retain(|_, min_borrow| !min_borrow.is_zero());
        Ok(Self(repr))
    }
}

impl From<SpotMarginConfig> for SpotMarginRepr {
    fn from(config: SpotMarginConfig) -> Self {
        config.0
    }
}

/// Why a [`SpotMarginConfig`] was refused.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum SpotMarginConfigError {
    /// A `max_leverage` below one, which would allow less than the account's own equity.
    #[error("spot margin max_leverage {0} is below 1; a leverage of 1 lends nothing")]
    LeverageBelowOne(Decimal),
    /// A negative minimum borrow for `asset`.
    #[error("spot margin min_borrow {min_borrow} of {asset} is negative")]
    #[non_exhaustive]
    NegativeMinBorrow {
        /// The asset given the minimum.
        asset: AssetNameExchange,
        /// The minimum given, below zero.
        min_borrow: Decimal,
    },
    /// A liquidation level below one, at which an account would already owe more than it holds.
    #[error("spot margin liquidation level {0} is below 1")]
    LiquidationLevelBelowOne(Decimal),
    /// A liquidation fee rate outside `[0, 1)`.
    #[error("spot margin liquidation fee rate {0} is outside [0, 1)")]
    LiquidationFeeOutOfRange(Decimal),
    /// A margin-call level not above the liquidation level, so it could never be reached first.
    #[error(
        "spot margin margin-call level {margin_call} is not above the liquidation level {level}"
    )]
    #[non_exhaustive]
    MarginCallNotAboveLiquidation {
        /// The margin-call level given.
        margin_call: Decimal,
        /// The liquidation level it must be above.
        level: Decimal,
    },
    /// A liquidation level an account borrowed to the leverage limit would already be at.
    #[error(
        "spot margin liquidation level {level} is reached by a borrow max_leverage {max_leverage} \
         allows; it must be below max_leverage / (max_leverage - 1)"
    )]
    #[non_exhaustive]
    LiquidationWithinLeverage {
        /// The liquidation level given.
        level: Decimal,
        /// The configuration's leverage limit.
        max_leverage: Decimal,
    },
}

/// When the simulated venue liquidates a spot margin account, what it charges for it, and whether
/// it warns first. Set by [`SpotMarginConfig::with_liquidation`].
///
/// # When
/// The venue checks the account's **margin level** after each of its inputs, a market tick or a
/// move of its clock, since those are what change it: prices move, and interest falls due. The
/// level is the value of everything the account holds over the value of what it owes, loans plus
/// interest, both valued as the leverage limit values them (see [`SpotMarginConfig`]'s
/// `# How much it lends`): at the mid of a spot instrument pairing each asset with the valuation
/// asset, else its last price. There is no index price, so a market's own mid stands in for the
/// one a venue liquidates on. An asset the venue cannot value counts for nothing, which can bring
/// a liquidation forward but never put one off. While it cannot value a debt it does not check,
/// and warns once.
///
/// At or below [`level`](Self::level) it liquidates.
///
/// # How
/// It sends an [`AccountNotice`](crate::notice::AccountNotice) of
/// [`NoticeKind::Liquidation`](crate::notice::NoticeKind::Liquidation), then closes every debt:
/// 1. It cancels every open order, resting or held, releasing what each held.
/// 2. It repays each debt from the asset's own free balance, interest first.
/// 3. It repays what is still owed of the valuation asset with the proceeds of selling the
///    account's other assets, each on a spot instrument pairing it with the valuation asset, in
///    name order, as much of each as is needed.
/// 4. It buys back what is still owed of each other asset, in name order, on a spot instrument
///    pairing it with the valuation asset, selling other assets for the valuation asset first as
///    in step 3 when the account holds too little of it. What a buy delivers repays the debt.
/// 5. It charges the fee: [`fee_rate`](Self::fee_rate) times the value of the debt this repaid,
///    in the valuation asset, raised as in step 3 if the account holds too little of it.
///
/// Each forced trade fills as a market order would on arrival, as a taker: priced by the venue's
/// fill model at its current market, slipped, limited by the depth on offer, and charged the
/// venue's fee. It is reported as `[balance.., trade]`, the [`Trade`](crate::trade::Trade)
/// carrying [`TradeOrigin::Liquidation`](crate::trade::TradeOrigin::Liquidation), no client order
/// id and an unknown strategy, with no order snapshot, since no client placed it. It is never
/// held for a bar or a fresh market. Once priced it has drawn from the venue's slippage stream,
/// even if it then trades nothing, so a liquidation that runs over several inputs draws on each.
/// The fee is reported as `[balance, cash flow]`, a
/// [`CashFlowKind::LiquidationFee`](crate::cash_flow::CashFlowKind::LiquidationFee) with no
/// instrument.
///
/// # When it cannot close every debt
/// A debt can outlast an input when the depth on offer is too thin, when an asset has no market
/// to trade on, or when the account holds too little to repay it. The venue then warns once, and
/// leaves the rest owed: it has no insurance fund, and nothing floors the account's balances.
///
/// The liquidation continues on each later input until no debt is left, without another notice:
/// each input cancels whatever the account has since placed, repays what it can, and charges the
/// fee on what it repaid.
///
/// Each pass's fee is charged out of the account's remaining assets after that pass's
/// repayments, selling for it as in step 3, as a venue takes its fee out of the proceeds of the
/// liquidation's trades. So a debt a pass leaves owed has that much less to be repaid from on the
/// next pass. A fee the account cannot pay takes the valuation asset's free balance below zero.
///
/// While the liquidation is under way the venue lends nothing. An order that would need a loan is
/// rejected as unaffordable, its reason showing nothing borrowable, and so is the fill of a held
/// order that would need one. One the account can pay for from its own balance is accepted, and
/// cancelled by the next input if it is still open. A debt the venue cannot trade out of, for want
/// of a market or of anything left to sell, keeps lending off until the account repays it.
///
/// # Margin call
/// Off by default. With [`with_margin_call`](Self::with_margin_call), the venue sends
/// [`NoticeKind::MarginCall`](crate::notice::NoticeKind::MarginCall) when the margin level falls
/// to or below that level, and
/// [`NoticeKind::MarginRestored`](crate::notice::NoticeKind::MarginRestored) when it rises back
/// above it or the account no longer owes anything, once per crossing. A liquidation ends a margin
/// call without a notice.
///
/// Each notice carries the margin level that caused it, `None` for an account that owes nothing,
/// and its kind's name as its status.
///
/// # Not liquidated
/// A CFD position: its margin is posted from the quote asset, outside the spot margin account.
///
/// # Serialised form
/// `level`, `fee_rate` and `margin_call_level` are decimal strings, and `margin_call_level` is
/// left out when off. Deserialising refuses what the constructors refuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[serde(
    try_from = "SpotMarginLiquidationRepr",
    into = "SpotMarginLiquidationRepr"
)]
pub struct SpotMarginLiquidation(SpotMarginLiquidationRepr);

/// The serialised form of a [`SpotMarginLiquidation`], before validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
struct SpotMarginLiquidationRepr {
    #[serde(with = "rust_decimal::serde::str")]
    level: Decimal,
    #[serde(with = "rust_decimal::serde::str")]
    fee_rate: Decimal,
    #[serde(
        default,
        with = "rust_decimal::serde::str_option",
        skip_serializing_if = "Option::is_none"
    )]
    margin_call_level: Option<Decimal>,
}

impl SpotMarginLiquidation {
    /// Liquidation at a margin level of `level` or below, charging `fee_rate` of the debt it
    /// repays, with no margin call.
    ///
    /// For example, a level of 1.1 and a fee rate of 0.02 liquidate an account whose holdings are
    /// worth 1.1 times its debt or less, and charge 2 % of what it repays.
    ///
    /// # Errors
    /// [`SpotMarginConfigError::LiquidationLevelBelowOne`] for a `level` below one, and
    /// [`SpotMarginConfigError::LiquidationFeeOutOfRange`] for a `fee_rate` below zero or not
    /// below one.
    pub fn new(level: Decimal, fee_rate: Decimal) -> Result<Self, SpotMarginConfigError> {
        Self::try_from(SpotMarginLiquidationRepr {
            level,
            fee_rate,
            margin_call_level: None,
        })
    }

    /// This liquidation, with a margin call at `margin_call_level`. See `# Margin call`.
    ///
    /// # Errors
    /// [`SpotMarginConfigError::MarginCallNotAboveLiquidation`] if `margin_call_level` is not
    /// above the liquidation [`level`](Self::level).
    pub fn with_margin_call(
        mut self,
        margin_call_level: Decimal,
    ) -> Result<Self, SpotMarginConfigError> {
        self.0.margin_call_level = Some(margin_call_level);
        Self::try_from(self.0)
    }

    /// The margin level at or below which the venue liquidates.
    pub fn level(&self) -> Decimal {
        self.0.level
    }

    /// The fee, as a fraction of the value of the debt a liquidation repays.
    pub fn fee_rate(&self) -> Decimal {
        self.0.fee_rate
    }

    /// The margin level at or below which the venue sends a margin call, or `None` for none.
    pub fn margin_call_level(&self) -> Option<Decimal> {
        self.0.margin_call_level
    }
}

impl TryFrom<SpotMarginLiquidationRepr> for SpotMarginLiquidation {
    type Error = SpotMarginConfigError;

    fn try_from(repr: SpotMarginLiquidationRepr) -> Result<Self, Self::Error> {
        if repr.level < Decimal::ONE {
            return Err(SpotMarginConfigError::LiquidationLevelBelowOne(repr.level));
        }
        if repr.fee_rate < Decimal::ZERO || repr.fee_rate >= Decimal::ONE {
            return Err(SpotMarginConfigError::LiquidationFeeOutOfRange(
                repr.fee_rate,
            ));
        }
        if let Some(margin_call) = repr.margin_call_level
            && margin_call <= repr.level
        {
            return Err(SpotMarginConfigError::MarginCallNotAboveLiquidation {
                margin_call,
                level: repr.level,
            });
        }
        Ok(Self(repr))
    }
}

impl From<SpotMarginLiquidation> for SpotMarginLiquidationRepr {
    fn from(liquidation: SpotMarginLiquidation) -> Self {
        liquidation.0
    }
}

/// Charges interest on a simulated margin loan: once when the loan is taken, and at boundaries in
/// time while it stays open.
///
/// The simulated venue asks for a charge on each loan its spot margin takes
/// ([`InterestDue::Borrow`], on the amount just lent), and, through
/// [`next_boundary`](Self::next_boundary), at each boundary its clock reaches while the account
/// owes the asset ([`InterestDue::Boundary`], on the principal owed when it falls). Each charge is
/// added to the asset's loan as interest, which the account repays before principal, and is
/// reported as a [`CashFlowKind::MarginInterest`](crate::cash_flow::CashFlowKind::MarginInterest)
/// in the borrowed asset.
///
/// Interest is charged on principal only: interest already owed earns none.
///
/// # Contract
/// - `next_boundary` must return an instant strictly after `after`, or `None` when the model
///   never charges the asset at a boundary. A boundary at or before `after` would charge one
///   instant forever, so the venue **panics** on one.
/// - A charge's [`interest`](InterestCharge::interest) must not be negative: interest is owed,
///   never paid out. The venue **panics** on a negative one.
/// - Both methods must be pure functions of their arguments: the venue may ask for the same
///   boundary more than once, and charges it once.
///
/// In return, the venue asks [`charge`](Self::charge) only about a positive `principal`: an order
/// that lends nothing, and an asset that owes nothing, is never charged.
///
/// `Send + Sync` because a venue holds its model behind an `Arc`, so that one model, and the
/// rate history it carries, can be shared by every run of a backtest sweep without being copied.
pub trait MarginInterestModel: Debug + Send + Sync {
    /// The first boundary strictly after `after` at which this model charges a loan of `asset`,
    /// or `None` if it never does.
    fn next_boundary(
        &self,
        asset: &AssetNameExchange,
        after: DateTime<Utc>,
    ) -> Option<DateTime<Utc>>;

    /// What a loan of `principal` of `asset` is charged when `due`, or `None` for nothing.
    fn charge(
        &self,
        asset: &AssetNameExchange,
        principal: Decimal,
        due: InterestDue,
    ) -> Option<InterestCharge>;
}

/// When a [`MarginInterestModel`] is asked to charge a loan.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum InterestDue {
    /// The loan was taken at this instant, and the principal is the amount just lent.
    Borrow(DateTime<Utc>),
    /// A boundary the model named, and the principal is what the account owed when it fell.
    Boundary(DateTime<Utc>),
}

impl InterestDue {
    /// The instant the charge is due.
    pub fn time(self) -> DateTime<Utc> {
        match self {
            Self::Borrow(time) | Self::Boundary(time) => time,
        }
    }
}

/// One charge a [`MarginInterestModel`] makes on one loan.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InterestCharge {
    /// The interest owed, in the borrowed asset. Never negative; zero charges nothing.
    pub interest: Decimal,
    /// The rate it was charged at, reported as the cash flow's `rate` and used for nothing else,
    /// so a model may quote it annualised or per its own period. [`PeriodicMarginInterest`]
    /// reports the annualised rate.
    pub rate: Option<Decimal>,
}

impl InterestCharge {
    /// A charge of `interest`, in the borrowed asset, at `rate`.
    pub fn new(interest: Decimal, rate: Option<Decimal>) -> Self {
        Self { interest, rate }
    }
}

/// When a [`PeriodicMarginInterest`] charges a loan.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum InterestSchedule {
    /// One period when the loan is taken, then one at every boundary while it is open.
    #[default]
    AtBorrowAndEachBoundary,
    /// One period when the loan is taken, and nothing afterwards however long it is held.
    AtBorrowOnly,
}

/// Margin interest charged a period at a time, at an annualised rate per asset: a crypto margin
/// account's hourly interest.
///
/// - **When.** One period when a loan is taken, whenever in the period that is, then one at every
///   boundary while the loan is open, unless [`InterestSchedule::AtBorrowOnly`]. Boundaries fall
///   at whole multiples of the period since the Unix epoch, so an hourly period charges on the
///   hour, UTC. Each charge pays in advance for the period it falls in: the one a borrow is taken
///   in, or the one a boundary begins. So a loan repaid mid-period has already paid for that
///   period, and one repaid just before a boundary is not charged there.
/// - **How much.** `principal × rate × period / basis`, where the rate is annualised, read from
///   the asset's [`RateSeries`] at the instant charged, and the basis is the [`DayCount`]'s year.
///   Each charge is rounded **up** to the asset's unit, `1e-8` unless set by
///   [`with_unit`](Self::with_unit), so a small loan is charged at least one unit, as a venue
///   that books interest in whole units charges it.
/// - **Which assets.** Only those given rates. A loan of any other asset, or one charged before its
///   series begins, is charged nothing.
///
/// A venue that quotes a daily rate is reproduced by an annual rate of `daily × 365` on
/// [`DayCount::Act365`].
#[derive(Debug, Clone)]
pub struct PeriodicMarginInterest {
    period: TimeDelta,
    day_count: DayCount,
    schedule: InterestSchedule,
    rates: FnvHashMap<AssetNameExchange, RateSeries>,
    units: FnvHashMap<AssetNameExchange, Decimal>,
}

/// The unit a [`PeriodicMarginInterest`] rounds an asset's charge up to unless told otherwise: one
/// satoshi, the unit crypto venues book interest in.
const DEFAULT_INTEREST_UNIT: Decimal = Decimal::from_parts(1, 0, 0, false, 8);

impl PeriodicMarginInterest {
    /// Interest charged every `period` on the `day_count` basis, at borrow and each boundary, with
    /// no asset's rates yet.
    ///
    /// # Panics
    /// If `period` is not positive.
    pub fn new(period: TimeDelta, day_count: DayCount) -> Self {
        assert!(
            period > TimeDelta::zero(),
            "PeriodicMarginInterest period must be positive, got {period}"
        );
        Self {
            period,
            day_count,
            schedule: InterestSchedule::default(),
            rates: FnvHashMap::default(),
            units: FnvHashMap::default(),
        }
    }

    /// Charges a loan of `asset` at the annualised rates in `rates`.
    ///
    /// # Panics
    /// If any rate in `rates` is negative: a borrower is never paid interest, so a negative rate is
    /// a fault in the data, refused here rather than when the venue first charges it.
    #[must_use]
    pub fn with_rates(mut self, asset: AssetNameExchange, rates: RateSeries) -> Self {
        if let Some(rate) = rates.rates().find(|rate| *rate < Decimal::ZERO) {
            panic!("PeriodicMarginInterest rate for {asset} must not be negative, got {rate}");
        }
        self.rates.insert(asset, rates);
        self
    }

    /// Rounds each charge on `asset` up to a multiple of `unit` instead of `1e-8`.
    ///
    /// # Panics
    /// If `unit` is not positive.
    #[must_use]
    pub fn with_unit(mut self, asset: AssetNameExchange, unit: Decimal) -> Self {
        assert!(
            unit > Decimal::ZERO,
            "PeriodicMarginInterest unit for {asset} must be positive, got {unit}"
        );
        self.units.insert(asset, unit);
        self
    }

    /// Charges on `schedule` instead of at borrow and each boundary.
    #[must_use]
    pub fn with_schedule(mut self, schedule: InterestSchedule) -> Self {
        self.schedule = schedule;
        self
    }
}

impl MarginInterestModel for PeriodicMarginInterest {
    fn next_boundary(
        &self,
        asset: &AssetNameExchange,
        after: DateTime<Utc>,
    ) -> Option<DateTime<Utc>> {
        match self.schedule {
            InterestSchedule::AtBorrowOnly => None,
            InterestSchedule::AtBorrowAndEachBoundary if !self.rates.contains_key(asset) => None,
            InterestSchedule::AtBorrowAndEachBoundary => next_whole_interval(self.period, after),
        }
    }

    fn charge(
        &self,
        asset: &AssetNameExchange,
        principal: Decimal,
        due: InterestDue,
    ) -> Option<InterestCharge> {
        let rate = self.rates.get(asset)?.at(due.time())?;
        let unit = self
            .units
            .get(asset)
            .copied()
            .unwrap_or(DEFAULT_INTEREST_UNIT);
        let exact = self.day_count.accrue_span(principal * rate, self.period);

        Some(InterestCharge::new(
            (exact / unit).ceil() * unit,
            Some(rate),
        ))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics are the correct failure mode
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    #[test]
    fn a_leverage_below_one_is_refused_by_the_constructor_and_by_deserialising() {
        assert_eq!(
            SpotMarginConfig::new(
                AssetNameExchange::new("usdt"),
                dec!(0.5),
                MarginEffect::NoSideEffect
            ),
            Err(SpotMarginConfigError::LeverageBelowOne(dec!(0.5)))
        );

        let json = r#"{"valuation_asset":"usdt","max_leverage":"0.5","margin_effect":"borrow"}"#;
        assert!(serde_json::from_str::<SpotMarginConfig>(json).is_err());
    }

    #[test]
    fn the_margin_effect_has_no_default_and_a_retired_side_effect_is_refused() {
        let omitted = r#"{"valuation_asset":"usdt","max_leverage":"3"}"#;
        assert!(serde_json::from_str::<SpotMarginConfig>(omitted).is_err());

        // The retired key alone is no substitute for the effect.
        let retired =
            r#"{"valuation_asset":"usdt","max_leverage":"3","side_effect":"auto_borrow_repay"}"#;
        assert!(serde_json::from_str::<SpotMarginConfig>(retired).is_err());
    }

    #[test]
    fn auto_repay_at_cancel_defaults_to_true_when_omitted() {
        let json = r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"repay"}"#;
        let config: SpotMarginConfig = serde_json::from_str(json).unwrap();

        assert_eq!(config.margin_effect(), MarginEffect::Repay);
        assert!(config.auto_repay_at_cancel());
        assert_eq!(config.max_leverage(), dec!(3));
        assert_eq!(config.valuation_asset(), &AssetNameExchange::new("usdt"));
    }

    #[test]
    fn a_minimum_borrow_is_per_asset_and_zero_where_none_is_set() {
        let config = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap()
        .with_min_borrow(AssetNameExchange::new("btc"), dec!(0.00006))
        .unwrap();

        assert_eq!(
            config.min_borrow(&AssetNameExchange::new("btc")),
            dec!(0.00006)
        );
        assert_eq!(
            config.min_borrow(&AssetNameExchange::new("usdt")),
            Decimal::ZERO
        );
    }

    #[test]
    fn a_negative_minimum_borrow_is_refused_by_the_builder_and_by_deserialising() {
        let config = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap();
        assert_eq!(
            config.with_min_borrow(AssetNameExchange::new("btc"), dec!(-0.1)),
            Err(SpotMarginConfigError::NegativeMinBorrow {
                asset: AssetNameExchange::new("btc"),
                min_borrow: dec!(-0.1),
            })
        );

        let json = r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"borrow","min_borrow":{"btc":"-0.1"}}"#;
        assert!(serde_json::from_str::<SpotMarginConfig>(json).is_err());
    }

    #[test]
    fn a_zero_minimum_borrow_removes_the_minimum() {
        let plain = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap();
        let cleared = plain
            .clone()
            .with_min_borrow(AssetNameExchange::new("btc"), dec!(5))
            .unwrap()
            .with_min_borrow(AssetNameExchange::new("btc"), Decimal::ZERO)
            .unwrap();

        assert_eq!(cleared, plain);
        assert_eq!(
            cleared.min_borrow(&AssetNameExchange::new("btc")),
            Decimal::ZERO
        );
    }

    #[test]
    fn a_minimum_borrow_round_trips_as_a_map_of_decimal_strings() {
        let config = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap()
        .with_min_borrow(AssetNameExchange::new("btc"), dec!(0.00006))
        .unwrap();

        let json = serde_json::to_string(&config).unwrap();
        assert_eq!(
            json,
            r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"borrow","auto_repay_at_cancel":true,"min_borrow":{"btc":"0.00006"}}"#
        );
        assert_eq!(
            serde_json::from_str::<SpotMarginConfig>(&json).unwrap(),
            config
        );
    }

    #[test]
    fn a_configuration_round_trips_through_its_serialised_form() {
        let config = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap()
        .with_auto_repay_at_cancel(false);

        let json = serde_json::to_string(&config).unwrap();
        assert_eq!(
            json,
            r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"borrow","auto_repay_at_cancel":false}"#
        );
        assert_eq!(
            serde_json::from_str::<SpotMarginConfig>(&json).unwrap(),
            config
        );
    }

    #[test]
    fn a_liquidation_round_trips_and_is_left_out_when_off() {
        let config = SpotMarginConfig::new(
            AssetNameExchange::new("usdt"),
            dec!(3),
            MarginEffect::Borrow,
        )
        .unwrap()
        .with_liquidation(
            SpotMarginLiquidation::new(dec!(1.1), dec!(0.02))
                .unwrap()
                .with_margin_call(dec!(1.3))
                .unwrap(),
        )
        .unwrap();

        let json = serde_json::to_string(&config).unwrap();
        assert_eq!(
            json,
            r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"borrow","auto_repay_at_cancel":true,"liquidation":{"level":"1.1","fee_rate":"0.02","margin_call_level":"1.3"}}"#
        );
        assert_eq!(
            serde_json::from_str::<SpotMarginConfig>(&json).unwrap(),
            config
        );

        let without_call = r#"{"level":"1.1","fee_rate":"0"}"#;
        let liquidation = serde_json::from_str::<SpotMarginLiquidation>(without_call).unwrap();
        assert_eq!(liquidation.margin_call_level(), None);
        assert_eq!(serde_json::to_string(&liquidation).unwrap(), without_call);
    }

    #[test]
    fn a_liquidation_out_of_range_is_refused_by_its_constructors_and_by_deserialising() {
        assert_eq!(
            SpotMarginLiquidation::new(dec!(0.99), dec!(0)),
            Err(SpotMarginConfigError::LiquidationLevelBelowOne(dec!(0.99)))
        );
        assert!(SpotMarginLiquidation::new(dec!(1), dec!(0)).is_ok());
        for fee_rate in [dec!(-0.01), dec!(1)] {
            assert_eq!(
                SpotMarginLiquidation::new(dec!(1.1), fee_rate),
                Err(SpotMarginConfigError::LiquidationFeeOutOfRange(fee_rate))
            );
        }
        assert_eq!(
            SpotMarginLiquidation::new(dec!(1.1), dec!(0))
                .unwrap()
                .with_margin_call(dec!(1.1)),
            Err(SpotMarginConfigError::MarginCallNotAboveLiquidation {
                margin_call: dec!(1.1),
                level: dec!(1.1),
            })
        );

        for json in [
            r#"{"level":"0.5","fee_rate":"0"}"#,
            r#"{"level":"1.1","fee_rate":"1"}"#,
            r#"{"level":"1.1","fee_rate":"0","margin_call_level":"1"}"#,
        ] {
            assert!(
                serde_json::from_str::<SpotMarginLiquidation>(json).is_err(),
                "{json}"
            );
        }
        let within_leverage = r#"{"valuation_asset":"usdt","max_leverage":"3","margin_effect":"borrow","liquidation":{"level":"1.5","fee_rate":"0"}}"#;
        assert!(serde_json::from_str::<SpotMarginConfig>(within_leverage).is_err());
    }

    #[test]
    fn the_effect_wire_names_are_snake_case_and_the_default_borrows_nothing() {
        assert_eq!(MarginEffect::default(), MarginEffect::NoSideEffect);
        for (effect, name) in [
            (MarginEffect::NoSideEffect, r#""no_side_effect""#),
            (MarginEffect::Borrow, r#""borrow""#),
            (MarginEffect::Repay, r#""repay""#),
        ] {
            assert_eq!(serde_json::to_string(&effect).unwrap(), name);
            assert_eq!(serde_json::from_str::<MarginEffect>(name).unwrap(), effect);
        }
    }

    fn btc() -> AssetNameExchange {
        AssetNameExchange::new("btc")
    }

    fn usdt() -> AssetNameExchange {
        AssetNameExchange::new("usdt")
    }

    fn utc(text: &str) -> DateTime<Utc> {
        text.parse().unwrap()
    }

    /// Hourly interest on BTC at `annual`, on the Act365 basis.
    fn hourly_btc(annual: Decimal) -> PeriodicMarginInterest {
        PeriodicMarginInterest::new(TimeDelta::hours(1), DayCount::Act365)
            .with_rates(btc(), RateSeries::constant(annual))
    }

    #[test]
    fn a_small_loan_is_charged_one_unit_for_its_first_period() {
        // 0.00063 BTC at 0.39% a year is about 2.8e-10 an hour, rounded up to one satoshi.
        let model = hourly_btc(dec!(0.0039));

        let charge = model.charge(
            &btc(),
            dec!(0.00063),
            InterestDue::Borrow(utc("2026-10-15T01:02:33Z")),
        );

        assert_eq!(
            charge,
            Some(InterestCharge::new(dec!(0.00000001), Some(dec!(0.0039))))
        );
    }

    #[test]
    fn a_daily_rate_times_365_on_act365_charges_a_24th_of_it_an_hour_without_rounding_up() {
        // 0.01% a day on 24 BTC is 0.0024 a day, exactly 0.0001 an hour.
        let model = hourly_btc(dec!(0.0001) * dec!(365));

        let charge = model
            .charge(
                &btc(),
                dec!(24),
                InterestDue::Boundary(utc("2026-10-15T02:00:00Z")),
            )
            .unwrap();

        assert_eq!(charge.interest, dec!(0.0001));
    }

    #[test]
    fn a_charge_is_rounded_up_to_the_asset_s_own_unit() {
        let model = PeriodicMarginInterest::new(TimeDelta::hours(1), DayCount::Act365)
            .with_rates(usdt(), RateSeries::constant(dec!(0.0876)))
            .with_unit(usdt(), dec!(0.01));
        let at = InterestDue::Borrow(utc("2026-10-15T01:00:00Z"));

        // 8.76% a year is 0.001% an hour: 0.01 on 1,000, and 0.01001 on 1,001.
        let charged = |principal| model.charge(&usdt(), principal, at).unwrap().interest;
        assert_eq!(charged(dec!(1000)), dec!(0.01));
        assert_eq!(charged(dec!(1001)), dec!(0.02));
    }

    #[test]
    fn boundaries_fall_on_whole_periods_and_one_on_a_boundary_steps_to_the_next() {
        let model = hourly_btc(dec!(0.01));

        assert_eq!(
            model.next_boundary(&btc(), utc("2026-10-15T01:02:33Z")),
            Some(utc("2026-10-15T02:00:00Z"))
        );
        assert_eq!(
            model.next_boundary(&btc(), utc("2026-10-15T02:00:00Z")),
            Some(utc("2026-10-15T03:00:00Z"))
        );
    }

    #[test]
    fn at_borrow_only_has_no_boundaries_but_still_charges_the_borrow() {
        let model = hourly_btc(dec!(0.01)).with_schedule(InterestSchedule::AtBorrowOnly);
        let at = utc("2026-10-15T01:02:33Z");

        assert_eq!(model.next_boundary(&btc(), at), None);
        assert!(
            model
                .charge(&btc(), dec!(1), InterestDue::Borrow(at))
                .is_some()
        );
    }

    #[test]
    fn an_asset_without_rates_or_before_its_rates_begin_is_charged_nothing() {
        let start = utc("2026-10-15T00:00:00Z");
        let model = PeriodicMarginInterest::new(TimeDelta::hours(1), DayCount::Act365)
            .with_rates(btc(), RateSeries::new(vec![(start, dec!(0.01))]).unwrap());
        let before = InterestDue::Borrow(start - TimeDelta::seconds(1));

        assert_eq!(model.next_boundary(&usdt(), start), None);
        assert_eq!(
            model.charge(&usdt(), dec!(1), InterestDue::Borrow(start)),
            None
        );
        assert_eq!(model.charge(&btc(), dec!(1), before), None);
        assert!(
            model
                .charge(&btc(), dec!(1), InterestDue::Borrow(start))
                .is_some()
        );
    }

    #[test]
    #[should_panic(expected = "period must be positive")]
    fn a_period_that_is_not_positive_panics() {
        let _ = PeriodicMarginInterest::new(TimeDelta::zero(), DayCount::Act365);
    }

    #[test]
    #[should_panic(expected = "unit for btc must be positive")]
    fn a_unit_that_is_not_positive_panics() {
        let _ = hourly_btc(dec!(0.01)).with_unit(btc(), Decimal::ZERO);
    }

    #[test]
    #[should_panic(expected = "rate for btc must not be negative, got -0.01")]
    fn a_negative_rate_anywhere_in_the_series_panics() {
        let rates = RateSeries::new(vec![
            (utc("2026-10-01T00:00:00Z"), dec!(0.02)),
            (utc("2026-10-02T00:00:00Z"), dec!(-0.01)),
        ])
        .unwrap();
        let _ = PeriodicMarginInterest::new(TimeDelta::hours(1), DayCount::Act365)
            .with_rates(btc(), rates);
    }

    #[test]
    fn a_zero_rate_is_accepted_and_charges_nothing() {
        let model = hourly_btc(Decimal::ZERO);

        let charge = model.charge(
            &btc(),
            dec!(1),
            InterestDue::Borrow(utc("2026-10-15T01:00:00Z")),
        );

        assert_eq!(charge.map(|charge| charge.interest), Some(Decimal::ZERO));
    }
}