nautilus-bitmex 0.55.0

BitMEX exchange integration adapter 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
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
// -------------------------------------------------------------------------------------------------
//  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.
// -------------------------------------------------------------------------------------------------

//! Shared parsing helpers that transform BitMEX payloads into Nautilus types.

use std::{borrow::Cow, str::FromStr};

use chrono::{DateTime, Utc};
use nautilus_core::{Params, nanos::UnixNanos, uuid::UUID4};
use nautilus_model::{
    data::bar::BarType,
    enums::{AccountType, AggressorSide, CurrencyType, LiquiditySide, PositionSide, TriggerType},
    events::AccountState,
    identifiers::{AccountId, InstrumentId, Symbol},
    instruments::{Instrument, InstrumentAny},
    types::{
        AccountBalance, Currency, MarginBalance, Money, Price, Quantity,
        quantity::{QUANTITY_RAW_MAX, QuantityRaw},
    },
};
use rust_decimal::{Decimal, RoundingStrategy, prelude::ToPrimitive};
use ustr::Ustr;

use crate::{
    common::{
        consts::BITMEX_VENUE,
        enums::{BitmexExecInstruction, BitmexLiquidityIndicator, BitmexPegPriceType, BitmexSide},
    },
    websocket::messages::BitmexMarginMsg,
};

/// Strip NautilusTrader identifier from BitMEX rejection/cancellation reasons.
///
/// BitMEX appends our `text` field as `\nNautilusTrader` to their messages.
#[must_use]
pub fn clean_reason(reason: &str) -> String {
    reason.replace("\nNautilusTrader", "").trim().to_string()
}

/// Extracts the trigger type from BitMEX exec instructions.
#[must_use]
pub fn extract_trigger_type(exec_inst: Option<&Vec<BitmexExecInstruction>>) -> TriggerType {
    if let Some(exec_insts) = exec_inst {
        if exec_insts.contains(&BitmexExecInstruction::MarkPrice) {
            TriggerType::MarkPrice
        } else if exec_insts.contains(&BitmexExecInstruction::IndexPrice) {
            TriggerType::IndexPrice
        } else if exec_insts.contains(&BitmexExecInstruction::LastPrice) {
            TriggerType::LastPrice
        } else {
            TriggerType::Default
        }
    } else {
        TriggerType::Default
    }
}

/// Parses a Nautilus instrument ID from the given BitMEX `symbol` value.
#[must_use]
pub fn parse_instrument_id(symbol: Ustr) -> InstrumentId {
    InstrumentId::new(Symbol::from_ustr_unchecked(symbol), *BITMEX_VENUE)
}

/// Safely converts a `Quantity` into the integer units expected by the BitMEX REST API.
///
/// The API expects whole-number "contract" counts which vary per instrument. We always use the
/// instrument size increment (sourced from BitMEX `underlyingToPositionMultiplier`) to translate
/// Nautilus quantities back to venue units, so each instrument can have its own contract multiplier.
/// Values are rounded to the nearest whole contract (midpoint rounds away from zero) and clamped
/// to `u32::MAX` when necessary.
#[must_use]
pub fn quantity_to_u32(quantity: &Quantity, instrument: &InstrumentAny) -> u32 {
    let size_increment = instrument.size_increment();
    let step_decimal = size_increment.as_decimal();

    if step_decimal.is_zero() {
        let value = quantity.as_f64();
        if value > u32::MAX as f64 {
            log::warn!("Quantity {value} exceeds u32::MAX without instrument increment, clamping",);
            return u32::MAX;
        }
        return value.max(0.0) as u32;
    }

    let units_decimal = quantity.as_decimal() / step_decimal;
    let rounded_units =
        units_decimal.round_dp_with_strategy(0, RoundingStrategy::MidpointAwayFromZero);

    match rounded_units.to_u128() {
        Some(units) if units <= u32::MAX as u128 => units as u32,
        Some(units) => {
            log::warn!(
                "Quantity {} converts to {units} contracts which exceeds u32::MAX, clamping",
                quantity.as_f64(),
            );
            u32::MAX
        }
        None => {
            log::warn!(
                "Failed to convert quantity {} to venue units, defaulting to 0",
                quantity.as_f64(),
            );
            0
        }
    }
}

