nautilus-model 0.56.0

Domain model for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Represents an account balance denominated in a particular currency.

use std::fmt::{Debug, Display};

use nautilus_core::correctness::{
    CorrectnessResult, CorrectnessResultExt, FAILED, check_predicate_true,
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::{
    identifiers::InstrumentId,
    types::{Currency, Money},
};

/// Represents an account balance denominated in a particular currency.
#[derive(Copy, Clone, Serialize, Deserialize)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(
        module = "nautilus_trader.core.nautilus_pyo3.model",
        frozen,
        eq,
        from_py_object
    )
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct AccountBalance {
    /// The account balance currency.
    pub currency: Currency,
    /// The total account balance.
    pub total: Money,
    /// The account balance locked (assigned to pending orders).
    pub locked: Money,
    /// The account balance free for trading.
    pub free: Money,
}

impl AccountBalance {
    /// Creates a new [`AccountBalance`] instance with correctness checking.
    ///
    /// # Errors
    ///
    /// Returns an error if `total` is not the result of `locked` + `free`.
    ///
    /// # Notes
    ///
    /// PyO3 requires a `Result` type that stacktrace can be printed for errors.
    pub fn new_checked(total: Money, locked: Money, free: Money) -> CorrectnessResult<Self> {
        check_predicate_true(
            total.currency == locked.currency,
            &format!(
                "`total` currency ({}) != `locked` currency ({})",
                total.currency, locked.currency
            ),
        )?;
        check_predicate_true(
            total.currency == free.currency,
            &format!(
                "`total` currency ({}) != `free` currency ({})",
                total.currency, free.currency
            ),
        )?;
        check_predicate_true(
            total == locked + free,
            &format!("`total` ({total}) - `locked` ({locked}) != `free` ({free})"),
        )?;
        Ok(Self {
            currency: total.currency,
            total,
            locked,
            free,
        })
    }

    /// Creates a new [`AccountBalance`] instance.
    ///
    /// # Panics
    ///
    /// Panics if a correctness check fails. See [`AccountBalance::new_checked`] for more details.
    #[must_use]
    pub fn new(total: Money, locked: Money, free: Money) -> Self {
        Self::new_checked(total, locked, free).expect_display(FAILED)
    }

    /// Creates a new [`AccountBalance`] from `total` and `locked` decimal amounts,
    /// deriving `free` in fixed-point so the `total == locked + free` invariant
    /// holds by construction at the currency precision.
    ///
    /// When `total` is non-negative, `locked` is clamped into `[0, total]` so
    /// a transient rounding glitch or overshoot cannot leave `free` negative.
    /// When `total` is negative (spot borrow deficit or underwater margin account),
    /// `locked` is passed through verbatim so venue-reported reserved margin is
    /// preserved and `free` carries the shortfall.
    ///
    /// # Errors
    ///
    /// Returns an error if `total` or `locked` cannot be represented at the currency
    /// precision.
    pub fn from_total_and_locked(
        total: Decimal,
        locked: Decimal,
        currency: Currency,
    ) -> CorrectnessResult<Self> {
        let total = Money::from_decimal(total, currency)?;
        let locked = Money::from_decimal(locked, currency)?;
        let locked_raw = if total.raw >= 0 {
            locked.raw.clamp(0, total.raw)
        } else {
            locked.raw
        };
        let clamped_locked = Money::from_raw(locked_raw, currency);
        let free = Money::from_raw(total.raw - clamped_locked.raw, currency);
        Ok(Self::new(total, clamped_locked, free))
    }

