-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_backtesting_framework.py
More file actions
1341 lines (1121 loc) · 53.1 KB
/
Copy pathadvanced_backtesting_framework.py
File metadata and controls
1341 lines (1121 loc) · 53.1 KB
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
#!/usr/bin/env python3
"""
Advanced Backtesting Framework
A comprehensive backtesting system implementing:
- Walk-forward analysis and out-of-sample testing
- Monte Carlo simulation and bootstrap analysis
- Regime-aware backtesting and regime detection
- Multi-asset and multi-strategy backtesting
- Advanced performance attribution and risk decomposition
- Transaction cost modeling and market impact
- Survivorship bias correction and data quality checks
- Statistical significance testing and confidence intervals
- Factor exposure analysis and style attribution
- Benchmark comparison and relative performance analysis
This framework provides institutional-grade backtesting capabilities
with rigorous statistical validation and comprehensive reporting.
Author: AI Trading System v2.0
Date: January 2025
"""
import numpy as np
import pandas as pd
from typing import Dict, List, Optional, Tuple, Union, Any, Callable
from dataclasses import dataclass, field
from enum import Enum
import asyncio
from datetime import datetime, timedelta
import logging
import json
from abc import ABC, abstractmethod
import warnings
warnings.filterwarnings('ignore')
# Statistical libraries
try:
from scipy import stats
from scipy.optimize import minimize
SCIPY_AVAILABLE = True
except ImportError:
SCIPY_AVAILABLE = False
logging.warning("SciPy not available. Advanced statistical analysis will be limited.")
# Machine learning for regime detection
try:
from sklearn.mixture import GaussianMixture
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
SKLEARN_AVAILABLE = True
except ImportError:
SKLEARN_AVAILABLE = False
logging.warning("Scikit-learn not available. Regime detection will be simplified.")
# Performance analytics
try:
import empyrical as emp
EMPYRICAL_AVAILABLE = True
except ImportError:
EMPYRICAL_AVAILABLE = False
logging.warning("Empyrical not available. Some performance metrics will be calculated manually.")
class BacktestType(Enum):
"""Backtesting methodology types"""
SIMPLE = "simple"
WALK_FORWARD = "walk_forward"
MONTE_CARLO = "monte_carlo"
BOOTSTRAP = "bootstrap"
REGIME_AWARE = "regime_aware"
CROSS_VALIDATION = "cross_validation"
OUT_OF_SAMPLE = "out_of_sample"
class RegimeType(Enum):
"""Market regime types"""
BULL_MARKET = "bull_market"
BEAR_MARKET = "bear_market"
SIDEWAYS_MARKET = "sideways_market"
HIGH_VOLATILITY = "high_volatility"
LOW_VOLATILITY = "low_volatility"
CRISIS = "crisis"
RECOVERY = "recovery"
EXPANSION = "expansion"
CONTRACTION = "contraction"
class PerformanceMetric(Enum):
"""Performance metrics"""
TOTAL_RETURN = "total_return"
ANNUALIZED_RETURN = "annualized_return"
VOLATILITY = "volatility"
SHARPE_RATIO = "sharpe_ratio"
SORTINO_RATIO = "sortino_ratio"
CALMAR_RATIO = "calmar_ratio"
MAX_DRAWDOWN = "max_drawdown"
VAR = "value_at_risk"
CVAR = "conditional_var"
SKEWNESS = "skewness"
KURTOSIS = "kurtosis"
BETA = "beta"
ALPHA = "alpha"
INFORMATION_RATIO = "information_ratio"
TRACKING_ERROR = "tracking_error"
HIT_RATE = "hit_rate"
PROFIT_FACTOR = "profit_factor"
RECOVERY_FACTOR = "recovery_factor"
@dataclass
class BacktestConfig:
"""Backtesting configuration"""
# Time period
start_date: datetime
end_date: datetime
# Initial conditions
initial_capital: float = 1000000.0
# Rebalancing
rebalance_frequency: str = "monthly" # daily, weekly, monthly, quarterly
# Transaction costs
commission_rate: float = 0.001 # 0.1% commission
bid_ask_spread: float = 0.0005 # 0.05% bid-ask spread
market_impact: float = 0.0001 # 0.01% market impact
# Risk management
max_leverage: float = 1.0
max_position_size: float = 0.10 # 10% max position
stop_loss: Optional[float] = None
take_profit: Optional[float] = None
# Walk-forward analysis
training_window: int = 252 # 1 year
testing_window: int = 63 # 3 months
step_size: int = 21 # 1 month
# Monte Carlo settings
n_simulations: int = 1000
confidence_level: float = 0.95
# Benchmark
benchmark: Optional[str] = "SPY"
risk_free_rate: float = 0.02 # 2% risk-free rate
# Data quality
min_history: int = 252 # Minimum data history required
handle_missing_data: str = "forward_fill" # forward_fill, drop, interpolate
# Regime detection
regime_detection: bool = True
regime_lookback: int = 252
n_regimes: int = 3
@dataclass
class Trade:
"""Individual trade record"""
timestamp: datetime
symbol: str
side: str # 'buy' or 'sell'
quantity: float
price: float
commission: float
market_impact: float
total_cost: float
portfolio_value: float
position_size: float
reason: str = "" # Trade reason/signal
def to_dict(self) -> Dict:
return {
'timestamp': self.timestamp.isoformat(),
'symbol': self.symbol,
'side': self.side,
'quantity': self.quantity,
'price': self.price,
'commission': self.commission,
'market_impact': self.market_impact,
'total_cost': self.total_cost,
'portfolio_value': self.portfolio_value,
'position_size': self.position_size,
'reason': self.reason
}
@dataclass
class Position:
"""Portfolio position"""
symbol: str
quantity: float
avg_price: float
current_price: float
market_value: float
unrealized_pnl: float
realized_pnl: float
weight: float
entry_date: datetime
last_update: datetime
def to_dict(self) -> Dict:
return {
'symbol': self.symbol,
'quantity': self.quantity,
'avg_price': self.avg_price,
'current_price': self.current_price,
'market_value': self.market_value,
'unrealized_pnl': self.unrealized_pnl,
'realized_pnl': self.realized_pnl,
'weight': self.weight,
'entry_date': self.entry_date.isoformat(),
'last_update': self.last_update.isoformat()
}
@dataclass
class PerformanceMetrics:
"""Comprehensive performance metrics"""
# Return metrics
total_return: float
annualized_return: float
cumulative_return: float
# Risk metrics
volatility: float
downside_volatility: float
max_drawdown: float
max_drawdown_duration: int
# Risk-adjusted returns
sharpe_ratio: float
sortino_ratio: float
calmar_ratio: float
# Distribution metrics
skewness: float
kurtosis: float
var_95: float
cvar_95: float
# Benchmark comparison
beta: float
alpha: float
information_ratio: float
tracking_error: float
# Trading metrics
total_trades: int
win_rate: float
profit_factor: float
avg_win: float
avg_loss: float
largest_win: float
largest_loss: float
# Additional metrics
recovery_factor: float
payoff_ratio: float
expectancy: float
def to_dict(self) -> Dict:
return {
'total_return': self.total_return,
'annualized_return': self.annualized_return,
'cumulative_return': self.cumulative_return,
'volatility': self.volatility,
'downside_volatility': self.downside_volatility,
'max_drawdown': self.max_drawdown,
'max_drawdown_duration': self.max_drawdown_duration,
'sharpe_ratio': self.sharpe_ratio,
'sortino_ratio': self.sortino_ratio,
'calmar_ratio': self.calmar_ratio,
'skewness': self.skewness,
'kurtosis': self.kurtosis,
'var_95': self.var_95,
'cvar_95': self.cvar_95,
'beta': self.beta,
'alpha': self.alpha,
'information_ratio': self.information_ratio,
'tracking_error': self.tracking_error,
'total_trades': self.total_trades,
'win_rate': self.win_rate,
'profit_factor': self.profit_factor,
'avg_win': self.avg_win,
'avg_loss': self.avg_loss,
'largest_win': self.largest_win,
'largest_loss': self.largest_loss,
'recovery_factor': self.recovery_factor,
'payoff_ratio': self.payoff_ratio,
'expectancy': self.expectancy
}
@dataclass
class RegimeAnalysis:
"""Market regime analysis results"""
regimes: pd.Series # Time series of regime classifications
regime_stats: Dict[RegimeType, Dict[str, float]] # Performance by regime
regime_transitions: pd.DataFrame # Regime transition matrix
regime_probabilities: pd.DataFrame # Regime probabilities over time
current_regime: RegimeType
regime_confidence: float
def to_dict(self) -> Dict:
return {
'regime_stats': {k.value: v for k, v in self.regime_stats.items()},
'current_regime': self.current_regime.value,
'regime_confidence': self.regime_confidence,
'regime_transitions': self.regime_transitions.to_dict() if hasattr(self.regime_transitions, 'to_dict') else {},
'n_regimes': len(self.regime_stats)
}
@dataclass
class BacktestResult:
"""Comprehensive backtesting results"""
config: BacktestConfig
performance_metrics: PerformanceMetrics
portfolio_returns: pd.Series
portfolio_values: pd.Series
positions_history: pd.DataFrame
trades_history: List[Trade]
drawdown_series: pd.Series
regime_analysis: Optional[RegimeAnalysis] = None
factor_exposures: Optional[Dict[str, float]] = None
attribution_analysis: Optional[Dict[str, Any]] = None
statistical_tests: Optional[Dict[str, Any]] = None
def to_dict(self) -> Dict:
return {
'performance_metrics': self.performance_metrics.to_dict(),
'total_trades': len(self.trades_history),
'start_date': self.config.start_date.isoformat(),
'end_date': self.config.end_date.isoformat(),
'initial_capital': self.config.initial_capital,
'final_value': float(self.portfolio_values.iloc[-1]) if len(self.portfolio_values) > 0 else self.config.initial_capital,
'regime_analysis': self.regime_analysis.to_dict() if self.regime_analysis else None,
'factor_exposures': self.factor_exposures,
'attribution_analysis': self.attribution_analysis,
'statistical_tests': self.statistical_tests
}
class RegimeDetector:
"""Market regime detection engine"""
def __init__(self, n_regimes: int = 3, lookback_window: int = 252):
self.n_regimes = n_regimes
self.lookback_window = lookback_window
self.logger = logging.getLogger("regime_detector")
def detect_regimes(self, market_data: pd.DataFrame,
features: Optional[List[str]] = None) -> RegimeAnalysis:
"""Detect market regimes using multiple indicators"""
try:
if features is None:
features = self._create_regime_features(market_data)
# Prepare feature matrix
feature_matrix = self._prepare_features(market_data, features)
# Detect regimes using Gaussian Mixture Model
regimes = self._fit_regime_model(feature_matrix)
# Classify regimes
regime_classifications = self._classify_regimes(regimes, market_data)
# Calculate regime statistics
regime_stats = self._calculate_regime_stats(regime_classifications, market_data)
# Calculate transition matrix
transition_matrix = self._calculate_transition_matrix(regime_classifications)
# Get current regime
current_regime = regime_classifications.iloc[-1] if len(regime_classifications) > 0 else RegimeType.SIDEWAYS_MARKET
return RegimeAnalysis(
regimes=regime_classifications,
regime_stats=regime_stats,
regime_transitions=transition_matrix,
regime_probabilities=pd.DataFrame(), # Simplified for now
current_regime=current_regime,
regime_confidence=0.8 # Simplified confidence score
)
except Exception as e:
self.logger.error(f"Regime detection failed: {e}")
return self._default_regime_analysis(market_data)
def _create_regime_features(self, market_data: pd.DataFrame) -> List[str]:
"""Create features for regime detection"""
features = []
# Assume market_data has 'close' column
if 'close' in market_data.columns:
# Returns
market_data['returns'] = market_data['close'].pct_change()
features.append('returns')
# Volatility
market_data['volatility'] = market_data['returns'].rolling(21).std()
features.append('volatility')
# Trend (moving average slope)
market_data['ma_20'] = market_data['close'].rolling(20).mean()
market_data['trend'] = market_data['ma_20'].pct_change(5)
features.append('trend')
# Momentum
market_data['momentum'] = market_data['close'].pct_change(20)
features.append('momentum')
return features
def _prepare_features(self, market_data: pd.DataFrame, features: List[str]) -> np.ndarray:
"""Prepare feature matrix for regime detection"""
feature_data = market_data[features].dropna()
if SKLEARN_AVAILABLE:
scaler = StandardScaler()
scaled_features = scaler.fit_transform(feature_data)
return scaled_features
else:
# Manual standardization
return (feature_data - feature_data.mean()) / feature_data.std()
def _fit_regime_model(self, feature_matrix: np.ndarray) -> np.ndarray:
"""Fit regime detection model"""
if SKLEARN_AVAILABLE:
try:
# Use Gaussian Mixture Model
gmm = GaussianMixture(n_components=self.n_regimes, random_state=42)
regimes = gmm.fit_predict(feature_matrix)
return regimes
except:
# Fallback to K-means
kmeans = KMeans(n_clusters=self.n_regimes, random_state=42)
regimes = kmeans.fit_predict(feature_matrix)
return regimes
else:
# Simple regime detection based on volatility quantiles
if len(feature_matrix) > 0 and feature_matrix.shape[1] > 1:
volatility_col = 1 # Assume second column is volatility
vol_data = feature_matrix[:, volatility_col]
# Create regimes based on volatility quantiles
low_vol_threshold = np.percentile(vol_data, 33)
high_vol_threshold = np.percentile(vol_data, 67)
regimes = np.zeros(len(vol_data))
regimes[vol_data <= low_vol_threshold] = 0 # Low vol
regimes[(vol_data > low_vol_threshold) & (vol_data <= high_vol_threshold)] = 1 # Medium vol
regimes[vol_data > high_vol_threshold] = 2 # High vol
return regimes.astype(int)
else:
return np.zeros(len(feature_matrix), dtype=int)
def _classify_regimes(self, regimes: np.ndarray, market_data: pd.DataFrame) -> pd.Series:
"""Classify numeric regimes into meaningful regime types"""
# Simple classification based on regime characteristics
regime_map = {
0: RegimeType.LOW_VOLATILITY,
1: RegimeType.SIDEWAYS_MARKET,
2: RegimeType.HIGH_VOLATILITY
}
# Create regime series
regime_series = pd.Series(regimes, index=market_data.index[-len(regimes):])
classified_regimes = regime_series.map(regime_map)
return classified_regimes
def _calculate_regime_stats(self, regimes: pd.Series, market_data: pd.DataFrame) -> Dict[RegimeType, Dict[str, float]]:
"""Calculate performance statistics by regime"""
stats = {}
if 'close' in market_data.columns:
returns = market_data['close'].pct_change()
for regime_type in regimes.unique():
if pd.isna(regime_type):
continue
regime_mask = regimes == regime_type
regime_returns = returns[regime_mask]
if len(regime_returns) > 0:
stats[regime_type] = {
'mean_return': regime_returns.mean(),
'volatility': regime_returns.std(),
'sharpe_ratio': regime_returns.mean() / regime_returns.std() if regime_returns.std() > 0 else 0,
'max_drawdown': self._calculate_max_drawdown(regime_returns),
'frequency': len(regime_returns) / len(returns)
}
return stats
def _calculate_transition_matrix(self, regimes: pd.Series) -> pd.DataFrame:
"""Calculate regime transition matrix"""
try:
unique_regimes = regimes.dropna().unique()
n_regimes = len(unique_regimes)
transition_matrix = np.zeros((n_regimes, n_regimes))
for i in range(len(regimes) - 1):
if pd.notna(regimes.iloc[i]) and pd.notna(regimes.iloc[i + 1]):
from_regime = list(unique_regimes).index(regimes.iloc[i])
to_regime = list(unique_regimes).index(regimes.iloc[i + 1])
transition_matrix[from_regime, to_regime] += 1
# Normalize to probabilities
row_sums = transition_matrix.sum(axis=1)
transition_matrix = transition_matrix / row_sums[:, np.newaxis]
transition_matrix = np.nan_to_num(transition_matrix)
return pd.DataFrame(transition_matrix,
index=[r.value for r in unique_regimes],
columns=[r.value for r in unique_regimes])
except Exception as e:
self.logger.error(f"Transition matrix calculation failed: {e}")
return pd.DataFrame()
def _calculate_max_drawdown(self, returns: pd.Series) -> float:
"""Calculate maximum drawdown"""
try:
cumulative = (1 + returns).cumprod()
running_max = cumulative.expanding().max()
drawdown = (cumulative - running_max) / running_max
return drawdown.min()
except:
return 0.0
def _default_regime_analysis(self, market_data: pd.DataFrame) -> RegimeAnalysis:
"""Default regime analysis for error cases"""
regimes = pd.Series([RegimeType.SIDEWAYS_MARKET] * len(market_data), index=market_data.index)
return RegimeAnalysis(
regimes=regimes,
regime_stats={RegimeType.SIDEWAYS_MARKET: {'mean_return': 0.0, 'volatility': 0.1}},
regime_transitions=pd.DataFrame(),
regime_probabilities=pd.DataFrame(),
current_regime=RegimeType.SIDEWAYS_MARKET,
regime_confidence=0.5
)
class PerformanceAnalyzer:
"""Advanced performance analysis engine"""
def __init__(self, risk_free_rate: float = 0.02):
self.risk_free_rate = risk_free_rate
self.logger = logging.getLogger("performance_analyzer")
def calculate_performance_metrics(self,
portfolio_returns: pd.Series,
benchmark_returns: Optional[pd.Series] = None,
trades: Optional[List[Trade]] = None) -> PerformanceMetrics:
"""Calculate comprehensive performance metrics"""
try:
# Basic return metrics
total_return = (1 + portfolio_returns).prod() - 1
annualized_return = (1 + total_return) ** (252 / len(portfolio_returns)) - 1
cumulative_return = total_return
# Risk metrics
volatility = portfolio_returns.std() * np.sqrt(252)
downside_returns = portfolio_returns[portfolio_returns < 0]
downside_volatility = downside_returns.std() * np.sqrt(252) if len(downside_returns) > 0 else 0
# Drawdown analysis
cumulative = (1 + portfolio_returns).cumprod()
running_max = cumulative.expanding().max()
drawdown = (cumulative - running_max) / running_max
max_drawdown = drawdown.min()
max_drawdown_duration = self._calculate_max_drawdown_duration(drawdown)
# Risk-adjusted returns
excess_returns = portfolio_returns - self.risk_free_rate / 252
sharpe_ratio = excess_returns.mean() / portfolio_returns.std() * np.sqrt(252) if portfolio_returns.std() > 0 else 0
sortino_ratio = excess_returns.mean() / downside_volatility * np.sqrt(252) if downside_volatility > 0 else 0
calmar_ratio = annualized_return / abs(max_drawdown) if max_drawdown != 0 else 0
# Distribution metrics
skewness = portfolio_returns.skew()
kurtosis = portfolio_returns.kurtosis()
var_95 = portfolio_returns.quantile(0.05)
cvar_95 = portfolio_returns[portfolio_returns <= var_95].mean() if len(portfolio_returns[portfolio_returns <= var_95]) > 0 else var_95
# Benchmark comparison
beta, alpha, information_ratio, tracking_error = self._calculate_benchmark_metrics(
portfolio_returns, benchmark_returns
)
# Trading metrics
trading_metrics = self._calculate_trading_metrics(trades) if trades else {
'total_trades': 0, 'win_rate': 0, 'profit_factor': 0,
'avg_win': 0, 'avg_loss': 0, 'largest_win': 0, 'largest_loss': 0
}
# Additional metrics
recovery_factor = abs(total_return / max_drawdown) if max_drawdown != 0 else 0
payoff_ratio = trading_metrics['avg_win'] / abs(trading_metrics['avg_loss']) if trading_metrics['avg_loss'] != 0 else 0
expectancy = trading_metrics['win_rate'] * trading_metrics['avg_win'] + (1 - trading_metrics['win_rate']) * trading_metrics['avg_loss']
return PerformanceMetrics(
total_return=total_return,
annualized_return=annualized_return,
cumulative_return=cumulative_return,
volatility=volatility,
downside_volatility=downside_volatility,
max_drawdown=max_drawdown,
max_drawdown_duration=max_drawdown_duration,
sharpe_ratio=sharpe_ratio,
sortino_ratio=sortino_ratio,
calmar_ratio=calmar_ratio,
skewness=skewness,
kurtosis=kurtosis,
var_95=var_95,
cvar_95=cvar_95,
beta=beta,
alpha=alpha,
information_ratio=information_ratio,
tracking_error=tracking_error,
total_trades=trading_metrics['total_trades'],
win_rate=trading_metrics['win_rate'],
profit_factor=trading_metrics['profit_factor'],
avg_win=trading_metrics['avg_win'],
avg_loss=trading_metrics['avg_loss'],
largest_win=trading_metrics['largest_win'],
largest_loss=trading_metrics['largest_loss'],
recovery_factor=recovery_factor,
payoff_ratio=payoff_ratio,
expectancy=expectancy
)
except Exception as e:
self.logger.error(f"Performance calculation failed: {e}")
return self._default_performance_metrics()
def _calculate_max_drawdown_duration(self, drawdown: pd.Series) -> int:
"""Calculate maximum drawdown duration in days"""
try:
# Find periods where drawdown is at maximum
is_at_max = drawdown == drawdown.min()
if not is_at_max.any():
return 0
# Find the longest consecutive period at maximum drawdown
max_duration = 0
current_duration = 0
for is_max in is_at_max:
if is_max:
current_duration += 1
max_duration = max(max_duration, current_duration)
else:
current_duration = 0
return max_duration
except:
return 0
def _calculate_benchmark_metrics(self, portfolio_returns: pd.Series,
benchmark_returns: Optional[pd.Series]) -> Tuple[float, float, float, float]:
"""Calculate benchmark comparison metrics"""
if benchmark_returns is None or len(benchmark_returns) == 0:
return 0.0, 0.0, 0.0, 0.0
try:
# Align returns
aligned_portfolio, aligned_benchmark = portfolio_returns.align(benchmark_returns, join='inner')
if len(aligned_portfolio) == 0:
return 0.0, 0.0, 0.0, 0.0
# Beta calculation
covariance = np.cov(aligned_portfolio, aligned_benchmark)[0, 1]
benchmark_variance = np.var(aligned_benchmark)
beta = covariance / benchmark_variance if benchmark_variance > 0 else 0
# Alpha calculation (CAPM)
portfolio_mean = aligned_portfolio.mean() * 252
benchmark_mean = aligned_benchmark.mean() * 252
alpha = portfolio_mean - (self.risk_free_rate + beta * (benchmark_mean - self.risk_free_rate))
# Information ratio and tracking error
excess_returns = aligned_portfolio - aligned_benchmark
tracking_error = excess_returns.std() * np.sqrt(252)
information_ratio = excess_returns.mean() / excess_returns.std() * np.sqrt(252) if excess_returns.std() > 0 else 0
return beta, alpha, information_ratio, tracking_error
except Exception as e:
self.logger.error(f"Benchmark metrics calculation failed: {e}")
return 0.0, 0.0, 0.0, 0.0
def _calculate_trading_metrics(self, trades: List[Trade]) -> Dict[str, float]:
"""Calculate trading-specific metrics"""
if not trades:
return {
'total_trades': 0, 'win_rate': 0, 'profit_factor': 0,
'avg_win': 0, 'avg_loss': 0, 'largest_win': 0, 'largest_loss': 0
}
try:
# Calculate P&L for each trade (simplified)
trade_pnls = []
for i, trade in enumerate(trades):
if i > 0 and trades[i-1].symbol == trade.symbol:
# Calculate P&L between entry and exit
if trades[i-1].side != trade.side:
pnl = (trade.price - trades[i-1].price) * trade.quantity
if trades[i-1].side == 'sell': # Short position
pnl = -pnl
trade_pnls.append(pnl)
if not trade_pnls:
return {
'total_trades': len(trades), 'win_rate': 0, 'profit_factor': 0,
'avg_win': 0, 'avg_loss': 0, 'largest_win': 0, 'largest_loss': 0
}
# Separate wins and losses
wins = [pnl for pnl in trade_pnls if pnl > 0]
losses = [pnl for pnl in trade_pnls if pnl < 0]
# Calculate metrics
total_trades = len(trade_pnls)
win_rate = len(wins) / total_trades if total_trades > 0 else 0
avg_win = np.mean(wins) if wins else 0
avg_loss = np.mean(losses) if losses else 0
largest_win = max(wins) if wins else 0
largest_loss = min(losses) if losses else 0
gross_profit = sum(wins) if wins else 0
gross_loss = abs(sum(losses)) if losses else 0
profit_factor = gross_profit / gross_loss if gross_loss > 0 else 0
return {
'total_trades': total_trades,
'win_rate': win_rate,
'profit_factor': profit_factor,
'avg_win': avg_win,
'avg_loss': avg_loss,
'largest_win': largest_win,
'largest_loss': largest_loss
}
except Exception as e:
self.logger.error(f"Trading metrics calculation failed: {e}")
return {
'total_trades': len(trades), 'win_rate': 0, 'profit_factor': 0,
'avg_win': 0, 'avg_loss': 0, 'largest_win': 0, 'largest_loss': 0
}
def _default_performance_metrics(self) -> PerformanceMetrics:
"""Default performance metrics for error cases"""
return PerformanceMetrics(
total_return=0.0, annualized_return=0.0, cumulative_return=0.0,
volatility=0.0, downside_volatility=0.0, max_drawdown=0.0, max_drawdown_duration=0,
sharpe_ratio=0.0, sortino_ratio=0.0, calmar_ratio=0.0,
skewness=0.0, kurtosis=0.0, var_95=0.0, cvar_95=0.0,
beta=0.0, alpha=0.0, information_ratio=0.0, tracking_error=0.0,
total_trades=0, win_rate=0.0, profit_factor=0.0,
avg_win=0.0, avg_loss=0.0, largest_win=0.0, largest_loss=0.0,
recovery_factor=0.0, payoff_ratio=0.0, expectancy=0.0
)
class AdvancedBacktester:
"""Advanced backtesting engine with multiple methodologies"""
def __init__(self, config: BacktestConfig):
self.config = config
self.logger = logging.getLogger("advanced_backtester")
self.regime_detector = RegimeDetector(config.n_regimes, config.regime_lookback)
self.performance_analyzer = PerformanceAnalyzer(config.risk_free_rate)
# Backtesting state
self.portfolio_values = []
self.portfolio_returns = []
self.positions = {}
self.trades = []
self.cash = config.initial_capital
self.current_date = config.start_date
async def run_backtest(self, strategy_func: Callable,
market_data: pd.DataFrame,
benchmark_data: Optional[pd.DataFrame] = None,
backtest_type: BacktestType = BacktestType.SIMPLE) -> BacktestResult:
"""Run comprehensive backtest"""
try:
if backtest_type == BacktestType.WALK_FORWARD:
return await self._walk_forward_backtest(strategy_func, market_data, benchmark_data)
elif backtest_type == BacktestType.MONTE_CARLO:
return await self._monte_carlo_backtest(strategy_func, market_data, benchmark_data)
elif backtest_type == BacktestType.REGIME_AWARE:
return await self._regime_aware_backtest(strategy_func, market_data, benchmark_data)
else:
return await self._simple_backtest(strategy_func, market_data, benchmark_data)
except Exception as e:
self.logger.error(f"Backtest failed: {e}")
return self._default_backtest_result()
async def _simple_backtest(self, strategy_func: Callable,
market_data: pd.DataFrame,
benchmark_data: Optional[pd.DataFrame]) -> BacktestResult:
"""Simple backtesting methodology"""
# Initialize portfolio
self._initialize_portfolio()
# Filter data for backtest period
backtest_data = market_data[
(market_data.index >= self.config.start_date) &
(market_data.index <= self.config.end_date)
].copy()
# Run backtest day by day
for date, row in backtest_data.iterrows():
self.current_date = date
# Get strategy signals
signals = await self._get_strategy_signals(strategy_func, backtest_data.loc[:date])
# Execute trades based on signals
await self._execute_trades(signals, row)
# Update portfolio value
self._update_portfolio_value(row)
# Record daily performance
self._record_daily_performance()
# Calculate final results
return self._generate_backtest_result(benchmark_data)
async def _walk_forward_backtest(self, strategy_func: Callable,
market_data: pd.DataFrame,
benchmark_data: Optional[pd.DataFrame]) -> BacktestResult:
"""Walk-forward analysis backtesting"""
self.logger.info("Running walk-forward backtest")
# Initialize results storage
all_results = []
# Calculate walk-forward windows
start_idx = 0
training_days = self.config.training_window
testing_days = self.config.testing_window
step_days = self.config.step_size
while start_idx + training_days + testing_days <= len(market_data):
# Define training and testing periods
train_start = start_idx
train_end = start_idx + training_days
test_start = train_end
test_end = test_start + testing_days
# Extract data
train_data = market_data.iloc[train_start:train_end]
test_data = market_data.iloc[test_start:test_end]
# Run backtest on test period
period_result = await self._run_period_backtest(
strategy_func, train_data, test_data
)
all_results.append(period_result)
# Move to next window
start_idx += step_days
# Combine results
return self._combine_walk_forward_results(all_results, benchmark_data)
async def _monte_carlo_backtest(self, strategy_func: Callable,
market_data: pd.DataFrame,
benchmark_data: Optional[pd.DataFrame]) -> BacktestResult:
"""Monte Carlo simulation backtesting"""
self.logger.info("Running Monte Carlo backtest")
simulation_results = []
for i in range(self.config.n_simulations):
# Generate random scenario
scenario_data = self._generate_monte_carlo_scenario(market_data, i)
# Run backtest on scenario
scenario_result = await self._simple_backtest(strategy_func, scenario_data, None)
simulation_results.append(scenario_result)
# Analyze simulation results
return self._analyze_monte_carlo_results(simulation_results, benchmark_data)
async def _regime_aware_backtest(self, strategy_func: Callable,
market_data: pd.DataFrame,
benchmark_data: Optional[pd.DataFrame]) -> BacktestResult:
"""Regime-aware backtesting"""
self.logger.info("Running regime-aware backtest")
# Detect market regimes
regime_analysis = self.regime_detector.detect_regimes(market_data)
# Run backtest with regime awareness
result = await self._simple_backtest(strategy_func, market_data, benchmark_data)
# Add regime analysis to result
result.regime_analysis = regime_analysis
return result
def _initialize_portfolio(self):
"""Initialize portfolio state"""
self.cash = self.config.initial_capital
self.positions = {}
self.trades = []
self.portfolio_values = [self.config.initial_capital]
self.portfolio_returns = [0.0]
async def _get_strategy_signals(self, strategy_func: Callable,
historical_data: pd.DataFrame) -> Dict[str, float]:
"""Get trading signals from strategy"""
try:
# Call strategy function with historical data
signals = await strategy_func(historical_data) if asyncio.iscoroutinefunction(strategy_func) else strategy_func(historical_data)
return signals if signals else {}
except Exception as e:
self.logger.error(f"Strategy signal generation failed: {e}")
return {}
async def _execute_trades(self, signals: Dict[str, float], market_row: pd.Series):
"""Execute trades based on signals"""
for symbol, target_weight in signals.items():
if symbol not in market_row.index:
continue
current_price = market_row[symbol]
current_weight = self._get_current_weight(symbol)
# Calculate trade size
weight_diff = target_weight - current_weight
if abs(weight_diff) > 0.01: # Minimum trade threshold
trade_value = weight_diff * self._get_portfolio_value(market_row)
quantity = trade_value / current_price
# Calculate transaction costs
commission = abs(trade_value) * self.config.commission_rate
market_impact = abs(trade_value) * self.config.market_impact
bid_ask_cost = abs(trade_value) * self.config.bid_ask_spread
total_cost = commission + market_impact + bid_ask_cost
# Execute trade
if quantity > 0:
side = 'buy'
else:
side = 'sell'
quantity = abs(quantity)
# Record trade
trade = Trade(
timestamp=self.current_date,
symbol=symbol,
side=side,
quantity=quantity,
price=current_price,
commission=commission,
market_impact=market_impact,
total_cost=total_cost,
portfolio_value=self._get_portfolio_value(market_row),
position_size=target_weight,
reason="Strategy signal"
)
self.trades.append(trade)
# Update positions
self._update_position(symbol, quantity, current_price, side)
# Update cash
if side == 'buy':
self.cash -= trade_value + total_cost
else:
self.cash += trade_value - total_cost
def _get_current_weight(self, symbol: str) -> float:
"""Get current weight of symbol in portfolio"""
if symbol not in self.positions:
return 0.0
position = self.positions[symbol]
portfolio_value = self._get_total_portfolio_value()
return position.market_value / portfolio_value if portfolio_value > 0 else 0.0
def _get_portfolio_value(self, market_row: pd.Series) -> float:
"""Get current portfolio value"""
total_value = self.cash
for symbol, position in self.positions.items():
if symbol in market_row.index:
current_price = market_row[symbol]