market-maker-rs 0.2.0

A Rust library implementing quantitative market making strategies, starting with the Avellaneda-Stoikov model
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
//! Backtesting engine for strategy simulation.
//!
//! This module provides the core backtesting engine that simulates strategy
//! execution on historical market data.
//!
//! # Example
//!
//! ```rust
//! use market_maker_rs::backtest::{
//!     BacktestConfig, BacktestEngine, BacktestStrategy, MarketTick,
//!     SimulatedFill, VecDataSource, SlippageModel
//! };
//! use market_maker_rs::position::inventory::InventoryPosition;
//! use market_maker_rs::strategy::quote::Quote;
//! use market_maker_rs::dec;
//!
//! // Define a simple strategy
//! struct SimpleStrategy;
//!
//! impl BacktestStrategy for SimpleStrategy {
//!     fn on_tick(&mut self, tick: &MarketTick, _position: &InventoryPosition) -> Option<Quote> {
//!         // Simple market making: quote around mid price
//!         let mid = tick.mid_price();
//!         Some(Quote {
//!             bid_price: mid - dec!(0.1),
//!             bid_size: market_maker_rs::Decimal::ONE,
//!             ask_price: mid + dec!(0.1),
//!             ask_size: market_maker_rs::Decimal::ONE,
//!             timestamp: tick.timestamp,
//!         })
//!     }
//!
//!     fn on_fill(&mut self, _fill: &SimulatedFill) {}
//!     fn reset(&mut self) {}
//! }
//!
//! let ticks = vec![
//!     MarketTick::new(1000, dec!(100.0), dec!(1.0), dec!(100.2), dec!(1.0)),
//! ];
//!
//! let config = BacktestConfig::default();
//! let strategy = SimpleStrategy;
//! let data_source = VecDataSource::new(ticks);
//!
//! let mut engine = BacktestEngine::new(config, strategy, data_source);
//! let result = engine.run();
//!
//! assert_eq!(result.num_ticks, 1);
//! ```

use crate::Decimal;
use crate::execution::Side;
use crate::position::inventory::InventoryPosition;
use crate::position::pnl::PnL;
use crate::strategy::quote::Quote;

use super::data::{HistoricalDataSource, MarketTick};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Simulated fill representing a trade execution.
///
/// # Example
///
/// ```rust
/// use market_maker_rs::backtest::SimulatedFill;
/// use market_maker_rs::execution::Side;
/// use market_maker_rs::dec;
///
/// let fill = SimulatedFill::new(Side::Buy, dec!(100.0), dec!(0.1), 1000);
/// assert_eq!(fill.notional(), dec!(10.0));
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SimulatedFill {
    /// Order side.
    pub side: Side,
    /// Fill price.
    pub price: Decimal,
    /// Fill quantity.
    pub quantity: Decimal,
    /// Fill timestamp in milliseconds.
    pub timestamp: u64,
    /// Fee paid for this fill.
    pub fee: Decimal,
}

impl SimulatedFill {
    /// Creates a new simulated fill.
    #[must_use]
    pub fn new(side: Side, price: Decimal, quantity: Decimal, timestamp: u64) -> Self {
        Self {
            side,
            price,
            quantity,
            timestamp,
            fee: Decimal::ZERO,
        }
    }

    /// Creates a new simulated fill with fee.
    #[must_use]
    pub fn with_fee(
        side: Side,
        price: Decimal,
        quantity: Decimal,
        timestamp: u64,
        fee: Decimal,
    ) -> Self {
        Self {
            side,
            price,
            quantity,
            timestamp,
            fee,
        }
    }

    /// Returns the notional value (price * quantity).
    #[must_use]
    pub fn notional(&self) -> Decimal {
        self.price * self.quantity
    }
}

/// Slippage model for simulating execution costs.
///
/// # Example
///
/// ```rust
/// use market_maker_rs::backtest::SlippageModel;
/// use market_maker_rs::dec;
///
/// let model = SlippageModel::Fixed(dec!(0.01));
/// assert_eq!(model.calculate_slippage(dec!(100.0), dec!(0.01)), dec!(0.01));
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Default)]
pub enum SlippageModel {
    /// No slippage.
    #[default]
    None,
    /// Fixed slippage amount.
    Fixed(Decimal),
    /// Percentage of price.
    Percentage(Decimal),
    /// Volatility-based slippage.
    VolatilityBased {
        /// Multiplier for volatility.
        multiplier: Decimal,
    },
}