/// Converts a BitMEX contracts value into a Nautilus quantity using instrument precision.
#[must_use]
pub fn parse_contracts_quantity(value: u64, instrument: &InstrumentAny) -> Quantity {
    let size_increment = instrument.size_increment();
    let precision = instrument.size_precision();

    let increment_raw: QuantityRaw = (&size_increment).into();
    let value_raw = QuantityRaw::from(value);

    let mut raw = increment_raw.saturating_mul(value_raw);
    if raw > QUANTITY_RAW_MAX {
        log::warn!("Quantity value {value} exceeds QUANTITY_RAW_MAX {QUANTITY_RAW_MAX}, clamping",);
        raw = QUANTITY_RAW_MAX;
    }

    Quantity::from_raw(raw, precision)
}

/// Converts the BitMEX `underlyingToPositionMultiplier` into a normalized contract size and
/// size increment for Nautilus instruments.
///
/// The returned decimal retains BitMEX precision (clamped to `max_scale`) so downstream
/// quantity conversions stay lossless.
///
/// # Errors
///
/// Returns an error when the multiplier cannot be represented with the configured precision.
pub fn derive_contract_decimal_and_increment(
    multiplier: Option<f64>,
    max_scale: u32,
) -> anyhow::Result<(Decimal, Quantity)> {
    let raw_multiplier = multiplier.unwrap_or(1.0);
    let contract_size = if raw_multiplier > 0.0 {
        1.0 / raw_multiplier
    } else {
        1.0
    };

    let mut contract_decimal = Decimal::from_str(&contract_size.to_string())
        .map_err(|_| anyhow::anyhow!("Invalid contract size {contract_size}"))?;

    if contract_decimal.scale() > max_scale {
        contract_decimal = contract_decimal
            .round_dp_with_strategy(max_scale, RoundingStrategy::MidpointAwayFromZero);
    }
    contract_decimal = contract_decimal.normalize();
    let contract_precision = contract_decimal.scale() as u8;
    let size_increment = Quantity::from_decimal_dp(contract_decimal, contract_precision)?;

    Ok((contract_decimal, size_increment))
}

/// Converts an optional contract-count field (e.g. `lotSize`, `maxOrderQty`) into a Nautilus
/// quantity using the previously derived contract size.
///
/// # Errors
///
/// Returns an error when the raw value cannot be represented with the available precision.
pub fn convert_contract_quantity(
    value: Option<f64>,
    contract_decimal: Decimal,
    max_scale: u32,
    field_name: &str,
) -> anyhow::Result<Option<Quantity>> {
    value
        .map(|raw| {
            let mut decimal = Decimal::from_str(&raw.to_string())
                .map_err(|_| anyhow::anyhow!("Invalid {field_name} value"))?
                * contract_decimal;
            let scale = decimal.scale();
            if scale > max_scale {
                decimal = decimal
                    .round_dp_with_strategy(max_scale, RoundingStrategy::MidpointAwayFromZero);
            }
            let decimal = decimal.normalize();
            let precision = decimal.scale() as u8;
            Quantity::from_decimal_dp(decimal, precision)
        })
        .transpose()
}

/// Converts a signed BitMEX contracts value into a Nautilus quantity using instrument precision.
#[must_use]
pub fn parse_signed_contracts_quantity(value: i64, instrument: &InstrumentAny) -> Quantity {
    let abs_value = value.checked_abs().unwrap_or_else(|| {
        log::warn!("Quantity value {value} overflowed when taking absolute value");
        i64::MAX
    }) as u64;
    parse_contracts_quantity(abs_value, instrument)
}

/// Converts a fractional size into a quantity honoring the instrument precision.
#[must_use]
pub fn parse_fractional_quantity(value: f64, instrument: &InstrumentAny) -> Quantity {
    if value < 0.0 {
        log::warn!("Received negative fractional quantity {value}, defaulting to 0.0");
        return instrument.make_qty(0.0, None);
    }

    instrument.try_make_qty(value, None).unwrap_or_else(|e| {
        log::warn!(
            "Failed to convert fractional quantity {value} with precision {}: {e}",
            instrument.size_precision(),
        );
        instrument.make_qty(0.0, None)
    })
}

