apple_quant_algorithmic/timestamp/
range_trade.rs1use crate::timestamp::{TimestampError, TradeTimestamp};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct TradeTimestampRangeExclusive {
5 pub start: TradeTimestamp,
6 pub end: TradeTimestamp,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum TradeTimestampRangeInclusive {
11 One(TradeTimestamp),
12
13 Multiple {
14 start: TradeTimestamp,
15 last: TradeTimestamp,
16 },
17}
18
19impl TradeTimestampRangeInclusive {
20 pub fn new_from_iter(
21 trade_timestamps: impl IntoIterator<Item = &TradeTimestamp>,
22 ) -> Result<Self, TimestampError> {
23 let mut iter = trade_timestamps.into_iter();
24
25 let Some(
26 mut start,
27 ) = iter.next().cloned() else {
28 return Err(TimestampError::RangeNotSatisfied { min: None, max: None });
29 };
30
31 let Some(
32 mut last,
33 ) = iter.next().cloned() else {
34 return Ok(Self::One(start));
35 };
36
37 if start > last {
38 let older_trade_timestamp = start;
39 start = last;
40 last = older_trade_timestamp;
41 }
42
43 for &trade_timestamp in iter {
44 if trade_timestamp < start {
45 start = trade_timestamp;
46 }
47
48 if trade_timestamp > last {
49 last = trade_timestamp;
50 }
51 }
52
53 Ok(Self::Multiple { start, last })
54 }
55
56 pub fn start(
57 &self,
58 ) -> &TradeTimestamp {
59 match self {
60 Self::One(
61 trade_timestamp,
62 ) => trade_timestamp,
63 Self::Multiple {
64 start,
65 last: _,
66 } => start,
67 }
68 }
69
70 pub fn last(
71 &self,
72 ) -> &TradeTimestamp {
73 match self {
74 Self::One(
75 trade_timestamp,
76 ) => trade_timestamp,
77 Self::Multiple {
78 start: _,
79 last,
80 } => last,
81 }
82 }
83}