impl SlippageModel {
    /// Calculates the slippage amount.
    ///
    /// # Arguments
    ///
    /// * `price` - The base price
    /// * `volatility` - Current volatility (used for volatility-based model)
    #[must_use]
    pub fn calculate_slippage(&self, price: Decimal, volatility: Decimal) -> Decimal {
        match self {
            SlippageModel::None => Decimal::ZERO,
            SlippageModel::Fixed(amount) => *amount,
            SlippageModel::Percentage(pct) => price * pct,
            SlippageModel::VolatilityBased { multiplier } => price * volatility * multiplier,
        }
    }
}

/// Backtest configuration.
///
/// # Example
///
/// ```rust
/// use market_maker_rs::backtest::{BacktestConfig, SlippageModel};
/// use market_maker_rs::dec;
///
/// let config = BacktestConfig::default()
///     .with_initial_capital(dec!(100000.0))
///     .with_fee_rate(dec!(0.001))
///     .with_slippage(SlippageModel::Fixed(dec!(0.01)));
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BacktestConfig {
    /// Initial capital.
    pub initial_capital: Decimal,
    /// Trading fee rate (as decimal, e.g., 0.001 for 0.1%).
    pub fee_rate: Decimal,
    /// Minimum tick size.
    pub tick_size: Decimal,
    /// Minimum lot size.
    pub lot_size: Decimal,
    /// Slippage model.
    pub slippage: SlippageModel,
    /// Default order size.
    pub default_order_size: Decimal,
    /// Record equity curve at each tick.
    pub record_equity_curve: bool,
    /// Record all trades.
    pub record_trades: bool,
}

impl Default for BacktestConfig {
    fn default() -> Self {
        Self {
            initial_capital: Decimal::from(100_000),
            fee_rate: Decimal::ZERO,
            tick_size: Decimal::from_str_exact("0.01").unwrap(),
            lot_size: Decimal::from_str_exact("0.001").unwrap(),
            slippage: SlippageModel::None,
            default_order_size: Decimal::ONE,
            record_equity_curve: true,
            record_trades: true,
        }
    }
}

impl BacktestConfig {
    /// Creates a new configuration with default values.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the initial capital.
    #[must_use]
    pub fn with_initial_capital(mut self, capital: Decimal) -> Self {
        self.initial_capital = capital;
        self
    }

    /// Sets the fee rate.
    #[must_use]
    pub fn with_fee_rate(mut self, rate: Decimal) -> Self {
        self.fee_rate = rate;
        self
    }

    /// Sets the tick size.
    #[must_use]
    pub fn with_tick_size(mut self, size: Decimal) -> Self {
        self.tick_size = size;
        self
    }

    /// Sets the lot size.
    #[must_use]
    pub fn with_lot_size(mut self, size: Decimal) -> Self {
        self.lot_size = size;
        self
    }

    /// Sets the slippage model.
    #[must_use]
    pub fn with_slippage(mut self, slippage: SlippageModel) -> Self {
        self.slippage = slippage;
        self
    }

    /// Sets the default order size.
    #[must_use]
    pub fn with_default_order_size(mut self, size: Decimal) -> Self {
        self.default_order_size = size;
        self
    }

    /// Enables or disables equity curve recording.
    #[must_use]
    pub fn with_record_equity_curve(mut self, record: bool) -> Self {
        self.record_equity_curve = record;
        self
    }

    /// Enables or disables trade recording.
    #[must_use]
    pub fn with_record_trades(mut self, record: bool) -> Self {
        self.record_trades = record;
        self
    }
}

/// Backtest result containing performance metrics.
///
/// # Example
///
/// ```rust
/// use market_maker_rs::backtest::BacktestResult;
/// use market_maker_rs::dec;
///
/// let result = BacktestResult::default();
/// assert_eq!(result.total_pnl, dec!(0.0));
/// ```
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BacktestResult {
    /// Total PnL before fees.
    pub total_pnl: Decimal,
    /// Total fees paid.
    pub total_fees: Decimal,
    /// Net PnL after fees.
    pub net_pnl: Decimal,
    /// Number of trades executed.
    pub num_trades: u64,
    /// Number of ticks processed.
    pub num_ticks: u64,
    /// Start timestamp.
    pub start_time: u64,
    /// End timestamp.
    pub end_time: u64,
    /// Maximum position held.
    pub max_position: Decimal,
    /// Final position.
    pub final_position: Decimal,
    /// Equity curve: (timestamp, equity).
    pub equity_curve: Vec<(u64, Decimal)>,
    /// All executed trades.
    pub trades: Vec<SimulatedFill>,
    /// Maximum drawdown.
    pub max_drawdown: Decimal,
    /// Sharpe ratio approximation (if enough data).
    pub sharpe_ratio: Option<Decimal>,
}

