#[repr(C)]pub struct Position {Show 38 fields
pub events: Vec<OrderFilled>,
pub adjustments: Vec<PositionAdjusted>,
pub replay_events: Vec<PositionReplayEvent>,
pub fill_voids: Vec<PositionFillVoid>,
pub trader_id: TraderId,
pub strategy_id: StrategyId,
pub instrument_id: InstrumentId,
pub id: PositionId,
pub account_id: AccountId,
pub opening_order_id: ClientOrderId,
pub closing_order_id: Option<ClientOrderId>,
pub entry: OrderSide,
pub side: PositionSide,
pub signed_qty: f64,
pub quantity: Quantity,
pub peak_qty: Quantity,
pub price_precision: u8,
pub size_precision: u8,
pub multiplier: Quantity,
pub is_inverse: bool,
pub is_currency_pair: bool,
pub instrument_class: InstrumentClass,
pub base_currency: Option<Currency>,
pub quote_currency: Currency,
pub settlement_currency: Currency,
pub ts_init: UnixNanos,
pub ts_opened: UnixNanos,
pub ts_last: UnixNanos,
pub ts_closed: Option<UnixNanos>,
pub duration_ns: u64,
pub avg_px_open: f64,
pub avg_px_close: Option<f64>,
pub realized_return: f64,
pub realized_pnl: Option<Money>,
pub trade_ids: AHashSet<TradeId>,
pub buy_qty: Quantity,
pub sell_qty: Quantity,
pub commissions: IndexMap<Currency, Money>,
}Expand description
Represents a position in a market.
The position ID may be assigned at the trading venue, or can be system generated depending on a strategies OMS (Order Management System) settings. Replay events and cumulative fill corrections preserve derived state across close and reopen cycles.
Fields§
§events: Vec<OrderFilled>§adjustments: Vec<PositionAdjusted>§replay_events: Vec<PositionReplayEvent>§fill_voids: Vec<PositionFillVoid>§trader_id: TraderId§strategy_id: StrategyId§instrument_id: InstrumentId§id: PositionId§account_id: AccountId§opening_order_id: ClientOrderId§closing_order_id: Option<ClientOrderId>§entry: OrderSide§side: PositionSide§signed_qty: f64§quantity: Quantity§peak_qty: Quantity§price_precision: u8§size_precision: u8§multiplier: Quantity§is_inverse: bool§is_currency_pair: bool§instrument_class: InstrumentClass§base_currency: Option<Currency>§quote_currency: Currency§settlement_currency: Currency§ts_init: UnixNanos§ts_opened: UnixNanos§ts_last: UnixNanos§ts_closed: Option<UnixNanos>§duration_ns: u64§avg_px_open: f64§avg_px_close: Option<f64>§realized_return: f64§realized_pnl: Option<Money>§trade_ids: AHashSet<TradeId>§buy_qty: Quantity§sell_qty: Quantity§commissions: IndexMap<Currency, Money>Implementations§
Source§impl Position
impl Position
Sourcepub fn new(instrument: &InstrumentAny, fill: OrderFilled) -> Self
pub fn new(instrument: &InstrumentAny, fill: OrderFilled) -> Self
Sourcepub fn purge_events_for_order(&mut self, client_order_id: ClientOrderId)
pub fn purge_events_for_order(&mut self, client_order_id: ClientOrderId)
Purges all order fill events for the given client order ID and recalculates derived state.
§Warning
This operation recalculates the entire position from scratch after removing the specified order’s fills. This is an expensive operation and should be used sparingly.
§Panics
Panics if after purging, no fills remain and the position cannot be reconstructed.
Sourcepub fn apply(&mut self, fill: &OrderFilled)
pub fn apply(&mut self, fill: &OrderFilled)
Applies an OrderFilled event to this position.
§Panics
Panics if the fill.trade_id is already present in the position’s trade_ids.
Sourcepub fn apply_adjustment(&mut self, adjustment: PositionAdjusted)
pub fn apply_adjustment(&mut self, adjustment: PositionAdjusted)
Applies a position adjustment event.
This method handles adjustments to position quantity or realized PnL that occur outside of normal order fills, such as:
- Commission adjustments in base currency (crypto spot markets).
- Funding payments (perpetual futures).
The adjustment event is stored in the position’s adjustment history for full audit trail.
§Panics
Panics if the adjustment’s quantity_change cannot be converted to f64.
Sourcepub fn apply_fill_void(
&mut self,
event: OrderFillVoided,
voided_qty: Quantity,
commission_voided: Option<Money>,
) -> Result<Option<Money>>
pub fn apply_fill_void( &mut self, event: OrderFillVoided, voided_qty: Quantity, commission_voided: Option<Money>, ) -> Result<Option<Money>>
Applies a cumulative fill correction allocated to this position and rebuilds derived state.
Returns the realized PnL of the cycles the rebuild closed before the current one, which
Self::realized_pnl no longer holds because reopening from flat resets it. A caller
archiving closed cycles needs this to keep their PnL once the correction has moved the
cycle boundaries its existing archive describes. None when the corrected history never
goes flat, so the current cycle covers all of it.
§Errors
Returns an error when the allocation is stale, duplicated, or exceeds known fragments.
Sourcepub fn fill_fragments(
&self,
client_order_id: ClientOrderId,
trade_id: TradeId,
) -> Vec<&OrderFilled>
pub fn fill_fragments( &self, client_order_id: ClientOrderId, trade_id: TradeId, ) -> Vec<&OrderFilled>
Returns durable fill fragments matching an order trade in local application order.
Sourcepub fn try_calculate_pnl(
&self,
avg_px_open: f64,
avg_px_close: f64,
quantity: Quantity,
) -> Result<Money>
pub fn try_calculate_pnl( &self, avg_px_open: f64, avg_px_close: f64, quantity: Quantity, ) -> Result<Money>
Sourcepub fn calculate_pnl(
&self,
avg_px_open: f64,
avg_px_close: f64,
quantity: Quantity,
) -> Money
pub fn calculate_pnl( &self, avg_px_open: f64, avg_px_close: f64, quantity: Quantity, ) -> Money
Calculates profit and loss from the given prices and quantity.
Sourcepub fn try_total_pnl(&self, last: Price) -> Result<Money>
pub fn try_total_pnl(&self, last: Price) -> Result<Money>
Sourcepub fn total_pnl(&self, last: Price) -> Money
pub fn total_pnl(&self, last: Price) -> Money
Returns total P&L (realized + unrealized) based on the last price.
Sourcepub fn try_unrealized_pnl(&self, last: Price) -> Result<Money>
pub fn try_unrealized_pnl(&self, last: Price) -> Result<Money>
Sourcepub fn unrealized_pnl(&self, last: Price) -> Money
pub fn unrealized_pnl(&self, last: Price) -> Money
Returns unrealized P&L based on the last price.
Sourcepub fn closing_order_side(&self) -> OrderSide
pub fn closing_order_side(&self) -> OrderSide
Returns the order side required to close this position.
Sourcepub fn is_opposite_side(&self, side: OrderSide) -> bool
pub fn is_opposite_side(&self, side: OrderSide) -> bool
Returns whether the given order side is opposite to the position entry side.
Sourcepub fn event_count(&self) -> usize
pub fn event_count(&self) -> usize
Returns the count of order fill events applied to this position.
Sourcepub fn client_order_ids(&self) -> Vec<ClientOrderId>
pub fn client_order_ids(&self) -> Vec<ClientOrderId>
Returns unique client order IDs from all fill events, sorted.
Sourcepub fn venue_order_ids(&self) -> Vec<VenueOrderId>
pub fn venue_order_ids(&self) -> Vec<VenueOrderId>
Returns unique venue order IDs from all fill events, sorted.
Sourcepub fn try_notional_value(&self, last: Price) -> Result<Money>
pub fn try_notional_value(&self, last: Price) -> Result<Money>
Sourcepub fn notional_value(&self, last: Price) -> Money
pub fn notional_value(&self, last: Price) -> Money
Calculates the notional value based on the last price.
§Panics
Panics if Position::try_notional_value returns an error.
Sourcepub fn last_event(&self) -> Option<OrderFilled>
pub fn last_event(&self) -> Option<OrderFilled>
Returns the last OrderFilled event for the position (if any after purging).
Sourcepub fn last_trade_id(&self) -> Option<TradeId>
pub fn last_trade_id(&self) -> Option<TradeId>
Returns the last TradeId for the position (if any after purging).
Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Returns whether the position is currently open (has quantity and no close timestamp).
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns whether the position is closed (flat with a close timestamp).
Sourcepub fn signed_decimal_qty(&self) -> Decimal
pub fn signed_decimal_qty(&self) -> Decimal
Returns the signed quantity as a Decimal.
Uses the raw signed_qty field to preserve full precision, as the quantity
field may have reduced precision based on the instrument’s size_precision.
Sourcepub fn commissions(&self) -> Vec<Money>
pub fn commissions(&self) -> Vec<Money>
Returns the cumulative commissions for the position as a vector.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnsafeUnpin for Position
impl UnwindSafe for Position
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.