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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Smart Execution Algorithms
//!
//! This module provides advanced order execution algorithms including TWAP, VWAP,
//! implementation shortfall, and iceberg orders.

use crate::error::{CoreError, Result};
use chrono::{DateTime, Duration, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use uuid::Uuid;

/// Order side
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderSide {
    /// Buy order
    Buy,
    /// Sell order
    Sell,
}

/// Time-Weighted Average Price (TWAP) execution strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TWAPStrategy {
    /// Strategy ID
    pub id: Uuid,
    /// Token ID
    pub token_id: i64,
    /// Total quantity to execute
    pub total_quantity: Decimal,
    /// Order side
    pub side: OrderSide,
    /// Number of slices
    pub num_slices: usize,
    /// Interval between slices (in seconds)
    pub interval_seconds: u64,
    /// Participation rate (% of market volume, 0-100)
    pub participation_rate: Option<Decimal>,
    /// Start time
    pub start_time: DateTime<Utc>,
    /// End time
    pub end_time: DateTime<Utc>,
    /// Created at
    pub created_at: DateTime<Utc>,
}

impl TWAPStrategy {
    /// Create a new TWAP strategy
    pub fn new(
        token_id: i64,
        total_quantity: Decimal,
        side: OrderSide,
        duration_seconds: u64,
        num_slices: usize,
    ) -> Result<Self> {
        if num_slices == 0 {
            return Err(CoreError::Validation(
                "Number of slices must be > 0".to_string(),
            ));
        }

        let now = Utc::now();
        let interval_seconds = duration_seconds / num_slices as u64;

        Ok(Self {
            id: Uuid::new_v4(),
            token_id,
            total_quantity,
            side,
            num_slices,
            interval_seconds,
            participation_rate: None,
            start_time: now,
            end_time: now + Duration::seconds(duration_seconds as i64),
            created_at: now,
        })
    }

    /// Create adaptive TWAP with participation rate
    pub fn with_participation_rate(mut self, rate: Decimal) -> Result<Self> {
        if rate <= dec!(0) || rate > dec!(100) {
            return Err(CoreError::Validation(
                "Participation rate must be 0-100".to_string(),
            ));
        }
        self.participation_rate = Some(rate);
        Ok(self)
    }

    /// Calculate quantity per slice
    pub fn quantity_per_slice(&self) -> Decimal {
        self.total_quantity / Decimal::from(self.num_slices)
    }

    /// Get slice schedule (timestamps and quantities)
    pub fn get_schedule(&self) -> Vec<(DateTime<Utc>, Decimal)> {
        let mut schedule = Vec::new();
        let quantity_per_slice = self.quantity_per_slice();

        for i in 0..self.num_slices {
            let slice_time =
                self.start_time + Duration::seconds((i as u64 * self.interval_seconds) as i64);
            schedule.push((slice_time, quantity_per_slice));
        }

        schedule
    }

    /// Adjust quantity based on market volume and participation rate
    pub fn adjust_for_market_volume(
        &self,
        market_volume: Decimal,
        slice_quantity: Decimal,
    ) -> Decimal {
        if let Some(rate) = self.participation_rate {
            let max_quantity = market_volume * (rate / dec!(100));
            slice_quantity.min(max_quantity)
        } else {
            slice_quantity
        }
    }

    /// Check if strategy is active
    pub fn is_active(&self, current_time: DateTime<Utc>) -> bool {
        current_time >= self.start_time && current_time <= self.end_time
    }
}

/// Volume-Weighted Average Price (VWAP) execution strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VWAPStrategy {
    /// Strategy ID
    pub id: Uuid,
    /// Token ID
    pub token_id: i64,
    /// Total quantity to execute
    pub total_quantity: Decimal,
    /// Order side
    pub side: OrderSide,
    /// Historical VWAP periods
    pub vwap_periods: Vec<VWAPPeriod>,
    /// Target completion time
    pub target_time: DateTime<Utc>,
    /// Created at
    pub created_at: DateTime<Utc>,
}

/// VWAP period data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VWAPPeriod {
    /// Period start
    pub start_time: DateTime<Utc>,
    /// Period end
    pub end_time: DateTime<Utc>,
    /// Average price
    pub avg_price: Decimal,
    /// Volume
    pub volume: Decimal,
}

