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 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 the market.bid price or fallback value) and the original value (based on the position’s size and level).
    • For a Sell position:
      • The PnL is calculated as the difference between the original value and the current_value (based on the market.offer price or fallback value).
§Assumptions
  • The market.bid and market.offer values are optional, so fallback to the original position value is used if they are unavailable.
  • self.position.direction must be either Direction::Buy or Direction::Sell.
Source

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 bid price from the market, or use the initial value if the bid price is not available.
    • PnL is the difference between the current value and the initial value.
  • 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 offer price from the market, or use the initial value if the offer price is not available.
    • PnL is the difference between the initial value and the current value.
§Fields Updated:
  • self.pnl: The calculated profit or loss is updated in this field. If no valid market price (bid/offer) is available, pnl will 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.

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§

type Output = Position

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

Returns a duplicate of the value. Read more
1.0.0§

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§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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
§

impl<T> From<T> for T

§

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
§

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

§

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: 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: 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> Same for T

Source§

type Output = T

Should always be Self
§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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

§

fn to_string(&self) -> String

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

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

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