apple_quant_algorithmic/volume/
resting.rs1use std::fmt::{Debug, Display};
2
3use num_traits::Zero;
4use rust_decimal::Decimal;
5
6use crate::instrument::{AsDecimal, InstrumentSpec};
7
8use super::{DirectionalIntentVolume, DirectionlessVolume, RestingSide, VolumeDelta};
9
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct RestingVolume<IS: InstrumentSpec> {
12 pub directionless_volume: DirectionlessVolume<IS>,
13 pub resting_side: RestingSide,
14}
15
16impl<IS: InstrumentSpec> RestingVolume<IS> {
17 pub fn new(
18 directionless_volume: DirectionlessVolume<IS>,
19 resting_side: RestingSide,
20 ) -> Self {
21 Self {
22 directionless_volume,
23 resting_side,
24 }
25 }
26
27 pub fn flip(self) -> Self {
28 Self {
29 directionless_volume: self.directionless_volume,
30 resting_side: self.resting_side.flip(),
31 }
32 }
33
34 pub fn as_decimal(&self) -> Decimal {
35 self.directionless_volume
36 .as_decimal()
37 * self
38 .resting_side
39 .as_directional_intent()
40 .as_decimal()
41 }
42
43 pub fn as_directional_intent_volume(&self) -> DirectionalIntentVolume<IS> {
44 (*self).into()
45 }
46
47 pub fn as_directionless_volume(&self) -> &DirectionlessVolume<IS> {
48 &self.directionless_volume
49 }
50
51 pub fn resting_side(&self) -> &RestingSide {
52 &self.resting_side
53 }
54
55 pub fn as_parts(
56 &self
57 ) -> (
58 &DirectionlessVolume<IS>,
59 &RestingSide,
60 ) {
61 (
62 &self.directionless_volume,
63 &self.resting_side,
64 )
65 }
66
67 pub fn as_volume_delta(&self) -> VolumeDelta<IS> {
68 if self
69 .directionless_volume
70 .is_zero()
71 {
72 return VolumeDelta::Zero;
73 }
74
75 VolumeDelta::RestingDelta {
76 directionless_volume: self.directionless_volume,
77 resting_side: self.resting_side,
78 }
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}