pub struct Position {
pub position: PositionDetails,
pub market: PositionMarket,
pub pnl: Option<f64>,
}Expand description
Individual position
Fields§
§position: PositionDetailsDetails of the position
market: PositionMarketMarket information for the position
pnl: Option<f64>Profit and loss for the position
Implementations§
Source§impl Position
impl Position
Sourcepub fn pnl(&self) -> f64
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.pnlis set, the cached value is returned directly. - Otherwise it delegates to
Position::pnl_checked, which marks a Buy againstmarket.bidand a Sell againstmarket.offer. When the direction’s market price is unavailable there is no basis for a P&L, so this returns0.0(usePosition::pnl_checkedto distinguish the unknown case asNone).
Sourcepub fn pnl_checked(&self) -> Option<f64>
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.
Sourcepub fn update_pnl(&mut self)
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 toSome(pnl), orSome(0.0)when the required market price is missing.
Source§impl Position
impl Position
Sourcepub fn is_call(&self) -> bool
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
trueif the instrument’s name contains the substring"CALL", indicating it is a call option.falseotherwise.
Sourcepub fn is_put(&self) -> bool
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- Ifinstrument_namecontains the substring “PUT”.false- Ifinstrument_namedoes not contain the substring “PUT”.
Trait Implementations§
Source§impl Add for Position
impl Add for Position
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>,
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§
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<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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