/// Normalizes the OHLC values reported by BitMEX trade bins to ensure `high >= max(open, close)`
/// and `low <= min(open, close)`.
///
/// # Panics
///
/// Panics if the price array is empty. This should never occur because the caller always supplies
/// four price values (open/high/low/close).
#[must_use]
pub fn normalize_trade_bin_prices(
    open: Price,
    mut high: Price,
    mut low: Price,
    close: Price,
    symbol: &Ustr,
    bar_type: Option<&BarType>,
) -> (Price, Price, Price, Price) {
    let price_extremes = [open, high, low, close];
    let max_price = *price_extremes
        .iter()
        .max()
        .expect("Price array contains values");
    let min_price = *price_extremes
        .iter()
        .min()
        .expect("Price array contains values");

    if high < max_price || low > min_price {
        match bar_type {
            Some(bt) => {
                log::warn!("Adjusting BitMEX trade bin extremes: symbol={symbol}, bar_type={bt:?}");
            }
            None => log::warn!("Adjusting BitMEX trade bin extremes: symbol={symbol}"),
        }
        high = max_price;
        low = min_price;
    }

    (open, high, low, close)
}

/// Normalizes the volume reported by BitMEX trade bins, defaulting to zero when the exchange
/// returns negative or missing values.
#[must_use]
pub fn normalize_trade_bin_volume(volume: Option<i64>, symbol: &Ustr) -> u64 {
    match volume {
        Some(v) if v >= 0 => v as u64,
        Some(v) => {
            log::warn!("Received negative volume in BitMEX trade bin: symbol={symbol}, volume={v}");
            0
        }
        None => {
            log::warn!("Trade bin missing volume, defaulting to 0: symbol={symbol}");
            0
        }
    }
}

/// Parses the given datetime (UTC) into a `UnixNanos` timestamp.
/// If `value` is `None`, then defaults to the UNIX epoch (0 nanoseconds).
///
/// Returns epoch (0) for invalid timestamps that cannot be converted to nanoseconds.
#[must_use]
pub fn parse_optional_datetime_to_unix_nanos(
    value: &Option<DateTime<Utc>>,
    field: &str,
) -> UnixNanos {
    value
        .map(|dt| {
            UnixNanos::from(dt.timestamp_nanos_opt().unwrap_or_else(|| {
                log::error!("Invalid timestamp - out of range: field={field}, timestamp={dt:?}");
                0
            }) as u64)
        })
        .unwrap_or_default()
}

/// Maps an optional BitMEX side to the corresponding Nautilus aggressor side.
#[must_use]
pub const fn parse_aggressor_side(side: &Option<BitmexSide>) -> AggressorSide {
    match side {
        Some(BitmexSide::Buy) => AggressorSide::Buyer,
        Some(BitmexSide::Sell) => AggressorSide::Seller,
        None => AggressorSide::NoAggressor,
    }
}

/// Maps BitMEX liquidity indicators onto Nautilus liquidity sides.
#[must_use]
pub fn parse_liquidity_side(liquidity: &Option<BitmexLiquidityIndicator>) -> LiquiditySide {
    liquidity.map_or(LiquiditySide::NoLiquiditySide, std::convert::Into::into)
}

/// Derives a Nautilus position side from the BitMEX `currentQty` value.
#[must_use]
pub const fn parse_position_side(current_qty: Option<i64>) -> PositionSide {
    match current_qty {
        Some(qty) if qty > 0 => PositionSide::Long,
        Some(qty) if qty < 0 => PositionSide::Short,
        _ => PositionSide::Flat,
    }
}

