apple-quant-algorithmic 0.3.0

Apple Quant's algorithmic library.
#[allow(unused_imports)]
use tracing::trace;

use crate::{
	backend::{OrderIdGenerator, OrdersBackend},
	order_manager::{OrderManager, OrdersCapacitySpec},
	instrument::InstrumentSpec, order::StateGoal, timestamp::TickTimestamp,
	volume::DirectionalExposure,
};

use super::{OrderAction, OrderDependency, TriggerError};

#[derive(Debug)]
pub struct DeferredOrderActions<IS: InstrumentSpec>(Vec<OrderAction<IS>>);

impl<IS: InstrumentSpec> DeferredOrderActions<IS> {
	pub fn push(
		&mut self,
		order_action: OrderAction<IS>,
	) {
		self.0.push(order_action);
	}

	/// Return true if polling needs to be enabled.
	pub(crate) async fn trigger<
		OB: OrdersBackend<IS>,
		CS: OrdersCapacitySpec,
	>(
		&mut self,
		tick_timestamp: &TickTimestamp,
		order_manager: &mut OrderManager<IS, CS>,
		orders_backend: &mut OB,
		order_id_generator: &mut OrderIdGenerator,
		directional_exposure: &mut DirectionalExposure<IS>,
		state_goal: &mut StateGoal,
	) -> Result<bool, TriggerError>
	where
		IS: Send,
	{
		#[cfg(feature = "log-trace-order-actions")]
		if !self.0.is_empty() {
			trace!("Processing {} deferred order action(s).", self.0.len());
		}

		let mut is_polling = false;

		for order_action in self.0.drain(..) {
			if order_action.trigger(
				tick_timestamp,
				order_manager,
				orders_backend,
				order_id_generator,
				directional_exposure,
				state_goal,
			).await? {
				is_polling = true;
			}
		}

		Ok(is_polling)
	}
}

impl<IS: InstrumentSpec> Default for DeferredOrderActions<IS> {
	fn default() -> Self {
		Self(Vec::default())
	}
}