impl VWAPStrategy {
    /// Create a new VWAP strategy
    pub fn new(
        token_id: i64,
        total_quantity: Decimal,
        side: OrderSide,
        target_time: DateTime<Utc>,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            token_id,
            total_quantity,
            side,
            vwap_periods: Vec::new(),
            target_time,
            created_at: Utc::now(),
        }
    }

    /// Add historical VWAP period
    pub fn add_period(&mut self, period: VWAPPeriod) {
        self.vwap_periods.push(period);
    }

    /// Calculate historical VWAP
    pub fn calculate_historical_vwap(&self) -> Option<Decimal> {
        if self.vwap_periods.is_empty() {
            return None;
        }

        let total_pv: Decimal = self
            .vwap_periods
            .iter()
            .map(|p| p.avg_price * p.volume)
            .sum();

        let total_volume: Decimal = self.vwap_periods.iter().map(|p| p.volume).sum();

        if total_volume == dec!(0) {
            return None;
        }

        Some(total_pv / total_volume)
    }

    /// Calculate execution schedule based on volume profile
    pub fn volume_weighted_schedule(&self) -> Vec<(DateTime<Utc>, Decimal)> {
        if self.vwap_periods.is_empty() {
            return Vec::new();
        }

        let total_volume: Decimal = self.vwap_periods.iter().map(|p| p.volume).sum();

        if total_volume == dec!(0) {
            return Vec::new();
        }

        self.vwap_periods
            .iter()
            .map(|period| {
                let volume_weight = period.volume / total_volume;
                let quantity = self.total_quantity * volume_weight;
                (period.start_time, quantity)
            })
            .collect()
    }

    /// Calculate VWAP deviation (percentage)
    pub fn vwap_deviation(&self, execution_price: Decimal) -> Option<Decimal> {
        let vwap = self.calculate_historical_vwap()?;
        if vwap == dec!(0) {
            return None;
        }
        Some(((execution_price - vwap) / vwap) * dec!(100))
    }
}

/// Implementation Shortfall strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImplementationShortfall {
    /// Strategy ID
    pub id: Uuid,
    /// Token ID
    pub token_id: i64,
    /// Total quantity
    pub total_quantity: Decimal,
    /// Order side
    pub side: OrderSide,
    /// Arrival price (decision price)
    pub arrival_price: Decimal,
    /// Executions
    pub executions: Vec<Execution>,
    /// Created at
    pub created_at: DateTime<Utc>,
}

/// Individual execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Execution {
    /// Execution time
    pub timestamp: DateTime<Utc>,
    /// Executed quantity
    pub quantity: Decimal,
    /// Execution price
    pub price: Decimal,
}

impl ImplementationShortfall {
    /// Create a new implementation shortfall tracker
    pub fn new(
        token_id: i64,
        total_quantity: Decimal,
        side: OrderSide,
        arrival_price: Decimal,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            token_id,
            total_quantity,
            side,
            arrival_price,
            executions: Vec::new(),
            created_at: Utc::now(),
        }
    }

    /// Add an execution
    pub fn add_execution(&mut self, quantity: Decimal, price: Decimal) {
        self.executions.push(Execution {
            timestamp: Utc::now(),
            quantity,
            price,
        });
    }

    /// Calculate total executed quantity
    pub fn total_executed(&self) -> Decimal {
        self.executions.iter().map(|e| e.quantity).sum()
    }

    /// Calculate average execution price
    pub fn average_execution_price(&self) -> Option<Decimal> {
        let total_quantity = self.total_executed();
        if total_quantity == dec!(0) {
            return None;
        }

        let total_cost: Decimal = self.executions.iter().map(|e| e.quantity * e.price).sum();

        Some(total_cost / total_quantity)
    }

    /// Calculate implementation shortfall (in basis points)
    pub fn calculate_shortfall(&self) -> Option<Decimal> {
        let avg_price = self.average_execution_price()?;

        if self.arrival_price == dec!(0) {
            return None;
        }

        let shortfall = match self.side {
            OrderSide::Buy => (avg_price - self.arrival_price) / self.arrival_price,
            OrderSide::Sell => (self.arrival_price - avg_price) / self.arrival_price,
        };

        Some(shortfall * dec!(10000)) // Convert to basis points
    }

    /// Decompose shortfall into components
    pub fn decompose_shortfall(&self, current_price: Decimal) -> ShortfallDecomposition {
        let total_executed = self.total_executed();
        let remaining = self.total_quantity - total_executed;

        // Delay cost (paper loss on unexecuted portion)
        let delay_cost = if remaining > dec!(0) {
            match self.side {
                OrderSide::Buy => (current_price - self.arrival_price) * remaining,
                OrderSide::Sell => (self.arrival_price - current_price) * remaining,
            }
        } else {
            dec!(0)
        };

        // Execution cost (slippage on executed portion)
        let execution_cost = if let Some(avg_price) = self.average_execution_price() {
            match self.side {
                OrderSide::Buy => (avg_price - self.arrival_price) * total_executed,
                OrderSide::Sell => (self.arrival_price - avg_price) * total_executed,
            }
        } else {
            dec!(0)
        };

        ShortfallDecomposition {
            delay_cost,
            execution_cost,
            total_cost: delay_cost + execution_cost,
        }
    }
}

