Skip to main content

apple_quant_algorithmic/volume/
directional_intent.rs

1use rust_decimal::Decimal;
2
3use crate::instrument::InstrumentSpec;
4
5use super::{AggressorSide, DirectionalIntentVolume, DirectionlessVolume, RestingSide};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[repr(u8)]
9pub enum DirectionalIntent {
10	Positive,
11	Negative,
12}
13
14impl DirectionalIntent {
15	pub fn with_volume<IS: InstrumentSpec>(
16		self,
17		directionless_volume: DirectionlessVolume<IS>,
18	) -> DirectionalIntentVolume<IS> {
19		DirectionalIntentVolume {
20			directional_intent: self,
21			directionless_volume,
22		}
23	}
24
25	pub fn flip(&mut self) {
26		*self = self.as_flipped();
27	}
28
29	pub fn as_flipped(&self) -> Self {
30		match self {
31			Self::Positive => Self::Negative,
32			Self::Negative => Self::Positive,
33		}
34	}
35
36	pub fn is_positive(&self) -> bool {
37		*self == Self::Positive
38	}
39
40	pub fn as_decimal(&self) -> Decimal {
41		match self {
42			Self::Negative => Decimal::NEGATIVE_ONE,
43			Self::Positive => Decimal::ONE,
44		}
45	}
46}
47
48impl From<AggressorSide> for DirectionalIntent {
49	fn from(value: AggressorSide) -> Self {
50		match value {
51			AggressorSide::Bid => Self::Negative,
52			AggressorSide::Ask => Self::Positive,
53		}
54	}
55}
56
57impl From<&AggressorSide> for DirectionalIntent {
58	fn from(value: &AggressorSide) -> Self {
59		(*value).into()
60	}
61}
62
63impl From<RestingSide> for DirectionalIntent {
64	fn from(value: RestingSide) -> Self {
65		match value {
66			RestingSide::Bid => Self::Positive,
67			RestingSide::Ask => Self::Negative,
68		}
69	}
70}
71
72impl From<&RestingSide> for DirectionalIntent {
73	fn from(value: &RestingSide) -> Self {
74		(*value).into()
75	}
76}