finance-query 3.0.0

A Rust library for querying financial data
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
//! Backtest configuration.

mod builder;
mod costs;
mod sizing;

use serde::{Deserialize, Serialize};

use super::error::{BacktestError, Result};

pub use builder::BacktestConfigBuilder;
pub use costs::CommissionFn;
pub use sizing::{PositionSizing, SizingContext};

/// Configuration for backtest execution.
///
/// Use `BacktestConfig::builder()` to construct with the builder pattern.
///
/// # Example
///
/// ```
/// use finance_query::backtesting::BacktestConfig;
///
/// let config = BacktestConfig::builder()
///     .initial_capital(50_000.0)
///     .commission_pct(0.001)
///     .slippage_pct(0.0005)
///     .allow_short(true)
///     .stop_loss_pct(0.05)
///     .take_profit_pct(0.10)
///     .build()
///     .unwrap();
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BacktestConfig {
    /// Initial portfolio capital in base currency
    pub initial_capital: f64,

    /// Commission per trade (flat fee)
    pub commission: f64,

    /// Commission as percentage of trade value (0.0 - 1.0)
    pub commission_pct: f64,

    /// Slippage as percentage of price (0.0 - 1.0)
    pub slippage_pct: f64,

    /// Position sizing: fraction of equity per trade (0.0 - 1.0)
    pub position_size_pct: f64,

    /// Maximum number of concurrent positions (None = unlimited)
    pub max_positions: Option<usize>,

    /// Allow short selling
    pub allow_short: bool,

    /// Require signal strength threshold to trigger trades (0.0 - 1.0)
    pub min_signal_strength: f64,

    /// Stop-loss percentage (0.0 - 1.0). Auto-exit if loss exceeds this.
    pub stop_loss_pct: Option<f64>,

    /// Take-profit percentage (0.0 - 1.0). Auto-exit if profit exceeds this.
    pub take_profit_pct: Option<f64>,

    /// Close any open position at end of backtest
    pub close_at_end: bool,

    /// Annual risk-free rate for Sharpe/Sortino/Calmar ratio calculations (0.0 - 1.0).
    ///
    /// Defaults to `0.0`. Use the current T-bill rate for accurate ratios
    /// (e.g. `0.05` for 5% annual). Converted to a per-period rate internally.
    pub risk_free_rate: f64,

    /// Trailing stop percentage (0.0 - 1.0).
    ///
    /// For **long** positions: tracks the peak (highest) price since entry and
    /// triggers an exit when the price drops this fraction below the peak.
    ///
    /// For **short** positions: tracks the trough (lowest) price since entry and
    /// triggers an exit when the price rises this fraction above the trough.
    ///
    /// Checked before strategy signals each bar, same as `stop_loss_pct` and
    /// `take_profit_pct`. Exit slippage is applied.
    pub trailing_stop_pct: Option<f64>,

    /// When `true`, dividend income received during a holding period is
    /// notionally reinvested: the income is included in the trade's P&L as
    /// if additional shares were purchased at the dividend ex-date close price.
    ///
    /// When `false` (default), dividend income is simply added to P&L at close.
    /// In both cases the dividend amount is recorded on the `Trade` for reporting.
    pub reinvest_dividends: bool,

    /// Number of bars per calendar year, used for annualising returns and ratios.
    ///
    /// Defaults to `252.0` (US equity daily bars). Set to `52.0` for weekly
    /// bars, `12.0` for monthly, or `252.0 * 6.5` (≈ 1638) for hourly bars.
    /// This affects annualised return, Sharpe, Sortino, Calmar, and all
    /// benchmark metrics.
    pub bars_per_year: f64,

    // ── Broker simulation ────────────────────────────────────────────────────
    /// Symmetric bid-ask spread as a fraction of price (0.0 – 1.0).
    ///
    /// On each fill, **half** the spread widens the entry price adversely and
    /// **half** widens the exit price adversely (independent of [`slippage_pct`],
    /// which models directional market impact). For example, a `0.0002` spread
    /// (2 bps) costs 1 bp on entry and 1 bp on exit.
    ///
    /// Defaults to `0.0`.
    ///
    /// [`slippage_pct`]: Self::slippage_pct
    pub spread_pct: f64,

    /// Transaction tax as a fraction of trade value, applied on **buy** orders
    /// only (0.0 – 1.0).
    ///
    /// Models jurisdiction-specific purchase taxes such as the UK Stamp Duty
    /// Reserve Tax (0.5 %). Applied on:
    /// - Long entries (buying shares)
    /// - Short exits (covering the short — i.e. buying to close)
    ///
    /// Defaults to `0.0`.
    pub transaction_tax_pct: f64,

    /// Custom commission function `f(size, price) -> commission`.
    ///
    /// When `Some`, **replaces** the flat [`commission`] + percentage
    /// [`commission_pct`] fields. The function receives the fill quantity
    /// (`size`) and the fill price (`price`) and must return the total
    /// commission amount in the same currency as [`initial_capital`].
    ///
    /// **Not serialized** — reconstruct after deserialization if needed.
    ///
    /// [`commission`]: Self::commission
    /// [`commission_pct`]: Self::commission_pct
    /// [`initial_capital`]: Self::initial_capital
    #[serde(skip)]
    pub commission_fn: Option<CommissionFn>,

    /// Maximum gross exposure as a multiple of equity.
    ///
    /// `1.0` (the default) is a cash account: an entry can commit at most the
    /// available equity. Above `1.0` the shortfall is a margin loan, charged at
    /// [`margin_interest_rate`] and subject to [`maintenance_margin_pct`]. A
    /// short credits its proceeds to cash, so it pays [`short_borrow_rate`]
    /// rather than margin interest at any leverage.
    ///
    /// [`margin_interest_rate`]: Self::margin_interest_rate
    /// [`maintenance_margin_pct`]: Self::maintenance_margin_pct
    /// [`short_borrow_rate`]: Self::short_borrow_rate
    #[serde(default = "default_max_leverage")]
    pub max_leverage: f64,

    /// Equity floor as a fraction of gross exposure, below which the broker
    /// liquidates the position (0.0 - 1.0).
    ///
    /// Consulted for any levered position, and for a short at any leverage: a
    /// short's exposure grows as price rises while its equity falls, so it can
    /// breach the floor without a margin loan. An unlevered long cannot.
    ///
    /// [`max_leverage`]: Self::max_leverage
    #[serde(default = "default_maintenance_margin_pct")]
    pub maintenance_margin_pct: f64,

    /// Annual rate charged on the value of borrowed shares while a short
    /// position is open (0.0 - 1.0).
    ///
    /// Prorated per bar by [`bars_per_year`]. Defaults to `0.0`.
    ///
    /// [`bars_per_year`]: Self::bars_per_year
    #[serde(default)]
    pub short_borrow_rate: f64,

    /// Annual rate charged on a debit cash balance (0.0 - 1.0).
    ///
    /// A leveraged long drives cash negative; that shortfall is the margin
    /// loan. Prorated per bar by [`bars_per_year`]. Defaults to `0.0`, which
    /// makes leverage free and will flatter any leveraged strategy.
    ///
    /// [`bars_per_year`]: Self::bars_per_year
    #[serde(default)]
    pub margin_interest_rate: f64,

    /// Scheme used to size each entry.
    ///
    /// Defaults to [`PositionSizing::FixedFraction`], which commits
    /// [`position_size_pct`] of equity. Every other scheme treats that field as
    /// a ceiling and sizes at or below it.
    ///
    /// [`position_size_pct`]: Self::position_size_pct
    #[serde(default)]
    pub position_sizing: PositionSizing,
}