/// Shortfall decomposition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShortfallDecomposition {
    /// Cost from delaying execution
    pub delay_cost: Decimal,
    /// Cost from execution slippage
    pub execution_cost: Decimal,
    /// Total implementation shortfall
    pub total_cost: Decimal,
}

/// Iceberg order
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IcebergOrder {
    /// Order ID
    pub id: Uuid,
    /// Token ID
    pub token_id: i64,
    /// Total quantity (hidden)
    pub total_quantity: Decimal,
    /// Visible quantity per display
    pub visible_quantity: Decimal,
    /// Order side
    pub side: OrderSide,
    /// Limit price
    pub limit_price: Decimal,
    /// Quantity filled so far
    pub filled_quantity: Decimal,
    /// Current visible slice
    pub current_slice: Decimal,
    /// Replenishment strategy
    pub replenishment_strategy: ReplenishmentStrategy,
    /// Created at
    pub created_at: DateTime<Utc>,
}

/// Replenishment strategy for iceberg orders
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ReplenishmentStrategy {
    /// Immediate replenishment after fill
    Immediate,
    /// Random delay (min, max seconds)
    RandomDelay {
        /// Minimum delay in seconds before replenishment.
        min_seconds: u64,
        /// Maximum delay in seconds before replenishment.
        max_seconds: u64,
    },
    /// Volume-based (wait for certain volume)
    VolumeBased {
        /// Cumulative volume threshold that triggers replenishment.
        threshold_volume: u64,
    },
}

impl IcebergOrder {
    /// Create a new iceberg order
    pub fn new(
        token_id: i64,
        total_quantity: Decimal,
        visible_quantity: Decimal,
        side: OrderSide,
        limit_price: Decimal,
        replenishment_strategy: ReplenishmentStrategy,
    ) -> Result<Self> {
        if visible_quantity > total_quantity {
            return Err(CoreError::Validation(
                "Visible quantity cannot exceed total quantity".to_string(),
            ));
        }

        if visible_quantity <= dec!(0) {
            return Err(CoreError::Validation(
                "Visible quantity must be positive".to_string(),
            ));
        }

        Ok(Self {
            id: Uuid::new_v4(),
            token_id,
            total_quantity,
            visible_quantity,
            side,
            limit_price,
            filled_quantity: dec!(0),
            current_slice: visible_quantity,
            replenishment_strategy,
            created_at: Utc::now(),
        })
    }

    /// Get remaining quantity
    pub fn remaining_quantity(&self) -> Decimal {
        self.total_quantity - self.filled_quantity
    }

    /// Check if order is fully filled
    pub fn is_filled(&self) -> bool {
        self.filled_quantity >= self.total_quantity
    }

    /// Fill current visible slice
    pub fn fill_slice(&mut self, quantity: Decimal) -> Result<()> {
        if quantity > self.current_slice {
            return Err(CoreError::Validation(
                "Fill quantity exceeds visible slice".to_string(),
            ));
        }

        self.filled_quantity += quantity;
        self.current_slice -= quantity;

        // Replenish if current slice is depleted
        if self.current_slice == dec!(0) && !self.is_filled() {
            self.replenish();
        }

        Ok(())
    }

    /// Replenish visible quantity
    fn replenish(&mut self) {
        let remaining = self.remaining_quantity();
        self.current_slice = remaining.min(self.visible_quantity);
    }

