-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
1536 lines (1248 loc) · 54.1 KB
/
Copy pathutils.py
File metadata and controls
1536 lines (1248 loc) · 54.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
"""
Utilities Module for RL Trading System
=====================================
This module provides helper functions, constants, and utilities
for the reinforcement learning trading system.
Author: Senior Quantitative Developer
Date: 2024
Version: 2.0
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Dict, List, Tuple, Any, Optional, Union, Callable
import logging
import json
import yaml
import os
from datetime import datetime, timedelta
import warnings
from pathlib import Path
import pickle
import hashlib
import torch
from scipy import stats
from sklearn.preprocessing import StandardScaler, RobustScaler
from sklearn.decomposition import PCA
from sklearn.metrics import mutual_info_score
import joblib
from collections import defaultdict
import time
import psutil
import GPUtil
warnings.filterwarnings('ignore')
# Configure logging
def setup_logging(log_level: str = 'INFO',
log_file: Optional[str] = None,
log_format: Optional[str] = None) -> None:
"""
Configure logging for the entire application with enhanced formatting.
Args:
log_level: Logging level (DEBUG, INFO, WARNING, ERROR)
log_file: Optional log file path
log_format: Optional custom log format
"""
if log_format is None:
log_format = '%(asctime)s | %(name)s | %(levelname)s | %(message)s'
# Create formatter with colors for console output
class ColoredFormatter(logging.Formatter):
"""Custom formatter with colors for different log levels."""
COLORS = {
'DEBUG': '\033[36m', # Cyan
'INFO': '\033[32m', # Green
'WARNING': '\033[33m', # Yellow
'ERROR': '\033[31m', # Red
'CRITICAL': '\033[35m', # Magenta
}
RESET = '\033[0m'
def format(self, record):
log_color = self.COLORS.get(record.levelname, self.RESET)
record.levelname = f"{log_color}{record.levelname}{self.RESET}"
return super().format(record)
# Configure handlers
handlers = []
# Console handler with colors
console_handler = logging.StreamHandler()
console_handler.setFormatter(ColoredFormatter(log_format))
handlers.append(console_handler)
# File handler without colors
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(logging.Formatter(log_format))
handlers.append(file_handler)
# Configure root logger
logging.basicConfig(
level=getattr(logging, log_level.upper()),
handlers=handlers
)
# Set levels for specific loggers
logging.getLogger('matplotlib').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
# Enhanced Constants
TRADING_DAYS_PER_YEAR = 252
SECONDS_PER_DAY = 86400
DEFAULT_RISK_FREE_RATE = 0.02
# Market hours (Eastern Time)
MARKET_OPEN = "09:30"
MARKET_CLOSE = "16:00"
PRE_MARKET_OPEN = "04:00"
AFTER_MARKET_CLOSE = "20:00"
# Technical indicator periods
DEFAULT_PERIODS = {
'SMA_SHORT': 20,
'SMA_LONG': 50,
'SMA_ULTRA_LONG': 200,
'RSI': 14,
'RSI_FAST': 7,
'MACD_FAST': 12,
'MACD_SLOW': 26,
'MACD_SIGNAL': 9,
'BOLLINGER_PERIOD': 20,
'BOLLINGER_STD': 2,
'ATR': 14,
'ADX': 14,
'STOCHASTIC': 14,
'VOLUME_MA': 20,
'MOMENTUM': 10
}
# Model hyperparameter ranges for optimization
HYPERPARAMETER_RANGES = {
'learning_rate': (1e-5, 1e-3),
'batch_size': [32, 64, 128, 256],
'gamma': (0.9, 0.999),
'gae_lambda': (0.9, 0.99),
'clip_range': (0.1, 0.3),
'clip_range_vf': (None, 0.1, 0.2),
'ent_coef': (0.0, 0.1),
'vf_coef': (0.25, 0.75),
'n_steps': [512, 1024, 2048, 4096],
'n_epochs': [5, 10, 20],
'target_kl': (0.005, 0.05)
}
# Market regimes
MARKET_REGIMES = {
'BULL': {'volatility': 'low', 'trend': 'up'},
'BEAR': {'volatility': 'high', 'trend': 'down'},
'VOLATILE': {'volatility': 'high', 'trend': 'sideways'},
'QUIET': {'volatility': 'low', 'trend': 'sideways'}
}
class PerformanceTracker:
"""Enhanced performance tracking with real-time monitoring."""
def __init__(self, metrics_file: str = 'performance_metrics.json'):
"""
Initialize performance tracker.
Args:
metrics_file: Path to store performance metrics
"""
self.metrics_file = metrics_file
self.metrics = self._load_metrics()
self.real_time_metrics = defaultdict(list)
self.start_time = time.time()
def _load_metrics(self) -> Dict[str, List[Dict[str, Any]]]:
"""Load existing metrics from file."""
if os.path.exists(self.metrics_file):
with open(self.metrics_file, 'r') as f:
return json.load(f)
return {}
def record_training_metrics(self,
model_id: str,
epoch: int,
metrics: Dict[str, float],
system_metrics: bool = True) -> None:
"""
Record training metrics with system resource usage.
Args:
model_id: Unique identifier for the model
epoch: Training epoch
metrics: Dictionary of metrics to record
system_metrics: Whether to record system metrics
"""
if model_id not in self.metrics:
self.metrics[model_id] = []
record = {
'timestamp': datetime.now().isoformat(),
'epoch': epoch,
'elapsed_time': time.time() - self.start_time,
**metrics
}
# Add system metrics
if system_metrics:
record.update(self._get_system_metrics())
self.metrics[model_id].append(record)
self._save_metrics()
# Update real-time metrics
for key, value in metrics.items():
self.real_time_metrics[key].append(value)
def _get_system_metrics(self) -> Dict[str, float]:
"""Get current system resource usage."""
metrics = {
'cpu_percent': psutil.cpu_percent(interval=0.1),
'memory_percent': psutil.virtual_memory().percent,
'memory_used_gb': psutil.virtual_memory().used / (1024**3)
}
# Add GPU metrics if available
try:
gpus = GPUtil.getGPUs()
if gpus:
gpu = gpus[0] # Use first GPU
metrics.update({
'gpu_percent': gpu.load * 100,
'gpu_memory_percent': gpu.memoryUtil * 100,
'gpu_temperature': gpu.temperature
})
except:
pass
return metrics
def _save_metrics(self) -> None:
"""Save metrics to file."""
with open(self.metrics_file, 'w') as f:
json.dump(self.metrics, f, indent=2)
def get_best_model(self,
metric: str = 'sharpe_ratio',
minimize: bool = False) -> Tuple[str, float, Dict[str, Any]]:
"""
Get the best performing model based on a metric.
Args:
metric: Metric to use for comparison
minimize: Whether to minimize the metric (e.g., for loss)
Returns:
Tuple of (model_id, best_metric_value, full_record)
"""
best_model = None
best_value = float('inf') if minimize else -float('inf')
best_record = None
for model_id, records in self.metrics.items():
if records:
# Get best record for this model
if minimize:
model_best = min(records, key=lambda x: x.get(metric, float('inf')))
else:
model_best = max(records, key=lambda x: x.get(metric, -float('inf')))
if metric in model_best:
if (minimize and model_best[metric] < best_value) or \
(not minimize and model_best[metric] > best_value):
best_value = model_best[metric]
best_model = model_id
best_record = model_best
return best_model, best_value, best_record
def plot_training_history(self,
model_id: str,
metrics: List[str] = None,
save_path: Optional[str] = None) -> None:
"""
Plot enhanced training history with multiple metrics.
Args:
model_id: Model identifier
metrics: List of metrics to plot (None for default)
save_path: Optional path to save plot
"""
if model_id not in self.metrics:
raise ValueError(f"No metrics found for model {model_id}")
records = self.metrics[model_id]
df = pd.DataFrame(records)
# Default metrics if not specified
if metrics is None:
metrics = ['sharpe_ratio', 'total_return', 'max_drawdown', 'win_rate']
# Filter available metrics
available_metrics = [m for m in metrics if m in df.columns]
# Create subplots
n_metrics = len(available_metrics)
fig, axes = plt.subplots(n_metrics, 1, figsize=(12, 4*n_metrics))
if n_metrics == 1:
axes = [axes]
for ax, metric in zip(axes, available_metrics):
# Plot metric
ax.plot(df['epoch'], df[metric], marker='o', linewidth=2, markersize=6)
# Add rolling average
if len(df) > 10:
rolling_avg = df[metric].rolling(window=5, center=True).mean()
ax.plot(df['epoch'], rolling_avg, '--', alpha=0.7, label='5-epoch avg')
# Highlight best value
if metric in ['loss', 'max_drawdown']: # Minimize these
best_idx = df[metric].idxmin()
else: # Maximize others
best_idx = df[metric].idxmax()
ax.scatter(df.loc[best_idx, 'epoch'], df.loc[best_idx, metric],
color='red', s=100, zorder=5)
ax.annotate(f'Best: {df.loc[best_idx, metric]:.3f}',
xy=(df.loc[best_idx, 'epoch'], df.loc[best_idx, metric]),
xytext=(10, 10), textcoords='offset points',
bbox=dict(boxstyle='round,pad=0.3', facecolor='yellow', alpha=0.5))
ax.set_xlabel('Epoch')
ax.set_ylabel(metric.replace('_', ' ').title())
ax.set_title(f'{metric} over Training')
ax.grid(True, alpha=0.3)
if len(df) > 10:
ax.legend()
plt.suptitle(f'Training History for {model_id}', fontsize=14, fontweight='bold')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.show()
def generate_summary_report(self) -> pd.DataFrame:
"""Generate summary report of all models."""
summary = []
for model_id, records in self.metrics.items():
if records:
# Get final metrics
final_record = records[-1]
# Get best metrics
best_sharpe = max(r.get('sharpe_ratio', -np.inf) for r in records)
best_return = max(r.get('total_return', -np.inf) for r in records)
summary.append({
'model_id': model_id,
'total_epochs': len(records),
'training_time_hours': final_record.get('elapsed_time', 0) / 3600,
'final_sharpe': final_record.get('sharpe_ratio', 0),
'best_sharpe': best_sharpe,
'final_return': final_record.get('total_return', 0),
'best_return': best_return,
'final_drawdown': final_record.get('max_drawdown', 0),
'convergence_epoch': self._find_convergence_epoch(records)
})
return pd.DataFrame(summary)
def _find_convergence_epoch(self, records: List[Dict],
metric: str = 'sharpe_ratio',
patience: int = 10) -> int:
"""Find epoch where model converged."""
if len(records) < patience:
return len(records)
values = [r.get(metric, 0) for r in records]
best_value = -np.inf
patience_counter = 0
for i, value in enumerate(values):
if value > best_value:
best_value = value
patience_counter = 0
else:
patience_counter += 1
if patience_counter >= patience:
return i - patience + 1
return len(records)
class ConfigManager:
"""Enhanced configuration management with validation."""
@staticmethod
def load_config(config_path: str) -> Dict[str, Any]:
"""
Load configuration from YAML or JSON file with validation.
Args:
config_path: Path to configuration file
Returns:
Configuration dictionary
"""
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file not found: {config_path}")
with open(config_path, 'r') as f:
if config_path.endswith('.yaml') or config_path.endswith('.yml'):
config = yaml.safe_load(f)
elif config_path.endswith('.json'):
config = json.load(f)
else:
raise ValueError("Config file must be YAML or JSON")
# Validate configuration
ConfigManager.validate_config(config)
return config
@staticmethod
def validate_config(config: Dict[str, Any]) -> None:
"""Validate configuration for required fields and types."""
required_fields = {
'data': ['symbol', 'start_date', 'end_date'],
'environment': ['initial_capital', 'transaction_cost'],
'agent': ['learning_rate', 'total_timesteps']
}
for section, fields in required_fields.items():
if section not in config:
raise ValueError(f"Missing required section: {section}")
for field in fields:
if field not in config[section]:
raise ValueError(f"Missing required field: {section}.{field}")
# Validate types and ranges
if config['environment']['transaction_cost'] < 0 or config['environment']['transaction_cost'] > 0.1:
raise ValueError("Transaction cost must be between 0 and 0.1")
if config['agent']['learning_rate'] <= 0 or config['agent']['learning_rate'] > 1:
raise ValueError("Learning rate must be between 0 and 1")
@staticmethod
def save_config(config: Dict[str, Any],
config_path: str,
backup: bool = True) -> None:
"""
Save configuration to file with optional backup.
Args:
config: Configuration dictionary
config_path: Path to save configuration
backup: Whether to create backup of existing config
"""
# Create backup if file exists
if backup and os.path.exists(config_path):
backup_path = f"{config_path}.{datetime.now().strftime('%Y%m%d_%H%M%S')}.backup"
os.rename(config_path, backup_path)
# Save configuration
with open(config_path, 'w') as f:
if config_path.endswith('.yaml') or config_path.endswith('.yml'):
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
elif config_path.endswith('.json'):
json.dump(config, f, indent=2)
@staticmethod
def merge_configs(base_config: Dict[str, Any],
override_config: Dict[str, Any]) -> Dict[str, Any]:
"""
Deep merge two configuration dictionaries.
Args:
base_config: Base configuration
override_config: Configuration to override base
Returns:
Merged configuration
"""
import copy
merged = copy.deepcopy(base_config)
def deep_update(d, u):
for k, v in u.items():
if isinstance(v, dict):
d[k] = deep_update(d.get(k, {}), v)
else:
d[k] = v
return d
return deep_update(merged, override_config)
@staticmethod
def generate_experiment_configs(base_config: Dict[str, Any],
param_grid: Dict[str, List[Any]]) -> List[Dict[str, Any]]:
"""
Generate multiple experiment configurations from parameter grid.
Args:
base_config: Base configuration
param_grid: Dictionary of parameters to vary
Returns:
List of experiment configurations
"""
from itertools import product
# Extract parameter names and values
param_names = list(param_grid.keys())
param_values = [param_grid[name] for name in param_names]
# Generate all combinations
configs = []
for values in product(*param_values):
# Create config for this combination
config = base_config.copy()
for name, value in zip(param_names, values):
# Handle nested parameters (e.g., 'agent.learning_rate')
parts = name.split('.')
current = config
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = value
configs.append(config)
return configs
class DataValidator:
"""Enhanced data validation and cleaning utilities."""
@staticmethod
def validate_ohlcv(df: pd.DataFrame) -> Tuple[bool, List[str]]:
"""
Comprehensive OHLCV data validation.
Args:
df: DataFrame with OHLCV data
Returns:
Tuple of (is_valid, list_of_issues)
"""
issues = []
# Check required columns
required_cols = ['Open', 'High', 'Low', 'Close', 'Volume']
missing_cols = [col for col in required_cols if col not in df.columns]
if missing_cols:
issues.append(f"Missing columns: {missing_cols}")
# Check for nulls
null_counts = df[required_cols].isnull().sum()
if null_counts.any():
issues.append(f"Null values found: {null_counts[null_counts > 0].to_dict()}")
# Check price consistency
if all(col in df.columns for col in ['High', 'Low', 'Open', 'Close']):
# High >= Low
invalid_hl = df['High'] < df['Low']
if invalid_hl.any():
issues.append(f"High < Low in {invalid_hl.sum()} rows")
# High >= Open, Close
invalid_high = (df['High'] < df['Open']) | (df['High'] < df['Close'])
if invalid_high.any():
issues.append(f"High < Open/Close in {invalid_high.sum()} rows")
# Low <= Open, Close
invalid_low = (df['Low'] > df['Open']) | (df['Low'] > df['Close'])
if invalid_low.any():
issues.append(f"Low > Open/Close in {invalid_low.sum()} rows")
# Check for zero or negative prices
price_cols = ['Open', 'High', 'Low', 'Close']
for col in price_cols:
if col in df.columns:
invalid_prices = df[col] <= 0
if invalid_prices.any():
issues.append(f"Invalid prices in {col}: {invalid_prices.sum()} rows")
# Check for duplicate dates
if df.index.duplicated().any():
issues.append(f"Duplicate dates found: {df.index.duplicated().sum()}")
# Check date continuity
if len(df) > 1:
date_diffs = pd.Series(df.index).diff()
# Check for backwards dates
if (date_diffs < pd.Timedelta(0)).any():
issues.append("Dates not in ascending order")
# Check for large gaps (more than 10 days)
large_gaps = date_diffs[date_diffs > pd.Timedelta(days=10)]
if len(large_gaps) > 0:
issues.append(f"Large date gaps found: {len(large_gaps)} gaps > 10 days")
# Check for suspicious volume patterns
if 'Volume' in df.columns:
zero_volume = (df['Volume'] == 0).sum()
if zero_volume > len(df) * 0.1: # More than 10% zero volume
issues.append(f"Excessive zero volume days: {zero_volume} ({zero_volume/len(df)*100:.1f}%)")
# Check for data anomalies
if len(df) > 20:
for col in price_cols:
if col in df.columns:
returns = df[col].pct_change().dropna()
extreme_moves = (returns.abs() > 0.2).sum() # 20% moves
if extreme_moves > 0:
issues.append(f"Extreme price moves in {col}: {extreme_moves} days with >20% change")
return len(issues) == 0, issues
@staticmethod
def clean_data(df: pd.DataFrame,
method: str = 'forward_fill',
remove_outliers: bool = True,
outlier_threshold: float = 10.0) -> pd.DataFrame:
"""
Enhanced data cleaning with outlier detection.
Args:
df: DataFrame to clean
method: Cleaning method ('forward_fill', 'interpolate', 'drop')
remove_outliers: Whether to remove outliers
outlier_threshold: Z-score threshold for outlier detection
Returns:
Cleaned DataFrame
"""
df_clean = df.copy()
# Handle missing values
if method == 'forward_fill':
df_clean = df_clean.fillna(method='ffill').fillna(method='bfill')
elif method == 'interpolate':
df_clean = df_clean.interpolate(method='linear', limit_direction='both')
elif method == 'drop':
df_clean = df_clean.dropna()
# Ensure price consistency
if all(col in df_clean.columns for col in ['High', 'Low', 'Open', 'Close']):
# Ensure High is highest
df_clean['High'] = df_clean[['High', 'Open', 'Close']].max(axis=1)
# Ensure Low is lowest
df_clean['Low'] = df_clean[['Low', 'Open', 'Close']].min(axis=1)
# Remove outliers
if remove_outliers and len(df_clean) > 100:
price_cols = ['Open', 'High', 'Low', 'Close']
for col in price_cols:
if col in df_clean.columns:
# Calculate returns
returns = df_clean[col].pct_change()
# Calculate z-scores
z_scores = np.abs(stats.zscore(returns.dropna()))
# Identify outliers
outlier_mask = z_scores > outlier_threshold
if outlier_mask.any():
# Replace outliers with interpolated values
df_clean.loc[returns.index[1:][outlier_mask], col] = np.nan
df_clean[col] = df_clean[col].interpolate(method='linear')
# Ensure no negative prices
price_cols = ['Open', 'High', 'Low', 'Close']
for col in price_cols:
if col in df_clean.columns:
df_clean[col] = df_clean[col].clip(lower=0.01)
return df_clean
@staticmethod
def validate_features(df: pd.DataFrame,
feature_columns: List[str]) -> Tuple[bool, List[str]]:
"""Validate feature data quality."""
issues = []
for col in feature_columns:
if col not in df.columns:
issues.append(f"Missing feature column: {col}")
continue
# Check for NaN values
nan_count = df[col].isna().sum()
if nan_count > 0:
issues.append(f"NaN values in {col}: {nan_count}")
# Check for infinite values
inf_count = np.isinf(df[col]).sum()
if inf_count > 0:
issues.append(f"Infinite values in {col}: {inf_count}")
# Check for constant features
if df[col].std() == 0:
issues.append(f"Constant feature: {col}")
return len(issues) == 0, issues
class ModelCheckpointer:
"""Enhanced model checkpointing with versioning and metadata."""
def __init__(self, checkpoint_dir: str = './checkpoints'):
"""
Initialize checkpointer.
Args:
checkpoint_dir: Directory to store checkpoints
"""
self.checkpoint_dir = Path(checkpoint_dir)
self.checkpoint_dir.mkdir(exist_ok=True)
# Create subdirectories
(self.checkpoint_dir / 'models').mkdir(exist_ok=True)
(self.checkpoint_dir / 'metadata').mkdir(exist_ok=True)
(self.checkpoint_dir / 'configs').mkdir(exist_ok=True)
def save_checkpoint(self,
model: Any,
metadata: Dict[str, Any],
checkpoint_name: Optional[str] = None,
save_optimizer: bool = True) -> str:
"""
Save enhanced model checkpoint with comprehensive metadata.
Args:
model: Model to save
metadata: Metadata about the model
checkpoint_name: Optional checkpoint name
save_optimizer: Whether to save optimizer state
Returns:
Path to saved checkpoint
"""
if checkpoint_name is None:
# Generate unique checkpoint name
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
model_hash = hashlib.md5(str(metadata).encode()).hexdigest()[:8]
checkpoint_name = f"checkpoint_{timestamp}_{model_hash}"
checkpoint_path = self.checkpoint_dir / checkpoint_name
checkpoint_path.mkdir(exist_ok=True)
# Save model
model_path = checkpoint_path / 'model.pkl'
if hasattr(model, 'save'):
model.save(str(model_path))
else:
with open(model_path, 'wb') as f:
pickle.dump(model, f, protocol=pickle.HIGHEST_PROTOCOL)
# Save optimizer state if applicable
if save_optimizer and hasattr(model, 'optimizer'):
optimizer_path = checkpoint_path / 'optimizer.pkl'
torch.save(model.optimizer.state_dict(), optimizer_path)
# Enhanced metadata
metadata['checkpoint_info'] = {
'checkpoint_time': datetime.now().isoformat(),
'checkpoint_name': checkpoint_name,
'model_size_mb': os.path.getsize(model_path) / (1024 * 1024),
'python_version': sys.version,
'pytorch_version': torch.__version__ if 'torch' in sys.modules else None,
'system_info': {
'platform': os.name,
'cpu_count': psutil.cpu_count(),
'memory_gb': psutil.virtual_memory().total / (1024**3)
}
}
# Save metadata
metadata_path = checkpoint_path / 'metadata.json'
with open(metadata_path, 'w') as f:
json.dump(metadata, f, indent=2, default=str)
# Save config if provided
if 'config' in metadata:
config_path = checkpoint_path / 'config.yaml'
with open(config_path, 'w') as f:
yaml.dump(metadata['config'], f, default_flow_style=False)
# Create summary file
self._create_checkpoint_summary(checkpoint_path, metadata)
return str(checkpoint_path)
def _create_checkpoint_summary(self, checkpoint_path: Path, metadata: Dict[str, Any]) -> None:
"""Create human-readable summary of checkpoint."""
summary_path = checkpoint_path / 'README.md'
summary = f"""# Checkpoint Summary
## Basic Information
- **Created**: {metadata['checkpoint_info']['checkpoint_time']}
- **Name**: {metadata['checkpoint_info']['checkpoint_name']}
- **Model Size**: {metadata['checkpoint_info']['model_size_mb']:.2f} MB
## Performance Metrics
"""
# Add key metrics if available
metrics_to_show = ['sharpe_ratio', 'total_return', 'max_drawdown', 'win_rate']
for metric in metrics_to_show:
if metric in metadata.get('metrics', {}):
summary += f"- **{metric.replace('_', ' ').title()}**: {metadata['metrics'][metric]:.4f}\n"
summary += f"""
## Training Information
- **Total Epochs**: {metadata.get('epoch', 'N/A')}
- **Training Time**: {metadata.get('training_time_hours', 'N/A'):.2f} hours
## Configuration
See `config.yaml` for full configuration details.
## Usage
```python
from utils import ModelCheckpointer
checkpointer = ModelCheckpointer()
model, metadata = checkpointer.load_checkpoint('{checkpoint_path}')
```
"""
with open(summary_path, 'w') as f:
f.write(summary)
def load_checkpoint(self, checkpoint_path: str) -> Tuple[Any, Dict[str, Any]]:
"""
Load model checkpoint with validation.
Args:
checkpoint_path: Path to checkpoint
Returns:
Tuple of (model, metadata)
"""
checkpoint_path = Path(checkpoint_path)
if not checkpoint_path.exists():
raise FileNotFoundError(f"Checkpoint not found: {checkpoint_path}")
# Load metadata
metadata_path = checkpoint_path / 'metadata.json'
with open(metadata_path, 'r') as f:
metadata = json.load(f)
# Load model
model_path = checkpoint_path / 'model.pkl'
try:
# Try loading with pickle first
with open(model_path, 'rb') as f:
model = pickle.load(f)
except:
# Try other loading methods based on metadata
if 'model_type' in metadata:
if metadata['model_type'] == 'stable_baselines3':
from stable_baselines3 import PPO
model = PPO.load(str(model_path))
else:
raise ValueError(f"Unknown model type: {metadata['model_type']}")
else:
raise
return model, metadata
def list_checkpoints(self,
sort_by: str = 'created',
filter_func: Optional[Callable] = None) -> List[Dict[str, Any]]:
"""
List all available checkpoints with filtering.
Args:
sort_by: Field to sort by ('created', 'sharpe_ratio', 'return')
filter_func: Optional function to filter checkpoints
Returns:
List of checkpoint information
"""
checkpoints = []
for checkpoint_dir in self.checkpoint_dir.iterdir():
if checkpoint_dir.is_dir() and checkpoint_dir.name.startswith('checkpoint_'):
metadata_path = checkpoint_dir / 'metadata.json'
if metadata_path.exists():
with open(metadata_path, 'r') as f:
metadata = json.load(f)
checkpoint_info = {
'name': checkpoint_dir.name,
'path': str(checkpoint_dir),
'created': metadata.get('checkpoint_info', {}).get('checkpoint_time', 'Unknown'),
'metrics': metadata.get('metrics', {}),
'size_mb': metadata.get('checkpoint_info', {}).get('model_size_mb', 0)
}
# Apply filter if provided
if filter_func is None or filter_func(checkpoint_info):
checkpoints.append(checkpoint_info)
# Sort checkpoints
if sort_by == 'created':
checkpoints.sort(key=lambda x: x['created'], reverse=True)
elif sort_by in ['sharpe_ratio', 'total_return']:
checkpoints.sort(key=lambda x: x['metrics'].get(sort_by, -np.inf), reverse=True)
return checkpoints
def cleanup_old_checkpoints(self, keep_n: int = 10, keep_best: int = 3) -> None:
"""
Clean up old checkpoints, keeping the most recent and best performing.
Args:
keep_n: Number of most recent checkpoints to keep
keep_best: Number of best performing checkpoints to keep
"""
checkpoints = self.list_checkpoints()
if len(checkpoints) <= keep_n + keep_best:
return # Nothing to clean up
# Get checkpoints to keep
recent_checkpoints = checkpoints[:keep_n]
best_checkpoints = sorted(
checkpoints,
key=lambda x: x['metrics'].get('sharpe_ratio', -np.inf),
reverse=True
)[:keep_best]
# Combine and get unique paths
keep_paths = set()
for ckpt in recent_checkpoints + best_checkpoints:
keep_paths.add(ckpt['path'])
# Remove others
for ckpt in checkpoints:
if ckpt['path'] not in keep_paths:
import shutil
shutil.rmtree(ckpt['path'])
logger.info(f"Removed old checkpoint: {ckpt['name']}")
# Market Analysis Functions
def calculate_market_regime(prices: pd.Series,
volatility_window: int = 20,
trend_window: int = 50) -> pd.Series:
"""
Calculate market regime based on volatility and trend.
Args:
prices: Series of prices
volatility_window: Window for volatility calculation
trend_window: Window for trend calculation
Returns:
Series with market regime labels
"""
# Calculate returns and volatility
returns = prices.pct_change()
volatility = returns.rolling(volatility_window).std() * np.sqrt(252)
# Calculate trend using linear regression slope
def calculate_trend(window):
if len(window) < 2:
return 0
x = np.arange(len(window))
slope, _ = np.polyfit(x, window.values, 1)
return slope / window.mean() # Normalize by mean
trend = prices.rolling(trend_window).apply(calculate_trend)
# Define regime thresholds
vol_median = volatility.median()
# Classify regimes
regimes = pd.Series(index=prices.index, dtype='object')
# Bull: Low volatility, positive trend
bull_mask = (volatility < vol_median) & (trend > 0.001)
regimes[bull_mask] = 'BULL'
# Bear: High volatility, negative trend
bear_mask = (volatility > vol_median) & (trend < -0.001)
regimes[bear_mask] = 'BEAR'
# Volatile: High volatility, sideways trend
volatile_mask = (volatility > vol_median) & (trend.abs() <= 0.001)
regimes[volatile_mask] = 'VOLATILE'
# Quiet: Low volatility, sideways trend
quiet_mask = (volatility < vol_median) & (trend.abs() <= 0.001)
regimes[quiet_mask] = 'QUIET'
# Fill NaN values
regimes = regimes.fillna('UNKNOWN')