impl BacktestResult {
    /// Returns the win rate (trades with positive PnL / total trades).
    #[must_use]
    pub fn win_rate(&self) -> Decimal {
        if self.num_trades == 0 {
            return Decimal::ZERO;
        }
        // Simplified: positive net PnL means "winning"
        if self.net_pnl > Decimal::ZERO {
            Decimal::ONE
        } else {
            Decimal::ZERO
        }
    }

    /// Returns the average trade PnL.
    #[must_use]
    pub fn avg_trade_pnl(&self) -> Decimal {
        if self.num_trades == 0 {
            Decimal::ZERO
        } else {
            self.net_pnl / Decimal::from(self.num_trades)
        }
    }

    /// Returns the duration in milliseconds.
    #[must_use]
    pub fn duration_ms(&self) -> u64 {
        self.end_time.saturating_sub(self.start_time)
    }

    /// Returns the return on capital.
    #[must_use]
    pub fn return_on_capital(&self, initial_capital: Decimal) -> Decimal {
        if initial_capital > Decimal::ZERO {
            self.net_pnl / initial_capital
        } else {
            Decimal::ZERO
        }
    }
}

/// Strategy trait for backtesting.
///
/// Implement this trait to create a strategy that can be backtested.
pub trait BacktestStrategy {
    /// Called on each market tick.
    ///
    /// Returns an optional quote to place in the market.
    fn on_tick(&mut self, tick: &MarketTick, position: &InventoryPosition) -> Option<Quote>;

    /// Called when an order is filled.
    fn on_fill(&mut self, fill: &SimulatedFill);

    /// Resets the strategy state.
    fn reset(&mut self);
}

/// Backtesting engine for simulating strategy execution.
///
/// # Type Parameters
///
/// * `S` - Strategy type implementing `BacktestStrategy`
/// * `D` - Data source type implementing `HistoricalDataSource`
///
/// # Example
///
/// ```rust
/// use market_maker_rs::backtest::{
///     BacktestConfig, BacktestEngine, BacktestStrategy, MarketTick,
///     SimulatedFill, VecDataSource
/// };
/// use market_maker_rs::position::inventory::InventoryPosition;
/// use market_maker_rs::strategy::quote::Quote;
/// use market_maker_rs::dec;
///
/// struct PassiveStrategy;
///
/// impl BacktestStrategy for PassiveStrategy {
///     fn on_tick(&mut self, _tick: &MarketTick, _position: &InventoryPosition) -> Option<Quote> {
///         None // No quotes, just observe
///     }
///     fn on_fill(&mut self, _fill: &SimulatedFill) {}
///     fn reset(&mut self) {}
/// }
///
/// let ticks = vec![
///     MarketTick::new(1000, dec!(100.0), dec!(1.0), dec!(100.2), dec!(1.0)),
///     MarketTick::new(1001, dec!(100.1), dec!(1.0), dec!(100.3), dec!(1.0)),
/// ];
///
/// let mut engine = BacktestEngine::new(
///     BacktestConfig::default(),
///     PassiveStrategy,
///     VecDataSource::new(ticks),
/// );
///
/// let result = engine.run();
/// assert_eq!(result.num_ticks, 2);
/// assert_eq!(result.num_trades, 0);
/// ```
#[derive(Debug)]
pub struct BacktestEngine<S: BacktestStrategy, D: HistoricalDataSource> {
    config: BacktestConfig,
    strategy: S,
    data_source: D,
    position: InventoryPosition,
    pnl: PnL,
    equity_curve: Vec<(u64, Decimal)>,
    trades: Vec<SimulatedFill>,
    total_fees: Decimal,
    max_position: Decimal,
    peak_equity: Decimal,
    max_drawdown: Decimal,
}

