Skip to main content

Balance

Struct Balance 

Source
pub struct Balance {
    pub symbol: String,
    pub quantity: Positive,
    pub average_premium: Positive,
    pub current_premium: Option<Positive>,
    pub exchange: String,
    pub underlying_asset_type: UnderlyingAssetType,
    pub margin_info: Option<MarginInfo>,
}
Expand description

Represents the balance of a specific option position in an exchange.

This struct encapsulates all the information needed to track an option position, including quantity, premium information, and profit/loss calculations. This balance is specifically designed for options trading.

Fields§

§symbol: String

Symbol or Epic of the option contract

§quantity: Positive

Quantity of option contracts held

§average_premium: Positive

Average premium at which the option was acquired

§current_premium: Option<Positive>

Current market premium of the option (if available)

§exchange: String

Name of the exchange where the option is held

§underlying_asset_type: UnderlyingAssetType

Type of underlying asset (e.g., Stock, Forex, Index)

§margin_info: Option<MarginInfo>

Margin information for accounts that support leverage

Implementations§

Source§

impl Balance

Source

pub fn new( symbol: String, quantity: Positive, average_premium: Positive, current_premium: Option<Positive>, exchange: String, underlying_asset_type: UnderlyingAssetType, margin_info: Option<MarginInfo>, ) -> Self

Creates a new Balance instance for an option position.

§Arguments
  • symbol - The option contract symbol (e.g., “AAPL240315C00150000”)
  • quantity - The quantity of option contracts held
  • average_premium - The average premium paid for the option
  • current_premium - The current market premium (optional)
  • exchange - The name of the exchange
  • underlying_asset_type - The type of the underlying asset
  • margin_info - Margin information for accounts that support leverage (optional)
§Returns

A new Balance instance for the option position

Source

pub fn get_total_value(&self) -> Result<Positive, PositiveError>

Calculates the total value of the option position based on current premium.

§Returns

The total value (quantity * current_premium) if current_premium is available, otherwise returns the cost basis (quantity * average_premium). The product is formed in Decimal, with no f64 step.

§Errors

Returns PositiveError::ArithmeticError when the product overflows Decimal.

Source

pub fn get_unrealized_pnl(&self) -> Result<Decimal, DecimalError>

Calculates the unrealized profit or loss of the position.

§Returns

The unrealized PnL as a Decimal. Positive values indicate profit, negative values indicate loss. Returns zero if current_price is not available.

§Errors

Returns DecimalError::Overflow when the current value or the cost basis leaves the Decimal range; the multiplication aborted with Multiplication overflowed (#788).

Source

pub fn update_current_premium(&mut self, new_premium: Positive)

Updates the current market premium of the option.

§Arguments
  • new_premium - The new current market premium
Source

pub fn is_profitable(&self) -> Result<bool, DecimalError>

Checks if the position is currently profitable.

§Returns

true if the position has unrealized gains, false otherwise. Returns false if current_price is not available.

§Errors

Propagates the error of Balance::get_unrealized_pnl.

Source

pub fn get_cost_basis(&self) -> Result<Positive, PositiveError>

Gets the cost basis of the option position.

§Returns

The total cost basis (quantity * average_premium), formed in Decimal with no f64 step.

§Errors

Returns PositiveError::ArithmeticError when the product overflows Decimal.

Source

pub fn get_percentage_return(&self) -> Result<Decimal, DecimalError>

Gets the percentage return of the position.

§Returns

The percentage return as a Decimal. Returns zero if current_price is not available, and zero for a zero cost basis, where no return is defined.

§Errors

Returns DecimalError::Overflow when the cost basis, the ratio of the P&L to it, or its scaling to a percentage overflows Decimal.

Trait Implementations§

Source§

impl Clone for Balance

Source§

fn clone(&self) -> Self

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 ComposeSchema for Balance

Source§

impl Debug for Balance

Source§

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

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

impl<'de> Deserialize<'de> for Balance

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 Balance

Source§

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

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

impl PartialEq for Balance

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 Serialize for Balance

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

impl StructuralPartialEq for Balance

Source§

impl ToSchema for Balance

Source§

fn name() -> Cow<'static, str>

Return name of the schema. Read more
Source§

fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>)

Implement reference utoipa::openapi::schema::Schemas for this type. 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<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> PartialSchema for T
where T: ComposeSchema + ?Sized,

Source§

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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