pub struct IsolatedPosition {
pub qty: Decimal,
pub avg_entry_px: Decimal,
pub leverage: Decimal,
pub isolated_margin_reserved: Decimal,
pub maintenance_margin_rate: Decimal,
pub realized_pnl: Decimal,
pub fee_paid: Decimal,
}Expand description
Per-instrument isolated margin position.
Tracks all state needed for accurate perp simulation: position size, average entry, reserved margin, PnL, and fees.
Fields§
§qty: DecimalSigned position quantity: positive = long, negative = short
avg_entry_px: DecimalWeighted average entry price
leverage: DecimalLeverage for this position (e.g., 10x)
isolated_margin_reserved: DecimalUSDC locked as isolated margin for this position
maintenance_margin_rate: DecimalMaintenance margin rate = 1 / max_leverage Used for liquidation price calculation
realized_pnl: DecimalAccumulated realized PnL from partial/full closes
fee_paid: DecimalAccumulated fees paid
Implementations§
Source§impl IsolatedPosition
impl IsolatedPosition
Sourcepub fn unrealized_pnl(&self, mark_price: Decimal) -> Decimal
pub fn unrealized_pnl(&self, mark_price: Decimal) -> Decimal
Unrealized PnL at given mark price. Long: profit when price goes up. Short: profit when price goes down.
Sourcepub fn liquidation_price(&self) -> Option<Decimal>
pub fn liquidation_price(&self) -> Option<Decimal>
Liquidation price for this isolated position (Hyperliquid formula).
For a long position: liq_price = entry - margin_available / |qty| / (1 - mmr) For a short position: liq_price = entry + margin_available / |qty| / (1 + mmr)
Returns None if position is flat.
Sourcepub fn required_margin_for_order(
notional: Decimal,
leverage: Decimal,
fee_rate: Decimal,
) -> Decimal
pub fn required_margin_for_order( notional: Decimal, leverage: Decimal, fee_rate: Decimal, ) -> Decimal
Required initial margin for a new order at given notional and leverage. Includes a fee buffer for entry + exit (2x fee_rate).
Sourcepub fn is_liquidated(&self, mark_price: Decimal) -> bool
pub fn is_liquidated(&self, mark_price: Decimal) -> bool
Check if this position is liquidated at the given mark price.
A long is liquidated when mark drops below liq_price. A short is liquidated when mark rises above liq_price.
Sourcepub fn is_order_reducing(&self, order_side: OrderSide) -> bool
pub fn is_order_reducing(&self, order_side: OrderSide) -> bool
Check if an order on this instrument is reducing the current position.
Trait Implementations§
Source§impl Clone for IsolatedPosition
impl Clone for IsolatedPosition
Source§fn clone(&self) -> IsolatedPosition
fn clone(&self) -> IsolatedPosition
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more