    /// Calculate detection risk (lower is better)
    pub fn detection_risk_score(&self) -> Decimal {
        // Risk increases with visibility ratio
        let visibility_ratio = self.visible_quantity / self.total_quantity;

        // Risk decreases with randomization in replenishment
        let randomization_factor = match self.replenishment_strategy {
            ReplenishmentStrategy::Immediate => dec!(1.0),
            ReplenishmentStrategy::RandomDelay { .. } => dec!(0.5),
            ReplenishmentStrategy::VolumeBased { .. } => dec!(0.7),
        };

        visibility_ratio * randomization_factor * dec!(100)
    }
}

/// Smart execution manager
pub struct SmartExecutionManager {
    /// Active TWAP strategies
    twap_strategies: Vec<TWAPStrategy>,
    /// Active VWAP strategies
    vwap_strategies: Vec<VWAPStrategy>,
    /// Implementation shortfall trackers
    shortfall_trackers: Vec<ImplementationShortfall>,
    /// Active iceberg orders
    iceberg_orders: VecDeque<IcebergOrder>,
}

impl SmartExecutionManager {
    /// Create a new smart execution manager
    pub fn new() -> Self {
        Self {
            twap_strategies: Vec::new(),
            vwap_strategies: Vec::new(),
            shortfall_trackers: Vec::new(),
            iceberg_orders: VecDeque::new(),
        }
    }

    /// Add TWAP strategy
    pub fn add_twap(&mut self, strategy: TWAPStrategy) {
        self.twap_strategies.push(strategy);
    }

    /// Add VWAP strategy
    pub fn add_vwap(&mut self, strategy: VWAPStrategy) {
        self.vwap_strategies.push(strategy);
    }

    /// Add implementation shortfall tracker
    pub fn add_shortfall_tracker(&mut self, tracker: ImplementationShortfall) {
        self.shortfall_trackers.push(tracker);
    }

    /// Add iceberg order
    pub fn add_iceberg(&mut self, order: IcebergOrder) {
        self.iceberg_orders.push_back(order);
    }

    /// Get active TWAP strategies
    pub fn get_active_twap(&self) -> Vec<&TWAPStrategy> {
        let now = Utc::now();
        self.twap_strategies
            .iter()
            .filter(|s| s.is_active(now))
            .collect()
    }

    /// Get TWAP strategy by ID
    pub fn get_twap(&self, id: Uuid) -> Option<&TWAPStrategy> {
        self.twap_strategies.iter().find(|s| s.id == id)
    }

    /// Get VWAP strategy by ID
    pub fn get_vwap(&self, id: Uuid) -> Option<&VWAPStrategy> {
        self.vwap_strategies.iter().find(|s| s.id == id)
    }

    /// Get implementation shortfall tracker by ID
    pub fn get_shortfall_tracker(&self, id: Uuid) -> Option<&ImplementationShortfall> {
        self.shortfall_trackers.iter().find(|s| s.id == id)
    }

    /// Get mutable implementation shortfall tracker
    pub fn get_shortfall_tracker_mut(&mut self, id: Uuid) -> Option<&mut ImplementationShortfall> {
        self.shortfall_trackers.iter_mut().find(|s| s.id == id)
    }

    /// Get iceberg order by ID
    pub fn get_iceberg(&self, id: Uuid) -> Option<&IcebergOrder> {
        self.iceberg_orders.iter().find(|o| o.id == id)
    }

    /// Get mutable iceberg order
    pub fn get_iceberg_mut(&mut self, id: Uuid) -> Option<&mut IcebergOrder> {
        self.iceberg_orders.iter_mut().find(|o| o.id == id)
    }

