apple-quant-algorithmic 0.3.0

Apple Quant's algorithmic library.
use std::fmt::{Debug, Display};

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

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

#[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 into_directional_intent_volume(
		self,
	) -> DirectionalIntentVolume<IS> {
		self.into()
	}

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

	pub fn into_directionless_volume(
		self,
	) -> DirectionlessVolume<IS> {
		self.directionless_volume
	}

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

	pub fn into_aggressor_side(
		self,
	) -> AggressorSide {
		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_directional_intent_volume()
			.as_volume_type_signed().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)
	}
}