pub struct OrderIdMap { /* private fields */ }Expand description
Bidirectional mapping between rustrade ClientOrderId and IB order IDs.
IB uses i32 order IDs from a sequence. Barter uses ClientOrderId (SmolStr).
This map maintains the bidirectional relationship and stores order context
for reconstructing full Order structs from OrderStatus callbacks.
§A client order id names one live order at a time
TWS receives a ClientOrderId only as the order’s reference (order_ref), whose uniqueness
it does not check, so this map is the only place it can be kept.
An id is held from register until its order ends, and registering an id
already held is refused with ClientOrderIdInUse. Once its order has ended the id may name a
new order.
An order ends here when the account stream reports it Filled, Cancelled or Inactive, or
when clear_stale reaps it. A Filled order releases its id
(release_client_id) but keeps its IB-id entry, so the executions
and commissions IB may still send for it resolve to the id it was placed under. That entry is
reaped by clear_stale.
Every removal is conditional on the id still naming the order removed, so an earlier order
under a reused id cannot take the mapping of the order now under it. Its late executions and
commissions still resolve to the id, carrying its own IB order ID, while a status IB re-sends
for it is dropped by the account stream (names_other_order).
Implementations§
Source§impl OrderIdMap
impl OrderIdMap
pub fn new() -> Self
Sourcepub fn register(
&self,
client_id: ClientOrderId,
ib_id: i32,
context: OrderContext,
) -> Result<(), ClientOrderIdInUse>
pub fn register( &self, client_id: ClientOrderId, ib_id: i32, context: OrderContext, ) -> Result<(), ClientOrderIdInUse>
Register a mapping between ClientOrderId and IB order ID with order context.
§Errors
ClientOrderIdInUse if a live order already holds client_id. Nothing is registered.
Sourcepub fn register_all<const N: usize>(
&self,
orders: [(ClientOrderId, i32, OrderContext); N],
) -> Result<(), ClientOrderIdInUse>
pub fn register_all<const N: usize>( &self, orders: [(ClientOrderId, i32, OrderContext); N], ) -> Result<(), ClientOrderIdInUse>
Register several orders at once, such as the legs of a bracket: all of them or none.
§Errors
ClientOrderIdInUse, naming the first id at fault, if a live order already holds one of
the ids or two of orders share one. Nothing is registered.
Sourcepub fn get_ib_id(&self, client_id: &ClientOrderId) -> Option<i32>
pub fn get_ib_id(&self, client_id: &ClientOrderId) -> Option<i32>
Look up the IB order ID of the live order under client_id.
Sourcepub fn contains(&self, ib_id: i32) -> bool
pub fn contains(&self, ib_id: i32) -> bool
Whether IB order ib_id has an entry: a live order, or a filled one not yet reaped.
Sourcepub fn get_client_id(&self, ib_id: i32) -> Option<ClientOrderId>
pub fn get_client_id(&self, ib_id: i32) -> Option<ClientOrderId>
Look up ClientOrderId by IB order ID.
Sourcepub fn get_client_id_and_context(
&self,
ib_id: i32,
) -> Option<(ClientOrderId, OrderContext)>
pub fn get_client_id_and_context( &self, ib_id: i32, ) -> Option<(ClientOrderId, OrderContext)>
Look up ClientOrderId and OrderContext together by IB order ID (single lookup).
Sourcepub fn names_other_order(&self, client_id: &ClientOrderId, ib_id: i32) -> bool
pub fn names_other_order(&self, client_id: &ClientOrderId, ib_id: i32) -> bool
Whether client_id now names a live order other than ib_id: the id was freed when ib_id
ended and has since been registered again.
Sourcepub fn release_client_id(
&self,
ib_id: i32,
) -> Option<(ClientOrderId, OrderContext)>
pub fn release_client_id( &self, ib_id: i32, ) -> Option<(ClientOrderId, OrderContext)>
Free the id of the filled order ib_id, keeping its entry, and return both, in a single
write lock acquisition.
Use this for a Filled status: the id may then name a new order, while the executions and
commissions IB may still send for this one resolve to it by IB id.
Sourcepub fn remove_and_get_context(
&self,
ib_id: i32,
) -> Option<(ClientOrderId, OrderContext)>
pub fn remove_and_get_context( &self, ib_id: i32, ) -> Option<(ClientOrderId, OrderContext)>
Remove mapping and return context in a single write lock acquisition.
Use this for terminal status events (Cancelled/Inactive) to avoid the
read-then-write pattern of get_client_id_and_context + remove_by_ib_id.
The id’s mapping is removed only if it still names ib_id.
Sourcepub fn remove_by_ib_id(&self, ib_id: i32) -> Option<ClientOrderId>
pub fn remove_by_ib_id(&self, ib_id: i32) -> Option<ClientOrderId>
Remove a mapping by IB order ID (used when an order failed or was rolled back).
The id’s mapping is removed only if it still names ib_id.
Sourcepub fn clear_stale(&self, max_age: Duration) -> usize
pub fn clear_stale(&self, max_age: Duration) -> usize
Clear order ID mappings older than the given duration.
Returns the number of cleared entries.
§Why This Is Needed
IB does not guarantee event ordering between OrderStatus("Filled") and
ExecutionData/CommissionReport. For fast-filling orders (especially
market orders), execution data may arrive AFTER the filled status — or
the filled status may not arrive at all. So a Filled status frees the
order’s client id but keeps its IB-id entry, and an order whose terminal
status never arrives keeps both.
Call this method periodically to clean up old mappings. A
reasonable interval is 5-10 minutes with a max_age of 1 hour. An order
still working past max_age is cleared too, freeing its id.
Trait Implementations§
Source§impl Clone for OrderIdMap
impl Clone for OrderIdMap
Source§impl Debug for OrderIdMap
impl Debug for OrderIdMap
Auto Trait Implementations§
impl !RefUnwindSafe for OrderIdMap
impl !UnwindSafe for OrderIdMap
impl Freeze for OrderIdMap
impl Send for OrderIdMap
impl Sync for OrderIdMap
impl Unpin for OrderIdMap
impl UnsafeUnpin for OrderIdMap
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,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.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> 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 moreimpl<T> JsonSchemaMaybe for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.