Skip to main content

apple_quant_algorithmic/points/
sub.rs

1use std::ops::Deref;
2
3use crate::{
4	instrument::{InstrumentSpec, XSpec},
5	price::RelativePrice, volume::DirectionlessVolume,
6};
7
8use super::{WholePoints, WholePointsType};
9
10#[cfg(feature = "subpoints-i32")]
11pub type SubpointsType = i32;
12
13#[cfg(not(feature = "subpoints-i32"))]
14pub type SubpointsType = i64;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct Subpoints(SubpointsType);
18
19impl Subpoints {
20	pub const ZERO: Self = Self(0);
21
22	pub const fn new(
23		subpoints_type: SubpointsType,
24	) -> Self {
25		Self(subpoints_type)
26	}
27
28	pub fn from_whole_points<XS: XSpec>(
29		whole_points: impl Into<WholePoints>,
30	) -> Self {
31		let whole_points_type = *whole_points.into().whole_points_type();
32
33		let subpoints_type = (whole_points_type *
34			XS::TICKS_PER_POINT as WholePointsType) as SubpointsType;
35
36		Self(subpoints_type)
37	}
38
39	pub fn as_subpoints_type(
40		&self,
41	) -> &SubpointsType {
42		&self.0
43	}
44
45	pub fn into_subpoints_type(
46		self,
47	) -> SubpointsType {
48		self.0
49	}
50
51	pub fn as_directionless_volume<IS: InstrumentSpec>(
52		&self,
53	) -> DirectionlessVolume<IS> {
54		self.into()
55	}
56
57	pub fn into_directionless_volume<IS: InstrumentSpec>(
58		self,
59	) -> DirectionlessVolume<IS> {
60		self.into()
61	}
62}
63
64impl Deref for Subpoints {
65	type Target = SubpointsType;
66
67	fn deref(
68		&self,
69	) -> &Self::Target {
70		&self.0
71	}
72}
73
74impl From<SubpointsType> for Subpoints {
75	fn from(
76		value: SubpointsType,
77	) -> Self {
78		Self(value)
79	}
80}
81
82impl From<RelativePrice> for Subpoints {
83	fn from(
84		value: RelativePrice,
85	) -> Self {
86		value.into_subpoints()
87	}
88}