use rust_decimal::Decimal;
use smallvec::SmallVec;
use crate::{
instrument::{InstrumentData, InstrumentSpec},
timestamp::{Timestamp, Timestamped, TradeTimestamp, TradeTimestamped},
aggregation::Aggregator, aggregation_std::StdTrades, price::AbsolutePrice,
volume::AggressiveVolume,
};
pub type TradeTradeTimestampListInline<IS, const N: usize> = SmallVec<[TradeTradeTimestamp<IS>; N]>;
pub type TradeTradeTimestampList<IS> = Vec<TradeTradeTimestamp<IS>>;
#[derive(Debug)]
pub struct RuntimeTrade {
pub price: Decimal,
pub volume: Decimal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Trade<IS: InstrumentSpec> {
pub price: AbsolutePrice<IS>,
pub aggressive_volume: AggressiveVolume<IS>,
}
impl<IS: InstrumentSpec> Trade<IS> {
pub fn as_runtime(
&self,
) -> RuntimeTrade {
RuntimeTrade {
price: self.price.as_decimal(),
volume: self.aggressive_volume
.as_directional_intent_volume()
.directional_intent.as_decimal(),
}
}
}
#[derive(Debug, Clone, Copy, Eq)]
pub struct TradeTradeTimestamp<IS: InstrumentSpec> {
trade: Trade<IS>,
trade_timestamp: TradeTimestamp,
}
impl<IS: InstrumentSpec> TradeTradeTimestamp<IS> {
pub fn new(
trade: Trade<IS>,
trade_timestamp: TradeTimestamp,
) -> Self {
Self { trade, trade_timestamp }
}
pub fn trade(
&self,
) -> &Trade<IS> {
&self.trade
}
}
impl<IS: InstrumentSpec> TradeTimestamped for TradeTradeTimestamp<IS> {
fn trade_timestamp(
&self,
) -> TradeTimestamp {
self.trade_timestamp
}
}
impl<IS: InstrumentSpec> Timestamped for TradeTradeTimestamp<IS> {
fn timestamp(
&self,
) -> Timestamp {
self.trade_timestamp.timestamp()
}
}
impl<
'instrument_data,
'aggregated_data,
IS: InstrumentSpec + 'aggregated_data,
> Aggregator<
'instrument_data,
'aggregated_data,
Self,
StdTrades<'instrument_data, IS>,
IS,
> for TradeTradeTimestamp<IS> {
fn hot_data<'iter>(
instrument_data: &'instrument_data InstrumentData<
'instrument_data,
'aggregated_data,
IS,
>,
_recent_aggregated_data_backward: impl Iterator<Item = &'iter Self>,
) -> StdTrades<'instrument_data, IS>
where
Self: 'iter,
{
StdTrades::<'instrument_data, IS>::new_aggregate::<Self>(instrument_data)
}
fn aggregate_hot(
aggregator_data: &StdTrades<'instrument_data, IS>,
) -> impl Iterator<Item = Self> {
aggregator_data.iter().cloned()
}
}
impl<IS: InstrumentSpec> Ord for TradeTradeTimestamp<IS> {
fn cmp(
&self,
other: &Self,
) -> std::cmp::Ordering {
self.trade_timestamp.cmp(&other.trade_timestamp)
}
}
impl<IS: InstrumentSpec> PartialOrd for TradeTradeTimestamp<IS> {
fn partial_cmp(
&self,
other: &Self,
) -> Option<std::cmp::Ordering> {
self.trade_timestamp.partial_cmp(&other.trade_timestamp)
}
}
impl<IS: InstrumentSpec> PartialEq for TradeTradeTimestamp<IS> {
fn eq(
&self,
other: &Self,
) -> bool {
self.trade_timestamp.eq(&other.trade_timestamp)
}
}