nautilus-interactive-brokers 0.62.0

Interactive Brokers 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
// -------------------------------------------------------------------------------------------------
//  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.
// -------------------------------------------------------------------------------------------------

//! Account management for Interactive Brokers execution client.

use std::{collections::HashMap, sync::Arc};

use anyhow::Context;
use ibapi::{
    accounts::{
        AccountSummary, AccountSummaryResult, AccountSummaryTags,
        types::{AccountGroup, AccountId as IbAccountId},
    },
    client::Client,
    prelude::{StreamExt, SubscriptionItemStreamExt},
};
use nautilus_common::{
    live::runner::get_exec_event_sender,
    messages::{ExecutionEvent, ExecutionReport},
};
use nautilus_core::{Params, time::get_atomic_clock_realtime};
use nautilus_model::{
    enums::PositionSideSpecified,
    identifiers::AccountId,
    instruments::Instrument,
    reports::PositionStatusReport,
    types::{AccountBalance, Currency, MarginBalance, Money, Quantity},
};
use rust_decimal::{Decimal, prelude::ToPrimitive};

pub(crate) fn raw_ib_account_code(account_id: &AccountId) -> String {
    account_id
        .to_string()
        .strip_prefix("IB-")
        .unwrap_or(account_id.as_str())
        .to_string()
}

/// Subscribe to account summary and parse to balances and margins.
///
/// # Errors
///
/// Returns an error if subscription fails.
pub async fn subscribe_account_summary(
    client: &Arc<Client>,
    account_id: AccountId,
) -> anyhow::Result<(Vec<AccountBalance>, Vec<MarginBalance>, Option<Params>)> {
    let raw_account_id = raw_ib_account_code(&account_id);
    // Request key account summary tags (includes TotalCashValue to match Python account summary info dict).
    let tags = &[
        AccountSummaryTags::NET_LIQUIDATION,
        AccountSummaryTags::TOTAL_CASH_VALUE,
        AccountSummaryTags::SETTLED_CASH,
        AccountSummaryTags::BUYING_POWER,
        AccountSummaryTags::EQUITY_WITH_LOAN_VALUE,
        AccountSummaryTags::AVAILABLE_FUNDS,
        AccountSummaryTags::EXCESS_LIQUIDITY,
        AccountSummaryTags::INIT_MARGIN_REQ,
        AccountSummaryTags::MAINT_MARGIN_REQ,
        AccountSummaryTags::CUSHION,
    ];

    let group = AccountGroup("All".to_string());
    let subscription = client
        .account_summary(&group, tags)
        .await
        .context("Failed to subscribe to account summary")?;
    let mut subscription = subscription.filter_data();

    tracing::debug!("Subscribed to account summary for account: {}", account_id);

    // Process initial account summary snapshot
    // We collect all summary items until the API sends AccountSummaryResult::End, so the
    // returned balances/margins are complete (matches Python behavior of waiting for all tags).
    let mut balances: Vec<AccountBalance> = Vec::new();
    let mut margins: Vec<MarginBalance> = Vec::new();
    let mut info = Params::new();

    while let Some(result) = subscription.next().await {
        match result {
            Ok(AccountSummaryResult::Summary(summary)) => {
                // Filter for the specific account
                if summary.account != raw_account_id {
                    continue;
                }

                // Record the raw summary tag so the account state carries the
                // venue-reported values (for example TotalCashValue) that do not
                // map to the typed balances and margins.
                info.insert(
                    summary.tag.to_string(),
                    serde_json::Value::from(summary.value.as_str()),
                );

                match parse_account_summary_to_balance(&summary) {
                    Ok(balance) => {
                        // Check if balance already exists for this currency
                        if let Some(existing) = balances
                            .iter_mut()
                            .find(|b| b.total.currency == balance.total.currency)
                        {
                            if let Some(merged) = merge_account_summary_balance(
                                existing,
                                summary.tag.as_str(),
                                &summary.value,
                                &summary.currency,
                            )? {
                                *existing = merged;
                            }
                        } else {
                            balances.push(balance);
                        }
                    }
                    Err(e) => {
                        tracing::warn!("Failed to parse account summary: {}", e);
                    }
                }

                // Accumulate margin requirements by currency. IB reports INIT_MARGIN_REQ
                // and MAINT_MARGIN_REQ as separate summary entries; merge them into one
                // `MarginBalance` per currency so neither half overwrites the other when
                // the account-wide margin store keys by `Currency`.
                merge_account_summary_margin(&mut margins, &summary);
            }
            Ok(AccountSummaryResult::End) => {
                break;
            }
            Err(e) => {
                tracing::warn!("Error receiving account summary: {}", e);
            }
        }
    }

    tracing::debug!(
        "Received account summary: {} balances, {} margins",
        balances.len(),
        margins.len()
    );

    Ok((
        balances,
        margins,
        if info.is_empty() { None } else { Some(info) },
    ))
}