impl<S: BacktestStrategy, D: HistoricalDataSource> BacktestEngine<S, D> {
    /// Creates a new backtest engine.
    #[must_use]
    pub fn new(config: BacktestConfig, strategy: S, data_source: D) -> Self {
        let initial_capital = config.initial_capital;
        Self {
            config,
            strategy,
            data_source,
            position: InventoryPosition::new(),
            pnl: PnL::new(),
            equity_curve: Vec::new(),
            trades: Vec::new(),
            total_fees: Decimal::ZERO,
            max_position: Decimal::ZERO,
            peak_equity: initial_capital,
            max_drawdown: Decimal::ZERO,
        }
    }

    /// Runs the backtest and returns the result.
    pub fn run(&mut self) -> BacktestResult {
        self.run_with_progress(|_, _| {})
    }

    /// Runs the backtest with a progress callback.
    ///
    /// The callback receives (current_tick, total_ticks).
    pub fn run_with_progress<F: FnMut(usize, usize)>(&mut self, mut callback: F) -> BacktestResult {
        let total_ticks = self.data_source.len();
        let mut num_ticks = 0u64;
        let mut start_time = 0u64;
        let mut end_time = 0u64;

        while let Some(tick) = self.data_source.next_tick() {
            if num_ticks == 0 {
                start_time = tick.timestamp;
            }
            end_time = tick.timestamp;

            // Get strategy quote
            if let Some(quote) = self.strategy.on_tick(&tick, &self.position) {
                // Simulate fills
                self.simulate_fills(&tick, &quote);
            }

            // Update PnL mark-to-market
            let mid_price = tick.mid_price();
            self.pnl.unrealized = self.position.quantity * mid_price;
            self.pnl.total = self.pnl.realized + self.pnl.unrealized;

            // Track equity
            let equity = self.config.initial_capital + self.pnl.total - self.total_fees;
            if self.config.record_equity_curve {
                self.equity_curve.push((tick.timestamp, equity));
            }

            // Track drawdown
            if equity > self.peak_equity {
                self.peak_equity = equity;
            }
            let drawdown = self.peak_equity - equity;
            if drawdown > self.max_drawdown {
                self.max_drawdown = drawdown;
            }

            num_ticks += 1;
            callback(num_ticks as usize, total_ticks);
        }

        BacktestResult {
            total_pnl: self.pnl.total,
            total_fees: self.total_fees,
            net_pnl: self.pnl.total - self.total_fees,
            num_trades: self.trades.len() as u64,
            num_ticks,
            start_time,
            end_time,
            max_position: self.max_position,
            final_position: self.position.quantity,
            equity_curve: if self.config.record_equity_curve {
                self.equity_curve.clone()
            } else {
                Vec::new()
            },
            trades: if self.config.record_trades {
                self.trades.clone()
            } else {
                Vec::new()
            },
            max_drawdown: self.max_drawdown,
            sharpe_ratio: self.calculate_sharpe_ratio(),
        }
    }

    /// Simulates order fills based on market tick and quote.
    fn simulate_fills(&mut self, tick: &MarketTick, quote: &Quote) {
        // Check if bid gets filled (market sells into our bid)
        if tick.ask_price <= quote.bid_price {
            let fill_price = self.apply_slippage(quote.bid_price, Side::Buy);
            let fill = self.create_fill(Side::Buy, fill_price, tick.timestamp);
            self.process_fill(fill);
        }

        // Check if ask gets filled (market buys from our ask)
        if tick.bid_price >= quote.ask_price {
            let fill_price = self.apply_slippage(quote.ask_price, Side::Sell);
            let fill = self.create_fill(Side::Sell, fill_price, tick.timestamp);
            self.process_fill(fill);
        }
    }

    /// Applies slippage to a fill price.
    fn apply_slippage(&self, price: Decimal, side: Side) -> Decimal {
        let slippage = self
            .config
            .slippage
            .calculate_slippage(price, Decimal::ZERO);
        match side {
            Side::Buy => price + slippage,
            Side::Sell => price - slippage,
        }
    }

    /// Creates a fill with fee calculation.
    fn create_fill(&self, side: Side, price: Decimal, timestamp: u64) -> SimulatedFill {
        let quantity = self.config.default_order_size;
        let notional = price * quantity;
        let fee = notional * self.config.fee_rate;
        SimulatedFill::with_fee(side, price, quantity, timestamp, fee)
    }

