Skip to main content

SpotPosition

Struct SpotPosition 

Source
pub struct SpotPosition {
    pub symbol: String,
    pub quantity: Positive,
    pub cost_basis: Positive,
    pub side: Side,
    pub date: DateTime<Utc>,
    pub open_fee: Positive,
    pub close_fee: Positive,
}
Expand description

Represents a spot/underlying asset position.

A spot position represents direct ownership of an asset without any derivative characteristics. This includes stocks, cryptocurrency spot holdings, forex spot, and commodities held for delivery.

§Fields

  • symbol - The ticker symbol or identifier of the asset
  • quantity - The number of units held
  • cost_basis - The average acquisition price per unit
  • side - Whether the position is Long (owned) or Short (borrowed/sold)
  • date - The timestamp when the position was opened
  • open_fee - Transaction fee paid to open the position
  • close_fee - Transaction fee to be paid when closing the position

Fields§

§symbol: String

The ticker symbol or identifier of the underlying asset.

§quantity: Positive

The number of units held in this position.

§cost_basis: Positive

The average cost per unit at which the position was acquired.

§side: Side

The direction of the position (Long = owned, Short = borrowed).

§date: DateTime<Utc>

The timestamp when this position was opened.

§open_fee: Positive

The fee paid to open this position.

§close_fee: Positive

The fee to be paid when closing this position.

Implementations§

Source§

impl SpotPosition

Source

pub fn new( symbol: String, quantity: Positive, cost_basis: Positive, side: Side, date: DateTime<Utc>, open_fee: Positive, close_fee: Positive, ) -> Self

Creates a new spot position.

§Arguments
  • symbol - The ticker symbol of the asset
  • quantity - Number of units to hold
  • cost_basis - Average price per unit
  • side - Long or Short position
  • date - Position open timestamp
  • open_fee - Fee to open the position
  • close_fee - Fee to close the position
§Returns

A new SpotPosition instance.

Source

pub fn long(symbol: String, quantity: Positive, cost_basis: Positive) -> Self

Creates a long spot position with default fees.

§Arguments
  • symbol - The ticker symbol
  • quantity - Number of units
  • cost_basis - Price per unit
§Returns

A new long SpotPosition with zero fees.

Source

pub fn short(symbol: String, quantity: Positive, cost_basis: Positive) -> Self

Creates a short spot position with default fees.

§Arguments
  • symbol - The ticker symbol
  • quantity - Number of units
  • cost_basis - Price per unit (entry price for short)
§Returns

A new short SpotPosition with zero fees.

Source

pub fn initial_value(&self) -> Result<Positive, PositionError>

Returns the total value of the position at the cost basis.

This represents the initial investment (for long) or proceeds (for short).

§Errors

Returns PositionError::PositiveError when the product leaves the Positive range; Positive * Positive aborted on it (#788).

Source

pub fn market_value( &self, current_price: Positive, ) -> Result<Positive, PositionError>

Returns the current market value at a given price.

§Arguments
  • current_price - The current market price per unit
§Errors

Returns PositionError::PositiveError when the product leaves the Positive range (#788).

Source

pub fn percentage_return( &self, current_price: Positive, ) -> Result<Decimal, PositionError>

Calculates the percentage return on the position.

§Arguments
  • current_price - The current market price per unit
§Returns

The percentage return as a Decimal (e.g., 0.10 = 10% gain).

§Errors

Returns PositionError::DecimalError when the return leaves the Decimal range: a cost basis near zero against a large price, where the division aborted with Division overflowed (#788).

Source

pub fn break_even_price(&self) -> Result<Positive, PositionError>

Calculates the break-even price including fees.

For long positions: cost_basis + (total_fees / quantity) For short positions: cost_basis - (total_fees / quantity), floored at zero.

§Errors

Returns PositionError::PositiveError when the fee total, the fee per unit or the long break-even leaves the Positive range: a quantity near zero against large fees, where the division aborted with Positive arithmetic overflow in div (#788).

Trait Implementations§

Source§

impl Clone for SpotPosition

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 SpotPosition

Source§

impl Debug for SpotPosition

Source§

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

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

impl Default for SpotPosition

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for SpotPosition

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 SpotPosition

Source§

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

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

impl From<SpotPosition> for Leg

Source§

fn from(position: SpotPosition) -> Self

Converts to this type from the input type.
Source§

impl LegAble for SpotPosition

Source§

fn get_symbol(&self) -> &str

Returns the symbol/ticker of the underlying instrument.
Source§

fn get_quantity(&self) -> Positive

Returns the position quantity in the leg’s native unit (#830). Read more
Source§

fn get_side(&self) -> Side

Returns the position side (Long or Short).
Source§

fn pnl_at_price(&self, price: Positive) -> Result<Decimal, PositionError>

Calculates the profit/loss at a given price. Read more
Source§

fn total_cost(&self) -> Result<Positive, PositionError>

Returns the total cost to establish this position. Read more
Source§

fn fees(&self) -> Result<Positive, PositionError>

Returns the total fees associated with this position. Read more
Source§

fn is_long(&self) -> bool

Checks if this is a long position.
Source§

fn is_short(&self) -> bool

Checks if this is a short position.
Source§

fn notional_value(&self, price: Positive) -> Result<Positive, PositionError>

Returns the notional value of the position at a given price. Read more
Source§

impl PartialEq for SpotPosition

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 SpotPosition

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 SpotPosition

Source§

impl ToSchema for SpotPosition

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