    /// Creates a new [`AccountBalance`] from `total` and `free` decimal amounts,
    /// deriving `locked` in fixed-point so the `total == locked + free` invariant
    /// holds by construction at the currency precision.
    ///
    /// When `total` is non-negative, `free` is clamped into `[0, total]` so
    /// a transient PnL overshoot cannot leave `locked` negative. When `total` is
    /// negative, `free` is passed through verbatim so the venue-reported available
    /// amount is preserved and `locked` carries the difference.
    ///
    /// # Errors
    ///
    /// Returns an error if `total` or `free` cannot be represented at the currency
    /// precision.
    pub fn from_total_and_free(
        total: Decimal,
        free: Decimal,
        currency: Currency,
    ) -> CorrectnessResult<Self> {
        let total = Money::from_decimal(total, currency)?;
        let free = Money::from_decimal(free, currency)?;
        let free_raw = if total.raw >= 0 {
            free.raw.clamp(0, total.raw)
        } else {
            free.raw
        };
        let clamped_free = Money::from_raw(free_raw, currency);
        let locked = Money::from_raw(total.raw - clamped_free.raw, currency);
        Ok(Self::new(total, locked, clamped_free))
    }
}

impl PartialEq for AccountBalance {
    fn eq(&self, other: &Self) -> bool {
        self.total == other.total && self.locked == other.locked && self.free == other.free
    }
}

impl Debug for AccountBalance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}(total={}, locked={}, free={})",
            stringify!(AccountBalance),
            self.total,
            self.locked,
            self.free,
        )
    }
}

impl Display for AccountBalance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

#[derive(Copy, Clone, Serialize, Deserialize)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(
        module = "nautilus_trader.core.nautilus_pyo3.model",
        frozen,
        eq,
        from_py_object
    )
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
/// Represents a margin balance.
///
/// Margin entries have two mutually exclusive scopes:
///
/// - Per-instrument: `instrument_id = Some(id)`. Used for isolated margin and
///   for calculated margin in backtest mode where each instrument carries its
///   own reserve.
/// - Account-wide (cross margin): `instrument_id = None`. Used for venues that
///   report a single aggregate margin per collateral currency (most derivatives
///   venues in cross-margin mode).
pub struct MarginBalance {
    pub initial: Money,
    pub maintenance: Money,
    pub currency: Currency,
    pub instrument_id: Option<InstrumentId>,
}

impl MarginBalance {
    /// Creates a new [`MarginBalance`] instance with correctness checking.
    ///
    /// # Errors
    ///
    /// Returns an error if `initial` and `maintenance` have different currencies.
    ///
    /// # Notes
    ///
    /// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
    pub fn new_checked(
        initial: Money,
        maintenance: Money,
        instrument_id: Option<InstrumentId>,
    ) -> CorrectnessResult<Self> {
        check_predicate_true(
            initial.currency == maintenance.currency,
            &format!(
                "`initial` currency ({}) != `maintenance` currency ({})",
                initial.currency, maintenance.currency
            ),
        )?;
        Ok(Self {
            initial,
            maintenance,
            currency: initial.currency,
            instrument_id,
        })
    }

    /// Creates a new [`MarginBalance`] instance.
    ///
    /// # Panics
    ///
    /// Panics if `initial` and `maintenance` have different currencies.
    #[must_use]
    pub fn new(initial: Money, maintenance: Money, instrument_id: Option<InstrumentId>) -> Self {
        Self::new_checked(initial, maintenance, instrument_id).expect_display(FAILED)
    }
}

impl PartialEq for MarginBalance {
    fn eq(&self, other: &Self) -> bool {
        self.initial == other.initial
            && self.maintenance == other.maintenance
            && self.instrument_id == other.instrument_id
    }
}

impl Debug for MarginBalance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.instrument_id {
            Some(id) => write!(
                f,
                "{}(initial={}, maintenance={}, instrument_id={})",
                stringify!(MarginBalance),
                self.initial,
                self.maintenance,
                id,
            ),
            None => write!(
                f,
                "{}(initial={}, maintenance={}, currency={})",
                stringify!(MarginBalance),
                self.initial,
                self.maintenance,
                self.currency,
            ),
        }
    }
}

