Skip to main content

apple_quant_algorithmic/order_manager/
completed.rs

1use smallvec::SmallVec;
2
3use crate::{
4	backend::{LocalOrderId, OrderIdError, OrdersBackend, RemoteOrderId},
5	instrument::InstrumentSpec,
6	order::PartialOrderFill,
7	timestamp::{TickTimestamp, Timestamp},
8};
9
10use super::{OrdersCapacitySpec, working::WorkingRemoteOrder};
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub struct CompletedRemoteOrder<IS: InstrumentSpec, CS: OrdersCapacitySpec> {
14	pub(crate) completed_timestamp: Timestamp,
15	pub(crate) local_order_id: LocalOrderId,
16	pub(crate) remote_order_id: RemoteOrderId,
17	pub(crate) partial_order_fills: SmallVec<[PartialOrderFill<IS>; CS::PARTIAL_FILL]>,
18}
19
20impl<IS: InstrumentSpec, CS: OrdersCapacitySpec> CompletedRemoteOrder<IS, CS> {
21	pub(crate) fn new(
22		completed_timestamp: Timestamp,
23		local_order_id: LocalOrderId,
24		remote_order_id: RemoteOrderId,
25		partial_order_fills: impl IntoIterator<Item = PartialOrderFill<IS>>,
26	) -> Self {
27		Self {
28			completed_timestamp,
29			local_order_id,
30			remote_order_id,
31			partial_order_fills: partial_order_fills
32				.into_iter()
33				.collect(),
34		}
35	}
36}
37
38/// Unordered collection of completed remote orders. Only when the client has confirmed these orders have either been 100% filled by the matching engine or canceled will it transition state to [`filled_remote_orders`].
39#[derive(Debug)]
40pub struct CompletedRemoteOrders<IS: InstrumentSpec, CS: OrdersCapacitySpec>(
41	SmallVec<[CompletedRemoteOrder<IS, CS>; CS::COMPLETED_REMOTE]>,
42);
43
44impl<IS: InstrumentSpec, CS: OrdersCapacitySpec> CompletedRemoteOrders<IS, CS> {
45	pub fn submit<OB: OrdersBackend<IS>>(
46		&mut self,
47		tick_timestamp: &TickTimestamp,
48		working_remote_order: WorkingRemoteOrder<IS, CS>,
49	) where
50		IS: Send,
51	{
52		let completed_timestamp = tick_timestamp.timestamp();
53
54		let completed_remote_order = CompletedRemoteOrder::new(
55			completed_timestamp,
56			working_remote_order.local_order_id,
57			working_remote_order.remote_order_id,
58			working_remote_order.partial_order_fills,
59		);
60
61		self.0
62			.push(completed_remote_order);
63	}
64
65	pub fn get(
66		&self,
67		local_order_id: &LocalOrderId,
68	) -> Result<&CompletedRemoteOrder<IS, CS>, OrderIdError> {
69		let Some(completed_remote_order) = self
70			.0
71			.iter()
72			.find(|completed_remote_order| {
73				&completed_remote_order.local_order_id == local_order_id
74			})
75		else {
76			return local_order_id.err_invalid();
77		};
78
79		Ok(completed_remote_order)
80	}
81
82	pub(crate) fn get_mut(
83		&mut self,
84		local_order_id: &LocalOrderId,
85	) -> Result<&mut CompletedRemoteOrder<IS, CS>, OrderIdError> {
86		let Some(completed_remote_order) = self
87			.0
88			.iter_mut()
89			.find(|completed_remote_order| {
90				&completed_remote_order.local_order_id == local_order_id
91			})
92		else {
93			return local_order_id.err_invalid();
94		};
95
96		Ok(completed_remote_order)
97	}
98}
99
100impl<IS: InstrumentSpec, CS: OrdersCapacitySpec> Default for CompletedRemoteOrders<IS, CS> {
101	fn default() -> Self {
102		Self(SmallVec::default())
103	}
104}