kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
Documentation
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! High-Frequency Trading Metrics
//!
//! Provides metrics and analysis tools for high-frequency trading strategies.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use uuid::Uuid;

/// Order event for HFT analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderEvent {
    /// Identifier of the order this event belongs to
    pub order_id: Uuid,
    /// Type of lifecycle event
    pub event_type: OrderEventType,
    /// Timestamp when the event occurred
    pub timestamp: DateTime<Utc>,
    /// Price associated with this event (if applicable)
    pub price: Option<Decimal>,
    /// Quantity associated with this event (if applicable)
    pub quantity: Option<Decimal>,
}

/// Type of order lifecycle event
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum OrderEventType {
    /// Order was submitted to the exchange
    Submitted,
    /// Order was fully filled
    Filled,
    /// Order was partially filled
    PartiallyFilled,
    /// Order was cancelled
    Cancelled,
    /// Order was rejected by the exchange
    Rejected,
}

/// HFT performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HftMetrics {
    /// Fill ratio (fills / submissions)
    pub fill_ratio: f64,
    /// Quote-to-trade ratio
    pub quote_to_trade_ratio: f64,
    /// Cancellation rate
    pub cancellation_rate: f64,
    /// Average order lifespan (milliseconds)
    pub avg_order_lifespan_ms: f64,
    /// Order velocity (orders per second)
    pub order_velocity: f64,
}

/// HFT metrics analyzer
#[derive(Debug)]
pub struct HftAnalyzer {
    /// Sliding window of recent order events
    events: Vec<OrderEvent>,
    /// Maximum number of events to retain
    window_size: usize,
}

impl HftAnalyzer {
    /// Create a new HFT analyzer with the given event window size
    pub fn new(window_size: usize) -> Self {
        Self {
            events: Vec::new(),
            window_size,
        }
    }

    /// Add an order event to the analyzer's window
    pub fn add_event(&mut self, event: OrderEvent) {
        self.events.push(event);

        // Keep only recent events
        if self.events.len() > self.window_size {
            self.events.drain(0..self.events.len() - self.window_size);
        }
    }

    /// Calculate fill ratio
    pub fn fill_ratio(&self) -> f64 {
        let submissions = self.count_event_type(OrderEventType::Submitted);
        let fills = self.count_event_type(OrderEventType::Filled)
            + self.count_event_type(OrderEventType::PartiallyFilled);

        if submissions > 0 {
            fills as f64 / submissions as f64
        } else {
            0.0
        }
    }

    /// Calculate quote-to-trade ratio
    pub fn quote_to_trade_ratio(&self) -> f64 {
        let quotes = self.count_event_type(OrderEventType::Submitted);
        let trades = self.count_event_type(OrderEventType::Filled);

        if trades > 0 {
            quotes as f64 / trades as f64
        } else {
            0.0
        }
    }

    /// Calculate cancellation rate
    pub fn cancellation_rate(&self) -> f64 {
        let submissions = self.count_event_type(OrderEventType::Submitted);
        let cancellations = self.count_event_type(OrderEventType::Cancelled);

        if submissions > 0 {
            cancellations as f64 / submissions as f64
        } else {
            0.0
        }
    }

    /// Calculate average order lifespan
    pub fn average_order_lifespan_ms(&self) -> f64 {
        let mut lifespans = Vec::new();

        // Group events by order_id
        let mut order_events: std::collections::HashMap<Uuid, Vec<&OrderEvent>> =
            std::collections::HashMap::new();

        for event in &self.events {
            order_events.entry(event.order_id).or_default().push(event);
        }

        for (_order_id, events) in order_events {
            if events.len() < 2 {
                continue;
            }

            // Find submission and terminal event
            let submission = events
                .iter()
                .find(|e| e.event_type == OrderEventType::Submitted);
            let terminal = events.iter().find(|e| {
                matches!(
                    e.event_type,
                    OrderEventType::Filled | OrderEventType::Cancelled
                )
            });

            if let (Some(sub), Some(term)) = (submission, terminal) {
                let lifespan = (term.timestamp - sub.timestamp).num_milliseconds();
                lifespans.push(lifespan as f64);
            }
        }

        if !lifespans.is_empty() {
            lifespans.iter().sum::<f64>() / lifespans.len() as f64
        } else {
            0.0
        }
    }