fn default_max_leverage() -> f64 {
    1.0
}

fn default_maintenance_margin_pct() -> f64 {
    0.25
}

impl Default for BacktestConfig {
    fn default() -> Self {
        Self {
            initial_capital: 10_000.0,
            commission: 0.0,
            commission_pct: 0.001,  // 0.1% per trade
            slippage_pct: 0.001,    // 0.1% slippage
            position_size_pct: 1.0, // Use 100% of available capital
            max_positions: Some(1), // Single position at a time
            allow_short: false,
            min_signal_strength: 0.0,
            stop_loss_pct: None,
            take_profit_pct: None,
            close_at_end: true,
            risk_free_rate: 0.0,
            trailing_stop_pct: None,
            reinvest_dividends: false,
            bars_per_year: 252.0,
            spread_pct: 0.0,
            transaction_tax_pct: 0.0,
            commission_fn: None,
            max_leverage: default_max_leverage(),
            maintenance_margin_pct: default_maintenance_margin_pct(),
            short_borrow_rate: 0.0,
            margin_interest_rate: 0.0,
            position_sizing: PositionSizing::default(),
        }
    }
}

impl BacktestConfig {
    /// Create a zero-cost configuration with no commission, slippage, spread, or tax.
    ///
    /// Useful for unit tests and frictionless benchmark comparisons.
    /// All other fields use the same defaults as [`BacktestConfig::default()`].
    pub fn zero_cost() -> Self {
        Self {
            commission: 0.0,
            commission_pct: 0.0,
            slippage_pct: 0.0,
            spread_pct: 0.0,
            transaction_tax_pct: 0.0,
            commission_fn: None,
            ..Default::default()
        }
    }