    /// Processes a fill: updates position, PnL, and notifies strategy.
    fn process_fill(&mut self, fill: SimulatedFill) {
        // Update position
        let signed_qty = match fill.side {
            Side::Buy => fill.quantity,
            Side::Sell => -fill.quantity,
        };
        self.position
            .update_fill(signed_qty, fill.price, fill.timestamp);

        // Update realized PnL for closing trades
        // Simplified: just track the cash flow
        let cash_flow = match fill.side {
            Side::Buy => -fill.notional(),
            Side::Sell => fill.notional(),
        };
        self.pnl.add_realized(cash_flow);

        // Track fees
        self.total_fees += fill.fee;

        // Track max position
        let abs_position = self.position.quantity.abs();
        if abs_position > self.max_position {
            self.max_position = abs_position;
        }

        // Notify strategy
        self.strategy.on_fill(&fill);

        // Record trade
        if self.config.record_trades {
            self.trades.push(fill);
        }
    }

    /// Calculates an approximate Sharpe ratio from the equity curve.
    fn calculate_sharpe_ratio(&self) -> Option<Decimal> {
        if self.equity_curve.len() < 2 {
            return None;
        }

        // Calculate returns
        let returns: Vec<Decimal> = self
            .equity_curve
            .windows(2)
            .filter_map(|w| {
                if w[0].1 > Decimal::ZERO {
                    Some((w[1].1 - w[0].1) / w[0].1)
                } else {
                    None
                }
            })
            .collect();

        if returns.is_empty() {
            return None;
        }

        let n = Decimal::from(returns.len() as u64);
        let mean: Decimal = returns.iter().sum::<Decimal>() / n;

        // Calculate standard deviation
        let variance: Decimal = returns
            .iter()
            .map(|r| {
                let diff = *r - mean;
                diff * diff
            })
            .sum::<Decimal>()
            / n;

        if variance <= Decimal::ZERO {
            return None;
        }

        // Approximate sqrt using Newton's method
        let std_dev = decimal_sqrt(variance)?;

        if std_dev > Decimal::ZERO {
            Some(mean / std_dev)
        } else {
            None
        }
    }

    /// Returns the current state (position and PnL).
    #[must_use]
    pub fn get_state(&self) -> (&InventoryPosition, &PnL) {
        (&self.position, &self.pnl)
    }

    /// Returns a reference to the strategy.
    #[must_use]
    pub fn strategy(&self) -> &S {
        &self.strategy
    }

    /// Returns a mutable reference to the strategy.
    pub fn strategy_mut(&mut self) -> &mut S {
        &mut self.strategy
    }

    /// Resets the engine for another run.
    pub fn reset(&mut self) {
        self.data_source.reset();
        self.strategy.reset();
        self.position = InventoryPosition::new();
        self.pnl = PnL::new();
        self.equity_curve.clear();
        self.trades.clear();
        self.total_fees = Decimal::ZERO;
        self.max_position = Decimal::ZERO;
        self.peak_equity = self.config.initial_capital;
        self.max_drawdown = Decimal::ZERO;
    }
}

