#[repr(C)]pub struct Position {Show 36 fields
pub events: Vec<OrderFilled>,
pub adjustments: Vec<PositionAdjusted>,
pub trader_id: TraderId,
pub strategy_id: StrategyId,
pub instrument_id: InstrumentId,
pub id: PositionId,
pub account_id: AccountId,
pub opening_order_id: ClientOrderId,
pub closing_order_id: Option<ClientOrderId>,
pub entry: OrderSide,
pub side: PositionSide,
pub signed_qty: f64,
pub quantity: Quantity,
pub peak_qty: Quantity,
pub price_precision: u8,
pub size_precision: u8,
pub multiplier: Quantity,
pub is_inverse: bool,
pub is_currency_pair: bool,
pub instrument_class: InstrumentClass,
pub base_currency: Option<Currency>,
pub quote_currency: Currency,
pub settlement_currency: Currency,
pub ts_init: UnixNanos,
pub ts_opened: UnixNanos,
pub ts_last: UnixNanos,
pub ts_closed: Option<UnixNanos>,
pub duration_ns: u64,
pub avg_px_open: f64,
pub avg_px_close: Option<f64>,
pub realized_return: f64,
pub realized_pnl: Option<Money>,
pub trade_ids: Vec<TradeId>,
pub buy_qty: Quantity,
pub sell_qty: Quantity,
pub commissions: HashMap<Currency, Money>,
}Expand description
Represents a position in a market.
The position ID may be assigned at the trading venue, or can be system generated depending on a strategies OMS (Order Management System) settings.
Fields§
§events: Vec<OrderFilled>§adjustments: Vec<PositionAdjusted>§trader_id: TraderId§strategy_id: StrategyId§instrument_id: InstrumentId§id: PositionId§account_id: AccountId§opening_order_id: ClientOrderId§closing_order_id: Option<ClientOrderId>§entry: OrderSide§side: PositionSide§signed_qty: f64§quantity: Quantity§peak_qty: Quantity§price_precision: u8§size_precision: u8§multiplier: Quantity§is_inverse: bool§is_currency_pair: bool§instrument_class: InstrumentClass§base_currency: Option<Currency>§quote_currency: Currency§settlement_currency: Currency§ts_init: UnixNanos§ts_opened: UnixNanos§ts_last: UnixNanos§ts_closed: Option<UnixNanos>§duration_ns: u64§avg_px_open: f64§avg_px_close: Option<f64>§realized_return: f64§realized_pnl: Option<Money>§trade_ids: Vec<TradeId>§buy_qty: Quantity§sell_qty: Quantity§commissions: HashMap<Currency, Money>Implementations§
Source§impl Position
impl Position
Sourcepub fn new(instrument: &InstrumentAny, fill: OrderFilled) -> Self
pub fn new(instrument: &InstrumentAny, fill: OrderFilled) -> Self
Sourcepub fn purge_events_for_order(&mut self, client_order_id: ClientOrderId)
pub fn purge_events_for_order(&mut self, client_order_id: ClientOrderId)
Purges all order fill events for the given client order ID and recalculates derived state.
§Warning
This operation recalculates the entire position from scratch after removing the specified order’s fills. This is an expensive operation and should be used sparingly.
§Panics
Panics if after purging, no fills remain and the position cannot be reconstructed.
Sourcepub fn apply(&mut self, fill: &OrderFilled)
pub fn apply(&mut self, fill: &OrderFilled)
Applies an OrderFilled event to this position.
§Panics
Panics if the fill.trade_id is already present in the position’s trade_ids.
Sourcepub fn apply_adjustment(&mut self, adjustment: PositionAdjusted)
pub fn apply_adjustment(&mut self, adjustment: PositionAdjusted)
Applies a position adjustment event.
This method handles adjustments to position quantity or realized PnL that occur outside of normal order fills, such as:
- Commission adjustments in base currency (crypto spot markets).
- Funding payments (perpetual futures).
The adjustment event is stored in the position’s adjustment history for full audit trail.
§Panics
Panics if the adjustment’s quantity_change cannot be converted to f64.
pub fn calculate_pnl( &self, avg_px_open: f64, avg_px_close: f64, quantity: Quantity, ) -> Money
pub fn total_pnl(&self, last: Price) -> Money
pub fn unrealized_pnl(&self, last: Price) -> Money
pub fn closing_order_side(&self) -> OrderSide
pub fn is_opposite_side(&self, side: OrderSide) -> bool
pub fn symbol(&self) -> Symbol
pub fn venue(&self) -> Venue
pub fn event_count(&self) -> usize
pub fn client_order_ids(&self) -> Vec<ClientOrderId>
pub fn venue_order_ids(&self) -> Vec<VenueOrderId>
pub fn trade_ids(&self) -> Vec<TradeId>
Sourcepub fn notional_value(&self, last: Price) -> Money
pub fn notional_value(&self, last: Price) -> Money
Calculates the notional value based on the last price.
§Panics
Panics if self.base_currency is None.
Sourcepub fn last_event(&self) -> Option<OrderFilled>
pub fn last_event(&self) -> Option<OrderFilled>
Returns the last OrderFilled event for the position (if any after purging).
pub fn last_trade_id(&self) -> Option<TradeId>
pub fn is_long(&self) -> bool
pub fn is_short(&self) -> bool
pub fn is_open(&self) -> bool
pub fn is_closed(&self) -> bool
pub fn commissions(&self) -> Vec<Money>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'py> IntoPyObject<'py> for Position
impl<'py> IntoPyObject<'py> for Position
Source§impl PyClassImpl for Position
impl PyClassImpl for Position
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// Represents a position in a market.
///
/// The position ID may be assigned at the trading venue, or can be system
/// generated depending on a strategies OMS (Order Management System) settings.
const RAW_DOC: &'static CStr = /// Represents a position in a market. /// /// The position ID may be assigned at the trading venue, or can be system /// generated depending on a strategies OMS (Order Management System) settings.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<Position>
type ThreadChecker = SendablePyClass<Position>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature for Position
impl PyClassNewTextSignature for Position
const TEXT_SIGNATURE: &'static str = "(instrument, fill)"
Source§impl PyMethods<Position> for PyClassImplCollector<Position>
impl PyMethods<Position> for PyClassImplCollector<Position>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Position
impl PyTypeInfo for Position
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for Position
impl Eq for Position
impl ExtractPyClassWithClone for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read moreSource§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
Source§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read moreSource§impl<T> Separable for Twhere
T: Display,
impl<T> Separable for Twhere
T: Display,
Source§fn separate_by_policy(&self, policy: SeparatorPolicy<'_>) -> String
fn separate_by_policy(&self, policy: SeparatorPolicy<'_>) -> String
SeparatorPolicy. Read more