Skip to main content

nautilus_interactive_brokers/execution/
account.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Account management for Interactive Brokers execution client.
17
18use std::{collections::HashMap, future::Future, sync::Arc, time::Duration};
19
20use anyhow::Context;
21use futures_util::Stream;
22use ibapi::{
23    accounts::{
24        AccountSummary, AccountSummaryResult, AccountSummaryTags, AccountUpdate, AccountValue,
25        types::{AccountGroup, AccountId as IbAccountId},
26    },
27    client::Client,
28    contracts::Contract,
29    orders::ExecutionSide,
30    prelude::{StreamExt, SubscriptionItemStreamExt},
31    subscriptions::{FilterDataStream, Subscription},
32};
33use indexmap::IndexMap;
34use nautilus_common::{
35    cache::fifo::FifoCache,
36    live::runner::get_exec_event_sender,
37    messages::{ExecutionEvent, ExecutionReport},
38};
39use nautilus_core::{Params, time::get_atomic_clock_realtime};
40use nautilus_live::task::TaskGroup;
41use nautilus_model::{
42    enums::PositionSide,
43    identifiers::AccountId,
44    instruments::Instrument,
45    reports::PositionStatusReport,
46    types::{AccountBalance, Currency, MarginBalance, Money, Quantity},
47};
48use rust_decimal::{Decimal, prelude::ToPrimitive};
49use tokio_util::sync::CancellationToken;
50use ustr::Ustr;
51
52use super::{
53    reports::IbReportContext,
54    updates::{RebindDelays, sleep_or_cancel},
55};
56
57/// Derives the raw IB account code sent to TWS from the configured `account_id`.
58///
59/// A configured bare code such as `DU123456` is used as is. A configured composite Nautilus
60/// account ID such as `IB-DU123456`, or no configured value, falls back to the segment of
61/// `account_id` after its last hyphen, because IB account codes contain no hyphen while client
62/// names such as `IB-TEST` may.
63pub(crate) fn ib_account_code(configured: Option<&str>, account_id: AccountId) -> Ustr {
64    match configured {
65        Some(code) if !code.contains('-') => Ustr::from(code),
66        _ => Ustr::from(
67            account_id
68                .as_str()
69                .rsplit_once('-')
70                .map_or(account_id.as_str(), |(_, code)| code),
71        ),
72    }
73}
74
75// Rows of one IB refresh batch arrive within milliseconds of each other.
76const ACCOUNT_SUMMARY_QUIET: Duration = Duration::from_secs(1);
77
78pub type AccountSnapshot = (Vec<AccountBalance>, Vec<MarginBalance>, Option<Params>);
79
80type AccountSummaryStream = FilterDataStream<Subscription<AccountSummaryResult>>;
81
82#[derive(Default)]
83struct AccountSummaryAccumulator {
84    balances: Vec<AccountSummaryBalance>,
85    margins: Vec<MarginBalance>,
86    info: IndexMap<String, serde_json::Value>,
87}
88
89/// Subscribe to account summary and parse to balances and margins.
90///
91/// The returned `info` also carries one `reqAccountUpdates` snapshot, because IB serves
92/// values such as `PostExpirationExcess` only on that stream and not as summary tags.
93///
94/// # Errors
95///
96/// Returns an error if the account summary subscription fails.
97pub async fn subscribe_account_summary(
98    client: &Arc<Client>,
99    ib_account: Ustr,
100) -> anyhow::Result<AccountSnapshot> {
101    let mut subscription = open_account_summary(client, ib_account).await?;
102    let mut accumulator = AccountSummaryAccumulator::default();
103    read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await;
104    account_snapshot(client, ib_account, &accumulator).await
105}
106
107/// Subscribe to account summary, deliver the first snapshot, and keep the subscription open.
108///
109/// IB repeats the summary about every 3 minutes on a live `reqAccountSummary` subscription. A
110/// session task delivers one further snapshot per refresh cycle through `on_snapshot`, and
111/// reopens the subscription when it ends.
112///
113/// # Errors
114///
115/// Returns an error if the subscription fails or the first snapshot cannot be built.
116pub async fn stream_account_summary<F>(
117    client: &Arc<Client>,
118    ib_account: Ustr,
119    request_timeout: Duration,
120    session_tasks: &TaskGroup,
121    on_snapshot: F,
122) -> anyhow::Result<()>
123where
124    F: Fn(AccountSnapshot) + Send + 'static,
125{
126    let mut subscription = open_account_summary(client, ib_account).await?;
127    let mut accumulator = AccountSummaryAccumulator::default();
128    read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await;
129    on_snapshot(account_snapshot(client, ib_account, &accumulator).await?);
130
131    let snapshot_client = Arc::clone(client);
132    let build_snapshot = async move |accumulator: &AccountSummaryAccumulator| {
133        account_snapshot(&snapshot_client, ib_account, accumulator).await
134    };
135    let open_client = Arc::clone(client);
136    let open = move || {
137        let client = Arc::clone(&open_client);
138        async move {
139            tokio::time::timeout(request_timeout, open_account_summary(&client, ib_account))
140                .await
141                .context("Timeout subscribing to account summary")?
142        }
143    };
144    let cancel = session_tasks.cancellation_token();
145
146    let future = async move {
147        run_account_summary_stream(
148            subscription,
149            open,
150            build_snapshot,
151            on_snapshot,
152            accumulator,
153            ib_account,
154            cancel,
155            RebindDelays::from_consts(),
156        )
157        .await;
158    };
159
160    session_tasks
161        .spawn(future)
162        .context("Failed to register IB account summary task")?;
163
164    Ok(())
165}
166
167/// Delivers a snapshot per refresh cycle and reopens the subscription when it ends, until
168/// `cancel` fires.
169///
170/// IB ends the subscription when the transport drops and ibapi does not resubscribe it, so
171/// failed opens are retried while ibapi reconnects.
172async fn run_account_summary_stream<S, O, F, T>(
173    initial: S,
174    mut open: O,
175    mut build_snapshot: impl AsyncFnMut(&AccountSummaryAccumulator) -> anyhow::Result<T>,
176    mut on_snapshot: impl FnMut(T),
177    mut accumulator: AccountSummaryAccumulator,
178    ib_account: Ustr,
179    cancel: CancellationToken,
180    delays: RebindDelays,
181) where
182    S: Stream<Item = Result<AccountSummaryResult, ibapi::Error>> + Unpin,
183    O: FnMut() -> F,
184    F: Future<Output = anyhow::Result<S>>,
185{
186    let mut subscription = initial;
187    let mut quiet = Some(ACCOUNT_SUMMARY_QUIET);
188
189    loop {
190        loop {
191            let refreshed = tokio::select! {
192                () = cancel.cancelled() => return,
193                refreshed = read_account_summary_cycle(
194                    &mut subscription,
195                    ib_account,
196                    &mut accumulator,
197                    quiet,
198                ) => refreshed,
199            };
200
201            if !refreshed {
202                break;
203            }
204
205            quiet = Some(ACCOUNT_SUMMARY_QUIET);
206
207            let snapshot = tokio::select! {
208                () = cancel.cancelled() => return,
209                snapshot = build_snapshot(&accumulator) => snapshot,
210            };
211
212            match snapshot {
213                Ok(snapshot) => on_snapshot(snapshot),
214                Err(e) => tracing::warn!("Failed to build account snapshot: {}", e),
215            }
216        }
217
218        tracing::warn!(
219            "IB account summary stream ended for account: {}; reopening in {:?}",
220            ib_account,
221            delays.after_stream
222        );
223        drop(subscription);
224
225        if !sleep_or_cancel(delays.after_stream, &cancel).await {
226            return;
227        }
228
229        subscription = loop {
230            let opened = tokio::select! {
231                () = cancel.cancelled() => return,
232                opened = open() => opened,
233            };
234
235            match opened {
236                Ok(subscription) => break subscription,
237                Err(e) => {
238                    tracing::warn!(
239                        "IB account summary reopen failed: {e:#}; retrying in {:?}",
240                        delays.after_bind_failure
241                    );
242
243                    if !sleep_or_cancel(delays.after_bind_failure, &cancel).await {
244                        return;
245                    }
246                }
247            }
248        };
249
250        // Drop rows the new subscription no longer reports
251        accumulator = AccountSummaryAccumulator::default();
252        quiet = None;
253        tracing::info!(
254            "IB account summary stream reopened for account: {}",
255            ib_account
256        );
257    }
258}
259
260async fn open_account_summary(
261    client: &Arc<Client>,
262    ib_account: Ustr,
263) -> anyhow::Result<AccountSummaryStream> {
264    // Request key account summary tags (includes TotalCashValue to match Python account summary info dict).
265    let tags = &[
266        AccountSummaryTags::NET_LIQUIDATION,
267        AccountSummaryTags::TOTAL_CASH_VALUE,
268        AccountSummaryTags::SETTLED_CASH,
269        AccountSummaryTags::BUYING_POWER,
270        AccountSummaryTags::EQUITY_WITH_LOAN_VALUE,
271        AccountSummaryTags::AVAILABLE_FUNDS,
272        AccountSummaryTags::EXCESS_LIQUIDITY,
273        AccountSummaryTags::INIT_MARGIN_REQ,
274        AccountSummaryTags::MAINT_MARGIN_REQ,
275        AccountSummaryTags::CUSHION,
276    ];
277
278    let group = AccountGroup("All".to_string());
279    let subscription = client
280        .account_summary(&group, tags)
281        .await
282        .context("Failed to subscribe to account summary")?;
283
284    tracing::debug!("Subscribed to account summary for account: {}", ib_account);
285
286    Ok(subscription.filter_data())
287}
288
289// Folds summary rows into the accumulator until the API sends AccountSummaryResult::End, so
290// each snapshot is complete (matches Python behavior of waiting for all tags). After the
291// initial snapshot IB pushes changed values without a further End, so a refresh passes
292// `quiet` and completes once rows stop arriving for that long. Returns false when the stream
293// closes before a snapshot completes.
294async fn read_account_summary_cycle<S>(
295    subscription: &mut S,
296    ib_account: Ustr,
297    accumulator: &mut AccountSummaryAccumulator,
298    quiet: Option<Duration>,
299) -> bool
300where
301    S: Stream<Item = Result<AccountSummaryResult, ibapi::Error>> + Unpin,
302{
303    let raw_account_id = ib_account.as_str();
304    let mut received_rows = false;
305
306    loop {
307        let next = match quiet {
308            Some(quiet) if received_rows => {
309                match tokio::time::timeout(quiet, subscription.next()).await {
310                    Ok(next) => next,
311                    Err(_) => return true,
312                }
313            }
314            _ => subscription.next().await,
315        };
316
317        match next {
318            Some(Ok(AccountSummaryResult::Summary(summary))) => {
319                // Filter for the specific account
320                if summary.account != raw_account_id {
321                    continue;
322                }
323
324                received_rows = true;
325
326                // Record the raw summary tag so the account state carries the
327                // venue-reported values (for example TotalCashValue) that do not
328                // map to the typed balances and margins.
329                accumulator.info.insert(
330                    summary.tag.clone(),
331                    serde_json::Value::from(summary.value.as_str()),
332                );
333
334                if let Err(e) = merge_account_summary_balance(&mut accumulator.balances, &summary) {
335                    tracing::warn!("Failed to parse account summary: {}", e);
336                }
337
338                // Accumulate margin requirements by currency. IB reports INIT_MARGIN_REQ
339                // and MAINT_MARGIN_REQ as separate summary entries; merge them into one
340                // `MarginBalance` per currency so neither half overwrites the other when
341                // the account-wide margin store keys by `Currency`.
342                merge_account_summary_margin(&mut accumulator.margins, &summary);
343            }
344            Some(Ok(AccountSummaryResult::End)) => return true,
345            Some(Err(e)) => {
346                tracing::warn!("Error receiving account summary: {}", e);
347            }
348            None => return false,
349        }
350    }
351}
352
353async fn account_snapshot(
354    client: &Arc<Client>,
355    ib_account: Ustr,
356    accumulator: &AccountSummaryAccumulator,
357) -> anyhow::Result<AccountSnapshot> {
358    let mut info = accumulator.info.clone();
359
360    if let Err(e) = merge_account_updates_snapshot(client, ib_account.as_str(), &mut info).await {
361        tracing::warn!("Failed to collect account updates: {}", e);
362    }
363
364    let balances = finalize_account_summary_balances(accumulator.balances.clone())?;
365    let mut margins = accumulator.margins.clone();
366    margins.sort_by(|a, b| a.currency.code.as_str().cmp(b.currency.code.as_str()));
367
368    tracing::debug!(
369        "Received account summary: {} balances, {} margins",
370        balances.len(),
371        margins.len()
372    );
373
374    Ok((
375        balances,
376        margins,
377        if info.is_empty() {
378            None
379        } else {
380            Some(Params::new(info))
381        },
382    ))
383}
384
385// Drains one `reqAccountUpdates` snapshot into `info`, keyed by the raw IB key like the
386// summary tags. Dropping the subscription sends the cancel, so no stream stays open.
387async fn merge_account_updates_snapshot(
388    client: &Arc<Client>,
389    raw_account_id: &str,
390    info: &mut IndexMap<String, serde_json::Value>,
391) -> anyhow::Result<()> {
392    let account = IbAccountId(raw_account_id.to_string());
393    let subscription = client
394        .account_updates(&account)
395        .await
396        .context("Failed to subscribe to account updates")?;
397    let mut subscription = subscription.filter_data();
398
399    while let Some(result) = subscription.next().await {
400        match result {
401            Ok(AccountUpdate::AccountValue(value)) => {
402                merge_account_value(info, raw_account_id, &value);
403            }
404            Ok(AccountUpdate::End) => break,
405            Ok(AccountUpdate::PortfolioValue(_) | AccountUpdate::UpdateTime(_)) => {}
406            Err(e) => {
407                tracing::warn!("Error receiving account updates: {}", e);
408            }
409        }
410    }
411
412    Ok(())
413}
414
415fn merge_account_value(
416    info: &mut IndexMap<String, serde_json::Value>,
417    raw_account_id: &str,
418    value: &AccountValue,
419) {
420    if value
421        .account
422        .as_deref()
423        .is_some_and(|account| account != raw_account_id)
424    {
425        return;
426    }
427
428    info.insert(
429        value.key.clone(),
430        serde_json::Value::from(value.value.as_str()),
431    );
432}
433
434fn merge_account_summary_margin(margins: &mut Vec<MarginBalance>, summary: &AccountSummary) {
435    let relevant = matches!(
436        summary.tag.as_str(),
437        AccountSummaryTags::INIT_MARGIN_REQ | AccountSummaryTags::MAINT_MARGIN_REQ
438    );
439
440    if !relevant {
441        return;
442    }
443
444    let currency = match parse_currency(&summary.currency) {
445        Ok(currency) => currency,
446        Err(e) => {
447            tracing::warn!("Skipping margin summary with unknown currency: {}", e);
448            return;
449        }
450    };
451
452    let value = match parse_balance_decimal(&summary.value)
453        .and_then(|d| Money::from_decimal(d, currency).map_err(|e| anyhow::anyhow!(e.to_string())))
454    {
455        Ok(money) => money,
456        Err(e) => {
457            tracing::warn!("Failed to parse margin value '{}': {}", summary.value, e);
458            return;
459        }
460    };
461
462    let existing = margins
463        .iter_mut()
464        .find(|m| m.currency == currency && m.instrument_id.is_none());
465
466    match summary.tag.as_str() {
467        AccountSummaryTags::INIT_MARGIN_REQ => match existing {
468            Some(margin) => margin.initial = value,
469            None => margins.push(MarginBalance::new(value, Money::zero(currency), None)),
470        },
471        AccountSummaryTags::MAINT_MARGIN_REQ => match existing {
472            Some(margin) => margin.maintenance = value,
473            None => margins.push(MarginBalance::new(Money::zero(currency), value, None)),
474        },
475        _ => unreachable!("relevant account summary tag was checked above"),
476    }
477}
478
479#[derive(Clone)]
480struct AccountSummaryBalance {
481    currency: Currency,
482    net_liquidation: Option<Decimal>,
483    settled_cash: Option<Decimal>,
484    total_cash_value: Option<Decimal>,
485    available_funds: Option<Decimal>,
486    buying_power: Option<Decimal>,
487}
488
489impl AccountSummaryBalance {
490    fn new(currency: Currency) -> Self {
491        Self {
492            currency,
493            net_liquidation: None,
494            settled_cash: None,
495            total_cash_value: None,
496            available_funds: None,
497            buying_power: None,
498        }
499    }
500
501    fn into_balance(self) -> anyhow::Result<Option<AccountBalance>> {
502        let Some(total) = self
503            .net_liquidation
504            .or(self.settled_cash)
505            .or(self.total_cash_value)
506            .or(self.available_funds)
507            .or(self.buying_power)
508        else {
509            return Ok(None);
510        };
511
512        let free = self
513            .available_funds
514            .or(self.buying_power)
515            .or(self.settled_cash)
516            .or(self.total_cash_value)
517            .unwrap_or(total);
518
519        Ok(Some(AccountBalance::from_total_and_free(
520            total,
521            free,
522            self.currency,
523        )?))
524    }
525}
526
527fn merge_account_summary_balance(
528    balances: &mut Vec<AccountSummaryBalance>,
529    summary: &AccountSummary,
530) -> anyhow::Result<()> {
531    let relevant = matches!(
532        summary.tag.as_str(),
533        AccountSummaryTags::NET_LIQUIDATION
534            | AccountSummaryTags::SETTLED_CASH
535            | AccountSummaryTags::TOTAL_CASH_VALUE
536            | AccountSummaryTags::AVAILABLE_FUNDS
537            | AccountSummaryTags::BUYING_POWER
538    );
539
540    if !relevant {
541        return Ok(());
542    }
543
544    let currency = parse_currency(&summary.currency)?;
545    let value = parse_balance_decimal(&summary.value)?;
546
547    let balance = match balances.iter_mut().find(|b| b.currency == currency) {
548        Some(balance) => balance,
549        None => {
550            balances.push(AccountSummaryBalance::new(currency));
551            balances.last_mut().expect("balance was just inserted")
552        }
553    };
554
555    match summary.tag.as_str() {
556        AccountSummaryTags::NET_LIQUIDATION => balance.net_liquidation = Some(value),
557        AccountSummaryTags::SETTLED_CASH => balance.settled_cash = Some(value),
558        AccountSummaryTags::TOTAL_CASH_VALUE => balance.total_cash_value = Some(value),
559        AccountSummaryTags::AVAILABLE_FUNDS => balance.available_funds = Some(value),
560        AccountSummaryTags::BUYING_POWER => balance.buying_power = Some(value),
561        _ => unreachable!("relevant account summary tag was checked above"),
562    }
563
564    Ok(())
565}
566
567fn finalize_account_summary_balances(
568    summaries: Vec<AccountSummaryBalance>,
569) -> anyhow::Result<Vec<AccountBalance>> {
570    let mut balances = summaries
571        .into_iter()
572        .map(AccountSummaryBalance::into_balance)
573        .collect::<anyhow::Result<Vec<_>>>()?
574        .into_iter()
575        .flatten()
576        .collect::<Vec<_>>();
577    balances.sort_by(|a, b| a.currency.code.as_str().cmp(b.currency.code.as_str()));
578    Ok(balances)
579}
580
581/// Subscribe to PnL updates for the account.
582///
583/// This spawns a background task to handle PnL updates.
584///
585/// # Errors
586///
587/// Returns an error if subscription fails.
588pub async fn subscribe_pnl(
589    client: &Arc<Client>,
590    ib_account: Ustr,
591    session_tasks: &TaskGroup,
592) -> anyhow::Result<()> {
593    let account = IbAccountId(ib_account.to_string());
594    let subscription = client
595        .pnl(&account, None)
596        .await
597        .context("Failed to subscribe to PnL")?;
598    let mut subscription = subscription.filter_data();
599
600    tracing::debug!("Subscribed to PnL updates for account: {}", ib_account);
601
602    // Process PnL updates in background task
603    let future = async move {
604        while let Some(result) = subscription.next().await {
605            match result {
606                Ok(pnl) => {
607                    tracing::debug!(
608                        "PnL update - Daily: {:.2}, Unrealized: {:?}, Realized: {:?}",
609                        pnl.daily_pnl,
610                        pnl.unrealized_pnl,
611                        pnl.realized_pnl
612                    );
613                    // Note: Account state updates are handled by position updates and account summary
614                    // PnL is informational and tracked separately. If needed, account state can be
615                    // generated by subscribing to account summary which includes updated balances.
616                }
617                Err(e) => {
618                    tracing::warn!("Error receiving PnL update: {}", e);
619                }
620            }
621        }
622    };
623
624    session_tasks
625        .spawn(future)
626        .context("Failed to register IB PnL task")?;
627
628    Ok(())
629}
630
631#[derive(Debug)]
632pub struct PositionTrackerState {
633    positions: HashMap<i32, Decimal>,
634    own_fill_ids: FifoCache<String, 10_000>,
635}
636
637/// Track known positions for detecting external changes (e.g., option exercises).
638pub type PositionTracker = Arc<tokio::sync::Mutex<PositionTrackerState>>;
639
640/// Create a new position tracker.
641pub fn create_position_tracker() -> PositionTracker {
642    Arc::new(tokio::sync::Mutex::new(PositionTrackerState {
643        positions: HashMap::new(),
644        own_fill_ids: FifoCache::new(),
645    }))
646}
647
648pub async fn record_own_fill(
649    position_tracker: &PositionTracker,
650    execution_id: &str,
651    contract_id: i32,
652    side: ExecutionSide,
653    quantity: f64,
654) -> anyhow::Result<bool> {
655    let quantity = Decimal::from_f64_retain(quantity)
656        .context("Failed to convert own fill quantity to Decimal")?;
657
658    let signed_quantity = match side {
659        ExecutionSide::Bought => quantity,
660        ExecutionSide::Sold => -quantity,
661    };
662
663    let mut tracker = position_tracker.lock().await;
664    if !tracker.own_fill_ids.insert(execution_id.to_string()) {
665        return Ok(false);
666    }
667
668    let position = tracker
669        .positions
670        .entry(contract_id)
671        .or_insert(Decimal::ZERO);
672    *position += signed_quantity;
673    if position.is_zero() {
674        tracker.positions.remove(&contract_id);
675    }
676
677    Ok(true)
678}
679
680/// Check if a position update represents an external change (e.g., option exercise).
681pub async fn check_external_position_change(
682    position_tracker: &PositionTracker,
683    contract_id: i32,
684    new_quantity: Decimal,
685) -> Option<(bool, Decimal)> {
686    let mut tracker = position_tracker.lock().await;
687    let known_quantity = tracker
688        .positions
689        .get(&contract_id)
690        .copied()
691        .unwrap_or(Decimal::ZERO);
692
693    if new_quantity.is_zero() {
694        return (!known_quantity.is_zero()).then_some((true, known_quantity));
695    }
696
697    // Check if this is an external position change
698    // If quantities match, this is likely from normal trading - not external
699    if known_quantity == new_quantity {
700        return None;
701    }
702
703    // This is a change - determine if it's external
704    // External changes occur when position changes without a corresponding execution
705    // Update tracked position
706    tracker.positions.insert(contract_id, new_quantity);
707
708    // If we had a known position and it changed, it's likely external
709    if known_quantity != Decimal::ZERO && known_quantity != new_quantity {
710        Some((true, known_quantity))
711    } else {
712        // New position or first time seeing it
713        Some((false, known_quantity))
714    }
715}
716
717/// Initialize position tracking with existing positions.
718///
719/// This fetches all current positions and initializes the position tracker
720/// to avoid processing duplicates from execDetails. Returns the contracts of the
721/// tracked positions so their instruments can be published before reconciliation.
722///
723/// # Errors
724///
725/// Returns an error if position request fails.
726pub async fn initialize_position_tracking(
727    client: &Arc<Client>,
728    ib_account: Ustr,
729    position_tracker: PositionTracker,
730) -> anyhow::Result<Vec<Contract>> {
731    let subscription = client
732        .positions()
733        .await
734        .context("Failed to request positions")?;
735    let mut subscription = subscription.filter_data();
736
737    tracing::debug!("Initializing position tracking for account: {}", ib_account);
738
739    let mut contracts = Vec::new();
740    let mut tracker = position_tracker.lock().await;
741
742    while let Some(result) = subscription.next().await {
743        match result {
744            Ok(ibapi::accounts::PositionUpdate::Position(position)) => {
745                // Filter for the specific account
746                if position.account != ib_account.as_str() {
747                    continue;
748                }
749
750                let contract_id = position.contract.contract_id;
751                let quantity = Decimal::from_f64_retain(position.position).unwrap_or_default();
752
753                // Only track non-zero positions
754                if !quantity.is_zero() {
755                    tracker.positions.insert(contract_id, quantity);
756                    contracts.push(position.contract);
757                }
758            }
759            Ok(ibapi::accounts::PositionUpdate::PositionEnd) => {
760                break;
761            }
762            Err(e) => {
763                tracing::warn!("Error receiving position update: {}", e);
764            }
765        }
766    }
767
768    tracing::debug!(
769        "Initialized tracking for {} existing positions",
770        contracts.len()
771    );
772
773    Ok(contracts)
774}
775
776/// Subscribe to real-time position updates for detecting external position changes (e.g., option exercises).
777///
778/// This spawns a background task to track position changes and generate position status reports
779/// for external changes.
780///
781/// # Errors
782///
783/// Returns an error if subscription fails.
784pub async fn subscribe_positions(
785    client: &Arc<Client>,
786    account_id: AccountId,
787    ib_account: Ustr,
788    position_tracker: PositionTracker,
789    instrument_provider: Arc<crate::providers::instruments::InteractiveBrokersInstrumentProvider>,
790    session_tasks: &TaskGroup,
791) -> anyhow::Result<()> {
792    let subscription = client
793        .positions()
794        .await
795        .context("Failed to subscribe to positions")?;
796    let mut subscription = subscription.filter_data();
797
798    tracing::debug!("Subscribed to position updates for account: {}", ib_account);
799
800    let exec_sender = get_exec_event_sender();
801    let clock = get_atomic_clock_realtime();
802    let client_for_instruments = Arc::clone(client);
803
804    // Spawn background task to handle position updates
805    let future = async move {
806        while let Some(result) = subscription.next().await {
807            match result {
808                Ok(ibapi::accounts::PositionUpdate::Position(position)) => {
809                    if position.account != ib_account.as_str() {
810                        continue;
811                    }
812
813                    let contract_id = position.contract.contract_id;
814                    let new_quantity =
815                        Decimal::from_f64_retain(position.position).unwrap_or_default();
816
817                    // Check if this is an external position change
818                    if let Some((is_external, old_quantity)) =
819                        check_external_position_change(&position_tracker, contract_id, new_quantity)
820                            .await
821                        && is_external
822                    {
823                        tracing::warn!(
824                            "External position change detected (likely option exercise): \
825                                Contract ID {}, quantity change: {} -> {}",
826                            contract_id,
827                            old_quantity,
828                            new_quantity
829                        );
830
831                        match instrument_provider
832                            .get_instrument(&client_for_instruments, &position.contract)
833                            .await
834                        {
835                            Ok(Some(instrument)) => {
836                                let instrument_id = instrument.id();
837
838                                let position_side = if new_quantity.is_zero() {
839                                    PositionSide::Flat
840                                } else if new_quantity > Decimal::ZERO {
841                                    PositionSide::Long
842                                } else {
843                                    PositionSide::Short
844                                };
845
846                                let quantity = Quantity::new(
847                                    new_quantity.abs().to_f64().unwrap_or(0.0),
848                                    instrument.size_precision(),
849                                );
850
851                                let avg_px_open = IbReportContext::position_avg_px_open(
852                                    &instrument,
853                                    position.average_cost,
854                                );
855
856                                let ts_init = clock.get_time_ns();
857
858                                let report = PositionStatusReport::new(
859                                    account_id,
860                                    instrument_id,
861                                    position_side,
862                                    quantity,
863                                    ts_init,
864                                    ts_init,
865                                    None,
866                                    None,
867                                    avg_px_open,
868                                )
869                                // Rounded to the price precision, commissions included
870                                .with_avg_px_open_precision(instrument.price_precision());
871                                let event = ExecutionEvent::Report(ExecutionReport::Position(
872                                    Box::new(report),
873                                ));
874
875                                if exec_sender.send(event).is_err() {
876                                    tracing::warn!(
877                                        "Failed to send position status report for external change"
878                                    );
879                                } else {
880                                    if new_quantity.is_zero() {
881                                        position_tracker
882                                            .lock()
883                                            .await
884                                            .positions
885                                            .remove(&contract_id);
886                                    }
887
888                                    tracing::info!(
889                                        "Generated position status report for external change (likely option exercise)"
890                                    );
891                                }
892                            }
893                            Ok(None) => {
894                                tracing::warn!(
895                                    "Instrument not found for external position contract ID: {}",
896                                    contract_id
897                                );
898                            }
899                            Err(e) => {
900                                tracing::warn!(
901                                    "Failed to resolve external position contract ID {}: {}",
902                                    contract_id,
903                                    e
904                                );
905                            }
906                        }
907                    }
908                }
909                Ok(ibapi::accounts::PositionUpdate::PositionEnd) => {
910                    tracing::debug!("Received end of initial IB position snapshot");
911                }
912                Err(e) => {
913                    tracing::warn!("Error receiving position update: {}", e);
914                }
915            }
916        }
917    };
918
919    session_tasks
920        .spawn(future)
921        .context("Failed to register IB position task")?;
922
923    Ok(())
924}
925
926fn parse_balance_decimal(value: &str) -> anyhow::Result<Decimal> {
927    value
928        .parse::<Decimal>()
929        .context(format!("Failed to parse balance value: {value}"))
930}
931
932fn parse_currency(currency: &str) -> anyhow::Result<Currency> {
933    anyhow::ensure!(!currency.is_empty(), "Account summary currency was empty");
934    Ok(Currency::from(currency))
935}
936
937#[cfg(test)]
938mod tests {
939    use std::time::Duration;
940
941    use futures_util::{StreamExt, stream, stream::BoxStream};
942    use ibapi::{
943        accounts::{AccountSummary, AccountSummaryResult, AccountValue},
944        orders::ExecutionSide,
945    };
946    use indexmap::IndexMap;
947    use nautilus_core::Params;
948    use nautilus_model::{
949        identifiers::AccountId,
950        types::{AccountBalance, Currency, MarginBalance, Money},
951    };
952    use rstest::rstest;
953    use rust_decimal::Decimal;
954    use tokio_util::sync::CancellationToken;
955
956    use super::{
957        ACCOUNT_SUMMARY_QUIET, AccountSummaryAccumulator, AccountSummaryTags,
958        check_external_position_change, create_position_tracker, finalize_account_summary_balances,
959        ib_account_code, merge_account_summary_balance, merge_account_summary_margin,
960        merge_account_value, parse_currency, read_account_summary_cycle, record_own_fill,
961        run_account_summary_stream,
962    };
963    use crate::execution::updates::RebindDelays;
964
965    fn margin_summary(tag: &str, value: &str, currency: &str) -> AccountSummary {
966        AccountSummary {
967            account: "DU123".to_string(),
968            tag: tag.to_string(),
969            value: value.to_string(),
970            currency: currency.to_string(),
971        }
972    }
973
974    fn account_value(account: Option<&str>, key: &str, value: &str) -> AccountValue {
975        AccountValue {
976            key: key.to_string(),
977            value: value.to_string(),
978            currency: "USD".to_string(),
979            account: account.map(str::to_string),
980        }
981    }
982
983    fn balances_from_summaries(summaries: &[AccountSummary]) -> Vec<AccountBalance> {
984        let mut balances = Vec::new();
985        for summary in summaries {
986            merge_account_summary_balance(&mut balances, summary).unwrap();
987        }
988
989        finalize_account_summary_balances(balances).unwrap()
990    }
991
992    #[rstest]
993    #[case(Some("U7654321"), "IB-TEST-U7654321", "U7654321")]
994    #[case(Some("IB_LIVE-U1234567"), "IB_LIVE-U1234567", "U1234567")]
995    #[case(Some("IB-TEST-U7654321"), "IB-TEST-U7654321", "U7654321")]
996    #[case(None, "IB-TEST-001", "001")]
997    #[case(None, "IB-001", "001")]
998    fn test_ib_account_code_prefers_configured_bare_code(
999        #[case] configured: Option<&str>,
1000        #[case] account_id: &str,
1001        #[case] expected: &str,
1002    ) {
1003        let code = ib_account_code(configured, AccountId::from(account_id));
1004
1005        assert_eq!(code.as_str(), expected);
1006    }
1007
1008    #[rstest]
1009    fn test_parse_currency_rejects_empty_string() {
1010        let result = parse_currency("");
1011        assert!(result.is_err());
1012        assert_eq!(
1013            result.unwrap_err().to_string(),
1014            "Account summary currency was empty",
1015        );
1016    }
1017
1018    #[rstest]
1019    #[tokio::test]
1020    async fn test_external_position_change_reports_tracked_zero_close() {
1021        let tracker = create_position_tracker();
1022        tracker
1023            .lock()
1024            .await
1025            .positions
1026            .insert(42, Decimal::new(5, 0));
1027
1028        let change = check_external_position_change(&tracker, 42, Decimal::ZERO).await;
1029
1030        assert_eq!(change, Some((true, Decimal::new(5, 0))));
1031        assert_eq!(
1032            tracker.lock().await.positions.get(&42).copied(),
1033            Some(Decimal::new(5, 0))
1034        );
1035    }
1036
1037    #[rstest]
1038    #[tokio::test]
1039    async fn test_own_fill_position_change_is_not_external() {
1040        let tracker = create_position_tracker();
1041        tracker
1042            .lock()
1043            .await
1044            .positions
1045            .insert(42, Decimal::new(5, 0));
1046
1047        assert!(
1048            record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Bought, 2.0)
1049                .await
1050                .unwrap()
1051        );
1052        let change = check_external_position_change(&tracker, 42, Decimal::new(7, 0)).await;
1053
1054        assert_eq!(change, None);
1055        assert_eq!(
1056            tracker.lock().await.positions.get(&42).copied(),
1057            Some(Decimal::new(7, 0))
1058        );
1059    }
1060
1061    #[rstest]
1062    #[tokio::test]
1063    async fn test_replayed_own_fill_does_not_advance_position_twice() {
1064        let tracker = create_position_tracker();
1065
1066        assert!(
1067            record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Sold, 2.0)
1068                .await
1069                .unwrap()
1070        );
1071        assert!(
1072            !record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Sold, 2.0)
1073                .await
1074                .unwrap()
1075        );
1076
1077        assert_eq!(
1078            tracker.lock().await.positions.get(&42).copied(),
1079            Some(Decimal::new(-2, 0))
1080        );
1081    }
1082
1083    #[rstest]
1084    fn test_net_liquidation_merge_clamps_free_to_total() {
1085        let merged = balances_from_summaries(&[
1086            margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "120.00", "USD"),
1087            margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"),
1088        ])
1089        .remove(0);
1090
1091        assert_eq!(merged.total.as_decimal(), "100.00".parse().unwrap());
1092        assert_eq!(merged.locked.as_decimal(), "0.00".parse().unwrap());
1093        assert_eq!(merged.free.as_decimal(), "100.00".parse().unwrap());
1094    }
1095
1096    fn net_liquidation_summary(value: &str) -> AccountSummaryResult {
1097        AccountSummaryResult::Summary(margin_summary("NetLiquidation", value, "USD"))
1098    }
1099
1100    #[rstest]
1101    #[tokio::test]
1102    async fn test_read_account_summary_cycle_refreshes_on_each_end() {
1103        let mut subscription = stream::iter([
1104            Ok(net_liquidation_summary("1000.50")),
1105            Ok(AccountSummaryResult::End),
1106            Ok(net_liquidation_summary("1100.25")),
1107            Ok(AccountSummaryResult::End),
1108        ]);
1109        let ib_account = ustr::Ustr::from("DU123");
1110        let mut accumulator = AccountSummaryAccumulator::default();
1111
1112        let first =
1113            read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await;
1114        let first_balances =
1115            finalize_account_summary_balances(accumulator.balances.clone()).unwrap();
1116        let second =
1117            read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await;
1118        let second_balances =
1119            finalize_account_summary_balances(accumulator.balances.clone()).unwrap();
1120        let closed =
1121            read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await;
1122
1123        assert!(first);
1124        assert_eq!(
1125            first_balances,
1126            vec![
1127                AccountBalance::from_total_and_free(
1128                    Decimal::new(100050, 2),
1129                    Decimal::new(100050, 2),
1130                    Currency::USD()
1131                )
1132                .unwrap()
1133            ]
1134        );
1135        assert!(second);
1136        assert_eq!(
1137            second_balances,
1138            vec![
1139                AccountBalance::from_total_and_free(
1140                    Decimal::new(110025, 2),
1141                    Decimal::new(110025, 2),
1142                    Currency::USD()
1143                )
1144                .unwrap()
1145            ]
1146        );
1147        assert!(!closed);
1148    }
1149
1150    #[rstest]
1151    #[tokio::test(start_paused = true)]
1152    async fn test_read_account_summary_cycle_flushes_refresh_without_end() {
1153        let mut subscription = stream::iter([
1154            Ok(net_liquidation_summary("1000.50")),
1155            Ok(net_liquidation_summary("1100.25")),
1156        ])
1157        .chain(stream::pending());
1158        let ib_account = ustr::Ustr::from("DU123");
1159        let mut accumulator = AccountSummaryAccumulator::default();
1160
1161        let flushed = read_account_summary_cycle(
1162            &mut subscription,
1163            ib_account,
1164            &mut accumulator,
1165            Some(Duration::from_secs(1)),
1166        )
1167        .await;
1168        let balances = finalize_account_summary_balances(accumulator.balances.clone()).unwrap();
1169
1170        assert!(flushed);
1171        assert_eq!(
1172            balances,
1173            vec![
1174                AccountBalance::from_total_and_free(
1175                    Decimal::new(110025, 2),
1176                    Decimal::new(110025, 2),
1177                    Currency::USD()
1178                )
1179                .unwrap()
1180            ]
1181        );
1182    }
1183
1184    #[rstest]
1185    fn test_account_summary_balance_merge_is_order_independent() {
1186        let summaries = [
1187            margin_summary(AccountSummaryTags::TOTAL_CASH_VALUE, "80.00", "USD"),
1188            margin_summary(AccountSummaryTags::SETTLED_CASH, "75.00", "USD"),
1189            margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "60.00", "USD"),
1190            margin_summary(AccountSummaryTags::BUYING_POWER, "120.00", "USD"),
1191            margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"),
1192        ];
1193        let reversed = [
1194            margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"),
1195            margin_summary(AccountSummaryTags::BUYING_POWER, "120.00", "USD"),
1196            margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "60.00", "USD"),
1197            margin_summary(AccountSummaryTags::SETTLED_CASH, "75.00", "USD"),
1198            margin_summary(AccountSummaryTags::TOTAL_CASH_VALUE, "80.00", "USD"),
1199        ];
1200
1201        let forward = balances_from_summaries(&summaries);
1202        let reverse = balances_from_summaries(&reversed);
1203
1204        assert_eq!(forward, reverse);
1205        assert_eq!(forward.len(), 1);
1206        assert_eq!(forward[0].total, Money::from("100.00 USD"));
1207        assert_eq!(forward[0].locked, Money::from("40.00 USD"));
1208        assert_eq!(forward[0].free, Money::from("60.00 USD"));
1209    }
1210
1211    #[rstest]
1212    fn test_account_summary_rows_without_currency_are_ignored() {
1213        // IB reports ratio tags such as `Cushion` with an empty currency. Neither merge may
1214        // treat that as a parse failure; the account is built from the currency-bearing rows.
1215        let cushion = margin_summary(AccountSummaryTags::CUSHION, "0.95", "");
1216        let mut margins: Vec<MarginBalance> = Vec::new();
1217
1218        merge_account_summary_margin(&mut margins, &cushion);
1219        let balances = balances_from_summaries(&[
1220            cushion,
1221            margin_summary(AccountSummaryTags::NET_LIQUIDATION, "1000.00", "EUR"),
1222        ]);
1223
1224        assert!(margins.is_empty());
1225        assert_eq!(
1226            balances,
1227            vec![
1228                AccountBalance::from_total_and_locked(
1229                    Decimal::new(100_000, 2),
1230                    Decimal::ZERO,
1231                    Currency::EUR(),
1232                )
1233                .unwrap()
1234            ]
1235        );
1236    }
1237
1238    #[rstest]
1239    fn test_merge_account_summary_margin_combines_init_and_maint() {
1240        // Regression: `INIT_MARGIN_REQ` and `MAINT_MARGIN_REQ` arrive as separate
1241        // summary entries. The merge must land in a single `MarginBalance` per
1242        // currency so neither half overwrites the other once the account-wide
1243        // store keys by `Currency`.
1244        let mut margins: Vec<MarginBalance> = Vec::new();
1245
1246        merge_account_summary_margin(
1247            &mut margins,
1248            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
1249        );
1250        merge_account_summary_margin(
1251            &mut margins,
1252            &margin_summary(AccountSummaryTags::MAINT_MARGIN_REQ, "250.00", "USD"),
1253        );
1254
1255        assert_eq!(margins.len(), 1);
1256        let margin = &margins[0];
1257        assert!(margin.instrument_id.is_none());
1258        assert_eq!(margin.currency, Currency::USD());
1259        assert_eq!(margin.initial, Money::from("500.00 USD"));
1260        assert_eq!(margin.maintenance, Money::from("250.00 USD"));
1261    }
1262
1263    #[rstest]
1264    fn test_merge_account_summary_margin_order_independent() {
1265        // Arrival order should not matter.
1266        let mut margins: Vec<MarginBalance> = Vec::new();
1267
1268        merge_account_summary_margin(
1269            &mut margins,
1270            &margin_summary(AccountSummaryTags::MAINT_MARGIN_REQ, "250.00", "USD"),
1271        );
1272        merge_account_summary_margin(
1273            &mut margins,
1274            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
1275        );
1276
1277        assert_eq!(margins.len(), 1);
1278        let margin = &margins[0];
1279        assert_eq!(margin.initial, Money::from("500.00 USD"));
1280        assert_eq!(margin.maintenance, Money::from("250.00 USD"));
1281    }
1282
1283    #[rstest]
1284    fn test_merge_account_summary_margin_separates_currencies() {
1285        let mut margins: Vec<MarginBalance> = Vec::new();
1286
1287        merge_account_summary_margin(
1288            &mut margins,
1289            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "500.00", "USD"),
1290        );
1291        merge_account_summary_margin(
1292            &mut margins,
1293            &margin_summary(AccountSummaryTags::INIT_MARGIN_REQ, "400.00", "EUR"),
1294        );
1295
1296        assert_eq!(margins.len(), 2);
1297        let usd = margins
1298            .iter()
1299            .find(|m| m.currency == Currency::USD())
1300            .unwrap();
1301        let eur = margins
1302            .iter()
1303            .find(|m| m.currency == Currency::EUR())
1304            .unwrap();
1305        assert_eq!(usd.initial, Money::from("500.00 USD"));
1306        assert_eq!(eur.initial, Money::from("400.00 EUR"));
1307    }
1308
1309    #[rstest]
1310    #[case(Some("DU123"), true)]
1311    #[case(None, true)]
1312    #[case(Some("DU999"), false)]
1313    fn test_merge_account_value_filters_by_account(
1314        #[case] account: Option<&str>,
1315        #[case] expected_inserted: bool,
1316    ) {
1317        let mut info = IndexMap::new();
1318
1319        merge_account_value(
1320            &mut info,
1321            "DU123",
1322            &account_value(account, "PostExpirationExcess", "-326492.00"),
1323        );
1324
1325        let info = Params::new(info);
1326        assert_eq!(
1327            info.get_str("PostExpirationExcess"),
1328            expected_inserted.then_some("-326492.00"),
1329        );
1330    }
1331
1332    #[rstest]
1333    fn test_merge_account_value_keeps_summary_tags_and_overwrites_same_key() {
1334        let mut info = IndexMap::from([(
1335            AccountSummaryTags::TOTAL_CASH_VALUE.to_string(),
1336            serde_json::Value::from("1000.00"),
1337        )]);
1338
1339        merge_account_value(
1340            &mut info,
1341            "DU123",
1342            &account_value(Some("DU123"), "PostExpirationMargin", "391668.36"),
1343        );
1344        merge_account_value(
1345            &mut info,
1346            "DU123",
1347            &account_value(
1348                Some("DU123"),
1349                AccountSummaryTags::TOTAL_CASH_VALUE,
1350                "1250.50",
1351            ),
1352        );
1353
1354        let info = Params::new(info);
1355        assert_eq!(info.len(), 2);
1356        assert_eq!(info.get_str("TotalCashValue"), Some("1250.50"));
1357        assert_eq!(info.get_str("PostExpirationMargin"), Some("391668.36"));
1358    }
1359
1360    type SummaryStream = BoxStream<'static, Result<AccountSummaryResult, ibapi::Error>>;
1361
1362    const TEST_DELAYS: RebindDelays = RebindDelays {
1363        after_stream: Duration::from_millis(10),
1364        after_bind_failure: Duration::from_millis(10),
1365    };
1366
1367    fn ending_stream(value: &str) -> SummaryStream {
1368        stream::iter([
1369            Ok(net_liquidation_summary(value)),
1370            Ok(AccountSummaryResult::End),
1371        ])
1372        .boxed()
1373    }
1374
1375    fn open_stream(value: &str) -> SummaryStream {
1376        ending_stream(value).chain(stream::pending()).boxed()
1377    }
1378
1379    fn net_liquidation(accumulator: &AccountSummaryAccumulator) -> Decimal {
1380        finalize_account_summary_balances(accumulator.balances.clone()).unwrap()[0]
1381            .total
1382            .as_decimal()
1383    }
1384
1385    #[rstest]
1386    #[tokio::test(start_paused = true)]
1387    async fn test_run_account_summary_stream_reopens_after_end_and_failed_open() {
1388        let mut opens = std::collections::VecDeque::from([
1389            Err(anyhow::anyhow!("not connected")),
1390            Ok(open_stream("1100.25")),
1391        ]);
1392        let mut open_calls = 0;
1393        let open = || {
1394            open_calls += 1;
1395            let next = opens.pop_front().expect("unexpected open");
1396            async move { next }
1397        };
1398        let cancel = CancellationToken::new();
1399        let mut seen = Vec::new();
1400        let on_snapshot = |value| {
1401            seen.push(value);
1402            if seen.len() == 2 {
1403                cancel.cancel();
1404            }
1405        };
1406
1407        run_account_summary_stream(
1408            ending_stream("1000.50"),
1409            open,
1410            async |accumulator: &AccountSummaryAccumulator| Ok(net_liquidation(accumulator)),
1411            on_snapshot,
1412            AccountSummaryAccumulator::default(),
1413            ustr::Ustr::from("DU123"),
1414            cancel.clone(),
1415            TEST_DELAYS,
1416        )
1417        .await;
1418
1419        assert_eq!(seen, vec![Decimal::new(100050, 2), Decimal::new(110025, 2)]);
1420        assert_eq!(open_calls, 2);
1421    }
1422
1423    #[rstest]
1424    #[tokio::test(start_paused = true)]
1425    async fn test_run_account_summary_stream_waits_for_reopened_snapshot_end() {
1426        let reopened = stream::iter([
1427            net_liquidation_summary("1100.25"),
1428            AccountSummaryResult::Summary(margin_summary(
1429                AccountSummaryTags::AVAILABLE_FUNDS,
1430                "700.10",
1431                "USD",
1432            )),
1433            AccountSummaryResult::Summary(margin_summary(
1434                AccountSummaryTags::INIT_MARGIN_REQ,
1435                "250.20",
1436                "USD",
1437            )),
1438            AccountSummaryResult::Summary(margin_summary(
1439                AccountSummaryTags::MAINT_MARGIN_REQ,
1440                "150.30",
1441                "USD",
1442            )),
1443            AccountSummaryResult::End,
1444            AccountSummaryResult::Summary(margin_summary(
1445                AccountSummaryTags::AVAILABLE_FUNDS,
1446                "650.05",
1447                "USD",
1448            )),
1449        ])
1450        .then(|row| async move {
1451            tokio::time::sleep(ACCOUNT_SUMMARY_QUIET * 2).await;
1452            Ok(row)
1453        })
1454        .chain(stream::pending())
1455        .boxed();
1456
1457        let mut reopened = Some(reopened);
1458        let open = || {
1459            let next = reopened.take().expect("unexpected open");
1460            async move { Ok(next) }
1461        };
1462
1463        let cancel = CancellationToken::new();
1464        let started = tokio::time::Instant::now();
1465        let mut seen = Vec::new();
1466        let on_snapshot = |snapshot| {
1467            seen.push((started.elapsed(), snapshot));
1468            if seen.len() == 2 {
1469                cancel.cancel();
1470            }
1471        };
1472
1473        run_account_summary_stream(
1474            stream::empty().boxed(),
1475            open,
1476            async |accumulator: &AccountSummaryAccumulator| {
1477                let balances = finalize_account_summary_balances(accumulator.balances.clone())?;
1478                Ok((balances, accumulator.margins.clone()))
1479            },
1480            on_snapshot,
1481            AccountSummaryAccumulator::default(),
1482            ustr::Ustr::from("DU123"),
1483            cancel.clone(),
1484            TEST_DELAYS,
1485        )
1486        .await;
1487
1488        let margin = MarginBalance::new(Money::from("250.20 USD"), Money::from("150.30 USD"), None);
1489        assert_eq!(
1490            seen,
1491            vec![
1492                (
1493                    TEST_DELAYS.after_stream + ACCOUNT_SUMMARY_QUIET * 10,
1494                    (
1495                        vec![AccountBalance::new(
1496                            Money::from("1100.25 USD"),
1497                            Money::from("400.15 USD"),
1498                            Money::from("700.10 USD"),
1499                        )],
1500                        vec![margin],
1501                    ),
1502                ),
1503                (
1504                    TEST_DELAYS.after_stream + ACCOUNT_SUMMARY_QUIET * 13,
1505                    (
1506                        vec![AccountBalance::new(
1507                            Money::from("1100.25 USD"),
1508                            Money::from("450.20 USD"),
1509                            Money::from("650.05 USD"),
1510                        )],
1511                        vec![margin],
1512                    ),
1513                ),
1514            ],
1515        );
1516    }
1517
1518    #[rstest]
1519    #[tokio::test(start_paused = true)]
1520    async fn test_run_account_summary_stream_drops_rows_the_reopened_stream_omits() {
1521        let initial = stream::iter([
1522            Ok(net_liquidation_summary("1000.50")),
1523            Ok(AccountSummaryResult::Summary(margin_summary(
1524                "NetLiquidation",
1525                "200.00",
1526                "EUR",
1527            ))),
1528            Ok(AccountSummaryResult::End),
1529        ])
1530        .boxed();
1531        let mut reopened = Some(open_stream("1100.25"));
1532        let open = || {
1533            let next = reopened.take().expect("unexpected open");
1534            async move { Ok(next) }
1535        };
1536        let cancel = CancellationToken::new();
1537        let mut seen = Vec::new();
1538        let on_snapshot = |currencies: Vec<String>| {
1539            seen.push(currencies);
1540            if seen.len() == 2 {
1541                cancel.cancel();
1542            }
1543        };
1544
1545        run_account_summary_stream(
1546            initial,
1547            open,
1548            async |accumulator: &AccountSummaryAccumulator| {
1549                Ok(
1550                    finalize_account_summary_balances(accumulator.balances.clone())?
1551                        .iter()
1552                        .map(|balance| balance.currency.code.to_string())
1553                        .collect(),
1554                )
1555            },
1556            on_snapshot,
1557            AccountSummaryAccumulator::default(),
1558            ustr::Ustr::from("DU123"),
1559            cancel.clone(),
1560            TEST_DELAYS,
1561        )
1562        .await;
1563
1564        assert_eq!(seen.len(), 2);
1565        assert_eq!(seen[0].len(), 2);
1566        assert_eq!(seen[1], vec!["USD".to_string()]);
1567    }
1568
1569    #[rstest]
1570    #[tokio::test(start_paused = true)]
1571    async fn test_run_account_summary_stream_retries_failed_opens_until_canceled() {
1572        let mut open_calls = 0;
1573        let open = || {
1574            open_calls += 1;
1575            async { Err::<SummaryStream, _>(anyhow::anyhow!("not connected")) }
1576        };
1577        let cancel = CancellationToken::new();
1578        let canceller = cancel.clone();
1579
1580        tokio::join!(
1581            run_account_summary_stream(
1582                ending_stream("1000.50"),
1583                open,
1584                async |accumulator: &AccountSummaryAccumulator| Ok(net_liquidation(accumulator)),
1585                |_| {},
1586                AccountSummaryAccumulator::default(),
1587                ustr::Ustr::from("DU123"),
1588                cancel,
1589                TEST_DELAYS,
1590            ),
1591            async move {
1592                tokio::time::sleep(Duration::from_millis(55)).await;
1593                canceller.cancel();
1594            },
1595        );
1596
1597        assert!(open_calls >= 3, "open_calls = {open_calls}");
1598    }
1599}