apple-quant-algorithmic 0.3.0

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

use crate::{instrument::InstrumentSpec, points::Subpoints};

use super::ProtoabsolutePrice;

/// Offset from an [`AbsolutePrice`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RelativePrice(Subpoints);

impl RelativePrice {
	pub fn new(
		subpoints: impl Into<Subpoints>,
	) -> Self {
		Self(subpoints.into())
	}

	pub fn as_subpoints(
		&self,
	) -> &Subpoints {
		&self.0
	}

	pub fn into_subpoints(
		self,
	) -> Subpoints {
		self.0
	}

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

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

impl<IS: InstrumentSpec> From<ProtoabsolutePrice<IS>> for Option<RelativePrice> {
	fn from(
		value: ProtoabsolutePrice<IS>,
	) -> Self {
		let ProtoabsolutePrice::Relative(
			relative_price,
		) = value else {
			return None;
		};

		Some(relative_price)
	}
}

impl<
	'a,
	IS: InstrumentSpec,
> From<&'a ProtoabsolutePrice<IS>> for Option<&'a RelativePrice> {
	fn from(
		value: &'a ProtoabsolutePrice<IS>,
	) -> Self {
		let ProtoabsolutePrice::Relative(
			relative_price,
		) = value else {
			return None;
		};

		Some(relative_price)
	}
}

impl Deref for RelativePrice {
	type Target = Subpoints;

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