    /// Calculate order velocity (orders per second)
    pub fn order_velocity(&self) -> f64 {
        if self.events.len() < 2 {
            return 0.0;
        }

        let first_time = self.events.first().unwrap().timestamp;
        let last_time = self.events.last().unwrap().timestamp;
        let duration_secs = (last_time - first_time).num_seconds() as f64;

        if duration_secs > 0.0 {
            self.events.len() as f64 / duration_secs
        } else {
            0.0
        }
    }

    /// Get comprehensive metrics
    pub fn get_metrics(&self) -> HftMetrics {
        HftMetrics {
            fill_ratio: self.fill_ratio(),
            quote_to_trade_ratio: self.quote_to_trade_ratio(),
            cancellation_rate: self.cancellation_rate(),
            avg_order_lifespan_ms: self.average_order_lifespan_ms(),
            order_velocity: self.order_velocity(),
        }
    }

    fn count_event_type(&self, event_type: OrderEventType) -> usize {
        self.events
            .iter()
            .filter(|e| e.event_type == event_type)
            .count()
    }
}

/// Latency tracker
#[derive(Debug)]
pub struct LatencyTracker {
    /// Sliding window of latency measurements in microseconds
    samples: VecDeque<f64>,
    /// Maximum number of samples to retain
    max_samples: usize,
}

impl LatencyTracker {
    /// Create a new latency tracker with the given sample window
    pub fn new(max_samples: usize) -> Self {
        Self {
            samples: VecDeque::with_capacity(max_samples),
            max_samples,
        }
    }

    /// Record a latency sample in microseconds
    pub fn record(&mut self, latency_us: f64) {
        if self.samples.len() >= self.max_samples {
            self.samples.pop_front();
        }
        self.samples.push_back(latency_us);
    }

    /// Return the mean latency of all samples
    pub fn mean(&self) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }
        self.samples.iter().sum::<f64>() / self.samples.len() as f64
    }

    /// Return the `p`th percentile latency (p is 0–100)
    pub fn percentile(&self, p: f64) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }

        let mut sorted: Vec<f64> = self.samples.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let idx = ((sorted.len() as f64) * p / 100.0) as usize;
        sorted[idx.min(sorted.len() - 1)]
    }

    /// Return the maximum latency sample
    pub fn max(&self) -> f64 {
        self.samples
            .iter()
            .copied()
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap_or(0.0)
    }

    /// Return the minimum latency sample
    pub fn min(&self) -> f64 {
        self.samples
            .iter()
            .copied()
            .min_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap_or(0.0)
    }
}

/// Adverse selection detector
#[derive(Debug)]
pub struct AdverseSelectionDetector {
    /// Historical trade records used to compute adverse selection cost
    trades: Vec<TradeRecord>,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
struct TradeRecord {
    price: Decimal,
    side: TradeSide,
    timestamp: DateTime<Utc>,
    midpoint_before: Decimal,
    midpoint_after: Decimal,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
enum TradeSide {
    Buy,
    Sell,
}

impl AdverseSelectionDetector {
    /// Create a new adverse selection detector
    pub fn new() -> Self {
        Self { trades: Vec::new() }
    }

    /// Calculate adverse selection cost
    pub fn adverse_selection_cost(&self) -> f64 {
        if self.trades.is_empty() {
            return 0.0;
        }

        let mut total_cost = 0.0;

        for trade in &self.trades {
            let price_move = match trade.side {
                TradeSide::Buy => trade.midpoint_after - trade.midpoint_before,
                TradeSide::Sell => trade.midpoint_before - trade.midpoint_after,
            };

            let cost_bps = if trade.midpoint_before > Decimal::ZERO {
                ((price_move / trade.midpoint_before) * Decimal::from(10000))
                    .to_string()
                    .parse()
                    .unwrap_or(0.0)
            } else {
                0.0
            };

            total_cost += cost_bps;
        }

        total_cost / self.trades.len() as f64
    }
}

impl Default for AdverseSelectionDetector {
    fn default() -> Self {
        Self::new()
    }
}

/// Inventory risk calculator
#[derive(Debug)]
pub struct InventoryRiskCalculator {
    /// Target inventory level
    pub target_inventory: Decimal,
    /// Maximum inventory deviation
    pub max_deviation: Decimal,
}

impl InventoryRiskCalculator {
    /// Create a new inventory risk calculator
    pub fn new(target_inventory: Decimal, max_deviation: Decimal) -> Self {
        Self {
            target_inventory,
            max_deviation,
        }
    }