fn merge_account_summary_margin(margins: &mut Vec<MarginBalance>, summary: &AccountSummary) {
    let currency = match parse_currency(&summary.currency) {
        Ok(currency) => currency,
        Err(e) => {
            tracing::warn!("Skipping margin summary with unknown currency: {}", e);
            return;
        }
    };
    let value = match parse_balance_decimal(&summary.value)
        .and_then(|d| Money::from_decimal(d, currency).map_err(|e| anyhow::anyhow!(e.to_string())))
    {
        Ok(money) => money,
        Err(e) => {
            tracing::warn!("Failed to parse margin value '{}': {}", summary.value, e);
            return;
        }
    };

    let existing = margins
        .iter_mut()
        .find(|m| m.currency == currency && m.instrument_id.is_none());

    match summary.tag.as_str() {
        AccountSummaryTags::INIT_MARGIN_REQ => match existing {
            Some(margin) => margin.initial = value,
            None => margins.push(MarginBalance::new(value, Money::zero(currency), None)),
        },
        AccountSummaryTags::MAINT_MARGIN_REQ => match existing {
            Some(margin) => margin.maintenance = value,
            None => margins.push(MarginBalance::new(Money::zero(currency), value, None)),
        },
        _ => {}
    }
}

fn merge_account_summary_balance(
    existing: &AccountBalance,
    tag: &str,
    value: &str,
    currency_code: &str,
) -> anyhow::Result<Option<AccountBalance>> {
    let currency = parse_currency(currency_code)?;

    match tag {
        AccountSummaryTags::SETTLED_CASH => {
            let settled_cash = parse_balance_decimal(value)?;
            Ok(Some(AccountBalance::from_total_and_locked(
                settled_cash,
                Decimal::ZERO,
                currency,
            )?))
        }
        AccountSummaryTags::NET_LIQUIDATION => {
            let net_liq = parse_balance_decimal(value)?;
            Ok(Some(AccountBalance::from_total_and_free(
                net_liq,
                existing.free.as_decimal(),
                currency,
            )?))
        }
        _ => Ok(None),
    }
}

/// Subscribe to PnL updates for the account.
///
/// This spawns a background task to handle PnL updates.
///
/// # Errors
///
/// Returns an error if subscription fails.
pub async fn subscribe_pnl(client: &Arc<Client>, account_id: AccountId) -> anyhow::Result<()> {
    let account = IbAccountId(raw_ib_account_code(&account_id));
    let subscription = client
        .pnl(&account, None)
        .await
        .context("Failed to subscribe to PnL")?;
    let mut subscription = subscription.filter_data();

    tracing::debug!("Subscribed to PnL updates for account: {}", account_id);

    // Process PnL updates in background task
    nautilus_common::live::get_runtime().spawn(async move {
        while let Some(result) = subscription.next().await {
            match result {
                Ok(pnl) => {
                    tracing::debug!(
                        "PnL update - Daily: {:.2}, Unrealized: {:?}, Realized: {:?}",
                        pnl.daily_pnl,
                        pnl.unrealized_pnl,
                        pnl.realized_pnl
                    );
                    // Note: Account state updates are handled by position updates and account summary
                    // PnL is informational and tracked separately. If needed, account state can be
                    // generated by subscribing to account summary which includes updated balances.
                }
                Err(e) => {
                    tracing::warn!("Error receiving PnL update: {}", e);
                }
            }
        }
    });

    Ok(())
}

/// Track known positions for detecting external changes (e.g., option exercises).
pub type PositionTracker = Arc<tokio::sync::Mutex<HashMap<i32, Decimal>>>;

/// Create a new position tracker.
pub fn create_position_tracker() -> PositionTracker {
    Arc::new(tokio::sync::Mutex::new(HashMap::new()))
}

