Skip to main content

apple_quant_algorithmic/price/
relative.rs

1use std::ops::Deref;
2
3use crate::{instrument::InstrumentSpec, points::Subpoints};
4
5use super::ProtoabsolutePrice;
6
7/// Offset from an [`AbsolutePrice`].
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct RelativePrice(Subpoints);
10
11impl RelativePrice {
12	pub fn new(
13		subpoints: impl Into<Subpoints>,
14	) -> Self {
15		Self(subpoints.into())
16	}
17
18	pub fn as_subpoints(
19		&self,
20	) -> &Subpoints {
21		&self.0
22	}
23
24	pub fn into_subpoints(
25		self,
26	) -> Subpoints {
27		self.0
28	}
29
30	pub fn as_protoabsolute_price<IS: InstrumentSpec>(
31		&self,
32	) -> ProtoabsolutePrice<IS> {
33		self.into()
34	}
35
36	pub fn into_protoabsolute_price<IS: InstrumentSpec>(
37		self,
38	) -> ProtoabsolutePrice<IS> {
39		self.into()
40	}
41}
42
43impl<IS: InstrumentSpec> From<ProtoabsolutePrice<IS>> for Option<RelativePrice> {
44	fn from(
45		value: ProtoabsolutePrice<IS>,
46	) -> Self {
47		let ProtoabsolutePrice::Relative(
48			relative_price,
49		) = value else {
50			return None;
51		};
52
53		Some(relative_price)
54	}
55}
56
57impl<
58	'a,
59	IS: InstrumentSpec,
60> From<&'a ProtoabsolutePrice<IS>> for Option<&'a RelativePrice> {
61	fn from(
62		value: &'a ProtoabsolutePrice<IS>,
63	) -> Self {
64		let ProtoabsolutePrice::Relative(
65			relative_price,
66		) = value else {
67			return None;
68		};
69
70		Some(relative_price)
71	}
72}
73
74impl Deref for RelativePrice {
75	type Target = Subpoints;
76
77	fn deref(
78		&self,
79	) -> &Self::Target {
80		&self.0
81	}
82}