Skip to main content

Position

Struct Position 

Source
pub struct Position {
    pub position: PositionDetails,
    pub market: PositionMarket,
    pub pnl: Option<f64>,
}
Expand description

Individual position

Fields§

§position: PositionDetails

Details of the position

§market: PositionMarket

Market information for the position

§pnl: Option<f64>

Profit and loss for the position

Implementations§

Source§

impl Position

Source

pub fn pnl(&self) -> f64

Calculates the profit and loss (PnL) for the current position of a trader.

The method determines PnL based on whether it is already cached (self.pnl) or needs to be calculated from the position and market details.

§Returns

A floating-point value that represents the PnL for the position. Positive values indicate a profit, and negative values indicate a loss.

§Logic
  • If self.pnl is set, the cached value is returned directly.
  • Otherwise it delegates to Position::pnl_checked, which marks a Buy against market.bid and a Sell against market.offer. When the direction’s market price is unavailable there is no basis for a P&L, so this returns 0.0 (use Position::pnl_checked to distinguish the unknown case as None).
Source

pub fn pnl_checked(&self) -> Option<f64>

Computes P&L from current market prices, or None when the required price for the position’s direction is unavailable.

This is the single source of truth for position P&L: a Buy is marked against market.bid, a Sell against market.offer, as (price - level) * size (sign flipped for a Sell). When that price is missing there is no basis for a P&L, so this returns None rather than fabricating a value — callers that need a number use pnl(), which treats the unknown case as 0.0. Unlike the pre-pnl() field override, this ignores any cached self.pnl.

§Returns

Some(pnl) when the direction’s market price is present, else None.

Source

pub fn update_pnl(&mut self)

Updates the cached profit and loss (PnL) for the current position from current market prices.

Delegates to Position::pnl_checked (a Buy marked against market.bid, a Sell against market.offer) and stores the result in self.pnl. When the direction’s market price is unavailable there is no basis for a P&L, so 0.0 is stored.

§Fields Updated
  • self.pnl: set to Some(pnl), or Some(0.0) when the required market price is missing.
Source§

impl Position

Source

pub fn is_call(&self) -> bool

Checks if the current financial instrument is a call option.

A call option is a financial derivative that gives the holder the right (but not the obligation) to buy an underlying asset at a specified price within a specified time period. This method checks whether the instrument represented by this instance is a call option by inspecting the instrument_name field.

§Returns
  • true if the instrument’s name contains the substring "CALL", indicating it is a call option.
  • false otherwise.
Source

pub fn is_put(&self) -> bool

Checks if the financial instrument is a “PUT” option.

This method examines the instrument_name field of the struct to determine if it contains the substring “PUT”. If the substring is found, the method returns true, indicating that the instrument is categorized as a “PUT” option. Otherwise, it returns false.

§Returns
  • true - If instrument_name contains the substring “PUT”.
  • false - If instrument_name does not contain the substring “PUT”.

Trait Implementations§

Source§

impl Add for Position

Source§

fn add(self, other: Position) -> Position

Adds two positions together.

§Invariants

Both positions must belong to the same market (same EPIC). In debug builds, adding positions from different markets will panic. In release builds, the left-hand side market is used.

Source§

type Output = Position

The resulting type after applying the + operator.
Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Position

Source§

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

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

impl<'de> Deserialize<'de> for Position

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Position

Source§

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

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

impl Serialize for Position

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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
Source§

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

Source§

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

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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 = Infallible

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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