sandbox-quant 1.0.7

Exchange-truth trading core for Binance Spot and Futures
Documentation
use std::collections::BTreeMap;

use crate::domain::instrument::Instrument;
use crate::exchange::types::AuthoritativeSnapshot;
use crate::portfolio::snapshot::PortfolioStateSnapshot;

pub fn apply_authoritative_snapshot(snapshot: AuthoritativeSnapshot) -> PortfolioStateSnapshot {
    let positions = snapshot
        .positions
        .into_iter()
        .filter(|position| !position.is_flat())
        .map(|position| (position.instrument.clone(), position))
        .collect();

    let mut open_orders: BTreeMap<Instrument, Vec<_>> = BTreeMap::new();
    for order in snapshot.open_orders {
        open_orders
            .entry(order.instrument.clone())
            .or_default()
            .push(order);
    }

    PortfolioStateSnapshot {
        balances: snapshot.balances,
        positions,
        open_orders,
    }
}