apple-quant-algorithmic 0.1.0

Apple Quant's algorithmic trading api
Documentation
use std::fmt::{Debug, Display};

use num_traits::Zero;

use crate::instrument::{AsDecimal, InstrumentSpec};

use super::{AggressorSide, DirectionalIntentVolume, DirectionlessVolume, VolumeDelta};

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AggressiveVolume<IS: InstrumentSpec> {
	pub directionless_volume: DirectionlessVolume<IS>,
	pub aggressor_side: AggressorSide,
}

impl<IS: InstrumentSpec> AggressiveVolume<IS> {
	pub fn new(
		directionless_volume: DirectionlessVolume<IS>,
		aggressor_side: AggressorSide,
	) -> Self {
		Self {
			directionless_volume,
			aggressor_side,
		}
	}

	pub fn flip(self) -> Self {
		Self {
			directionless_volume: self.directionless_volume,
			aggressor_side: self.aggressor_side.flip(),
		}
	}

	pub fn as_directional_intent_volume(&self) -> DirectionalIntentVolume<IS> {
		self.into()
	}

	pub fn as_directionless_volume(&self) -> &DirectionlessVolume<IS> {
		&self.directionless_volume
	}

	pub fn aggressor_side(&self) -> AggressorSide {
		self.aggressor_side
	}

	pub fn as_volume_delta(&self) -> VolumeDelta<IS> {
		if self
			.directionless_volume
			.is_zero()
		{
			return VolumeDelta::Zero;
		}

		VolumeDelta::AggressiveDelta {
			directionless_volume: self.directionless_volume,
			aggressor_side: self.aggressor_side,
		}
	}
}

impl<IS: InstrumentSpec> Display for AggressiveVolume<IS> {
	fn fmt(
		&self,
		f: &mut std::fmt::Formatter<'_>,
	) -> std::fmt::Result {
		let volume = self
			.as_volume_delta()
			.as_decimal();

		write!(f, "{}", volume)
	}
}

impl<IS: InstrumentSpec> Debug for AggressiveVolume<IS> {
	fn fmt(
		&self,
		f: &mut std::fmt::Formatter<'_>,
	) -> std::fmt::Result {
		<Self as Display>::fmt(&self, f)
	}
}