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 available, it directly returns the cached value. - If not, the PnL is calculated based on the direction of the position:
- For a Buy position:
- The PnL is calculated as the difference between the
current_value(based on themarket.bidprice or fallback value) and the originalvalue(based on the position’s size and level).
- The PnL is calculated as the difference between the
- For a Sell position:
- The PnL is calculated as the difference between the original
valueand thecurrent_value(based on themarket.offerprice or fallback value).
- The PnL is calculated as the difference between the original
- For a Buy position:
§Assumptions
- The
market.bidandmarket.offervalues are optional, so fallback to the original position value is used if they are unavailable. self.position.directionmust be eitherDirection::BuyorDirection::Sell.
Sourcepub fn update_pnl(&mut self)
pub fn update_pnl(&mut self)
Updates the profit and loss (PnL) for the current position in the market.
The method calculates the PnL based on the position’s direction (Buy or Sell),
size, level (entry price), and the current bid or offer price from the market data.
The result is stored in the pnl field.
§Calculation:
- If the position is a Buy:
- Calculate the initial value of the position as
size * level. - Calculate the current value of the position using the current
bidprice from the market, or use the initial value if thebidprice is not available. - PnL is the difference between the current value and the initial value.
- Calculate the initial value of the position as
- If the position is a Sell:
- Calculate the initial value of the position as
size * level. - Calculate the current value of the position using the current
offerprice from the market, or use the initial value if theofferprice is not available. - PnL is the difference between the initial value and the current value.
- Calculate the initial value of the position as
§Fields Updated:
self.pnl: The calculated profit or loss is updated in this field. If no valid market price (bid/offer) is available,pnlwill be calculated based on the initial value.
§Panics:
This function does not explicitly panic but relies on the unwrap_or method to handle cases
where the bid or offer is unavailable. It assumes that the market or position data are initialized correctly.
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>,
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.