apple_quant_algorithmic/aggregation/
trade.rs1use rust_decimal::Decimal;
2use smallvec::SmallVec;
3
4use crate::{
5 instrument::{InstrumentData, InstrumentSpec},
6 timestamp::{Timestamp, Timestamped, TradeTimestamp, TradeTimestamped},
7 aggregation::Aggregator, aggregation_std::StdTrades, price::AbsolutePrice,
8 volume::AggressiveVolume,
9};
10
11pub type TradeTradeTimestampListInline<IS, const N: usize> = SmallVec<[TradeTradeTimestamp<IS>; N]>;
12
13pub type TradeTradeTimestampList<IS> = Vec<TradeTradeTimestamp<IS>>;
14
15#[derive(Debug)]
16pub struct RuntimeTrade {
17 pub price: Decimal,
18 pub volume: Decimal,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct Trade<IS: InstrumentSpec> {
23 pub price: AbsolutePrice<IS>,
24 pub aggressive_volume: AggressiveVolume<IS>,
25}
26
27impl<IS: InstrumentSpec> Trade<IS> {
28 pub fn as_runtime(
29 &self,
30 ) -> RuntimeTrade {
31 RuntimeTrade {
32 price: self.price.as_decimal(),
33 volume: self.aggressive_volume
34 .as_directional_intent_volume()
35 .directional_intent.as_decimal(),
36 }
37 }
38}
39
40#[derive(Debug, Clone, Copy, Eq)]
42pub struct TradeTradeTimestamp<IS: InstrumentSpec> {
43 trade: Trade<IS>,
44 trade_timestamp: TradeTimestamp,
45}
46
47impl<IS: InstrumentSpec> TradeTradeTimestamp<IS> {
48 pub fn new(
49 trade: Trade<IS>,
50 trade_timestamp: TradeTimestamp,
51 ) -> Self {
52 Self { trade, trade_timestamp }
53 }
54
55 pub fn trade(
56 &self,
57 ) -> &Trade<IS> {
58 &self.trade
59 }
60}
61
62impl<IS: InstrumentSpec> TradeTimestamped for TradeTradeTimestamp<IS> {
63 fn trade_timestamp(
64 &self,
65 ) -> TradeTimestamp {
66 self.trade_timestamp
67 }
68}
69
70impl<IS: InstrumentSpec> Timestamped for TradeTradeTimestamp<IS> {
71 fn timestamp(
72 &self,
73 ) -> Timestamp {
74 self.trade_timestamp.timestamp()
75 }
76}
77
78impl<
79 'instrument_data,
80 'aggregated_data,
81 IS: InstrumentSpec + 'aggregated_data,
82> Aggregator<
83 'instrument_data,
84 'aggregated_data,
85 Self,
86 StdTrades<'instrument_data, IS>,
87 IS,
88> for TradeTradeTimestamp<IS> {
89 fn hot_data<'iter>(
90 instrument_data: &'instrument_data InstrumentData<
91 'instrument_data,
92 'aggregated_data,
93 IS,
94 >,
95 _recent_aggregated_data_backward: impl Iterator<Item = &'iter Self>,
96 ) -> StdTrades<'instrument_data, IS>
97 where
98 Self: 'iter,
99 {
100 StdTrades::<'instrument_data, IS>::new_aggregate::<Self>(instrument_data)
101 }
102
103 fn aggregate_hot(
104 aggregator_data: &StdTrades<'instrument_data, IS>,
105 ) -> impl Iterator<Item = Self> {
106 aggregator_data.iter().cloned()
107 }
108}
109
110impl<IS: InstrumentSpec> Ord for TradeTradeTimestamp<IS> {
111 fn cmp(
112 &self,
113 other: &Self,
114 ) -> std::cmp::Ordering {
115 self.trade_timestamp.cmp(&other.trade_timestamp)
116 }
117}
118
119impl<IS: InstrumentSpec> PartialOrd for TradeTradeTimestamp<IS> {
120 fn partial_cmp(
121 &self,
122 other: &Self,
123 ) -> Option<std::cmp::Ordering> {
124 self.trade_timestamp.partial_cmp(&other.trade_timestamp)
125 }
126}
127
128impl<IS: InstrumentSpec> PartialEq for TradeTradeTimestamp<IS> {
129 fn eq(
130 &self,
131 other: &Self,
132 ) -> bool {
133 self.trade_timestamp.eq(&other.trade_timestamp)
134 }
135}