1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use cxmr_candles::CandleCollector;
use cxmr_feeds::EventData;
use cxmr_orderbook::OrderBook;

use super::{Snapshot, State};

/// Snapshot collector.
#[derive(Default)]
pub struct SnapshotCollector {
    collector: CandleCollector,
    orderbook: OrderBook,
}

impl SnapshotCollector {
    /// Creates snapshot collector with `interval`.
    pub fn new(interval: u64) -> Self {
        SnapshotCollector {
            collector: CandleCollector::new(interval),
            ..SnapshotCollector::default()
        }
    }

    /// Returns last state.
    pub fn finish(self) -> Option<State> {
        let orderbook = self.orderbook;
        self.collector
            .finish()
            .map(move |candle| State::new_close(candle, orderbook))
    }

    /// Pops state from collector if timestamp can close the candle.
    pub fn try_pop(&mut self, ts: u64) -> Option<State> {
        self.collector
            .try_pop(ts)
            .map(|candle| State::new_close(candle, self.orderbook.clone()))
    }

    /// Collects data into a snapshot and returns if state is filled.
    pub fn update(&mut self, event: &EventData) -> Option<State> {
        self.orderbook.update_from_row(event);
        if event.is_trade {
            let update = self.collector.update(event)?;
            Some(State::new(update, self.orderbook.clone()))
        } else {
            self.try_pop(event.ts)
        }
    }

    /// Creates a snapshot from data rows.
    /// Returns `None` if input is empty vector.
    pub fn consume(mut self, events: &Vec<EventData>) -> Option<Snapshot> {
        let mut states: Vec<_> = events
            .iter()
            .filter_map(|event| self.update(event))
            .collect();
        let interval = self.collector.interval;
        if let Some(state) = self.finish() {
            states.push(state);
        }
        Some(Snapshot {
            market: None,
            start_time: events.first().map(|ev| ev.ts),
            interval,
            states,
        })
    }

    /// Creates a snapshot from data rows. Filters close state only.
    /// Returns `None` if input is empty vector.
    pub fn consume_close(mut self, events: &Vec<EventData>) -> Option<Snapshot> {
        self.collector.close_only = true;
        self.consume(events)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use cxmr_tectonic::read_dtf;

    static TEST_FILE: &'static str = "../../tests/data-002/bnc_ETH_PAX.dtf";
    #[test]
    fn dtf_snapshots() {
        let interval = 4 * 60 * 60 * 1000;
        let mut events = read_dtf(TEST_FILE).unwrap().events.unwrap();
        events.iter_mut().for_each(|event| {
            assert!(event.amount >= 0.0);
            event.amount = event.amount.round();
        });
        let mut snapshot: Vec<State> = SnapshotCollector::new(interval)
            .consume(&events)
            .unwrap()
            .states
            .into_iter()
            .filter_map(|state| state.into_close())
            .collect();
        let events = <Vec<EventData>>::from(&Snapshot {
            market: None,
            start_time: None,
            interval,
            states: snapshot.clone(),
        });
        let mut snapshot_squashed: Vec<State> = SnapshotCollector::new(interval)
            .consume(&events)
            .unwrap()
            .states
            .into_iter()
            .filter_map(|state| state.into_close())
            .collect();
        snapshot.iter_mut().for_each(|state| {
            state.candle.volume = state.candle.volume.round();
            state.orderbook.last_update = 0;
        });
        snapshot_squashed.iter_mut().for_each(|state| {
            state.candle.volume = state.candle.volume.round();
            state.orderbook.last_update = 0;
        });
        assert_eq!(snapshot.len(), 7);
        for (index, a) in snapshot.iter().enumerate() {
            let sq = snapshot_squashed.get(index).unwrap();
            assert_eq!(a.candle, sq.candle, "Index: {}", index);
            assert_eq!(a.orderbook, sq.orderbook, "Index: {}", index);
        }
    }

    #[test]
    fn downscale() {
        let interval = 60 * 60 * 1000;
        let longer_interval = 4 * interval;
        let events = read_dtf(TEST_FILE).unwrap().events.unwrap();
        let snapshot = SnapshotCollector::new(interval)
            .consume(&events)
            .unwrap()
            .into_close();
        assert_eq!(snapshot.states.len(), 23);
        let snapshot_bin = Snapshot::open(TEST_FILE, longer_interval).unwrap();
        let snapshot_longerned = snapshot.downscale(longer_interval).unwrap();
        assert_eq!(snapshot_longerned.states.len(), snapshot_bin.states.len());
        for (index, a) in snapshot_bin.states.iter().enumerate() {
            let sq = snapshot_longerned.states.get(index).unwrap();
            assert_eq!(a.candle, sq.candle, "Index: {}", index);
            assert_eq!(a.orderbook, sq.orderbook, "Index: {}", index);
        }
    }
}