Skip to main content

apple_quant_algorithmic/order/dependency/
deferred.rs

1#[allow(unused_imports)]
2use tracing::trace;
3
4use crate::{
5	backend::{OrderIdGenerator, OrdersBackend},
6	order_manager::{OrderManager, OrdersCapacitySpec},
7	instrument::InstrumentSpec, order::StateGoal, timestamp::TickTimestamp,
8	volume::DirectionalExposure,
9};
10
11use super::{OrderAction, OrderDependency, TriggerError};
12
13#[derive(Debug)]
14pub struct DeferredOrderActions<IS: InstrumentSpec>(Vec<OrderAction<IS>>);
15
16impl<IS: InstrumentSpec> DeferredOrderActions<IS> {
17	pub fn push(
18		&mut self,
19		order_action: OrderAction<IS>,
20	) {
21		self.0.push(order_action);
22	}
23
24	/// Return true if polling needs to be enabled.
25	pub(crate) async fn trigger<
26		OB: OrdersBackend<IS>,
27		CS: OrdersCapacitySpec,
28	>(
29		&mut self,
30		tick_timestamp: &TickTimestamp,
31		order_manager: &mut OrderManager<IS, CS>,
32		orders_backend: &mut OB,
33		order_id_generator: &mut OrderIdGenerator,
34		directional_exposure: &mut DirectionalExposure<IS>,
35		state_goal: &mut StateGoal,
36	) -> Result<bool, TriggerError>
37	where
38		IS: Send,
39	{
40		#[cfg(feature = "log-trace-order-actions")]
41		if !self.0.is_empty() {
42			trace!("Processing {} deferred order action(s).", self.0.len());
43		}
44
45		let mut is_polling = false;
46
47		for order_action in self.0.drain(..) {
48			if order_action.trigger(
49				tick_timestamp,
50				order_manager,
51				orders_backend,
52				order_id_generator,
53				directional_exposure,
54				state_goal,
55			).await? {
56				is_polling = true;
57			}
58		}
59
60		Ok(is_polling)
61	}
62}
63
64impl<IS: InstrumentSpec> Default for DeferredOrderActions<IS> {
65	fn default() -> Self {
66		Self(Vec::default())
67	}
68}