    /// Remove completed iceberg orders
    pub fn cleanup_filled_icebergs(&mut self) {
        self.iceberg_orders.retain(|o| !o.is_filled());
    }
}

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

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

    #[test]
    fn test_twap_strategy_creation() {
        let strategy = TWAPStrategy::new(
            1,
            dec!(1000),
            OrderSide::Buy,
            3600, // 1 hour
            10,   // 10 slices
        )
        .unwrap();

        assert_eq!(strategy.token_id, 1);
        assert_eq!(strategy.total_quantity, dec!(1000));
        assert_eq!(strategy.num_slices, 10);
        assert_eq!(strategy.quantity_per_slice(), dec!(100));
        assert_eq!(strategy.interval_seconds, 360);
    }

    #[test]
    fn test_twap_schedule() {
        let strategy = TWAPStrategy::new(1, dec!(1000), OrderSide::Buy, 1000, 5).unwrap();

        let schedule = strategy.get_schedule();
        assert_eq!(schedule.len(), 5);

        for (_, quantity) in &schedule {
            assert_eq!(*quantity, dec!(200));
        }
    }

    #[test]
    fn test_twap_participation_rate() {
        let strategy = TWAPStrategy::new(1, dec!(1000), OrderSide::Buy, 3600, 10)
            .unwrap()
            .with_participation_rate(dec!(20))
            .unwrap();

        assert_eq!(strategy.participation_rate, Some(dec!(20)));

        // With market volume of 1000, max execution is 20% = 200
        let adjusted = strategy.adjust_for_market_volume(dec!(1000), dec!(300));
        assert_eq!(adjusted, dec!(200));
    }

    #[test]
    fn test_vwap_calculation() {
        let mut strategy = VWAPStrategy::new(
            1,
            dec!(1000),
            OrderSide::Buy,
            Utc::now() + Duration::hours(1),
        );

        strategy.add_period(VWAPPeriod {
            start_time: Utc::now(),
            end_time: Utc::now() + Duration::minutes(15),
            avg_price: dec!(100),
            volume: dec!(500),
        });

        strategy.add_period(VWAPPeriod {
            start_time: Utc::now() + Duration::minutes(15),
            end_time: Utc::now() + Duration::minutes(30),
            avg_price: dec!(110),
            volume: dec!(300),
        });

        // VWAP = (100*500 + 110*300) / (500 + 300) = 83000 / 800 = 103.75
        let vwap = strategy.calculate_historical_vwap().unwrap();
        assert_eq!(vwap, dec!(103.75));
    }

    #[test]
    fn test_implementation_shortfall() {
        let mut tracker = ImplementationShortfall::new(1, dec!(1000), OrderSide::Buy, dec!(100));

        tracker.add_execution(dec!(500), dec!(101));
        tracker.add_execution(dec!(500), dec!(103));

        assert_eq!(tracker.total_executed(), dec!(1000));

        // Average price = (500*101 + 500*103) / 1000 = 102
        let avg_price = tracker.average_execution_price().unwrap();
        assert_eq!(avg_price, dec!(102));

        // Shortfall = (102 - 100) / 100 * 10000 = 200 bps
        let shortfall = tracker.calculate_shortfall().unwrap();
        assert_eq!(shortfall, dec!(200));
    }

    #[test]
    fn test_iceberg_order() {
        let mut order = IcebergOrder::new(
            1,
            dec!(1000),
            dec!(100),
            OrderSide::Buy,
            dec!(50),
            ReplenishmentStrategy::Immediate,
        )
        .unwrap();

        assert_eq!(order.remaining_quantity(), dec!(1000));
        assert_eq!(order.current_slice, dec!(100));

        // Fill the visible slice
        order.fill_slice(dec!(100)).unwrap();

        assert_eq!(order.filled_quantity, dec!(100));
        assert_eq!(order.remaining_quantity(), dec!(900));
        // Should auto-replenish
        assert_eq!(order.current_slice, dec!(100));
    }

    #[test]
    fn test_iceberg_partial_fill() {
        let mut order = IcebergOrder::new(
            1,
            dec!(1000),
            dec!(100),
            OrderSide::Buy,
            dec!(50),
            ReplenishmentStrategy::Immediate,
        )
        .unwrap();

        // Partial fill
        order.fill_slice(dec!(30)).unwrap();

        assert_eq!(order.filled_quantity, dec!(30));
        assert_eq!(order.current_slice, dec!(70));
    }

    #[test]
    fn test_smart_execution_manager() {
        let mut manager = SmartExecutionManager::new();

        let twap = TWAPStrategy::new(1, dec!(1000), OrderSide::Buy, 3600, 10).unwrap();

        let twap_id = twap.id;
        manager.add_twap(twap);

        assert!(manager.get_twap(twap_id).is_some());
    }

    #[test]
    fn test_shortfall_decomposition() {
        let mut tracker = ImplementationShortfall::new(1, dec!(1000), OrderSide::Buy, dec!(100));

        tracker.add_execution(dec!(600), dec!(102));

        // Current price is 105, remaining 400
        let decomposition = tracker.decompose_shortfall(dec!(105));

        // Execution cost = (102 - 100) * 600 = 1200
        assert_eq!(decomposition.execution_cost, dec!(1200));

        // Delay cost = (105 - 100) * 400 = 2000
        assert_eq!(decomposition.delay_cost, dec!(2000));

        // Total = 3200
        assert_eq!(decomposition.total_cost, dec!(3200));
    }
}