Skip to main content

apple_quant_algorithmic/volume/
resting.rs

1use std::fmt::{Debug, Display};
2
3use rust_decimal::Decimal;
4
5use crate::instrument::{AsDecimal, InstrumentSpec};
6
7use super::{DirectionalIntentVolume, DirectionlessVolume, RestingSide};
8
9#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct RestingVolume<IS: InstrumentSpec> {
11	pub directionless_volume: DirectionlessVolume<IS>,
12	pub resting_side: RestingSide,
13}
14
15impl<IS: InstrumentSpec> RestingVolume<IS> {
16	pub fn new(
17		directionless_volume: DirectionlessVolume<IS>,
18		resting_side: RestingSide,
19	) -> Self {
20		Self {
21			directionless_volume,
22			resting_side,
23		}
24	}
25
26	pub fn flip(
27		self,
28	) -> Self {
29		Self {
30			directionless_volume: self.directionless_volume,
31			resting_side: self.resting_side.flip(),
32		}
33	}
34
35	pub fn as_decimal(
36		&self,
37	) -> Decimal {
38		self.directionless_volume.as_decimal() *
39		self.resting_side.as_directional_intent().as_decimal()
40	}
41
42	pub fn as_directional_intent_volume(
43		&self,
44	) -> DirectionalIntentVolume<IS> {
45		(*self).into()
46	}
47
48	pub fn as_directionless_volume(
49		&self,
50	) -> &DirectionlessVolume<IS> {
51		&self.directionless_volume
52	}
53
54	pub fn into_directionless_volume(
55		self,
56	) -> DirectionlessVolume<IS> {
57		self.directionless_volume
58	}
59
60	pub fn as_resting_side(
61		&self,
62	) -> &RestingSide {
63		&self.resting_side
64	}
65
66	pub fn into_resting_side(
67		self,
68	) -> RestingSide {
69		self.resting_side
70	}
71
72	pub fn as_parts(
73		&self,
74	) -> (
75		&DirectionlessVolume<IS>,
76		&RestingSide,
77	) {
78		(&self.directionless_volume, &self.resting_side)
79	}
80}
81
82impl<IS: InstrumentSpec> Display for RestingVolume<IS> {
83	fn fmt(
84		&self,
85		f: &mut std::fmt::Formatter<'_>,
86	) -> std::fmt::Result {
87		let volume = self.as_decimal();
88		write!(f, "{}", volume)
89	}
90}
91
92impl<IS: InstrumentSpec> Debug for RestingVolume<IS> {
93	fn fmt(
94		&self,
95		f: &mut std::fmt::Formatter<'_>,
96	) -> std::fmt::Result {
97		<Self as Display>::fmt(&self, f)
98	}
99}