Skip to main content

MarginService

Trait MarginService 

Source
pub trait MarginService: Send + Sync {
    // Required method
    fn compute_margin_for_account<'life0, 'life1, 'async_trait>(
        &'life0 self,
        account: &'life1 Account,
    ) -> Pin<Box<dyn Future<Output = Result<MarginDetails, MarginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided method
    fn compute_margin_for_accounts<'life0, 'life1, 'async_trait>(
        &'life0 self,
        accounts: &'life1 [Account],
    ) -> Pin<Box<dyn Future<Output = Result<Vec<MarginDetails>, MarginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Trait for margin calculation services.

Implementations compute margin requirements for accounts based on their current positions encoded in Account. The margin represents the collateral required to maintain positions.

Hypotheticals that include open orders are built by the engine layer before calling this trait - the margin service only sees the resulting Account state.

§Portfolio Margin Semantics

  • equity: Executed PM equity = cash + executed option UPNL + executed perp UPNL
  • initial_margin_required: Scanning risk (worst-case scenario loss)
  • maintenance_margin_required: IM * 0.85 (liquidation threshold)

Order admission: equity >= initial_margin_required

Required Methods§

Source

fn compute_margin_for_account<'life0, 'life1, 'async_trait>( &'life0 self, account: &'life1 Account, ) -> Pin<Box<dyn Future<Output = Result<MarginDetails, MarginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Compute margin for a single account.

§Returns
  • Some(MarginDetails) with computed margin (always returned by SpanMarginService)
  • None reserved for future implementations that may skip margin calculation (e.g., accounts not subject to PM, or internal system accounts)

Empty portfolios return MarginDetails with zero margin requirements.

Provided Methods§

Source

fn compute_margin_for_accounts<'life0, 'life1, 'async_trait>( &'life0 self, accounts: &'life1 [Account], ) -> Pin<Box<dyn Future<Output = Result<Vec<MarginDetails>, MarginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Convenience batch API for computing margin across multiple accounts.

Implementors§