/// Maps BitMEX currency codes to standard Nautilus currency codes.
///
/// BitMEX uses some non-standard currency codes:
/// - "XBt" -> "XBT" (Bitcoin)
/// - "USDt" -> "USDT" (Tether)
/// - "LAMp" -> "USDT" (Test currency, mapped to USDT)
/// - "RLUSd" -> "RLUSD" (Ripple USD stablecoin)
/// - "MAMUSd" -> "MAMUSD" (Unknown stablecoin)
///
/// For other currencies, converts to uppercase.
#[must_use]
pub fn map_bitmex_currency(bitmex_currency: &str) -> Cow<'static, str> {
    match bitmex_currency {
        "XBt" => Cow::Borrowed("XBT"),
        "USDt" | "LAMp" => Cow::Borrowed("USDT"), // LAMp is test currency
        "RLUSd" => Cow::Borrowed("RLUSD"),
        "MAMUSd" => Cow::Borrowed("MAMUSD"),
        other => Cow::Owned(other.to_uppercase()),
    }
}

/// Returns the Decimal divisor for converting BitMEX raw integer units to standard units.
#[must_use]
pub fn bitmex_currency_divisor(bitmex_currency: &str) -> Decimal {
    match bitmex_currency {
        "XBt" => Decimal::from(100_000_000),
        "USDt" | "LAMp" | "MAMUSd" | "RLUSd" => Decimal::from(1_000_000),
        _ => Decimal::ONE,
    }
}

/// Parses a BitMEX margin message into a Nautilus account balance.
pub fn parse_account_balance(margin: &BitmexMarginMsg) -> AccountBalance {
    log::debug!(
        "Parsing margin: currency={}, wallet_balance={:?}, available_margin={:?}, init_margin={:?}, maint_margin={:?}",
        margin.currency,
        margin.wallet_balance,
        margin.available_margin,
        margin.init_margin,
        margin.maint_margin,
    );

    let currency_str = map_bitmex_currency(&margin.currency);

    let currency = match Currency::try_from_str(&currency_str) {
        Some(c) => c,
        None => {
            // Create a default crypto currency for unknown codes to avoid disrupting flows
            log::warn!(
                "Unknown currency '{currency_str}' in margin message, creating default crypto currency"
            );
            let currency = Currency::new(&currency_str, 8, 0, &currency_str, CurrencyType::Crypto);
            if let Err(e) = Currency::register(currency, false) {
                log::error!("Failed to register currency '{currency_str}': {e}");
            }
            currency
        }
    };

    // BitMEX returns values in satoshis for BTC (XBt) or microunits for stablecoins
    let divisor = match margin.currency.as_str() {
        "XBt" => 100_000_000.0,                              // Satoshis to BTC
        "USDt" | "LAMp" | "MAMUSd" | "RLUSd" => 1_000_000.0, // Microunits to units
        _ => 1.0,
    };

    // Wallet balance is the actual asset amount
    let total = if let Some(wallet_balance) = margin.wallet_balance {
        Money::new(wallet_balance as f64 / divisor, currency)
    } else if let Some(margin_balance) = margin.margin_balance {
        Money::new(margin_balance as f64 / divisor, currency)
    } else if let Some(available) = margin.available_margin {
        // Fallback when only available_margin is provided
        Money::new(available as f64 / divisor, currency)
    } else {
        Money::new(0.0, currency)
    };

    // Calculate how much is locked for margin requirements
    let margin_used = if let Some(init_margin) = margin.init_margin {
        Money::new(init_margin as f64 / divisor, currency)
    } else {
        Money::new(0.0, currency)
    };

    // Free balance: prefer withdrawable_margin, then available_margin, then calculate
    let free = if let Some(withdrawable) = margin.withdrawable_margin {
        Money::new(withdrawable as f64 / divisor, currency)
    } else if let Some(available) = margin.available_margin {
        // Available margin already accounts for orders and positions
        let available_money = Money::new(available as f64 / divisor, currency);
        // Ensure it doesn't exceed total (can happen with unrealized PnL)
        if available_money > total {
            total
        } else {
            available_money
        }
    } else {
        // Fallback: free = total - init_margin
        let calculated_free = total - margin_used;
        if calculated_free < Money::new(0.0, currency) {
            Money::new(0.0, currency)
        } else {
            calculated_free
        }
    };

    // Locked is what's being used for margin
    let locked = total - free;

    AccountBalance::new(total, locked, free)
}

