Skip to main content

apple_quant_algorithmic/
binned_method.rs

1mod fixed;
2mod flex;
3
4use std::range::Range;
5
6use thiserror::Error;
7
8use crate::timestamp::{
9	Timestamp, TimestampRangeInclusive, TradeTimestampRangeIncluded, TradeTimestamped,
10};
11
12pub use fixed::*;
13pub use flex::*;
14
15pub trait BinnedRange<T: TradeTimestampRangeIncluded> {
16	fn insert(
17		&mut self,
18		data: impl Iterator<Item = T>,
19	);
20
21	fn get<'a>(
22		&'a self,
23		timestamp_range: &TimestampRangeInclusive,
24	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
25	where
26		T: 'a;
27
28	fn get_first(
29		&self,
30		timestamp_range: &TimestampRangeInclusive,
31	) -> Result<Option<&T>, BinnedDataError>;
32
33	fn iter<'a>(
34		&'a self,
35		range: Range<&Timestamp>,
36	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
37	where
38		T: 'a;
39}
40
41pub trait BinnedPoint<T: TradeTimestamped> {
42	fn insert(
43		&mut self,
44		data: impl Iterator<Item = T>,
45	);
46
47	fn get<'a>(
48		&'a self,
49		timestamp: &Timestamp,
50	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
51	where
52		T: 'a;
53
54	fn get_first(
55		&self,
56		timestamp: &Timestamp,
57	) -> Result<Option<&T>, BinnedDataError>;
58
59	fn iter<'a>(
60		&'a self,
61		range: Range<&Timestamp>,
62	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
63	where
64		T: 'a;
65}
66
67#[derive(Debug, Error)]
68pub enum BinnedDataError {
69	#[error(
70		"Min {min_timestamp:?} and max {max_timestamp:?} ranges must be in bounds of min {bounds_min_timestamp:?} and max {bounds_max_timestamp:?}."
71	)]
72	MinMaxTimestampOutOfRange {
73		min_timestamp: u64,
74		max_timestamp: u64,
75		bounds_min_timestamp: u64,
76		bounds_max_timestamp: u64,
77	},
78
79	#[error("Aggregation error.")]
80	AggregationError,
81
82	#[error("Timestamp {timestamp:?} must be in bounds of min {bounds_min:?} and max {bounds_max:?}.")]
83	TimestampOutOfRange {
84		timestamp: u64,
85		bounds_min: u64,
86		bounds_max: u64,
87	},
88
89	#[error("Invalid min {min:?} and/or max {max:?}.")]
90	InvalidMinMax { min: u64, max: u64 },
91
92	#[error("Expected min {min:?} and max {max:?} to be equal.")]
93	UnequalMinMax { min: u64, max: u64 },
94
95	#[error("Data index {data_index:?} out of range of sector data count {sector_data_count:?}")]
96	SectorDataOutOfRange {
97		data_index: usize,
98		sector_data_count: usize,
99	},
100
101	#[error("Invalid sector {sector_idx:?}")]
102	InvalidSector { sector_idx: u64 },
103
104	#[error("Internal error.")]
105	InternalError,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
109pub struct FixedSectorDef {
110	pub sector_idx: u64,
111	pub data_idx: usize,
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
115pub struct FlexSectorDef {
116	pub sector_idx: u64,
117}
118
119pub struct SectorDataIdx<'sector, T> {
120	pub sector: &'sector [T],
121	pub sector_idx: u64,
122	pub data_idx: usize,
123}