apple-quant-algorithmic 0.3.0

Apple Quant's algorithmic library.
use crate::{
	instrument::{InstrumentSpec, XSpec},
	volume::DirectionlessVolume,
};

use super::{Subpoints, SubpointsType};

#[cfg(feature = "points-i32")]
pub type WholePointsType = i32;

#[cfg(not(feature = "points-i32"))]
pub type WholePointsType = i64;

pub trait VolumeFromWholePoints: From<Subpoints> {
	fn volume_from_whole_points(
		whole_points: WholePoints,
	) -> Self
	where
		Self: Sized,
	{
		let subpoints_type = *whole_points.whole_points_type() as SubpointsType;
		Self::from(subpoints_type.into())
	}
}

pub trait PriceFromWholePoints<XS: XSpec>: From<Subpoints> {
	fn price_from_whole_points(
		whole_points: WholePoints,
	) -> Self
	where
		Self: Sized,
	{
		let mut subpoints_type = *whole_points.whole_points_type() as SubpointsType;
		subpoints_type *= XS::TICKS_PER_POINT as SubpointsType;
		Self::from(subpoints_type.into())
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WholePoints(WholePointsType);

impl WholePoints {
	pub(crate) unsafe fn from_whole_points_type(
		whole_points_type: WholePointsType,
	) -> Self {
		Self(whole_points_type)
	}

	pub fn whole_points_type(
		&self,
	) -> &WholePointsType {
		&self.0
	}

	pub fn into_subpoints<XS: XSpec>(
		self,
	) -> Subpoints {
		Subpoints::from_whole_points::<XS>(self)
	}

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

	pub fn into_directionless_volume<IS: InstrumentSpec>(
		self,
	) -> DirectionlessVolume<IS> {
		self.into()
	}
}