/// Parses a BitMEX margin message into a Nautilus account state.
///
/// # Errors
///
/// Returns an error if the margin data cannot be parsed into valid balance values.
pub fn parse_account_state(
    margin: &BitmexMarginMsg,
    account_id: AccountId,
    ts_init: UnixNanos,
) -> anyhow::Result<AccountState> {
    let balance = parse_account_balance(margin);
    let balances = vec![balance];

    let currency_str = map_bitmex_currency(margin.currency.as_str());
    let currency = balance.total.currency;
    let mut margins = Vec::new();

    let divisor = bitmex_currency_divisor(margin.currency.as_str());
    let initial_dec = Decimal::from(margin.init_margin.unwrap_or(0).max(0)) / divisor;
    let maintenance_dec = Decimal::from(margin.maint_margin.unwrap_or(0).max(0)) / divisor;

    if !initial_dec.is_zero() || !maintenance_dec.is_zero() {
        let margin_instrument_id = InstrumentId::new(
            Symbol::from_str_unchecked(format!("ACCOUNT-{currency_str}")),
            *BITMEX_VENUE,
        );
        margins.push(MarginBalance::new(
            Money::from_decimal(initial_dec, currency).unwrap_or_else(|_| Money::zero(currency)),
            Money::from_decimal(maintenance_dec, currency)
                .unwrap_or_else(|_| Money::zero(currency)),
            margin_instrument_id,
        ));
    }

    let account_type = AccountType::Margin;
    let is_reported = true;
    let event_id = UUID4::new();
    let ts_event =
        UnixNanos::from(margin.timestamp.timestamp_nanos_opt().unwrap_or_default() as u64);

    Ok(AccountState::new(
        account_id,
        account_type,
        balances,
        margins,
        is_reported,
        event_id,
        ts_event,
        ts_init,
        None,
    ))
}

/// Extracts the peg price type from order command parameters.
///
/// # Errors
///
/// Returns an error if the value is present but not a valid `BitmexPegPriceType`.
pub fn parse_peg_price_type(params: Option<&Params>) -> anyhow::Result<Option<BitmexPegPriceType>> {
    let value = params.and_then(|p| p.get_str("peg_price_type"));
    match value {
        Some(s) => BitmexPegPriceType::from_str(s)
            .map(Some)
            .map_err(|_| anyhow::anyhow!("Invalid peg_price_type: {s}")),
        None => Ok(None),
    }
}

/// Extracts the peg offset value from order command parameters.
///
/// # Errors
///
/// Returns an error if the value is present but not a valid `f64`.
pub fn parse_peg_offset_value(params: Option<&Params>) -> anyhow::Result<Option<f64>> {
    let value = params.and_then(|p| p.get_str("peg_offset_value"));
    match value {
        Some(s) => s
            .parse::<f64>()
            .map(Some)
            .map_err(|_| anyhow::anyhow!("Invalid peg_offset_value: {s}")),
        None => Ok(None),
    }
}

#[cfg(test)]
mod tests {
    use chrono::TimeZone;
    use nautilus_model::{instruments::CurrencyPair, types::fixed::FIXED_PRECISION};
    use rstest::rstest;
    use ustr::Ustr;

    use super::*;

    #[rstest]
    fn test_clean_reason_strips_nautilus_trader() {
        assert_eq!(
            clean_reason(
                "Canceled: Order had execInst of ParticipateDoNotInitiate\nNautilusTrader"
            ),
            "Canceled: Order had execInst of ParticipateDoNotInitiate"
        );

        assert_eq!(clean_reason("Some error\nNautilusTrader"), "Some error");
        assert_eq!(
            clean_reason("Multiple lines\nSome content\nNautilusTrader"),
            "Multiple lines\nSome content"
        );
        assert_eq!(clean_reason("No identifier here"), "No identifier here");
        assert_eq!(clean_reason("  \nNautilusTrader  "), "");
    }

