apple-quant-algorithmic 0.1.0

Apple Quant's algorithmic trading api
Documentation
use rust_decimal::Decimal;

use crate::instrument::InstrumentSpec;

use super::{AggressorSide, DirectionalIntentVolume, DirectionlessVolume, RestingSide};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum DirectionalIntent {
	Positive,
	Negative,
}

impl DirectionalIntent {
	pub fn with_volume<IS: InstrumentSpec>(
		self,
		directionless_volume: DirectionlessVolume<IS>,
	) -> DirectionalIntentVolume<IS> {
		DirectionalIntentVolume {
			directional_intent: self,
			directionless_volume,
		}
	}

	pub fn flip(&mut self) {
		*self = self.as_flipped();
	}

	pub fn as_flipped(&self) -> Self {
		match self {
			Self::Positive => Self::Negative,
			Self::Negative => Self::Positive,
		}
	}

	pub fn is_positive(&self) -> bool {
		*self == Self::Positive
	}

	pub fn as_decimal(&self) -> Decimal {
		match self {
			Self::Negative => Decimal::NEGATIVE_ONE,
			Self::Positive => Decimal::ONE,
		}
	}
}

impl From<AggressorSide> for DirectionalIntent {
	fn from(value: AggressorSide) -> Self {
		match value {
			AggressorSide::Bid => Self::Negative,
			AggressorSide::Ask => Self::Positive,
		}
	}
}

impl From<&AggressorSide> for DirectionalIntent {
	fn from(value: &AggressorSide) -> Self {
		(*value).into()
	}
}

impl From<RestingSide> for DirectionalIntent {
	fn from(value: RestingSide) -> Self {
		match value {
			RestingSide::Bid => Self::Positive,
			RestingSide::Ask => Self::Negative,
		}
	}
}

impl From<&RestingSide> for DirectionalIntent {
	fn from(value: &RestingSide) -> Self {
		(*value).into()
	}
}