apple-quant-algorithmic 0.1.0

Apple Quant's algorithmic trading api
Documentation
use crate::timestamp::{TimestampError, TradeTimestamp};

#[derive(Debug, Clone, Copy)]
pub enum TradeTimestampRange {
	One(TradeTimestamp),
	Many {
		min: TradeTimestamp,
		max: TradeTimestamp,
	},
}

impl Default for TradeTimestampRange {
	fn default() -> Self {
		Self::One(TradeTimestamp::default())
	}
}

impl TradeTimestampRange {
	pub fn new_from_iter(
		trade_timestamps: impl IntoIterator<Item = &TradeTimestamp>
	) -> Result<Self, TimestampError> {
		let mut iter = trade_timestamps.into_iter();

		let Some(mut min) = iter.next().cloned() else {
			return Err(TimestampError::RangeNotSatisfied { min: None, max: None });
		};

		let Some(mut max) = iter.next().cloned() else {
			return Ok(Self::One(min));
		};

		for timestamp in iter {
			if timestamp < &min {
				min = *timestamp;
			}

			if timestamp > &max {
				max = *timestamp;
			}
		}

		Ok(Self::Many { min, max })
	}

	pub fn new_with_many_from_parts(
		min: TradeTimestamp,
		max: TradeTimestamp,
	) -> Self {
		Self::Many { min, max }
	}

	pub fn min(&self) -> &TradeTimestamp {
		match self {
			Self::One(trade_timestamp) => trade_timestamp,
			Self::Many { min, max: _ } => min,
		}
	}

	pub fn max(&self) -> &TradeTimestamp {
		match self {
			Self::One(trade_timestamp) => trade_timestamp,
			Self::Many { min: _, max } => max,
		}
	}
}