    /// Create a new builder
    pub fn builder() -> BacktestConfigBuilder {
        BacktestConfigBuilder::default()
    }

    /// Validate configuration parameters
    pub fn validate(&self) -> Result<()> {
        if !self.initial_capital.is_finite() || self.initial_capital <= 0.0 {
            return Err(BacktestError::invalid_param(
                "initial_capital",
                "must be finite and positive",
            ));
        }

        if !self.commission.is_finite() || self.commission < 0.0 {
            return Err(BacktestError::invalid_param(
                "commission",
                "must be finite and cannot be negative",
            ));
        }

        if !(0.0..=1.0).contains(&self.commission_pct) {
            return Err(BacktestError::invalid_param(
                "commission_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !(0.0..=1.0).contains(&self.slippage_pct) {
            return Err(BacktestError::invalid_param(
                "slippage_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !(self.position_size_pct > 0.0 && self.position_size_pct <= 1.0) {
            return Err(BacktestError::invalid_param(
                "position_size_pct",
                "must be between 0.0 (exclusive) and 1.0 (inclusive)",
            ));
        }

        if !(0.0..=1.0).contains(&self.min_signal_strength) {
            return Err(BacktestError::invalid_param(
                "min_signal_strength",
                "must be between 0.0 and 1.0",
            ));
        }

        if let Some(sl) = self.stop_loss_pct
            && !(0.0..=1.0).contains(&sl)
        {
            return Err(BacktestError::invalid_param(
                "stop_loss_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if let Some(tp) = self.take_profit_pct
            && !(0.0..=1.0).contains(&tp)
        {
            return Err(BacktestError::invalid_param(
                "take_profit_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !(0.0..=1.0).contains(&self.risk_free_rate) {
            return Err(BacktestError::invalid_param(
                "risk_free_rate",
                "must be between 0.0 and 1.0",
            ));
        }

        if let Some(trail) = self.trailing_stop_pct
            && !(0.0..=1.0).contains(&trail)
        {
            return Err(BacktestError::invalid_param(
                "trailing_stop_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !self.bars_per_year.is_finite() || self.bars_per_year <= 0.0 {
            return Err(BacktestError::invalid_param(
                "bars_per_year",
                "must be finite and positive (e.g. 252 for daily, 52 for weekly)",
            ));
        }

        if !(0.0..=1.0).contains(&self.spread_pct) {
            return Err(BacktestError::invalid_param(
                "spread_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !(0.0..=1.0).contains(&self.transaction_tax_pct) {
            return Err(BacktestError::invalid_param(
                "transaction_tax_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if !self.max_leverage.is_finite() || self.max_leverage < 1.0 {
            return Err(BacktestError::invalid_param(
                "max_leverage",
                "must be finite and at least 1.0",
            ));
        }

        if !(0.0..=1.0).contains(&self.maintenance_margin_pct) {
            return Err(BacktestError::invalid_param(
                "maintenance_margin_pct",
                "must be between 0.0 and 1.0",
            ));
        }

        if (self.max_leverage > 1.0 || self.allow_short)
            && self.max_leverage * self.maintenance_margin_pct >= 1.0
        {
            return Err(BacktestError::invalid_param(
                "max_leverage",
                "leverage times maintenance_margin_pct must be below 1.0, or a \
                 full-size entry (levered, or short at any leverage) is \
                 liquidated on the bar after it opens",
            ));
        }

        if !(0.0..=1.0).contains(&self.short_borrow_rate) {
            return Err(BacktestError::invalid_param(
                "short_borrow_rate",
                "must be between 0.0 and 1.0",
            ));
        }

        if !(0.0..=1.0).contains(&self.margin_interest_rate) {
            return Err(BacktestError::invalid_param(
                "margin_interest_rate",
                "must be between 0.0 and 1.0",
            ));
        }

        self.validate_position_sizing()?;

        Ok(())
    }

    fn validate_position_sizing(&self) -> Result<()> {
        match self.position_sizing {
            PositionSizing::FixedFraction => {}
            PositionSizing::Atr {
                risk_pct,
                atr_period,
                atr_multiple,
            } => {
                if !(0.0..=1.0).contains(&risk_pct) {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.risk_pct",
                        "must be between 0.0 and 1.0",
                    ));
                }
                if atr_period == 0 {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.atr_period",
                        "must be at least 1",
                    ));
                }
                if !atr_multiple.is_finite() || atr_multiple <= 0.0 {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.atr_multiple",
                        "must be finite and positive",
                    ));
                }
            }
            PositionSizing::VolatilityTarget {
                target_vol_pct,
                lookback,
            } => {
                if !(0.0..=1.0).contains(&target_vol_pct) {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.target_vol_pct",
                        "must be between 0.0 and 1.0",
                    ));
                }
                if lookback < 2 {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.lookback",
                        "must be at least 2",
                    ));
                }
            }
            PositionSizing::FractionalKelly {
                kelly_fraction,
                lookback_trades,
            } => {
                if !(0.0..=1.0).contains(&kelly_fraction) {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.kelly_fraction",
                        "must be between 0.0 and 1.0",
                    ));
                }
                if lookback_trades == 0 {
                    return Err(BacktestError::invalid_param(
                        "position_sizing.lookback_trades",
                        "must be at least 1",
                    ));
                }
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_default_config() {
        let config = BacktestConfig::default();
        assert_eq!(config.initial_capital, 10_000.0);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_leverage_rejected_when_it_cannot_survive_its_own_entry() {
        let levered = |leverage: f64, maintenance: f64| {
            BacktestConfig::builder()
                .max_leverage(leverage)
                .maintenance_margin_pct(maintenance)
                .build()
        };

        assert!(levered(5.0, 0.25).is_err());
        assert!(levered(4.0, 0.25).is_err());
        assert!(levered(3.0, 0.25).is_ok());
        assert!(levered(1.0, 1.0).is_ok());

        assert!(
            BacktestConfig::builder()
                .max_leverage(1.0)
                .maintenance_margin_pct(1.0)
                .allow_short(true)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_position_sizing_validation_failures() {
        let sizing = |s: PositionSizing| BacktestConfig::builder().position_sizing(s).build();

        assert!(
            sizing(PositionSizing::Atr {
                risk_pct: 0.02,
                atr_period: 0,
                atr_multiple: 2.0,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::Atr {
                risk_pct: -0.01,
                atr_period: 14,
                atr_multiple: 2.0,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::Atr {
                risk_pct: 0.02,
                atr_period: 14,
                atr_multiple: 0.0,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::VolatilityTarget {
                target_vol_pct: 0.01,
                lookback: 1,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::FractionalKelly {
                kelly_fraction: 0.5,
                lookback_trades: 0,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::FractionalKelly {
                kelly_fraction: -0.5,
                lookback_trades: 20,
            })
            .is_err()
        );
        assert!(
            sizing(PositionSizing::Atr {
                risk_pct: 0.02,
                atr_period: 14,
                atr_multiple: 2.0,
            })
            .is_ok()
        );
    }

    #[test]
    fn test_validation_failures() {
        assert!(
            BacktestConfig::builder()
                .initial_capital(-100.0)
                .build()
                .is_err()
        );

        assert!(
            BacktestConfig::builder()
                .commission_pct(1.5)
                .build()
                .is_err()
        );

        assert!(
            BacktestConfig::builder()
                .stop_loss_pct(2.0)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_risk_free_rate() {
        let config = BacktestConfig::builder()
            .risk_free_rate(0.05)
            .build()
            .unwrap();
        assert!((config.risk_free_rate - 0.05).abs() < f64::EPSILON);

        // Out-of-range should fail
        assert!(
            BacktestConfig::builder()
                .risk_free_rate(1.5)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_position_size_zero_rejected() {
        assert!(
            BacktestConfig::builder()
                .position_size_pct(0.0)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_bars_per_year_validation() {
        // Default is 252
        let config = BacktestConfig::default();
        assert!((config.bars_per_year - 252.0).abs() < f64::EPSILON);
        assert!(config.validate().is_ok());

        // Valid custom value
        let config = BacktestConfig::builder()
            .bars_per_year(52.0)
            .build()
            .unwrap();
        assert!((config.bars_per_year - 52.0).abs() < f64::EPSILON);

        // Zero must be rejected
        assert!(
            BacktestConfig::builder()
                .bars_per_year(0.0)
                .build()
                .is_err()
        );

        // Negative must be rejected
        assert!(
            BacktestConfig::builder()
                .bars_per_year(-1.0)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_spread_validation() {
        assert!(BacktestConfig::builder().spread_pct(1.5).build().is_err());
        assert!(BacktestConfig::builder().spread_pct(-0.01).build().is_err());
        assert!(BacktestConfig::builder().spread_pct(0.0).build().is_ok());
        assert!(BacktestConfig::builder().spread_pct(1.0).build().is_ok());
    }

    #[test]
    fn test_transaction_tax_validation() {
        assert!(
            BacktestConfig::builder()
                .transaction_tax_pct(1.5)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .transaction_tax_pct(-0.001)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_margin_defaults_are_a_cash_account() {
        let config = BacktestConfig::default();
        assert_eq!(config.max_leverage, 1.0);
        assert_eq!(config.maintenance_margin_pct, 0.25);
        assert_eq!(config.short_borrow_rate, 0.0);
        assert_eq!(config.margin_interest_rate, 0.0);
        assert_eq!(config.position_sizing, PositionSizing::FixedFraction);
    }

    #[test]
    fn test_margin_field_validation() {
        assert!(BacktestConfig::builder().max_leverage(0.5).build().is_err());
        assert!(
            BacktestConfig::builder()
                .max_leverage(f64::NAN)
                .build()
                .is_err()
        );
        assert!(BacktestConfig::builder().max_leverage(3.0).build().is_ok());

        assert!(
            BacktestConfig::builder()
                .maintenance_margin_pct(1.5)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .short_borrow_rate(-0.01)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .margin_interest_rate(1.5)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_non_finite_fields_rejected() {
        assert!(
            BacktestConfig::builder()
                .initial_capital(f64::NAN)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .initial_capital(f64::INFINITY)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .commission(f64::NAN)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .commission(f64::INFINITY)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .position_size_pct(f64::NAN)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .bars_per_year(f64::NAN)
                .build()
                .is_err()
        );
        assert!(
            BacktestConfig::builder()
                .bars_per_year(f64::INFINITY)
                .build()
                .is_err()
        );
    }

    #[test]
    fn test_config_without_margin_fields_deserializes_to_defaults() {
        let json = serde_json::json!({
            "initial_capital": 10_000.0,
            "commission": 0.0,
            "commission_pct": 0.001,
            "slippage_pct": 0.001,
            "position_size_pct": 1.0,
            "max_positions": 1,
            "allow_short": false,
            "min_signal_strength": 0.0,
            "stop_loss_pct": null,
            "take_profit_pct": null,
            "close_at_end": true,
            "risk_free_rate": 0.0,
            "trailing_stop_pct": null,
            "reinvest_dividends": false,
            "bars_per_year": 252.0,
            "spread_pct": 0.0,
            "transaction_tax_pct": 0.0,
        });
        let config: BacktestConfig = serde_json::from_value(json).unwrap();
        assert_eq!(config.max_leverage, 1.0);
        assert_eq!(config.maintenance_margin_pct, 0.25);
        assert_eq!(config.short_borrow_rate, 0.0);
        assert_eq!(config.margin_interest_rate, 0.0);
        assert_eq!(config.position_sizing, PositionSizing::FixedFraction);
    }

    #[test]
    fn test_zero_cost_clears_new_fields() {
        let config = BacktestConfig::zero_cost();
        assert_eq!(config.spread_pct, 0.0);
        assert_eq!(config.transaction_tax_pct, 0.0);
        assert!(config.commission_fn.is_none());
    }
}