/// Check if a position update represents an external change (e.g., option exercise).
pub async fn check_external_position_change(
    position_tracker: &PositionTracker,
    contract_id: i32,
    new_quantity: Decimal,
) -> Option<(bool, Decimal)> {
    let mut tracker = position_tracker.lock().await;
    let known_quantity = tracker.get(&contract_id).copied().unwrap_or(Decimal::ZERO);

    if new_quantity.is_zero() {
        return (!known_quantity.is_zero()).then_some((true, known_quantity));
    }

    // Check if this is an external position change
    // If quantities match, this is likely from normal trading - not external
    if known_quantity == new_quantity {
        return None;
    }

    // This is a change - determine if it's external
    // External changes occur when position changes without a corresponding execution
    // Update tracked position
    tracker.insert(contract_id, new_quantity);

    // If we had a known position and it changed, it's likely external
    if known_quantity != Decimal::ZERO && known_quantity != new_quantity {
        Some((true, known_quantity))
    } else {
        // New position or first time seeing it
        Some((false, known_quantity))
    }
}

/// Initialize position tracking with existing positions.
///
/// This fetches all current positions and initializes the position tracker
/// to avoid processing duplicates from execDetails.
///
/// # Errors
///
/// Returns an error if position request fails.
pub async fn initialize_position_tracking(
    client: &Arc<Client>,
    account_id: AccountId,
    position_tracker: PositionTracker,
) -> anyhow::Result<()> {
    let raw_account_id = raw_ib_account_code(&account_id);
    let subscription = client
        .positions()
        .await
        .context("Failed to request positions")?;
    let mut subscription = subscription.filter_data();

    tracing::debug!("Initializing position tracking for account: {}", account_id);

    let mut position_count = 0;
    let mut tracker = position_tracker.lock().await;

    while let Some(result) = subscription.next().await {
        match result {
            Ok(ibapi::accounts::PositionUpdate::Position(position)) => {
                // Filter for the specific account
                if position.account != raw_account_id {
                    continue;
                }

                let contract_id = position.contract.contract_id;
                let quantity = Decimal::from_f64_retain(position.position).unwrap_or_default();

                // Only track non-zero positions
                if !quantity.is_zero() {
                    tracker.insert(contract_id, quantity);
                    position_count += 1;
                }
            }
            Ok(ibapi::accounts::PositionUpdate::PositionEnd) => {
                break;
            }
            Err(e) => {
                tracing::warn!("Error receiving position update: {}", e);
            }
        }
    }

    tracing::debug!(
        "Initialized tracking for {} existing positions",
        position_count
    );

    Ok(())
}

