apple-quant-algorithmic 0.1.0

Apple Quant's algorithmic trading api
Documentation
use std::range::Range;

use thiserror::Error;

use crate::timestamp::{Timestamp, TimestampRange, TradeTimestamped, TradeTimestampedRange};

mod fixed;
mod flex;

pub use fixed::*;
pub use flex::*;

pub trait BinnedRange<T: TradeTimestampedRange> {
	fn insert(
		&mut self,
		data: impl Iterator<Item = T>,
	);

	fn get<'a>(
		&'a self,
		timestamp_range: &TimestampRange,
	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
	where
		T: 'a;

	fn get_first(
		&self,
		timestamp_range: &TimestampRange,
	) -> Result<Option<&T>, BinnedDataError>;

	fn iter<'a>(
		&'a self,
		range: Range<&Timestamp>,
	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
	where
		T: 'a;
}

pub trait BinnedPoint<T: TradeTimestamped> {
	fn insert(
		&mut self,
		data: impl Iterator<Item = T>,
	);

	fn get<'a>(
		&'a self,
		timestamp: &Timestamp,
	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
	where
		T: 'a;

	fn get_first(
		&self,
		timestamp: &Timestamp,
	) -> Result<Option<&T>, BinnedDataError>;

	fn iter<'a>(
		&'a self,
		range: Range<&Timestamp>,
	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
	where
		T: 'a;
}

#[derive(Debug, Error)]
pub enum BinnedDataError {
	#[error(
		"Min {min_timestamp:?} and max {max_timestamp:?} ranges must be in bounds of min {bounds_min_timestamp:?} and max {bounds_max_timestamp:?}."
	)]
	MinMaxTimestampOutOfRange {
		min_timestamp: u64,
		max_timestamp: u64,
		bounds_min_timestamp: u64,
		bounds_max_timestamp: u64,
	},

	#[error("Aggregation error.")]
	AggregationError,

	#[error(
		"Timestamp {timestamp:?} must be in bounds of min {bounds_min:?} and max {bounds_max:?}."
	)]
	TimestampOutOfRange {
		timestamp: u64,
		bounds_min: u64,
		bounds_max: u64,
	},

	#[error("Invalid min {min:?} and/or max {max:?}.")]
	InvalidMinMax { min: u64, max: u64 },

	#[error("Expected min {min:?} and max {max:?} to be equal.")]
	UnequalMinMax { min: u64, max: u64 },

	#[error("Data index {data_index:?} out of range of sector data count {sector_data_count:?}")]
	SectorDataOutOfRange {
		data_index: usize,
		sector_data_count: usize,
	},

	#[error("Invalid sector {sector_idx:?}")]
	InvalidSector { sector_idx: u64 },

	#[error("Internal error.")]
	InternalError,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FixedSectorDef {
	pub sector_idx: u64,
	pub data_idx: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FlexSectorDef {
	pub sector_idx: u64,
}

pub struct SectorDataIdx<'sector, T> {
	pub sector: &'sector [T],
	pub sector_idx: u64,

	pub data_idx: usize,
}