apple-quant-algorithmic 0.4.0

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

use rust_decimal::Decimal;

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

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

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

impl<IS: InstrumentSpec> RestingVolume<IS> {
	pub fn new(
		directionless_volume: DirectionlessVolume<IS>,
		resting_side: RestingSide,
	) -> Self {
		Self {
			directionless_volume,
			resting_side,
		}
	}

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

	pub fn as_decimal(
		&self,
	) -> Decimal {
		self.directionless_volume.as_decimal() *
		self.resting_side.as_directional_intent().as_decimal()
	}

	pub fn as_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_resting_side(
		&self,
	) -> &RestingSide {
		&self.resting_side
	}

	pub fn into_resting_side(
		self,
	) -> RestingSide {
		self.resting_side
	}

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

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

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