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