    fn make_test_spot_instrument(size_increment: f64, size_precision: u8) -> InstrumentAny {
        let instrument_id = InstrumentId::from("SOLUSDT.BITMEX");
        let raw_symbol = Symbol::from("SOLUSDT");
        let base_currency = Currency::from("SOL");
        let quote_currency = Currency::from("USDT");
        let price_precision = 2;
        let price_increment = Price::new(0.01, price_precision);
        let size_increment = Quantity::new(size_increment, size_precision);
        let instrument = CurrencyPair::new(
            instrument_id,
            raw_symbol,
            base_currency,
            quote_currency,
            price_precision,
            size_precision,
            price_increment,
            size_increment,
            None, // multiplier
            None, // lot_size
            None, // max_quantity
            None, // min_quantity
            None, // max_notional
            None, // min_notional
            None, // max_price
            None, // min_price
            None, // margin_init
            None, // margin_maint
            None, // maker_fee
            None, // taker_fee
            None, // info
            UnixNanos::from(0),
            UnixNanos::from(0),
        );
        InstrumentAny::CurrencyPair(instrument)
    }

    #[rstest]
    fn test_quantity_to_u32_scaled() {
        let instrument = make_test_spot_instrument(0.0001, 4);
        let qty = Quantity::new(0.1, 4);
        assert_eq!(quantity_to_u32(&qty, &instrument), 1_000);
    }

    #[rstest]
    fn test_parse_contracts_quantity_scaled() {
        let instrument = make_test_spot_instrument(0.0001, 4);
        let qty = parse_contracts_quantity(1_000, &instrument);
        assert!((qty.as_f64() - 0.1).abs() < 1e-9);
        assert_eq!(qty.precision, 4);
    }

    #[rstest]
    fn test_convert_contract_quantity_scaling() {
        let max_scale = FIXED_PRECISION as u32;
        let (contract_decimal, size_increment) =
            derive_contract_decimal_and_increment(Some(10_000.0), max_scale).unwrap();
        assert!((size_increment.as_f64() - 0.0001).abs() < 1e-12);

        let lot_qty =
            convert_contract_quantity(Some(1_000.0), contract_decimal, max_scale, "lot size")
                .unwrap()
                .unwrap();
        assert!((lot_qty.as_f64() - 0.1).abs() < 1e-9);
        assert_eq!(lot_qty.precision, 1);
    }

    #[rstest]
    fn test_derive_contract_decimal_defaults_to_one() {
        let max_scale = FIXED_PRECISION as u32;
        let (contract_decimal, size_increment) =
            derive_contract_decimal_and_increment(Some(0.0), max_scale).unwrap();
        assert_eq!(contract_decimal, Decimal::ONE);
        assert_eq!(size_increment.as_f64(), 1.0);
    }

    #[rstest]
    fn test_parse_account_state() {
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("XBt"),
            risk_limit: Some(1000000000),
            amount: Some(5000000),
            prev_realised_pnl: Some(100000),
            gross_comm: Some(1000),
            gross_open_cost: Some(200000),
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: Some(210000),
            risk_value: Some(50000),
            init_margin: Some(20000),
            maint_margin: Some(10000),
            target_excess_margin: Some(5000),
            realised_pnl: Some(100000),
            unrealised_pnl: Some(10000),
            wallet_balance: Some(5000000),
            margin_balance: Some(5010000),
            margin_leverage: Some(2.5),
            margin_used_pcnt: Some(0.25),
            excess_margin: Some(4990000),
            available_margin: Some(4980000),
            withdrawable_margin: Some(4900000),
            maker_fee_discount: Some(0.1),
            taker_fee_discount: Some(0.05),
            timestamp: chrono::Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap(),
            foreign_margin_balance: None,
            foreign_requirement: None,
        };

