pub struct MarginLedger { /* private fields */ }Expand description
Manages all isolated margin positions and free USDC balance.
This is the single source of truth for simulated account state, replacing the scattered tracking in PaperExchange and FillSimulator.
Implementations§
Source§impl MarginLedger
impl MarginLedger
Sourcepub fn new(starting_balance: Decimal, fee_rate: Decimal) -> Self
pub fn new(starting_balance: Decimal, fee_rate: Decimal) -> Self
Create a new ledger with starting USDC balance.
Sourcepub fn set_free_usdc(&mut self, amount: Decimal)
pub fn set_free_usdc(&mut self, amount: Decimal)
Set free USDC directly (for compatibility with FillSimulator spot logic).
Sourcepub fn adjust_free_usdc(&mut self, delta: Decimal)
pub fn adjust_free_usdc(&mut self, delta: Decimal)
Adjust free_usdc by delta (positive = add, negative = subtract).
Sourcepub fn set_fee_rate(&mut self, fee_rate: Decimal)
pub fn set_fee_rate(&mut self, fee_rate: Decimal)
Set fee rate.
Sourcepub fn position(&self, instrument: &InstrumentId) -> Option<&IsolatedPosition>
pub fn position(&self, instrument: &InstrumentId) -> Option<&IsolatedPosition>
Get position for an instrument (if any).
Sourcepub fn position_mut(
&mut self,
instrument: &InstrumentId,
) -> Option<&mut IsolatedPosition>
pub fn position_mut( &mut self, instrument: &InstrumentId, ) -> Option<&mut IsolatedPosition>
Get mutable position for an instrument.
Sourcepub fn leverage_for(&self, instrument: &InstrumentId) -> Decimal
pub fn leverage_for(&self, instrument: &InstrumentId) -> Decimal
Get leverage for an instrument.
Sourcepub fn total_reserved_margin(&self) -> Decimal
pub fn total_reserved_margin(&self) -> Decimal
Total reserved margin across all positions.
Sourcepub fn total_unrealized_pnl(
&self,
marks: &HashMap<InstrumentId, Decimal>,
) -> Decimal
pub fn total_unrealized_pnl( &self, marks: &HashMap<InstrumentId, Decimal>, ) -> Decimal
Total unrealized PnL across all positions at given mark prices.
Sourcepub fn equity(&self, marks: &HashMap<InstrumentId, Decimal>) -> Decimal
pub fn equity(&self, marks: &HashMap<InstrumentId, Decimal>) -> Decimal
Account equity = free_usdc + total_reserved_margin + total_unrealized_pnl
Sourcepub fn position_qty(&self, instrument: &InstrumentId) -> Decimal
pub fn position_qty(&self, instrument: &InstrumentId) -> Decimal
Position quantity for an instrument (signed: +long, -short).
Sourcepub fn set_leverage(
&mut self,
instrument: &InstrumentId,
leverage: Decimal,
max_leverage: Decimal,
)
pub fn set_leverage( &mut self, instrument: &InstrumentId, leverage: Decimal, max_leverage: Decimal, )
Set leverage for an instrument.
leverage is the user’s chosen leverage (e.g. 10x).
max_leverage is the exchange maximum for this asset (determines MMR).
Sourcepub fn set_default_leverage(&mut self, leverage: Decimal)
pub fn set_default_leverage(&mut self, leverage: Decimal)
Set default leverage for instruments without explicit settings.
Sourcepub fn check_margin_for_perp_order(
&self,
instrument: &InstrumentId,
side: OrderSide,
price: Decimal,
qty: Decimal,
reduce_only: bool,
) -> Result<(), String>
pub fn check_margin_for_perp_order( &self, instrument: &InstrumentId, side: OrderSide, price: Decimal, qty: Decimal, reduce_only: bool, ) -> Result<(), String>
Check if there’s sufficient margin for a PERP order.
- Reducing orders always pass (they release margin).
- New/increasing orders need: notional / leverage + 2x fee buffer.
- Spot orders are not handled here (use FillSimulator’s check_balance).
Sourcepub fn apply_perp_fill(
&mut self,
instrument: &InstrumentId,
side: OrderSide,
fill_price: Decimal,
fill_qty: Decimal,
fee_amount: Decimal,
) -> Decimal
pub fn apply_perp_fill( &mut self, instrument: &InstrumentId, side: OrderSide, fill_price: Decimal, fill_qty: Decimal, fee_amount: Decimal, ) -> Decimal
Apply a PERP fill to the margin ledger.
Handles all scenarios:
- Opening new position: reserve margin, set entry price
- Increasing existing position: reserve additional margin, update avg entry
- Reducing existing position: release margin proportionally, realize PnL
- Flipping position: close old + open new in opposite direction
Returns the realized PnL from this fill (zero for new positions).
Sourcepub fn check_liquidations(
&self,
marks: &HashMap<InstrumentId, Decimal>,
) -> Vec<InstrumentId>
pub fn check_liquidations( &self, marks: &HashMap<InstrumentId, Decimal>, ) -> Vec<InstrumentId>
Check all positions for liquidation at current mark prices. Returns list of instruments that should be liquidated.
Sourcepub fn liquidate(&mut self, instrument: &InstrumentId, mark_price: Decimal)
pub fn liquidate(&mut self, instrument: &InstrumentId, mark_price: Decimal)
Force-liquidate a position at the given mark price. Closes the entire position, realizes the loss, releases remaining margin.
Sourcepub fn position_snapshots(
&self,
marks: &HashMap<InstrumentId, Decimal>,
) -> Vec<PositionSnapshot>
pub fn position_snapshots( &self, marks: &HashMap<InstrumentId, Decimal>, ) -> Vec<PositionSnapshot>
Generate position snapshots for poll_account_state().
Returns non-flat positions in the format expected by AccountState.