Skip to main content

PositionData

Struct PositionData 

Source
pub struct PositionData {
Show 18 fields pub id: PositionId, pub symbol: String, pub side: Side, pub order_type: OrderType, pub status: PositionStatus, pub pending_price: Option<f64>, pub size: f64, pub entries: Vec<Fill>, pub remaining_ratio: f64, pub closed_size: f64, pub open_entry_value: f64, pub target_hits: u32, pub open_ts: Option<NaiveDateTime>, pub close_ts: Option<NaiveDateTime>, pub group: Option<GroupId>, pub trade_id: Option<TradeId>, pub stop_origin: Option<StopOrigin>, pub records: Vec<(PositionRecord, NaiveDateTime)>,
}
Expand description

Core position data — the pure state without rules.

Fields§

§id: PositionId

Unique identifier.

§symbol: String

Instrument symbol (e.g. “EURUSD”, “XAUUSD”).

§side: Side

Trade direction.

§order_type: OrderType

How the order was placed.

§status: PositionStatus

Current lifecycle status.

§pending_price: Option<f64>

For Limit/Stop orders: the price at which the order should fill.

§size: f64

Intended order size (lots / units).

§entries: Vec<Fill>

Actual execution fills (one for market, potentially many for scale-in).

§remaining_ratio: f64

Fraction of all entered size still open (1.0 = full, 0.0 = closed).

Retained for wire compatibility and rule views. Absolute sizes are the source of truth and every core mutation keeps this value synchronized.

§closed_size: f64

Absolute size closed across all partial and full exits.

Entry size is the sum of entries; open size is derived as entered size minus this value. Older serialized positions infer this from remaining_ratio during deserialization.

§open_entry_value: f64

Cost basis assigned to the inventory that is still open.

Positions use average-cost accounting: every close releases average_entry * close_size from this value, while a scale-in adds only the new fill’s value. Historical entries remain unchanged for audit. Older serialized positions infer this from their historical weighted average and remaining size.

§target_hits: u32

Number of take-profit levels that have been hit. Used by BreakevenAfterTargets rule.

§open_ts: Option<NaiveDateTime>

When the position first filled.

§close_ts: Option<NaiveDateTime>

When the position was fully closed.

§group: Option<GroupId>

Optional group for per-signal-source tracking and group-level actions.

§trade_id: Option<TradeId>

Optional application-defined trade identity.

Parsers mint a stable TradeId (for example, chat_id:msg_id) and reference it from later management signals via PositionRef::ByTradeId. When None, management signals must use bulk references or the engine’s PositionId.

§stop_origin: Option<StopOrigin>

Provenance of the current fixed protective stop.

§records: Vec<(PositionRecord, NaiveDateTime)>

Immutable audit trail.

Implementations§

Source§

impl PositionData

Source

pub fn average_entry(&self) -> f64

Average-cost entry price of the inventory that is still open.

Historical fills are intentionally not re-averaged here: after a partial close, only the remaining inventory basis participates in a later scale-in and subsequent close.

Source

pub fn historical_average_entry(&self) -> f64

Volume-weighted average across all historical entry fills.

Source

pub fn total_filled_size(&self) -> f64

Total filled size (sum of all fills).

Source

pub fn remaining_size(&self) -> f64

Size still active in the market, derived from absolute quantities.

Source

pub fn open_ratio(&self) -> f64

Fraction of all entered size that is still open.

Source

pub fn capped_close_ratio(&self, ratio: f64) -> f64

Cap an original-entered-size close ratio to the exposure still open.

Source

pub fn close_size_for_ratio(&self, ratio: f64) -> f64

Absolute size represented by a close ratio, capped to open exposure.

Source

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

Unrealised P&L at the given price.

Source

pub fn is_active(&self) -> bool

Whether the position is live (Open) and has remaining size.

Source

pub fn add_fill(&mut self, fill: Fill)

Add a fill (scale-in), preserving previously closed absolute size and adding the fill only to active inventory cost basis.

Source

pub fn replace_latest_fill_execution( &mut self, price: f64, ts: NaiveDateTime, ) -> bool

Replace the most recent entry fill price and timestamp.

Future-quote backtests use this after a pending order triggers so the engine’s average entry matches the authoritative gap-aware execution fill produced by the execution pricer. Returns false when no fill exists or the replacement is invalid.

Source

pub fn synchronize_latest_fill(&mut self, fill: Fill) -> bool

Replace the latest entry fill and its audit record.

Future-quote executors use this after the engine transitions a pending order to Open, keeping core position state synchronized with the externally calculated gap/improvement execution price.

Source

pub fn apply_partial_close( &mut self, ratio: f64, price: f64, reason: CloseReason, ts: NaiveDateTime, )

Record a partial close using an original-entered-size ratio.

The close is capped to the absolute size still open. If no exposure remains, the status is flipped to Closed and both absolute and ratio accounting reach exact zero.

Source

pub fn apply_full_close(&mut self, reason: CloseReason, ts: NaiveDateTime)

Mark the position as fully closed.

Source

pub fn view(&self) -> PositionView<'_>

Create a read-only view for rule evaluation.

Trait Implementations§

Source§

impl Clone for PositionData

Source§

fn clone(&self) -> PositionData

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 Debug for PositionData

Source§

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

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

impl<'de> Deserialize<'de> for PositionData

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 PositionData

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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V