/// Subscribe to real-time position updates for detecting external position changes (e.g., option exercises).
///
/// This spawns a background task to track position changes and generate position status reports
/// for external changes.
///
/// # Errors
///
/// Returns an error if subscription fails.
pub async fn subscribe_positions(
    client: &Arc<Client>,
    account_id: AccountId,
    position_tracker: PositionTracker,
    instrument_provider: Arc<crate::providers::instruments::InteractiveBrokersInstrumentProvider>,
) -> anyhow::Result<()> {
    let raw_account_id = raw_ib_account_code(&account_id);
    let subscription = client
        .positions()
        .await
        .context("Failed to subscribe to positions")?;
    let mut subscription = subscription.filter_data();

    tracing::debug!("Subscribed to position updates for account: {}", account_id);

    let exec_sender = get_exec_event_sender();
    let clock = get_atomic_clock_realtime();
    let client_for_instruments = Arc::clone(client);

    // Spawn background task to handle position updates
    nautilus_common::live::get_runtime().spawn(async move {
        while let Some(result) = subscription.next().await {
            match result {
                Ok(ibapi::accounts::PositionUpdate::Position(position)) => {
                    if position.account != raw_account_id {
                        continue;
                    }

                    let contract_id = position.contract.contract_id;
                    let new_quantity =
                        Decimal::from_f64_retain(position.position).unwrap_or_default();

                    // Check if this is an external position change
                    if let Some((is_external, old_quantity)) =
                        check_external_position_change(&position_tracker, contract_id, new_quantity)
                            .await
                        && is_external
                    {
                        tracing::warn!(
                            "External position change detected (likely option exercise): \
                                Contract ID {}, quantity change: {} -> {}",
                            contract_id,
                            old_quantity,
                            new_quantity
                        );

                        match instrument_provider
                            .get_instrument(&client_for_instruments, &position.contract)
                            .await
                        {
                            Ok(Some(instrument)) => {
                                let instrument_id = instrument.id();
                                let position_side = if new_quantity.is_zero() {
                                    PositionSideSpecified::Flat
                                } else if new_quantity > Decimal::ZERO {
                                    PositionSideSpecified::Long
                                } else {
                                    PositionSideSpecified::Short
                                };

                                let quantity = Quantity::new(
                                    new_quantity.abs().to_f64().unwrap_or(0.0),
                                    instrument.size_precision(),
                                );

                                let avg_px_open = if position.average_cost > 0.0 {
                                    let price_magnifier =
                                        instrument_provider.get_price_magnifier(&instrument_id)
                                            as f64;
                                    let multiplier = instrument.multiplier().as_f64();
                                    let converted_avg_cost =
                                        position.average_cost / (multiplier * price_magnifier);
                                    let price_precision = instrument.price_precision();
                                    Some(
                                        Decimal::from_f64_retain(converted_avg_cost)
                                            .map(|d| d.round_dp(price_precision as u32))
                                            .unwrap_or_default(),
                                    )
                                } else {
                                    None
                                };

                                let ts_init = clock.get_time_ns();

                                let report = PositionStatusReport::new(
                                    account_id,
                                    instrument_id,
                                    position_side,
                                    quantity,
                                    ts_init,
                                    ts_init,
                                    None,
                                    None,
                                    avg_px_open,
                                );
                                let event = ExecutionEvent::Report(ExecutionReport::Position(
                                    Box::new(report),
                                ));

                                if exec_sender.send(event).is_err() {
                                    tracing::warn!(
                                        "Failed to send position status report for external change"
                                    );
                                } else {
                                    if new_quantity.is_zero() {
                                        position_tracker.lock().await.remove(&contract_id);
                                    }

                                    tracing::info!(
                                        "Generated position status report for external change (likely option exercise)"
                                    );
                                }
                            }
                            Ok(None) => {
                                tracing::warn!(
                                    "Instrument not found for external position contract ID: {}",
                                    contract_id
                                );
                            }
                            Err(e) => {
                                tracing::warn!(
                                    "Failed to resolve external position contract ID {}: {}",
                                    contract_id,
                                    e
                                );
                            }
                        }
                    }
                }
                Ok(ibapi::accounts::PositionUpdate::PositionEnd) => {
                    break;
                }
                Err(e) => {
                    tracing::warn!("Error receiving position update: {}", e);
                }
            }
        }
    });

    Ok(())
}

/// Parse IB account summary to Nautilus AccountBalance.
fn parse_account_summary_to_balance(summary: &AccountSummary) -> anyhow::Result<AccountBalance> {
    let currency = parse_currency(&summary.currency)?;
    let balance = parse_balance_decimal(&summary.value)?;

    match summary.tag.as_str() {
        AccountSummaryTags::SETTLED_CASH | AccountSummaryTags::TOTAL_CASH_VALUE => {
            // Cash balance - free equals total for settled cash
            AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency)
                .map_err(Into::into)
        }
        AccountSummaryTags::NET_LIQUIDATION => {
            // Net liquidation - represents total equity
            // Free would be calculated from available funds
            AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency)
                .map_err(Into::into)
        }
        AccountSummaryTags::BUYING_POWER | AccountSummaryTags::AVAILABLE_FUNDS => {
            // Available funds - this is the free amount
            AccountBalance::from_total_and_free(balance, balance, currency).map_err(Into::into)
        }
        _ => {
            // Default: treat as total balance
            AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency)
                .map_err(Into::into)
        }
    }
}

fn parse_balance_decimal(value: &str) -> anyhow::Result<Decimal> {
    value
        .parse::<Decimal>()
        .context(format!("Failed to parse balance value: {}", value))
}

fn parse_currency(currency: &str) -> anyhow::Result<Currency> {
    anyhow::ensure!(!currency.is_empty(), "Account summary currency was empty");
    Ok(Currency::from(currency))
}

#[cfg(test)]
mod tests {
    use ibapi::accounts::AccountSummary;
    use nautilus_model::types::{AccountBalance, Currency, MarginBalance, Money};
    use rstest::rstest;
    use rust_decimal::Decimal;

    use super::{
        AccountSummaryTags, check_external_position_change, create_position_tracker,
        merge_account_summary_balance, merge_account_summary_margin, parse_currency,
    };