impl Display for MarginBalance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;
    use rust_decimal::Decimal;
    use rust_decimal_macros::dec;

    use crate::{
        identifiers::InstrumentId,
        types::{
            AccountBalance, Currency, MarginBalance, Money,
            stubs::{stub_account_balance, stub_margin_balance},
        },
    };

    #[rstest]
    fn test_account_balance_equality() {
        let account_balance_1 = stub_account_balance();
        let account_balance_2 = stub_account_balance();
        assert_eq!(account_balance_1, account_balance_2);
    }

    #[rstest]
    fn test_account_balance_debug(stub_account_balance: AccountBalance) {
        let result = format!("{stub_account_balance:?}");
        let expected =
            "AccountBalance(total=1525000.00 USD, locked=25000.00 USD, free=1500000.00 USD)";
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_account_balance_display(stub_account_balance: AccountBalance) {
        let result = format!("{stub_account_balance}");
        let expected =
            "AccountBalance(total=1525000.00 USD, locked=25000.00 USD, free=1500000.00 USD)";
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_account_balance_new_checked_with_currency_mismatch_returns_error() {
        let usd = Currency::USD();
        let eur = Currency::EUR();
        let result = AccountBalance::new_checked(
            Money::new(1000.0, usd),
            Money::new(250.0, eur),
            Money::new(750.0, usd),
        );
        assert!(result.is_err());
    }

    #[rstest]
    #[should_panic(expected = "`total` currency (USD) != `locked` currency (EUR)")]
    fn test_account_balance_new_with_currency_mismatch_panics() {
        let usd = Currency::USD();
        let eur = Currency::EUR();
        let _ = AccountBalance::new(
            Money::new(1000.0, usd),
            Money::new(250.0, eur),
            Money::new(750.0, usd),
        );
    }

    fn parse_dec(s: &str) -> Decimal {
        s.parse().unwrap()
    }

    #[rstest]
    #[case::zero_zero_usd("0", "0")]
    #[case::total_zero_positive_locked_usd("0", "5")]
    #[case::round_usd("1000", "250")]
    #[case::free_is_zero_usd("1000", "1000")]
    #[case::locked_is_zero_usd("1000", "0")]
    #[case::fractional_usd("1234.56", "789.01")]
    #[case::fractional_btc("10.12345678", "2.87654321")]
    #[case::small_btc("0.00000001", "0")]
    #[case::large_usd("1000000000.00", "123.45")]
    #[case::drift_af_btc("10.000000035", "10.000000031")]
    #[case::drift_locked_over_precision_btc("10.000000034999", "0.000000004999")]
    #[case::locked_above_total_usd("100", "150")]
    #[case::locked_above_total_btc("1.50000000", "5.00000000")]
    #[case::negative_locked_usd("100", "-5")]
    #[case::negative_locked_btc("0.50000000", "-0.00000001")]
    #[case::negative_total_with_reserved("-10", "5")]
    #[case::negative_total_negative_locked("-10", "-5")]
    #[case::deep_underwater_with_reserved("-100", "50")]
    fn test_from_total_and_locked_preserves_invariant(
        #[case] total_str: &str,
        #[case] locked_str: &str,
    ) {
        for currency in [Currency::USD(), Currency::BTC()] {
            let total = parse_dec(total_str);
            let locked = parse_dec(locked_str);
            let balance = AccountBalance::from_total_and_locked(total, locked, currency).unwrap();

            assert_eq!(
                balance.total.raw,
                balance.locked.raw + balance.free.raw,
                "invariant violated for total={total}, locked={locked}, currency={}",
                currency.code,
            );
            // When total is non-negative, locked must also be non-negative; when total is
            // negative the helper passes venue values through so locked may be negative too.
            if balance.total.raw >= 0 {
                assert!(
                    balance.locked.raw >= 0,
                    "locked must be non-negative for non-negative total (found raw={})",
                    balance.locked.raw,
                );
            }
            assert_eq!(balance.total.currency, currency);
            assert_eq!(balance.locked.currency, currency);
            assert_eq!(balance.free.currency, currency);
        }
    }

    #[rstest]
    #[case::zero_zero_usd("0", "0")]
    #[case::round_usd("1000", "750")]
    #[case::free_equals_total_usd("1000", "1000")]
    #[case::free_is_zero_usd("1000", "0")]
    #[case::fractional_usd("1234.56", "444.55")]
    #[case::fractional_btc("10.12345678", "7.24691356")]
    #[case::drift_over_precision_btc("10.000000034999", "9.999999994999")]
    #[case::free_above_total_usd("100", "120")]
    #[case::free_above_total_btc("0.50000000", "0.99999999")]
    #[case::negative_free_usd("100", "-5")]
    #[case::negative_total_usd("-10", "0")]
    #[case::negative_total_positive_free("-10", "5")]
    fn test_from_total_and_free_preserves_invariant(
        #[case] total_str: &str,
        #[case] free_str: &str,
    ) {
        for currency in [Currency::USD(), Currency::BTC()] {
            let total = parse_dec(total_str);
            let free = parse_dec(free_str);
            let balance = AccountBalance::from_total_and_free(total, free, currency).unwrap();

            assert_eq!(
                balance.total.raw,
                balance.locked.raw + balance.free.raw,
                "invariant violated for total={total}, free={free}, currency={}",
                currency.code,
            );

            if balance.total.raw >= 0 {
                assert!(
                    balance.free.raw >= 0,
                    "free must be non-negative for non-negative total (found raw={})",
                    balance.free.raw,
                );
            }
            assert_eq!(balance.total.currency, currency);
            assert_eq!(balance.locked.currency, currency);
            assert_eq!(balance.free.currency, currency);
        }
    }

    #[rstest]
    #[case::usd_basic(dec!(1000.00), dec!(250.00), dec!(1000.00), dec!(250.00), dec!(750.00))]
    #[case::usd_all_free(dec!(500.00), dec!(0.00), dec!(500.00), dec!(0.00), dec!(500.00))]
    #[case::usd_all_locked(dec!(500.00), dec!(500.00), dec!(500.00), dec!(500.00), dec!(0.00))]
    #[case::usd_clamp_above(dec!(100.00), dec!(150.00), dec!(100.00), dec!(100.00), dec!(0.00))]
    #[case::usd_clamp_negative(dec!(100.00), dec!(-5.00), dec!(100.00), dec!(0.00), dec!(100.00))]
    fn test_from_total_and_locked_exact_usd(
        #[case] total_in: Decimal,
        #[case] locked_in: Decimal,
        #[case] expected_total: Decimal,
        #[case] expected_locked: Decimal,
        #[case] expected_free: Decimal,
    ) {
        let usd = Currency::USD();
        let balance = AccountBalance::from_total_and_locked(total_in, locked_in, usd).unwrap();

        assert_eq!(
            balance.total,
            Money::from_decimal(expected_total, usd).unwrap()
        );
        assert_eq!(
            balance.locked,
            Money::from_decimal(expected_locked, usd).unwrap()
        );
        assert_eq!(
            balance.free,
            Money::from_decimal(expected_free, usd).unwrap()
        );
    }

    #[rstest]
    #[case::usd_basic(dec!(1000.00), dec!(750.00), dec!(1000.00), dec!(250.00), dec!(750.00))]
    #[case::usd_all_free(dec!(500.00), dec!(500.00), dec!(500.00), dec!(0.00), dec!(500.00))]
    #[case::usd_all_locked(dec!(500.00), dec!(0.00), dec!(500.00), dec!(500.00), dec!(0.00))]
    #[case::usd_clamp_above(dec!(100.00), dec!(120.00), dec!(100.00), dec!(0.00), dec!(100.00))]
    #[case::usd_clamp_negative(dec!(100.00), dec!(-5.00), dec!(100.00), dec!(100.00), dec!(0.00))]
    fn test_from_total_and_free_exact_usd(
        #[case] total_in: Decimal,
        #[case] free_in: Decimal,
        #[case] expected_total: Decimal,
        #[case] expected_locked: Decimal,
        #[case] expected_free: Decimal,
    ) {
        let usd = Currency::USD();
        let balance = AccountBalance::from_total_and_free(total_in, free_in, usd).unwrap();

        assert_eq!(
            balance.total,
            Money::from_decimal(expected_total, usd).unwrap()
        );
        assert_eq!(
            balance.locked,
            Money::from_decimal(expected_locked, usd).unwrap()
        );
        assert_eq!(
            balance.free,
            Money::from_decimal(expected_free, usd).unwrap()
        );
    }

    // Reproducer for issue #3867: three independent `Money::new` calls at currency
    // precision 8 rounded `(total, locked=amount-af, free=af)` to `1_000_000_003`,
    // `1_000_000_000`, `4` respectively, violating `total == locked + free`.
    #[rstest]
    fn test_from_total_and_locked_issue_3867_drift() {
        let btc = Currency::BTC();
        let af = parse_dec("0.000000035");
        let amount = parse_dec("10") + af;
        let locked = amount - af;

        let balance = AccountBalance::from_total_and_locked(amount, locked, btc).unwrap();

        assert_eq!(balance.total.raw, balance.locked.raw + balance.free.raw);
    }

    #[rstest]
    #[case(dec!(0), dec!(100))]
    #[case(dec!(1), dec!(1000000))]
    #[case(dec!(500), dec!(500000))]
    fn test_from_total_and_locked_non_negative_total_never_leaves_free_negative(
        #[case] total: Decimal,
        #[case] locked: Decimal,
    ) {
        let usd = Currency::USD();
        let balance = AccountBalance::from_total_and_locked(total, locked, usd).unwrap();
        assert!(
            balance.free.raw >= 0,
            "free went negative: total={total}, locked={locked}"
        );
        assert_eq!(balance.total.raw, balance.locked.raw + balance.free.raw);
    }

    #[rstest]
    #[case(dec!(1000.00), dec!(250.00), dec!(750.00))]
    #[case(dec!(0.00), dec!(0.00), dec!(0.00))]
    #[case(dec!(500.00), dec!(500.00), dec!(0.00))]
    #[case(dec!(500.00), dec!(0.00), dec!(500.00))]
    fn test_locked_and_free_forms_agree_when_consistent(
        #[case] total: Decimal,
        #[case] locked: Decimal,
        #[case] free: Decimal,
    ) {
        let usd = Currency::USD();
        let from_locked = AccountBalance::from_total_and_locked(total, locked, usd).unwrap();
        let from_free = AccountBalance::from_total_and_free(total, free, usd).unwrap();
        assert_eq!(from_locked, from_free);
    }

    #[rstest]
    #[case::borrow_deficit(dec!(-100), dec!(50), dec!(-100), dec!(50), dec!(-150))]
    #[case::underwater_no_reserve(dec!(-10), dec!(0), dec!(-10), dec!(0), dec!(-10))]
    #[case::negative_locked_passed_through(dec!(-10), dec!(-5), dec!(-10), dec!(-5), dec!(-5))]
    fn test_from_total_and_locked_preserves_reserved_on_negative_total(
        #[case] total_in: Decimal,
        #[case] locked_in: Decimal,
        #[case] expected_total: Decimal,
        #[case] expected_locked: Decimal,
        #[case] expected_free: Decimal,
    ) {
        let usd = Currency::USD();
        let balance = AccountBalance::from_total_and_locked(total_in, locked_in, usd).unwrap();

        assert_eq!(
            balance.total,
            Money::from_decimal(expected_total, usd).unwrap()
        );
        assert_eq!(
            balance.locked,
            Money::from_decimal(expected_locked, usd).unwrap()
        );
        assert_eq!(
            balance.free,
            Money::from_decimal(expected_free, usd).unwrap()
        );
        assert_eq!(balance.total.raw, balance.locked.raw + balance.free.raw);
    }

    #[rstest]
    #[case::available_below_total(dec!(-100), dec!(-150), dec!(-100), dec!(50), dec!(-150))]
    #[case::available_zero_preserved(dec!(-100), dec!(0), dec!(-100), dec!(-100), dec!(0))]
    fn test_from_total_and_free_preserves_available_on_negative_total(
        #[case] total_in: Decimal,
        #[case] free_in: Decimal,
        #[case] expected_total: Decimal,
        #[case] expected_locked: Decimal,
        #[case] expected_free: Decimal,
    ) {
        let usd = Currency::USD();
        let balance = AccountBalance::from_total_and_free(total_in, free_in, usd).unwrap();

        assert_eq!(
            balance.total,
            Money::from_decimal(expected_total, usd).unwrap()
        );
        assert_eq!(
            balance.locked,
            Money::from_decimal(expected_locked, usd).unwrap()
        );
        assert_eq!(
            balance.free,
            Money::from_decimal(expected_free, usd).unwrap()
        );
        assert_eq!(balance.total.raw, balance.locked.raw + balance.free.raw);
    }

    #[rstest]
    fn test_from_total_and_locked_invalid_decimal_returns_error() {
        let btc = Currency::BTC();
        // 28 leading digits scaled to BTC precision 8 exceeds MoneyRaw bounds, so
        // `Money::from_decimal` rejects it and the error propagates.
        let too_large: Decimal = "79228162514264337593543950335".parse().unwrap();
        let result = AccountBalance::from_total_and_locked(too_large, dec!(0), btc);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_margin_balance_equality() {
        let margin_balance_1 = stub_margin_balance();
        let margin_balance_2 = stub_margin_balance();
        assert_eq!(margin_balance_1, margin_balance_2);
    }

    #[rstest]
    fn test_margin_balance_debug(stub_margin_balance: MarginBalance) {
        let display = format!("{stub_margin_balance:?}");
        assert_eq!(
            "MarginBalance(initial=5000.00 USD, maintenance=20000.00 USD, instrument_id=BTCUSDT.COINBASE)",
            display
        );
    }

    #[rstest]
    fn test_margin_balance_display(stub_margin_balance: MarginBalance) {
        let display = format!("{stub_margin_balance}");
        assert_eq!(
            "MarginBalance(initial=5000.00 USD, maintenance=20000.00 USD, instrument_id=BTCUSDT.COINBASE)",
            display
        );
    }

    #[rstest]
    fn test_margin_balance_new_checked_with_currency_mismatch_returns_error() {
        let usd = Currency::USD();
        let eur = Currency::EUR();
        let instrument_id = InstrumentId::from("BTCUSDT.COINBASE");
        let result = MarginBalance::new_checked(
            Money::new(5000.0, usd),
            Money::new(20000.0, eur),
            Some(instrument_id),
        );
        assert!(result.is_err());
    }

    #[rstest]
    #[should_panic(expected = "`initial` currency (USD) != `maintenance` currency (EUR)")]
    fn test_margin_balance_new_with_currency_mismatch_panics() {
        let usd = Currency::USD();
        let eur = Currency::EUR();
        let instrument_id = InstrumentId::from("BTCUSDT.COINBASE");
        let _ = MarginBalance::new(
            Money::new(5000.0, usd),
            Money::new(20000.0, eur),
            Some(instrument_id),
        );
    }

    #[rstest]
    fn test_margin_balance_account_scope_display() {
        let usd = Currency::USD();
        let balance = MarginBalance::new(Money::new(500.0, usd), Money::new(200.0, usd), None);
        assert_eq!(
            "MarginBalance(initial=500.00 USD, maintenance=200.00 USD, currency=USD)",
            format!("{balance}")
        );
    }
}