Skip to main content

Position

Struct Position 

Source
pub struct Position {
    pub symbol: Symbol,
    pub quantity: Decimal,
    pub avg_cost: Decimal,
    pub realized_pnl: Decimal,
    pub open_bar: usize,
}
Expand description

A single-symbol position tracking quantity, average cost, and realized P&L.

Fields§

§symbol: Symbol

The instrument.

§quantity: Decimal

Current net quantity (positive = long, negative = short, zero = flat).

§avg_cost: Decimal

Volume-weighted average cost of the current position.

§realized_pnl: Decimal

Cumulative realized P&L for this position (net of commissions).

§open_bar: usize

Bar index at which the current position leg was opened. Set via Position::set_open_bar.

Implementations§

Source§

impl Position

Source

pub fn new(symbol: Symbol) -> Self

Creates a new flat Position for symbol.

Source

pub fn set_open_bar(&mut self, bar: usize)

Records the bar index at which the current position leg was opened.

Call this whenever transitioning from flat to a new position.

Source

pub fn position_age_bars(&self, current_bar: usize) -> usize

Returns how many bars the current position has been open.

age = current_bar - self.open_bar (saturating at 0).

Source

pub fn max_favorable_excursion(&self, prices: &[Price]) -> Option<Decimal>

Maximum favorable excursion (MFE): the best unrealized P&L seen across prices.

For a long position, this is max(price - avg_cost) * quantity. For a short position, this is max(avg_cost - price) * |quantity|.

Returns None when the position is flat, avg_cost is zero, or prices is empty.

Source

pub fn kelly_fraction( win_rate: Decimal, avg_win: Decimal, avg_loss: Decimal, ) -> Option<Decimal>

Kelly fraction: optimal bet size as a fraction of capital.

Kelly = win_rate - (1 - win_rate) / (avg_win / avg_loss)

Returns None when avg_loss or avg_win is zero. The result is clamped to [0, 1] — never bet more than 100% or go short via Kelly.

Source

pub fn apply_fill(&mut self, fill: &Fill) -> Result<Decimal, FinError>

Applies a fill, updating quantity, avg_cost, and realized_pnl.

§Returns

The realized P&L contributed by this fill (0 if position is increasing).

§Errors

Returns FinError::ArithmeticOverflow on checked arithmetic failure.

Source

pub fn unrealized_pnl(&self, current_price: Price) -> Decimal

Returns unrealized P&L at current_price.

Source

pub fn checked_unrealized_pnl( &self, current_price: Price, ) -> Result<Decimal, FinError>

Returns unrealized P&L at current_price, returning Err on arithmetic overflow.

Source

pub fn unrealized_pnl_pct(&self, current_price: Price) -> Option<Decimal>

Returns unrealized P&L as a percentage of cost basis at current_price.

pct = unrealized_pnl / (|quantity| × avg_cost) × 100. Returns None if the position is flat or avg_cost is zero.

Source

pub fn total_cost_basis(&self) -> Decimal

Returns the total cost basis: |quantity| * avg_cost.

Represents the total capital committed to this position. Returns zero for flat positions.

Source

pub fn market_value(&self, current_price: Price) -> Decimal

Returns the market value of this position at current_price.

Source

pub fn is_flat(&self) -> bool

Returns true if the position is flat (zero quantity).

Source

pub fn is_long(&self) -> bool

Returns true if the position is long (positive quantity).

Source

pub fn is_short(&self) -> bool

Returns true if the position is short (negative quantity).

Source

pub fn direction(&self) -> PositionDirection

Returns the direction of the position.

Source

pub fn total_pnl(&self, current_price: Price) -> Decimal

Returns total P&L: realized_pnl + unrealized_pnl(current_price).

Source

pub fn quantity_abs(&self) -> Decimal

Returns the absolute magnitude of the current quantity.

Source

pub fn cost_basis(&self) -> Decimal

Returns the cost basis of the current position: avg_cost * |quantity|.

Represents total capital deployed, excluding any realized P&L. Returns 0 when the position is flat.

Source

pub fn is_profitable(&self, current_price: Price) -> bool

Returns true if unrealized PnL at current_price is strictly positive.

Source

pub fn avg_entry_price(&self) -> Option<Price>

Returns the average entry price as a Price, or None if the position is flat.

This is avg_cost expressed as a validated Price. Returns None when avg_cost == 0 (no open position).

Source

pub fn exposure_pct( &self, current_price: Price, total_portfolio_value: Decimal, ) -> Option<Decimal>

Returns the position’s current market value as a percentage of total_portfolio_value.

exposure_pct = |quantity × current_price| / total_portfolio_value × 100

Returns None when total_portfolio_value is zero, the position is flat, or current_price is zero.

Source

pub fn stop_loss_price(&self, stop_pct: Decimal) -> Option<Price>

Returns the stop-loss price at stop_pct percent below (long) or above (short) entry.

  • Long: stop = avg_cost × (1 - stop_pct / 100)
  • Short: stop = avg_cost × (1 + stop_pct / 100)

Returns None when the position is flat or avg_cost is zero.

§Example
// A 2% stop loss on a long position at avg_cost=100 → stop at 98
position.stop_loss_price(dec!(2)).unwrap() == Price::new(dec!(98)).unwrap()
Source

pub fn take_profit_price(&self, tp_pct: Decimal) -> Option<Price>

Returns the take-profit price for the current position at tp_pct percent gain.

Returns None when the position is flat or avg_cost is zero. For a long position, the take-profit price is avg_cost * (1 + tp_pct / 100). For a short position, the take-profit price is avg_cost * (1 - tp_pct / 100).

Source

pub fn margin_requirement(&self, margin_pct: Decimal) -> Option<Decimal>

Returns the margin requirement for the current position: |net_quantity| × avg_cost × margin_pct / 100.

Returns None if the position is flat or avg_cost is zero.

Source

pub fn risk_reward_ratio(stop_pct: Decimal, target_pct: Decimal) -> Option<f64>

Returns the risk/reward ratio: target_pct / stop_pct.

This is a pure calculation and does not depend on position state. Returns None if stop_pct is zero or negative.

Source

pub fn leverage(&self, portfolio_value: Decimal) -> Option<Decimal>

Leverage: |quantity × avg_cost| / portfolio_value.

Returns None if the position is flat, avg_cost is zero, or portfolio_value is zero.

Trait Implementations§

Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

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

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,