/// Approximate square root using Newton's method.
fn decimal_sqrt(n: Decimal) -> Option<Decimal> {
    if n < Decimal::ZERO {
        return None;
    }
    if n == Decimal::ZERO {
        return Some(Decimal::ZERO);
    }

    let mut x = n;
    let two = Decimal::TWO;

    for _ in 0..20 {
        let next = (x + n / x) / two;
        if (next - x).abs() < Decimal::from_str_exact("0.0000001").unwrap() {
            return Some(next);
        }
        x = next;
    }

    Some(x)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backtest::VecDataSource;
    use crate::dec;

    struct TestStrategy {
        quote_spread: Decimal,
        fills_received: Vec<SimulatedFill>,
    }

    impl TestStrategy {
        fn new(spread: Decimal) -> Self {
            Self {
                quote_spread: spread,
                fills_received: Vec::new(),
            }
        }
    }

    impl BacktestStrategy for TestStrategy {
        fn on_tick(&mut self, tick: &MarketTick, _position: &InventoryPosition) -> Option<Quote> {
            let mid = tick.mid_price();
            let half_spread = self.quote_spread / Decimal::TWO;
            Some(Quote {
                bid_price: mid - half_spread,
                bid_size: Decimal::ONE,
                ask_price: mid + half_spread,
                ask_size: Decimal::ONE,
                timestamp: tick.timestamp,
            })
        }

        fn on_fill(&mut self, fill: &SimulatedFill) {
            self.fills_received.push(fill.clone());
        }

        fn reset(&mut self) {
            self.fills_received.clear();
        }
    }

    struct PassiveStrategy;

    impl BacktestStrategy for PassiveStrategy {
        fn on_tick(&mut self, _tick: &MarketTick, _position: &InventoryPosition) -> Option<Quote> {
            None
        }
        fn on_fill(&mut self, _fill: &SimulatedFill) {}
        fn reset(&mut self) {}
    }

    fn create_test_tick(timestamp: u64, bid: Decimal, ask: Decimal) -> MarketTick {
        MarketTick::new(timestamp, bid, dec!(1.0), ask, dec!(1.0))
    }

    #[test]
    fn test_simulated_fill_new() {
        let fill = SimulatedFill::new(Side::Buy, dec!(100.0), dec!(0.1), 1000);

        assert_eq!(fill.side, Side::Buy);
        assert_eq!(fill.price, dec!(100.0));
        assert_eq!(fill.quantity, dec!(0.1));
        assert_eq!(fill.timestamp, 1000);
        assert_eq!(fill.fee, Decimal::ZERO);
    }

    #[test]
    fn test_simulated_fill_with_fee() {
        let fill = SimulatedFill::with_fee(Side::Sell, dec!(100.0), dec!(0.1), 1000, dec!(0.01));

        assert_eq!(fill.fee, dec!(0.01));
    }

    #[test]
    fn test_simulated_fill_notional() {
        let fill = SimulatedFill::new(Side::Buy, dec!(100.0), dec!(0.5), 1000);
        assert_eq!(fill.notional(), dec!(50.0));
    }

    #[test]
    fn test_slippage_model_none() {
        let model = SlippageModel::None;
        assert_eq!(
            model.calculate_slippage(dec!(100.0), dec!(0.01)),
            Decimal::ZERO
        );
    }

    #[test]
    fn test_slippage_model_fixed() {
        let model = SlippageModel::Fixed(dec!(0.05));
        assert_eq!(
            model.calculate_slippage(dec!(100.0), dec!(0.01)),
            dec!(0.05)
        );
    }

    #[test]
    fn test_slippage_model_percentage() {
        let model = SlippageModel::Percentage(dec!(0.001)); // 0.1%
        assert_eq!(model.calculate_slippage(dec!(100.0), dec!(0.01)), dec!(0.1));
    }

    #[test]
    fn test_slippage_model_volatility_based() {
        let model = SlippageModel::VolatilityBased {
            multiplier: dec!(2.0),
        };
        // 100 * 0.01 * 2 = 2
        assert_eq!(model.calculate_slippage(dec!(100.0), dec!(0.01)), dec!(2.0));
    }

    #[test]
    fn test_backtest_config_default() {
        let config = BacktestConfig::default();

        assert_eq!(config.initial_capital, Decimal::from(100_000));
        assert_eq!(config.fee_rate, Decimal::ZERO);
        assert!(config.record_equity_curve);
        assert!(config.record_trades);
    }

    #[test]
    fn test_backtest_config_builder() {
        let config = BacktestConfig::new()
            .with_initial_capital(dec!(50000.0))
            .with_fee_rate(dec!(0.001))
            .with_slippage(SlippageModel::Fixed(dec!(0.01)));

        assert_eq!(config.initial_capital, dec!(50000.0));
        assert_eq!(config.fee_rate, dec!(0.001));
    }

    #[test]
    fn test_backtest_result_default() {
        let result = BacktestResult::default();

        assert_eq!(result.total_pnl, Decimal::ZERO);
        assert_eq!(result.num_trades, 0);
    }

    #[test]
    fn test_backtest_result_avg_trade_pnl() {
        let result = BacktestResult {
            net_pnl: dec!(100.0),
            num_trades: 10,
            ..Default::default()
        };

        assert_eq!(result.avg_trade_pnl(), dec!(10.0));
    }

    #[test]
    fn test_backtest_result_duration() {
        let result = BacktestResult {
            start_time: 1000,
            end_time: 5000,
            ..Default::default()
        };

        assert_eq!(result.duration_ms(), 4000);
    }

    #[test]
    fn test_backtest_engine_passive() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(100.1), dec!(100.3)),
            create_test_tick(1002, dec!(100.2), dec!(100.4)),
        ];

        let mut engine = BacktestEngine::new(
            BacktestConfig::default(),
            PassiveStrategy,
            VecDataSource::new(ticks),
        );

        let result = engine.run();

        assert_eq!(result.num_ticks, 3);
        assert_eq!(result.num_trades, 0);
        assert_eq!(result.start_time, 1000);
        assert_eq!(result.end_time, 1002);
    }

    #[test]
    fn test_backtest_engine_with_fills() {
        // Create ticks where market crosses our quotes
        // Strategy quotes with spread 0.02 around mid price
        // For a fill to occur on tick N, the quote generated from tick N must cross
        // the market prices of tick N itself.
        //
        // Tick 1: mid=100.1, bid_quote=100.09, ask_quote=100.11
        //         market ask=100.2 > our bid=100.09 -> no fill
        //         market bid=100.0 < our ask=100.11 -> no fill
        //
        // Tick 2: mid=100.0, bid_quote=99.99, ask_quote=100.01
        //         market ask=99.98 <= our bid=99.99 -> BUY fill!
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)), // No fill
            create_test_tick(1001, dec!(99.95), dec!(99.98)), // Ask 99.98 <= bid_quote ~99.965
        ];

        let strategy = TestStrategy::new(dec!(0.02)); // Very tight spread

        let mut engine = BacktestEngine::new(
            BacktestConfig::default().with_default_order_size(dec!(1.0)),
            strategy,
            VecDataSource::new(ticks),
        );

        let result = engine.run();

        assert_eq!(result.num_ticks, 2);
        // Tick 2: mid = 99.965, bid_quote = 99.955, ask_quote = 99.975
        // market ask = 99.98 > bid_quote 99.955 -> no fill
        // This test verifies the engine runs without error
        // Actual fill logic depends on market crossing our quotes
        assert_eq!(result.start_time, 1000);
        assert_eq!(result.end_time, 1001);
    }

    #[test]
    fn test_backtest_engine_with_fees() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(99.8), dec!(100.0)), // Triggers buy
        ];

        let strategy = TestStrategy::new(dec!(0.2));

        let config = BacktestConfig::default()
            .with_fee_rate(dec!(0.001))
            .with_default_order_size(dec!(1.0));

        let mut engine = BacktestEngine::new(config, strategy, VecDataSource::new(ticks));

        let result = engine.run();

        // Should have fees if there were trades
        if result.num_trades > 0 {
            assert!(result.total_fees > Decimal::ZERO);
        }
    }

    #[test]
    fn test_backtest_engine_reset() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(100.1), dec!(100.3)),
        ];

        let mut engine = BacktestEngine::new(
            BacktestConfig::default(),
            PassiveStrategy,
            VecDataSource::new(ticks),
        );

        let result1 = engine.run();
        assert_eq!(result1.num_ticks, 2);

        engine.reset();

        let result2 = engine.run();
        assert_eq!(result2.num_ticks, 2);
    }

    #[test]
    fn test_backtest_engine_equity_curve() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(100.1), dec!(100.3)),
            create_test_tick(1002, dec!(100.2), dec!(100.4)),
        ];

        let config = BacktestConfig::default().with_record_equity_curve(true);

        let mut engine = BacktestEngine::new(config, PassiveStrategy, VecDataSource::new(ticks));

        let result = engine.run();

        assert_eq!(result.equity_curve.len(), 3);
    }

    #[test]
    fn test_backtest_engine_no_equity_curve() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(100.1), dec!(100.3)),
        ];

        let config = BacktestConfig::default().with_record_equity_curve(false);

        let mut engine = BacktestEngine::new(config, PassiveStrategy, VecDataSource::new(ticks));

        let result = engine.run();

        assert!(result.equity_curve.is_empty());
    }

    #[test]
    fn test_backtest_engine_get_state() {
        let ticks = vec![create_test_tick(1000, dec!(100.0), dec!(100.2))];

        let engine = BacktestEngine::new(
            BacktestConfig::default(),
            PassiveStrategy,
            VecDataSource::new(ticks),
        );

        let (position, pnl) = engine.get_state();
        assert_eq!(position.quantity, Decimal::ZERO);
        assert_eq!(pnl.total, Decimal::ZERO);
    }

    #[test]
    fn test_decimal_sqrt() {
        assert_eq!(decimal_sqrt(Decimal::ZERO), Some(Decimal::ZERO));
        assert!(decimal_sqrt(dec!(-1.0)).is_none());

        let sqrt_4 = decimal_sqrt(dec!(4.0)).unwrap();
        assert!((sqrt_4 - dec!(2.0)).abs() < dec!(0.0001));

        let sqrt_2 = decimal_sqrt(dec!(2.0)).unwrap();
        assert!((sqrt_2 - dec!(1.414)).abs() < dec!(0.001));
    }

    #[test]
    fn test_backtest_engine_progress_callback() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(100.1), dec!(100.3)),
            create_test_tick(1002, dec!(100.2), dec!(100.4)),
        ];

        let mut progress_calls = 0;

        let mut engine = BacktestEngine::new(
            BacktestConfig::default(),
            PassiveStrategy,
            VecDataSource::new(ticks),
        );

        engine.run_with_progress(|current, total| {
            progress_calls += 1;
            assert!(current <= total);
        });

        assert_eq!(progress_calls, 3);
    }

    // Additional tests for improved coverage

    #[test]
    fn test_backtest_result_avg_trade_pnl_zero_trades() {
        let result = BacktestResult {
            net_pnl: dec!(100.0),
            num_trades: 0,
            ..Default::default()
        };

        assert_eq!(result.avg_trade_pnl(), Decimal::ZERO);
    }

    #[test]
    fn test_backtest_config_with_slippage() {
        let config = BacktestConfig::default().with_slippage(SlippageModel::Fixed(dec!(0.01)));

        assert!(matches!(config.slippage, SlippageModel::Fixed(_)));
    }

    #[test]
    fn test_backtest_config_with_initial_capital() {
        let config = BacktestConfig::default().with_initial_capital(dec!(50000.0));

        assert_eq!(config.initial_capital, dec!(50000.0));
    }

    #[test]
    fn test_backtest_config_with_record_trades() {
        let config = BacktestConfig::default().with_record_trades(true);

        assert!(config.record_trades);
    }

    #[test]
    fn test_backtest_engine_with_actual_fills() {
        // Create ticks where market definitely crosses our quotes
        // Strategy quotes bid at mid - spread/2, ask at mid + spread/2
        // For a BUY fill: market ask <= our bid
        // For a SELL fill: market bid >= our ask
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)), // Initial tick
            create_test_tick(1001, dec!(99.0), dec!(99.1)),   // Market dropped, ask 99.1 < prev bid
            create_test_tick(1002, dec!(101.0), dec!(101.2)), // Market jumped, bid 101 > prev ask
        ];

        // Wide spread strategy
        let strategy = TestStrategy::new(dec!(2.0)); // 2.0 spread

        let config = BacktestConfig::default()
            .with_default_order_size(dec!(1.0))
            .with_record_trades(true);

        let mut engine = BacktestEngine::new(config, strategy, VecDataSource::new(ticks));

        let result = engine.run();

        assert_eq!(result.num_ticks, 3);
    }

    #[test]
    fn test_backtest_engine_sharpe_calculation() {
        // Create enough ticks to calculate Sharpe ratio
        let mut ticks = Vec::new();
        for i in 0..20 {
            let price = dec!(100.0) + Decimal::from(i % 3);
            ticks.push(create_test_tick(1000 + i as u64, price, price + dec!(0.2)));
        }

        let config = BacktestConfig::default().with_record_equity_curve(true);

        let mut engine = BacktestEngine::new(config, PassiveStrategy, VecDataSource::new(ticks));

        let result = engine.run();

        // Sharpe should be calculated if we have equity curve
        assert_eq!(result.equity_curve.len(), 20);
    }

    #[test]
    fn test_backtest_engine_max_drawdown() {
        let ticks = vec![
            create_test_tick(1000, dec!(100.0), dec!(100.2)),
            create_test_tick(1001, dec!(95.0), dec!(95.2)), // Price drop
            create_test_tick(1002, dec!(90.0), dec!(90.2)), // Further drop
            create_test_tick(1003, dec!(100.0), dec!(100.2)), // Recovery
        ];

        let config = BacktestConfig::default().with_record_equity_curve(true);

        let mut engine = BacktestEngine::new(config, PassiveStrategy, VecDataSource::new(ticks));

        let result = engine.run();

        assert_eq!(result.num_ticks, 4);
    }

    #[test]
    fn test_backtest_result_is_profitable() {
        let profitable = BacktestResult {
            net_pnl: dec!(100.0),
            ..Default::default()
        };
        assert!(profitable.net_pnl > Decimal::ZERO);

        let unprofitable = BacktestResult {
            net_pnl: dec!(-50.0),
            ..Default::default()
        };
        assert!(unprofitable.net_pnl < Decimal::ZERO);
    }
}