pub struct PositionManager<AssetKey = AssetIndex, InstrumentKey = InstrumentIndex> {
pub mode: OmsMode,
pub positions: IndexMap<PositionId, Position<AssetKey, InstrumentKey>>,
}Expand description
Manages open positions for a single instrument.
Supports two OMS modes (configurable at construction):
Netting(default): at most one entry; same flip/close logic as before.Hedging: multiple concurrent entries, keyed byPositionId.
Fields§
§mode: OmsMode§positions: IndexMap<PositionId, Position<AssetKey, InstrumentKey>>All currently open positions keyed by PositionId.
Implementations§
Source§impl<AssetKey, InstrumentKey> PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> PositionManager<AssetKey, InstrumentKey>
Source§impl<AssetKey: Debug + Clone, InstrumentKey> PositionManager<AssetKey, InstrumentKey>
impl<AssetKey: Debug + Clone, InstrumentKey> PositionManager<AssetKey, InstrumentKey>
Sourcepub fn update_from_trade(
&mut self,
trade: &Trade<AssetKey, InstrumentKey>,
contract_size: Decimal,
) -> Option<PositionExited<AssetKey, InstrumentKey>>
pub fn update_from_trade( &mut self, trade: &Trade<AssetKey, InstrumentKey>, contract_size: Decimal, ) -> Option<PositionExited<AssetKey, InstrumentKey>>
Updates the current position state based on a new trade.
This method handles:
- Opening a new position if none exists
- Updating an existing position (increase/decrease/close)
- Handling position flips (close existing & open new with any remaining trade quantity)
In Netting mode the PositionId is derived deterministically as "netting".
In Hedging mode the PositionId is derived from trade.order_id.
§Warning — Hedging mode callers
In OmsMode::Hedging, this method derives the PositionId directly from
trade.order_id. The engine’s actual fill routing instead resolves the
PositionId via a ClientOrderId → PositionId lookup table populated at
order submission time (see InstrumentState::update_from_trade). Calling this
method directly in Hedging mode bypasses that lookup and will open a spurious
position slot under the exchange order ID rather than the caller’s chosen
PositionId. Prefer routing fills through InstrumentState::update_from_trade,
or call update_from_trade_with_id with the already-resolved PositionId.
§Arguments
trade- The trade to processcontract_size- Contract multiplier for derivatives (1 for spot, 100 for standard options)
Sourcepub fn update_from_trade_with_id(
&mut self,
trade: &Trade<AssetKey, InstrumentKey>,
position_id: &PositionId,
contract_size: Decimal,
) -> Option<PositionExited<AssetKey, InstrumentKey>>
pub fn update_from_trade_with_id( &mut self, trade: &Trade<AssetKey, InstrumentKey>, position_id: &PositionId, contract_size: Decimal, ) -> Option<PositionExited<AssetKey, InstrumentKey>>
Updates position state routing to a specific PositionId.
Suitable for callers (e.g. expiry settlement) that have already resolved
the correct PositionId and want to bypass the automatic derivation.
§Arguments
trade- The trade to processposition_id- The resolved position ID to route the fill tocontract_size- Contract multiplier for derivatives (1 for spot, 100 for standard options)
§Fee model
This method does not apply a fee model — trade.fees.fees is used as-is.
The caller is responsible for ensuring the fee value is correct before calling.
For contract expiry settlement the caller must set fees to Decimal::ZERO (exchange
settlement commission, if any, is not modelled and must be tracked separately).
Trait Implementations§
Source§impl<AssetKey: Clone, InstrumentKey: Clone> Clone for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey: Clone, InstrumentKey: Clone> Clone for PositionManager<AssetKey, InstrumentKey>
Source§fn clone(&self) -> PositionManager<AssetKey, InstrumentKey>
fn clone(&self) -> PositionManager<AssetKey, InstrumentKey>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<AssetKey: Debug, InstrumentKey: Debug> Debug for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey: Debug, InstrumentKey: Debug> Debug for PositionManager<AssetKey, InstrumentKey>
Source§impl<AssetKey, InstrumentKey> Default for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> Default for PositionManager<AssetKey, InstrumentKey>
Source§impl<'de, AssetKey, InstrumentKey> Deserialize<'de> for PositionManager<AssetKey, InstrumentKey>where
AssetKey: Deserialize<'de>,
InstrumentKey: Deserialize<'de>,
impl<'de, AssetKey, InstrumentKey> Deserialize<'de> for PositionManager<AssetKey, InstrumentKey>where
AssetKey: Deserialize<'de>,
InstrumentKey: Deserialize<'de>,
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<AssetKey: PartialEq, InstrumentKey: PartialEq> PartialEq for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey: PartialEq, InstrumentKey: PartialEq> PartialEq for PositionManager<AssetKey, InstrumentKey>
Source§fn eq(&self, other: &PositionManager<AssetKey, InstrumentKey>) -> bool
fn eq(&self, other: &PositionManager<AssetKey, InstrumentKey>) -> bool
self and other values to be equal, and is used by ==.Source§impl<AssetKey, InstrumentKey> Serialize for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> Serialize for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey: PartialEq, InstrumentKey: PartialEq> StructuralPartialEq for PositionManager<AssetKey, InstrumentKey>
Auto Trait Implementations§
impl<AssetKey, InstrumentKey> Freeze for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> RefUnwindSafe for PositionManager<AssetKey, InstrumentKey>where
InstrumentKey: RefUnwindSafe,
AssetKey: RefUnwindSafe,
impl<AssetKey, InstrumentKey> Send for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> Sync for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> Unpin for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> UnsafeUnpin for PositionManager<AssetKey, InstrumentKey>
impl<AssetKey, InstrumentKey> UnwindSafe for PositionManager<AssetKey, InstrumentKey>where
InstrumentKey: UnwindSafe,
AssetKey: UnwindSafe,
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<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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