        let account_id = AccountId::new("BITMEX-001");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init).unwrap();

        assert_eq!(account_state.account_id, account_id);
        assert_eq!(account_state.account_type, AccountType::Margin);
        assert_eq!(account_state.balances.len(), 1);
        assert_eq!(account_state.margins.len(), 1);
        assert!(account_state.is_reported);

        let xbt_balance = &account_state.balances[0];
        assert_eq!(xbt_balance.currency, Currency::from("XBT"));
        assert_eq!(xbt_balance.total.as_f64(), 0.05); // 5000000 satoshis = 0.05 XBT wallet balance
        assert_eq!(xbt_balance.free.as_f64(), 0.049); // 4900000 satoshis = 0.049 XBT withdrawable
        assert_eq!(xbt_balance.locked.as_f64(), 0.001); // 100000 satoshis locked

        let xbt_margin = &account_state.margins[0];
        assert_eq!(xbt_margin.initial.as_f64(), 0.0002); // 20000 satoshis
        assert_eq!(xbt_margin.maintenance.as_f64(), 0.0001); // 10000 satoshis
    }

    #[rstest]
    fn test_parse_account_state_usdt() {
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("USDt"),
            risk_limit: Some(1000000000),
            amount: Some(10000000000), // 10000 USDT in microunits
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: Some(500000),  // 0.5 USDT in microunits
            maint_margin: Some(250000), // 0.25 USDT in microunits
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: Some(10000000000),
            margin_balance: Some(10000000000),
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(9500000000), // 9500 USDT available
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap(),
            foreign_margin_balance: None,
            foreign_requirement: None,
        };

        let account_id = AccountId::new("BITMEX-001");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init).unwrap();

        let usdt_balance = &account_state.balances[0];
        assert_eq!(usdt_balance.currency, Currency::USDT());
        assert_eq!(usdt_balance.total.as_f64(), 10000.0);
        assert_eq!(usdt_balance.free.as_f64(), 9500.0);
        assert_eq!(usdt_balance.locked.as_f64(), 500.0);

        assert_eq!(account_state.margins.len(), 1);
        let usdt_margin = &account_state.margins[0];
        assert_eq!(usdt_margin.initial.as_f64(), 0.5); // 500000 microunits
        assert_eq!(usdt_margin.maintenance.as_f64(), 0.25); // 250000 microunits
    }

    #[rstest]
    fn test_parse_margin_message_with_missing_fields() {
        // Create a margin message with missing optional fields
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("XBt"),
            risk_limit: None,
            amount: None,
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: None,  // Missing
            maint_margin: None, // Missing
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: Some(100000),
            margin_balance: None,
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(95000),
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc::now(),
            foreign_margin_balance: None,
            foreign_requirement: None,
        };

        let account_id = AccountId::new("BITMEX-123456");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init)
            .expect("Should parse even with missing margin fields");

        // Should have balance but no margins
        assert_eq!(account_state.balances.len(), 1);
        assert_eq!(account_state.margins.len(), 0); // No margins tracked
    }

    #[rstest]
    fn test_parse_margin_message_with_only_available_margin() {
        // This is the case we saw in the logs - only available_margin is populated
        let margin_msg = BitmexMarginMsg {
            account: 1667725,
            currency: Ustr::from("USDt"),
            risk_limit: None,
            amount: None,
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: None,
            maint_margin: None,
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: None, // None
            margin_balance: None, // None
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(107859036), // Only this is populated
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc::now(),
            foreign_margin_balance: None,
            foreign_requirement: None,
        };

        let account_id = AccountId::new("BITMEX-1667725");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init)
            .expect("Should handle case with only available_margin");

        // Check the balance accounting equation holds
        let balance = &account_state.balances[0];
        assert_eq!(balance.currency, Currency::USDT());
        assert_eq!(balance.total.as_f64(), 107.859036); // Total should equal free when only available_margin is present
        assert_eq!(balance.free.as_f64(), 107.859036);
        assert_eq!(balance.locked.as_f64(), 0.0);

        // Verify the accounting equation: total = locked + free
        assert_eq!(balance.total, balance.locked + balance.free);
    }

    #[rstest]
    fn test_parse_margin_available_exceeds_wallet() {
        // Test case where available margin exceeds wallet balance (bonus margin scenario)
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("XBt"),
            risk_limit: None,
            amount: Some(70772),
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: Some(0),
            maint_margin: Some(0),
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: Some(70772), // 0.00070772 BTC
            margin_balance: None,
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(94381), // 0.00094381 BTC - exceeds wallet!
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc::now(),
            foreign_margin_balance: None,
            foreign_requirement: None,
        };

        let account_id = AccountId::new("BITMEX-123456");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init)
            .expect("Should handle available > wallet case");

        // Wallet balance is the actual asset amount, not available margin
        let balance = &account_state.balances[0];
        assert_eq!(balance.currency, Currency::from("XBT"));
        assert_eq!(balance.total.as_f64(), 0.00070772); // Wallet balance (actual assets)
        assert_eq!(balance.free.as_f64(), 0.00070772); // All free since no margin locked
        assert_eq!(balance.locked.as_f64(), 0.0);

        // Verify the accounting equation: total = locked + free
        assert_eq!(balance.total, balance.locked + balance.free);
    }

    #[rstest]
    fn test_parse_margin_message_with_foreign_requirements() {
        // Test case where trading USDT-settled contracts with XBT margin
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("XBt"),
            risk_limit: Some(1000000000),
            amount: Some(100000000), // 1 BTC
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: None,  // No direct margin in XBT
            maint_margin: None, // No direct margin in XBT
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: Some(100000000),
            margin_balance: Some(100000000),
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(95000000), // 0.95 BTC available
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc::now(),
            foreign_margin_balance: Some(100000000), // Foreign margin balance in satoshis
            foreign_requirement: Some(5000000),      // 0.05 BTC required for USDT positions
        };

        let account_id = AccountId::new("BITMEX-123456");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init)
            .expect("Failed to parse account state with foreign requirements");

        // Check balance
        let balance = &account_state.balances[0];
        assert_eq!(balance.currency, Currency::from("XBT"));
        assert_eq!(balance.total.as_f64(), 1.0);
        assert_eq!(balance.free.as_f64(), 0.95);
        assert_eq!(balance.locked.as_f64(), 0.05);

        // No margins tracked
        assert_eq!(account_state.margins.len(), 0);
    }

    #[rstest]
    fn test_parse_margin_message_with_both_standard_and_foreign() {
        // Test case with both standard and foreign margin requirements
        let margin_msg = BitmexMarginMsg {
            account: 123456,
            currency: Ustr::from("XBt"),
            risk_limit: Some(1000000000),
            amount: Some(100000000), // 1 BTC
            prev_realised_pnl: None,
            gross_comm: None,
            gross_open_cost: None,
            gross_open_premium: None,
            gross_exec_cost: None,
            gross_mark_value: None,
            risk_value: None,
            init_margin: Some(2000000),  // 0.02 BTC for XBT positions
            maint_margin: Some(1000000), // 0.01 BTC for XBT positions
            target_excess_margin: None,
            realised_pnl: None,
            unrealised_pnl: None,
            wallet_balance: Some(100000000),
            margin_balance: Some(100000000),
            margin_leverage: None,
            margin_used_pcnt: None,
            excess_margin: None,
            available_margin: Some(93000000), // 0.93 BTC available
            withdrawable_margin: None,
            maker_fee_discount: None,
            taker_fee_discount: None,
            timestamp: chrono::Utc::now(),
            foreign_margin_balance: Some(100000000),
            foreign_requirement: Some(5000000), // 0.05 BTC for USDT positions
        };

        let account_id = AccountId::new("BITMEX-123456");
        let ts_init = UnixNanos::from(1_000_000_000);

        let account_state = parse_account_state(&margin_msg, account_id, ts_init)
            .expect("Failed to parse account state with both margins");

        // Check balance
        let balance = &account_state.balances[0];
        assert_eq!(balance.currency, Currency::from("XBT"));
        assert_eq!(balance.total.as_f64(), 1.0);
        assert_eq!(balance.free.as_f64(), 0.93);
        assert_eq!(balance.locked.as_f64(), 0.07); // 0.02 + 0.05 = 0.07 total margin

        assert_eq!(account_state.margins.len(), 1);
        let xbt_margin = &account_state.margins[0];
        assert_eq!(xbt_margin.initial.as_f64(), 0.02); // 2000000 satoshis
        assert_eq!(xbt_margin.maintenance.as_f64(), 0.01); // 1000000 satoshis
    }
}