    fn margin_summary(tag: &str, value: &str, currency: &str) -> AccountSummary {
        AccountSummary {
            account: "DU123".to_string(),
            tag: tag.to_string(),
            value: value.to_string(),
            currency: currency.to_string(),
        }
    }

    /// Verifies the IB avg cost to Nautilus price conversion formula used in position parsing.
    /// Python: converted_avg_cost = avg_cost / (multiplier * price_magnifier)
    #[rstest]
    fn test_ib_avg_cost_to_price_conversion() {
        let avg_cost = 100.0;
        let multiplier = 10.0;
        let price_magnifier = 2.0;
        let converted = avg_cost / (multiplier * price_magnifier);
        assert_eq!(converted, 5.0);

        let avg_cost2 = 1_500_000.0;
        let multiplier2 = 50.0;
        let price_magnifier2 = 10;
        let converted2 = avg_cost2 / (multiplier2 * (price_magnifier2 as f64));
        assert_eq!(converted2, 3000.0);
    }

    #[rstest]
    fn test_parse_currency_rejects_empty_string() {
        let result = parse_currency("");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "Account summary currency was empty",
        );
    }

    #[rstest]
    #[tokio::test]
    async fn test_external_position_change_reports_tracked_zero_close() {
        let tracker = create_position_tracker();
        tracker.lock().await.insert(42, Decimal::new(5, 0));

        let change = check_external_position_change(&tracker, 42, Decimal::ZERO).await;

        assert_eq!(change, Some((true, Decimal::new(5, 0))));
        assert_eq!(
            tracker.lock().await.get(&42).copied(),
            Some(Decimal::new(5, 0))
        );
    }

    #[rstest]
    fn test_net_liquidation_merge_clamps_free_to_total() {
        let existing = AccountBalance::from_total_and_free(
            "120.00".parse().unwrap(),
            "120.00".parse().unwrap(),
            Currency::USD(),
        )
        .unwrap();

        let merged = merge_account_summary_balance(
            &existing,
            AccountSummaryTags::NET_LIQUIDATION,
            "100.00",
            "USD",
        )
        .unwrap()
        .unwrap();

        assert_eq!(merged.total.as_decimal(), "100.00".parse().unwrap());
        assert_eq!(merged.locked.as_decimal(), "0.00".parse().unwrap());
        assert_eq!(merged.free.as_decimal(), "100.00".parse().unwrap());
    }

    #[rstest]
    fn test_merge_account_summary_margin_combines_init_and_maint() {
        // Regression: `INIT_MARGIN_REQ` and `MAINT_MARGIN_REQ` arrive as separate
        // summary entries. The merge must land in a single `MarginBalance` per
        // currency so neither half overwrites the other once the account-wide
        // store keys by `Currency`.
        let mut margins: Vec<MarginBalance> = Vec::new();

        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
        );
        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::MAINT_MARGIN_REQ, "250.00", "USD"),
        );

        assert_eq!(margins.len(), 1);
        let margin = &margins[0];
        assert!(margin.instrument_id.is_none());
        assert_eq!(margin.currency, Currency::USD());
        assert_eq!(margin.initial, Money::from("500.00 USD"));
        assert_eq!(margin.maintenance, Money::from("250.00 USD"));
    }

    #[rstest]
    fn test_merge_account_summary_margin_order_independent() {
        // Arrival order should not matter.
        let mut margins: Vec<MarginBalance> = Vec::new();

        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::MAINT_MARGIN_REQ, "250.00", "USD"),
        );
        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
        );

        assert_eq!(margins.len(), 1);
        let margin = &margins[0];
        assert_eq!(margin.initial, Money::from("500.00 USD"));
        assert_eq!(margin.maintenance, Money::from("250.00 USD"));
    }

    #[rstest]
    fn test_merge_account_summary_margin_separates_currencies() {
        let mut margins: Vec<MarginBalance> = Vec::new();

        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
        );
        merge_account_summary_margin(
            &mut margins,
            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "400.00", "EUR"),
        );

        assert_eq!(margins.len(), 2);
        let usd = margins
            .iter()
            .find(|m| m.currency == Currency::USD())
            .unwrap();
        let eur = margins
            .iter()
            .find(|m| m.currency == Currency::EUR())
            .unwrap();
        assert_eq!(usd.initial, Money::from("500.00 USD"));
        assert_eq!(eur.initial, Money::from("400.00 EUR"));
    }
}