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: StringSymbol or Epic of the option contract
quantity: PositiveQuantity of option contracts held
Average premium at which the option was acquired
Current market premium of the option (if available)
exchange: StringName of the exchange where the option is held
underlying_asset_type: UnderlyingAssetTypeType of underlying asset (e.g., Stock, Forex, Index)
margin_info: Option<MarginInfo>Margin information for accounts that support leverage
Implementations§
Source§impl Balance
impl Balance
Sourcepub fn new(
symbol: String,
quantity: Positive,
average_premium: Positive,
current_premium: Option<Positive>,
exchange: String,
underlying_asset_type: UnderlyingAssetType,
margin_info: Option<MarginInfo>,
) -> Self
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 heldaverage_premium- The average premium paid for the optioncurrent_premium- The current market premium (optional)exchange- The name of the exchangeunderlying_asset_type- The type of the underlying assetmargin_info- Margin information for accounts that support leverage (optional)
§Returns
A new Balance instance for the option position
Sourcepub fn get_total_value(&self) -> Result<Positive, PositiveError>
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.
Sourcepub fn get_unrealized_pnl(&self) -> Result<Decimal, DecimalError>
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).
Updates the current market premium of the option.
§Arguments
new_premium- The new current market premium
Sourcepub fn is_profitable(&self) -> Result<bool, DecimalError>
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.
Sourcepub fn get_cost_basis(&self) -> Result<Positive, PositiveError>
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.
Sourcepub fn get_percentage_return(&self) -> Result<Decimal, DecimalError>
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.