apple-quant-algorithmic 0.3.0

Apple Quant's algorithmic library.
use std::ops::Deref;

use crate::{
	instrument::{InstrumentSpec, XSpec},
	price::RelativePrice, volume::DirectionlessVolume,
};

use super::{WholePoints, WholePointsType};

#[cfg(feature = "subpoints-i32")]
pub type SubpointsType = i32;

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

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

impl Subpoints {
	pub const ZERO: Self = Self(0);

	pub const fn new(
		subpoints_type: SubpointsType,
	) -> Self {
		Self(subpoints_type)
	}

	pub fn from_whole_points<XS: XSpec>(
		whole_points: impl Into<WholePoints>,
	) -> Self {
		let whole_points_type = *whole_points.into().whole_points_type();

		let subpoints_type = (whole_points_type *
			XS::TICKS_PER_POINT as WholePointsType) as SubpointsType;

		Self(subpoints_type)
	}

	pub fn as_subpoints_type(
		&self,
	) -> &SubpointsType {
		&self.0
	}

	pub fn into_subpoints_type(
		self,
	) -> SubpointsType {
		self.0
	}

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

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

impl Deref for Subpoints {
	type Target = SubpointsType;

	fn deref(
		&self,
	) -> &Self::Target {
		&self.0
	}
}

impl From<SubpointsType> for Subpoints {
	fn from(
		value: SubpointsType,
	) -> Self {
		Self(value)
	}
}

impl From<RelativePrice> for Subpoints {
	fn from(
		value: RelativePrice,
	) -> Self {
		value.into_subpoints()
	}
}