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
147
148
149
150
151
152
153
154
155
use crate::{AggregationRule, Error, ModularCandle, Result, TakerTrade};

/// Creates Candles once the price changed by a give relative absolute price delta
#[derive(Debug, Clone)]
pub struct RelativePriceRule {
    init: bool,
    init_price: f64,
    threshold_fraction: f64,
}

impl RelativePriceRule {
    /// Create a new instance.
    ///
    /// # Arguments:
    /// `threshold_fraction`: The relative distance ((`p_t` - `p_i`) / `p_i`) the price needs to move before a new candle creation is triggered.
    ///
    pub fn new(threshold_fraction: f64) -> Result<Self> {
        if threshold_fraction <= 0.0 {
            return Err(Error::InvalidParam);
        }
        Ok(Self {
            init: true,
            init_price: 0.0,
            threshold_fraction,
        })
    }
}

impl<C, T> AggregationRule<C, T> for RelativePriceRule
where
    C: ModularCandle<T>,
    T: TakerTrade,
{
    fn should_trigger(&mut self, trade: &T, _candle: &C) -> bool {
        if self.init {
            self.init = false;
            self.init_price = trade.price();
            return false;
        }

        let price_delta = (trade.price() - self.init_price).abs() / self.init_price;

        if price_delta >= self.threshold_fraction {
            self.init_price = trade.price();
            return true;
        }
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        aggregate_all_trades, load_trades_from_csv,
        plot::{plot_ohlc_candles, OhlcCandle},
        GenericAggregator, Trade,
    };

    #[test]
    fn relative_price_rule() {
        let mut rule = RelativePriceRule::new(0.01).unwrap();

        assert_eq!(
            rule.should_trigger(
                &Trade {
                    timestamp: 0,
                    price: 100.0,
                    size: 10.0
                },
                &OhlcCandle::default()
            ),
            false
        );
        assert_eq!(
            rule.should_trigger(
                &Trade {
                    timestamp: 0,
                    price: 100.5,
                    size: 10.0
                },
                &OhlcCandle::default()
            ),
            false
        );
        assert_eq!(
            rule.should_trigger(
                &Trade {
                    timestamp: 0,
                    price: 101.0,
                    size: 10.0
                },
                &OhlcCandle::default()
            ),
            true
        );
        assert_eq!(
            rule.should_trigger(
                &Trade {
                    timestamp: 0,
                    price: 100.5,
                    size: 10.0
                },
                &OhlcCandle::default()
            ),
            false
        );
        assert_eq!(
            rule.should_trigger(
                &Trade {
                    timestamp: 0,
                    price: 99.0,
                    size: 10.0
                },
                &OhlcCandle::default()
            ),
            true
        );
    }

    #[test]
    fn relative_price_rule_real_data() {
        let trades = load_trades_from_csv("./data/Bitmex_XBTUSD_1M.csv").expect("Unable to load trades at this path, are you sure you're in the root directory of the project?");

        // 0.5% candles
        const THRESHOLD: f64 = 0.005;
        let rule = RelativePriceRule::new(0.01).unwrap();
        let mut aggregator = GenericAggregator::<OhlcCandle, _, Trade>::new(rule);
        let candles = aggregate_all_trades(&trades, &mut aggregator);
        assert!(!candles.is_empty());

        for c in candles {
            assert!((c.high() - c.low()) / c.low() >= THRESHOLD);
            assert!((c.close() - c.open()).abs() / c.open() >= THRESHOLD);
        }
    }

    #[test]
    fn relative_price_candles_plot() {
        let trades = load_trades_from_csv("data/Bitmex_XBTUSD_1M.csv").unwrap();

        const THRESHOLD: f64 = 0.005;
        let rule = RelativePriceRule::new(THRESHOLD).unwrap();
        let mut aggregator = GenericAggregator::<OhlcCandle, _, Trade>::new(rule);
        let candles = aggregate_all_trades(&trades, &mut aggregator);
        println!("got {} candles", candles.len());

        plot_ohlc_candles(
            &candles,
            "img/relative_price_candles_plot.png",
            (3840, 2160),
        )
        .unwrap();
    }
}