Skip to main content

PositionRef

Struct PositionRef 

Source
pub struct PositionRef<'a>(/* private fields */);
Expand description

Lifetime-scoped read borrow of a cached position.

Returned by crate::cache::Cache::position. The borrow drops with the enclosing scope, which makes any attempt to hold it across a cache mutation panic at runtime: a loud failure beats the silent staleness that a stored clone would produce.

Method calls on the inner [Position] resolve via Deref; comparisons against &Position or owned Position values are direct (position_ref == &position); Display and Debug forward to the inner record. Use cloned when an owned snapshot is required (for example, before crossing a boundary that may dispatch events).

Implementations§

Source§

impl<'a> PositionRef<'a>

Source

pub fn new(inner: Ref<'a, Position>) -> Self

Wraps the given Ref borrow.

Source

pub fn cloned(&self) -> Position

Returns an owned snapshot of the borrowed position.

Mirrors Option::cloned and Iterator::cloned; the snapshot will not reflect later mutations of the underlying cell.

Methods from Deref<Target = Position>§

pub fn clone_without_events(&self) -> Position

Returns a copy without stored events, adjustments, replay events, fill voids, or trade IDs.

§Warning

Use this copy only as transient read state. Applying events or caching this copy can bypass replay and duplicate-fill checks and discard position history.

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.

pub fn try_calculate_pnl( &self, avg_px_open: f64, avg_px_close: f64, quantity: Quantity, ) -> Result<Money, Error>

Calculates profit and loss from the given prices and quantity.

§Errors

Returns an error if inverse P&L cannot be calculated or the result cannot be represented as [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.

pub fn try_total_pnl(&self, last: Price) -> Result<Money, Error>

Returns total P&L (realized + unrealized) based on the last price.

§Errors

Returns an error if unrealized P&L cannot be calculated, the realized and unrealized currencies differ, or the total cannot be represented as [Money].

pub fn total_pnl(&self, last: Price) -> Money

Returns total P&L (realized + unrealized) based on the last price.

pub fn try_unrealized_pnl(&self, last: Price) -> Result<Money, Error>

Returns unrealized P&L based on the last price.

§Errors

Returns an error if inverse P&L cannot be calculated or the result cannot be represented as [Money].

pub fn unrealized_pnl(&self, last: Price) -> Money

Returns unrealized P&L based on the last price.

pub fn closing_order_side(&self) -> Option<OrderSide>

Returns the order side required to close this position.

pub fn is_opposite_side(&self, side: OrderSide) -> bool

Returns whether the given order side is opposite to the position entry side.

pub fn symbol(&self) -> Symbol

Returns the instrument symbol.

pub fn venue(&self) -> Venue

Returns the trading venue.

pub fn event_count(&self) -> usize

Returns the count of order fill events applied to this position.

pub fn client_order_ids(&self) -> Vec<ClientOrderId>

Returns unique client order IDs from all fill events, sorted.

pub fn venue_order_ids(&self) -> Vec<VenueOrderId>

Returns unique venue order IDs from all fill events, sorted.

pub fn trade_ids(&self) -> Vec<TradeId>

Returns unique trade IDs from all fill events, sorted.

pub fn try_notional_value(&self, last: Price) -> Result<Money, Error>

Calculates the notional value based on the last price.

§Errors

Returns an error if this is an inverse position without a base currency, the price is not positive for inverse valuation, or the result cannot be represented as [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.

pub fn last_event(&self) -> Option<OrderFilled>

Returns the last OrderFilled event for the position (if any after purging).

pub fn last_trade_id(&self) -> Option<TradeId>

Returns the last TradeId for the position (if any after purging).

pub fn is_long(&self) -> bool

Returns whether the position is long (positive quantity).

pub fn is_short(&self) -> bool

Returns whether the position is short (negative quantity).

pub fn is_open(&self) -> bool

Returns whether the position is currently open (has quantity and no close timestamp).

pub fn is_closed(&self) -> bool

Returns whether the position is closed (flat with a close timestamp).

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.

pub fn commissions(&self) -> Vec<Money>

Returns the cumulative commissions for the position as a vector.

Trait Implementations§

Source§

impl AsRef<Position> for PositionRef<'_>

Source§

fn as_ref(&self) -> &Position

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for PositionRef<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for PositionRef<'_>

Source§

type Target = Position

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for PositionRef<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<Ref<'a, Position>> for PositionRef<'a>

Source§

fn from(inner: Ref<'a, Position>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for PositionRef<'_>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<&Position> for PositionRef<'_>

Source§

fn eq(&self, other: &&Position) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<Position> for PositionRef<'_>

Source§

fn eq(&self, other: &Position) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for PositionRef<'a>

§

impl<'a> !Send for PositionRef<'a>

§

impl<'a> !Sync for PositionRef<'a>

§

impl<'a> !UnwindSafe for PositionRef<'a>

§

impl<'a> Freeze for PositionRef<'a>

§

impl<'a> Unpin for PositionRef<'a>

§

impl<'a> UnsafeUnpin for PositionRef<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more