Skip to main content

apple_quant_algorithmic/
binned_method.rs

1use std::range::Range;
2
3use thiserror::Error;
4
5use crate::timestamp::{Timestamp, TimestampRange, TradeTimestamped, TradeTimestampedRange};
6
7mod fixed;
8mod flex;
9
10pub use fixed::*;
11pub use flex::*;
12
13pub trait BinnedRange<T: TradeTimestampedRange> {
14	fn insert(
15		&mut self,
16		data: impl Iterator<Item = T>,
17	);
18
19	fn get<'a>(
20		&'a self,
21		timestamp_range: &TimestampRange,
22	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
23	where
24		T: 'a;
25
26	fn get_first(
27		&self,
28		timestamp_range: &TimestampRange,
29	) -> Result<Option<&T>, BinnedDataError>;
30
31	fn iter<'a>(
32		&'a self,
33		range: Range<&Timestamp>,
34	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
35	where
36		T: 'a;
37}
38
39pub trait BinnedPoint<T: TradeTimestamped> {
40	fn insert(
41		&mut self,
42		data: impl Iterator<Item = T>,
43	);
44
45	fn get<'a>(
46		&'a self,
47		timestamp: &Timestamp,
48	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
49	where
50		T: 'a;
51
52	fn get_first(
53		&self,
54		timestamp: &Timestamp,
55	) -> Result<Option<&T>, BinnedDataError>;
56
57	fn iter<'a>(
58		&'a self,
59		range: Range<&Timestamp>,
60	) -> Result<impl Iterator<Item = &'a T>, BinnedDataError>
61	where
62		T: 'a;
63}
64
65#[derive(Debug, Error)]
66pub enum BinnedDataError {
67	#[error(
68		"Min {min_timestamp:?} and max {max_timestamp:?} ranges must be in bounds of min {bounds_min_timestamp:?} and max {bounds_max_timestamp:?}."
69	)]
70	MinMaxTimestampOutOfRange {
71		min_timestamp: u64,
72		max_timestamp: u64,
73		bounds_min_timestamp: u64,
74		bounds_max_timestamp: u64,
75	},
76
77	#[error("Aggregation error.")]
78	AggregationError,
79
80	#[error(
81		"Timestamp {timestamp:?} must be in bounds of min {bounds_min:?} and max {bounds_max:?}."
82	)]
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
123	pub data_idx: usize,
124}