pub fn reconcile_ledger(
ledger: &[OrderSnapshot],
broker_active: &[OrderSnapshot],
) -> ReconcileReportExpand description
Compares the client’s ledger of orders with the broker’s active orders and sorts them into three groups.
confirmed: in both. The broker’s snapshot is returned.closed_offline: in the ledger, not active at the broker. They filled, were cancelled or were rejected while the client was away. Their cids are returned. The function does not say which of these happened. Ask the broker for the order state.foreign: active at the broker, not in the ledger. They were placed by someone else (another program, the broker’s app). They must not be touched. Why: cancelling an order the client never created could damage someone else’s strategy.
Broker orders whose cid is the nil UUID (no client id) are ignored. They cannot be matched with a ledger entry and are not reported as foreign. The order of the returned lists is not defined.
The function is pure: no I/O and no state.
§Examples
use std::time::Instant;
use brokapi_core::{
BrokerOrderId, ClientOrderId, InstrumentId, InstrumentKey, NativeId, OrderSnapshot,
OrderState, OrderType, Price, Qty, Side, Timestamps, UnixNanos,
};
use brokapi_runtime::reconcile_ledger;
let sber = InstrumentId::new(
InstrumentKey::new("SBER", "TQBR"),
NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
let order = |cid: ClientOrderId| OrderSnapshot::new(cid, sber.clone(), OrderState::Acked { cid, broker_id: BrokerOrderId::new("1") }, Side::Buy, OrderType::Limit, Qty::from(1), Timestamps::new(UnixNanos::ZERO, Instant::now())).with_filled(Qty::from(0)).with_price(Some(Price::from(100)));
let (kept, gone, other) = (ClientOrderId::new_v4(), ClientOrderId::new_v4(), ClientOrderId::new_v4());
// We remember `kept` and `gone`. The broker shows `kept` and one we never placed.
let ledger = [order(kept), order(gone)];
let at_broker = [order(kept), order(other)];
let report = reconcile_ledger(&ledger, &at_broker);
assert_eq!(report.confirmed.len(), 1);
assert_eq!(report.confirmed[0].cid, kept);
assert_eq!(report.closed_offline, vec![gone]);
assert_eq!(report.foreign[0].cid, other);