    /// Calculate inventory risk score (0-1)
    pub fn risk_score(&self, current_inventory: Decimal) -> f64 {
        let deviation = (current_inventory - self.target_inventory).abs();

        if self.max_deviation == Decimal::ZERO {
            return 0.0;
        }

        let risk: f64 = (deviation / self.max_deviation)
            .to_string()
            .parse()
            .unwrap_or(0.0);

        risk.min(1.0)
    }

    /// Check if inventory is within acceptable range
    pub fn is_acceptable(&self, current_inventory: Decimal) -> bool {
        let deviation = (current_inventory - self.target_inventory).abs();
        deviation <= self.max_deviation
    }

    /// Suggest rebalance amount
    pub fn suggest_rebalance(&self, current_inventory: Decimal) -> Decimal {
        if self.is_acceptable(current_inventory) {
            Decimal::ZERO
        } else {
            self.target_inventory - current_inventory
        }
    }
}

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

    fn create_test_event(event_type: OrderEventType) -> OrderEvent {
        OrderEvent {
            order_id: Uuid::new_v4(),
            event_type,
            timestamp: Utc::now(),
            price: Some(Decimal::from(100)),
            quantity: Some(Decimal::from(10)),
        }
    }

    #[test]
    fn test_fill_ratio() {
        let mut analyzer = HftAnalyzer::new(1000);

        // Add 10 submissions, 7 fills
        for _ in 0..10 {
            analyzer.add_event(create_test_event(OrderEventType::Submitted));
        }
        for _ in 0..7 {
            analyzer.add_event(create_test_event(OrderEventType::Filled));
        }

        let fill_ratio = analyzer.fill_ratio();
        assert!((fill_ratio - 0.7).abs() < 0.01);
    }

    #[test]
    fn test_cancellation_rate() {
        let mut analyzer = HftAnalyzer::new(1000);

        // Add 10 submissions, 3 cancellations
        for _ in 0..10 {
            analyzer.add_event(create_test_event(OrderEventType::Submitted));
        }
        for _ in 0..3 {
            analyzer.add_event(create_test_event(OrderEventType::Cancelled));
        }

        let cancel_rate = analyzer.cancellation_rate();
        assert!((cancel_rate - 0.3).abs() < 0.01);
    }

    #[test]
    fn test_latency_tracker() {
        let mut tracker = LatencyTracker::new(100);

        tracker.record(100.0);
        tracker.record(200.0);
        tracker.record(300.0);

        assert!((tracker.mean() - 200.0).abs() < 0.01);
        assert_eq!(tracker.max(), 300.0);
        assert_eq!(tracker.min(), 100.0);
    }

    #[test]
    fn test_latency_percentile() {
        let mut tracker = LatencyTracker::new(100);

        for i in 1..=100 {
            tracker.record(i as f64);
        }

        let p50 = tracker.percentile(50.0);
        let p95 = tracker.percentile(95.0);

        assert!((p50 - 50.0).abs() < 5.0);
        assert!((p95 - 95.0).abs() < 5.0);
    }

    #[test]
    fn test_inventory_risk() {
        let calculator = InventoryRiskCalculator::new(Decimal::from(1000), Decimal::from(100));

        // Within range
        assert!(calculator.is_acceptable(Decimal::from(1050)));
        assert_eq!(calculator.risk_score(Decimal::from(1050)), 0.5);

        // Outside range
        assert!(!calculator.is_acceptable(Decimal::from(1200)));
        assert!(calculator.risk_score(Decimal::from(1200)) > 0.9);

        // Rebalance suggestion
        let rebalance = calculator.suggest_rebalance(Decimal::from(1200));
        assert_eq!(rebalance, Decimal::from(-200));
    }
}