ivolatility-backtesting 1.9.0__py3-none-any.whl → 1.10.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ivolatility-backtesting might be problematic. Click here for more details.

@@ -0,0 +1,4175 @@
1
+ # ============================================================
2
+ # ivolatility_backtesting.py - ENHANCED VERSION
3
+ #
4
+ # NEW FEATURES:
5
+ # 1. Combined stop-loss (requires BOTH conditions)
6
+ # 2. Parameter optimization framework
7
+ # 3. Optimization results visualization
8
+ # ============================================================
9
+
10
+ import pandas as pd
11
+ import numpy as np
12
+ import matplotlib.pyplot as plt
13
+ import seaborn as sns
14
+ from datetime import datetime, timedelta
15
+ import ivolatility as ivol
16
+ import os
17
+ import time
18
+ import psutil
19
+ import warnings
20
+ from itertools import product
21
+ import sys
22
+ from typing import Dict, List, Optional, Tuple, Union, Any
23
+ warnings.filterwarnings('ignore', category=pd.errors.SettingWithCopyWarning)
24
+ warnings.filterwarnings('ignore', message='.*SettingWithCopyWarning.*')
25
+ warnings.filterwarnings('ignore', category=FutureWarning)
26
+ warnings.filterwarnings('ignore', category=DeprecationWarning)
27
+
28
+ sns.set_style('darkgrid')
29
+ plt.rcParams['figure.figsize'] = (15, 8)
30
+
31
+ def create_optimization_folder(base_dir='optimization_results'):
32
+ """
33
+ Create timestamped folder for optimization run
34
+ Returns: folder path (e.g., 'optimization_results/20250122_143025')
35
+ """
36
+ from pathlib import Path
37
+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
38
+ folder_path = Path(base_dir) / timestamp
39
+ folder_path.mkdir(parents=True, exist_ok=True)
40
+ print(f"\n📁 Created optimization folder: {folder_path}")
41
+ return str(folder_path)
42
+
43
+ # ============================================================
44
+ # RESOURCE MONITOR
45
+ # ============================================================
46
+ class ResourceMonitor:
47
+ """Monitor CPU and RAM with container support"""
48
+
49
+ def __init__(self, show_container_total=False):
50
+ self.process = psutil.Process()
51
+ self.cpu_count = psutil.cpu_count()
52
+ self.last_cpu_time = None
53
+ self.last_check_time = None
54
+ self.use_cgroups = self._check_cgroups_v2()
55
+ self.show_container_total = show_container_total
56
+ self.cpu_history = []
57
+ self.cpu_history_max = 5
58
+
59
+ if self.use_cgroups:
60
+ quota = self._read_cpu_quota()
61
+ if quota and quota > 0:
62
+ self.cpu_count = quota
63
+
64
+ self.context = "Container" if self.use_cgroups else "Host"
65
+
66
+ def _read_cpu_quota(self):
67
+ try:
68
+ with open('/sys/fs/cgroup/cpu.max', 'r') as f:
69
+ line = f.read().strip()
70
+ if line == 'max':
71
+ return None
72
+ parts = line.split()
73
+ if len(parts) == 2:
74
+ quota = int(parts[0])
75
+ period = int(parts[1])
76
+ return quota / period
77
+ except:
78
+ pass
79
+ return None
80
+
81
+ def get_context_info(self):
82
+ if self.use_cgroups:
83
+ current, max_mem = self._read_cgroup_memory()
84
+ ram_info = ""
85
+ if max_mem:
86
+ max_mem_gb = max_mem / (1024**3)
87
+ ram_info = f", {max_mem_gb:.1f}GB limit"
88
+
89
+ mem_type = "container total" if self.show_container_total else "process only"
90
+ return f"Container (CPU: {self.cpu_count:.1f} cores{ram_info}) - RAM: {mem_type}"
91
+ else:
92
+ total_ram_gb = psutil.virtual_memory().total / (1024**3)
93
+ return f"Host ({self.cpu_count} cores, {total_ram_gb:.0f}GB RAM) - RAM: process"
94
+
95
+ def _check_cgroups_v2(self):
96
+ try:
97
+ return os.path.exists('/sys/fs/cgroup/cpu.stat') and \
98
+ os.path.exists('/sys/fs/cgroup/memory.current')
99
+ except:
100
+ return False
101
+
102
+ def _read_cgroup_cpu(self):
103
+ try:
104
+ with open('/sys/fs/cgroup/cpu.stat', 'r') as f:
105
+ for line in f:
106
+ if line.startswith('usage_usec'):
107
+ return int(line.split()[1])
108
+ except:
109
+ pass
110
+ return None
111
+
112
+ def _read_cgroup_memory(self):
113
+ try:
114
+ with open('/sys/fs/cgroup/memory.current', 'r') as f:
115
+ current = int(f.read().strip())
116
+ with open('/sys/fs/cgroup/memory.max', 'r') as f:
117
+ max_mem = f.read().strip()
118
+ if max_mem == 'max':
119
+ max_mem = psutil.virtual_memory().total
120
+ else:
121
+ max_mem = int(max_mem)
122
+ return current, max_mem
123
+ except:
124
+ pass
125
+ return None, None
126
+
127
+ def get_cpu_percent(self):
128
+ if self.use_cgroups:
129
+ current_time = time.time()
130
+ current_cpu = self._read_cgroup_cpu()
131
+
132
+ if current_cpu and self.last_cpu_time and self.last_check_time:
133
+ time_delta = current_time - self.last_check_time
134
+ cpu_delta = current_cpu - self.last_cpu_time
135
+
136
+ if time_delta > 0:
137
+ cpu_percent = (cpu_delta / (time_delta * 1_000_000)) * 100
138
+ cpu_percent = min(cpu_percent, 100 * self.cpu_count)
139
+
140
+ self.cpu_history.append(cpu_percent)
141
+ if len(self.cpu_history) > self.cpu_history_max:
142
+ self.cpu_history.pop(0)
143
+
144
+ self.last_cpu_time = current_cpu
145
+ self.last_check_time = current_time
146
+
147
+ return round(sum(self.cpu_history) / len(self.cpu_history), 1)
148
+
149
+ self.last_cpu_time = current_cpu
150
+ self.last_check_time = current_time
151
+
152
+ try:
153
+ cpu = self.process.cpu_percent(interval=0.1)
154
+ if cpu == 0:
155
+ cpu = psutil.cpu_percent(interval=0.1)
156
+
157
+ self.cpu_history.append(cpu)
158
+ if len(self.cpu_history) > self.cpu_history_max:
159
+ self.cpu_history.pop(0)
160
+
161
+ return round(sum(self.cpu_history) / len(self.cpu_history), 1)
162
+ except:
163
+ return 0.0
164
+
165
+ def get_memory_info(self):
166
+ try:
167
+ mem = self.process.memory_info()
168
+ process_mb = mem.rss / (1024 * 1024)
169
+
170
+ if self.use_cgroups:
171
+ current, max_mem = self._read_cgroup_memory()
172
+ if max_mem:
173
+ process_percent = (mem.rss / max_mem) * 100
174
+
175
+ if current:
176
+ container_mb = current / (1024 * 1024)
177
+ container_percent = (current / max_mem) * 100
178
+ return (
179
+ round(process_mb, 1),
180
+ round(process_percent, 1),
181
+ round(container_mb, 1),
182
+ round(container_percent, 1)
183
+ )
184
+
185
+ return (
186
+ round(process_mb, 1),
187
+ round(process_percent, 1),
188
+ round(process_mb, 1),
189
+ round(process_percent, 1)
190
+ )
191
+
192
+ total = psutil.virtual_memory().total
193
+ percent = (mem.rss / total) * 100
194
+
195
+ return (
196
+ round(process_mb, 1),
197
+ round(percent, 1),
198
+ round(process_mb, 1),
199
+ round(percent, 1)
200
+ )
201
+
202
+ except:
203
+ return 0.0, 0.0, 0.0, 0.0
204
+
205
+
206
+ def create_progress_bar(reuse_existing=None):
207
+ """Create or reuse enhanced progress bar"""
208
+ if reuse_existing is not None:
209
+ progress_bar, status_label, monitor, start_time = reuse_existing
210
+ progress_bar.value = 0
211
+ progress_bar.bar_style = 'info'
212
+ status_label.value = "<b style='color:#0066cc'>Starting...</b>"
213
+ return progress_bar, status_label, monitor, time.time()
214
+
215
+ try:
216
+ from IPython.display import display
217
+ import ipywidgets as widgets
218
+
219
+ progress_bar = widgets.FloatProgress(
220
+ value=0, min=0, max=100,
221
+ description='Progress:',
222
+ bar_style='info',
223
+ style={'bar_color': '#00ff00'},
224
+ layout=widgets.Layout(width='100%', height='30px')
225
+ )
226
+
227
+ status_label = widgets.HTML(
228
+ value="<b style='color:#0066cc'>Starting...</b>"
229
+ )
230
+
231
+ display(widgets.VBox([progress_bar, status_label]))
232
+
233
+ monitor = ResourceMonitor()
234
+ start_time = time.time()
235
+
236
+ return progress_bar, status_label, monitor, start_time
237
+ except ImportError:
238
+ print("Warning: ipywidgets not available. Progress bar disabled.")
239
+ return None, None, ResourceMonitor(), time.time()
240
+
241
+
242
+ def update_progress(progress_bar, status_label, monitor, current, total, start_time, message="Processing"):
243
+ """Update progress bar with ETA, CPU%, RAM"""
244
+ if progress_bar is None or status_label is None:
245
+ return
246
+
247
+ progress = (current / total) * 100
248
+ progress_bar.value = progress
249
+
250
+ elapsed = time.time() - start_time
251
+ if current > 0:
252
+ eta_seconds = (elapsed / current) * (total - current)
253
+ eta_str = format_time(eta_seconds)
254
+ else:
255
+ eta_str = "calculating..."
256
+
257
+ cpu = monitor.get_cpu_percent()
258
+ process_mb, process_pct, container_mb, container_pct = monitor.get_memory_info()
259
+
260
+ if abs(container_mb - process_mb) > 10:
261
+ ram_display = (
262
+ f"RAM: <span style='color:#4CAF50'>{process_mb}MB ({process_pct}%)</span> Python | "
263
+ f"<span style='color:#2196F3'>{container_mb}MB ({container_pct}%)</span> Container"
264
+ )
265
+ else:
266
+ ram_display = f"RAM: {process_mb}MB ({process_pct}%)"
267
+
268
+ context_info = monitor.get_context_info()
269
+
270
+ elapsed_str = format_time(elapsed)
271
+ start_time_str = datetime.fromtimestamp(start_time).strftime('%H:%M:%S')
272
+
273
+ status_label.value = (
274
+ f"<b style='color:#0066cc'>{message} ({current}/{total})</b><br>"
275
+ f"<span style='color:#666'>⏱️ Elapsed: {elapsed_str} | ETA: {eta_str} | Started: {start_time_str}</span><br>"
276
+ f"<span style='color:#666'>CPU: {cpu}% | {ram_display}</span><br>"
277
+ f"<span style='color:#999;font-size:10px'>{context_info}</span>"
278
+ )
279
+
280
+
281
+ def format_time(seconds):
282
+ """Format seconds to human readable time"""
283
+ if seconds < 60:
284
+ return f"{int(seconds)}s"
285
+ elif seconds < 3600:
286
+ return f"{int(seconds // 60)}m {int(seconds % 60)}s"
287
+ else:
288
+ hours = int(seconds // 3600)
289
+ minutes = int((seconds % 3600) // 60)
290
+ return f"{hours}h {minutes}m"
291
+
292
+
293
+ # ============================================================
294
+ # API HELPER
295
+ # ============================================================
296
+ class APIHelper:
297
+ """Normalizes API responses"""
298
+
299
+ @staticmethod
300
+ def normalize_response(response, debug=False):
301
+ if response is None:
302
+ if debug:
303
+ print("[APIHelper] Response is None")
304
+ return None
305
+
306
+ if isinstance(response, dict):
307
+ if 'data' in response:
308
+ if debug:
309
+ print(f"[APIHelper] Dict response: {len(response['data'])} records")
310
+ return response
311
+ else:
312
+ if debug:
313
+ print("[APIHelper] Dict without 'data' key")
314
+ return None
315
+
316
+ if isinstance(response, pd.DataFrame):
317
+ if response.empty:
318
+ if debug:
319
+ print("[APIHelper] Empty DataFrame")
320
+ return None
321
+
322
+ records = response.to_dict('records')
323
+ if debug:
324
+ print(f"[APIHelper] DataFrame converted: {len(records)} records")
325
+ return {'data': records, 'status': 'success'}
326
+
327
+ if debug:
328
+ print(f"[APIHelper] Unexpected type: {type(response)}")
329
+ return None
330
+
331
+
332
+ class APIManager:
333
+ """Centralized API key management"""
334
+ _api_key = None
335
+ _methods = {}
336
+
337
+ @classmethod
338
+ def initialize(cls, api_key):
339
+ if not api_key:
340
+ raise ValueError("API key cannot be empty")
341
+ cls._api_key = api_key
342
+ ivol.setLoginParams(apiKey=api_key)
343
+ print(f"[API] Initialized: {api_key[:10]}...{api_key[-5:]}")
344
+
345
+ @classmethod
346
+ def get_method(cls, endpoint):
347
+ if cls._api_key is None:
348
+ api_key = os.getenv("API_KEY")
349
+ if not api_key:
350
+ raise ValueError("API key not set. Call init_api(key) first")
351
+ cls.initialize(api_key)
352
+
353
+ if endpoint not in cls._methods:
354
+ ivol.setLoginParams(apiKey=cls._api_key)
355
+ cls._methods[endpoint] = ivol.setMethod(endpoint)
356
+
357
+ return cls._methods[endpoint]
358
+
359
+
360
+ def init_api(api_key=None):
361
+ """Initialize IVolatility API"""
362
+ if api_key is None:
363
+ api_key = os.getenv("API_KEY")
364
+ APIManager.initialize(api_key)
365
+
366
+
367
+ def api_call(endpoint, cache_config=None, debug=False, **kwargs):
368
+ """
369
+ Make API call with automatic response normalization and caching
370
+
371
+ Args:
372
+ endpoint: API endpoint path
373
+ cache_config: Cache configuration dict (optional, enables caching if provided)
374
+ debug: Debug mode flag
375
+ **kwargs: API parameters
376
+
377
+ Returns:
378
+ Normalized API response or None
379
+ """
380
+ try:
381
+ # Check if caching is enabled
382
+ use_cache = cache_config is not None and (
383
+ cache_config.get('disk_enabled', False) or
384
+ cache_config.get('memory_enabled', False)
385
+ )
386
+
387
+ cache_manager = None
388
+ cache_key = None
389
+ data_type = None
390
+
391
+ if use_cache:
392
+ # Initialize cache manager
393
+ cache_manager = UniversalCacheManager(cache_config)
394
+
395
+ # Create cache key from endpoint and params (human-readable)
396
+ # Determine data type based on endpoint (supports EOD + INTRADAY for both STOCK + OPTIONS)
397
+ is_intraday = 'intraday' in endpoint
398
+ is_options = 'options' in endpoint
399
+ is_stock = 'stock' in endpoint
400
+
401
+ if is_intraday and is_options:
402
+ # Intraday options data: /equities/intraday/options-rawiv
403
+ data_type = 'options_intraday'
404
+ symbol = kwargs.get('symbol', 'UNKNOWN')
405
+ date = kwargs.get('date', 'UNKNOWN')
406
+ cache_key = f"{symbol}_{date}"
407
+ elif is_intraday and is_stock:
408
+ # Intraday stock data: /equities/intraday/stock-prices
409
+ data_type = 'stock_intraday'
410
+ symbol = kwargs.get('symbol', 'UNKNOWN')
411
+ date = kwargs.get('date', 'UNKNOWN')
412
+ cache_key = f"{symbol}_{date}"
413
+ elif is_options:
414
+ # EOD options data: /equities/eod/options-rawiv
415
+ data_type = 'options_eod'
416
+ symbol = kwargs.get('symbol', 'UNKNOWN')
417
+ from_date = kwargs.get('from_', kwargs.get('date', 'UNKNOWN'))
418
+ to_date = kwargs.get('to', from_date)
419
+ if from_date != to_date:
420
+ cache_key = f"{symbol}_{from_date}_{to_date}"
421
+ else:
422
+ cache_key = f"{symbol}_{from_date}"
423
+ elif is_stock:
424
+ # EOD stock data: /equities/eod/stock-prices
425
+ data_type = 'stock_eod'
426
+ symbol = kwargs.get('symbol', 'UNKNOWN')
427
+ from_date = kwargs.get('from_', kwargs.get('date', 'UNKNOWN'))
428
+ to_date = kwargs.get('to', from_date)
429
+ if from_date != to_date:
430
+ cache_key = f"{symbol}_{from_date}_{to_date}"
431
+ else:
432
+ cache_key = f"{symbol}_{from_date}"
433
+ else:
434
+ # Fallback for other endpoints
435
+ sorted_params = sorted([(k, v) for k, v in kwargs.items()])
436
+ param_hash = abs(hash(str(sorted_params)))
437
+ cache_key = f"{endpoint.replace('/', '_')}_{param_hash}"
438
+ data_type = 'default'
439
+
440
+ # Try to get from cache
441
+ cached_data = cache_manager.get(cache_key, data_type)
442
+ if cached_data is not None:
443
+ if debug or cache_config.get('debug', False):
444
+ print(f"[CACHE] ✓ Cache hit: {endpoint} ({len(cached_data) if hasattr(cached_data, '__len__') else '?'} records)")
445
+ # Return in same format as API (dict with 'data' key)
446
+ if isinstance(cached_data, pd.DataFrame):
447
+ return {'data': cached_data.to_dict('records'), 'status': 'success'}
448
+ return cached_data
449
+
450
+ # Cache miss or caching disabled - make API call
451
+ if debug and APIManager._api_key:
452
+ base_url = "https://restapi.ivolatility.com"
453
+ url_params = {}
454
+ for key, value in kwargs.items():
455
+ clean_key = key.rstrip('_') if key.endswith('_') else key
456
+ url_params[clean_key] = value
457
+
458
+ params_str = "&".join([f"{k}={v}" for k, v in url_params.items()])
459
+ full_url = f"{base_url}{endpoint}?apiKey={APIManager._api_key}&{params_str}"
460
+ print(f"\n[API] Full URL:")
461
+ print(f"[API] {full_url}\n")
462
+
463
+ method = APIManager.get_method(endpoint)
464
+ response = method(**kwargs)
465
+
466
+ normalized = APIHelper.normalize_response(response, debug=debug)
467
+
468
+ if normalized is None and debug:
469
+ print(f"[api_call] Failed to get data")
470
+ print(f"[api_call] Endpoint: {endpoint}")
471
+ print(f"[api_call] Params: {kwargs}")
472
+
473
+ # Save to cache if enabled and data is valid
474
+ if use_cache and normalized is not None and cache_manager is not None:
475
+ # Convert dict response to DataFrame for caching
476
+ if isinstance(normalized, dict) and 'data' in normalized:
477
+ try:
478
+ cache_data = pd.DataFrame(normalized['data'])
479
+ if len(cache_data) > 0: # Only cache non-empty data
480
+ cache_manager.set(cache_key, cache_data, data_type)
481
+ if debug or cache_config.get('debug', False):
482
+ print(f"[CACHE] 💾 Saved to cache: {endpoint} ({len(cache_data)} records)")
483
+ else:
484
+ if debug or cache_config.get('debug', False):
485
+ print(f"[CACHE] ⚠️ Skipped caching empty data: {endpoint}")
486
+ except Exception as e:
487
+ if debug or cache_config.get('debug', False):
488
+ print(f"[CACHE] ❌ Error converting to cache format: {e}")
489
+
490
+ return normalized
491
+
492
+ except Exception as e:
493
+ if debug:
494
+ print(f"[api_call] Exception: {e}")
495
+ print(f"[api_call] Endpoint: {endpoint}")
496
+ print(f"[api_call] Params: {kwargs}")
497
+ return None
498
+
499
+
500
+ # ============================================================
501
+ # BACKTEST RESULTS
502
+ # ============================================================
503
+ class BacktestResults:
504
+ """Universal container for backtest results"""
505
+
506
+ def __init__(self, equity_curve, equity_dates, trades, initial_capital,
507
+ config, benchmark_prices=None, benchmark_symbol='SPY',
508
+ daily_returns=None, debug_info=None):
509
+
510
+ self.equity_curve = equity_curve
511
+ self.equity_dates = equity_dates
512
+ self.trades = trades
513
+ self.initial_capital = initial_capital
514
+ self.final_capital = equity_curve[-1] if len(equity_curve) > 0 else initial_capital
515
+ self.config = config
516
+ self.benchmark_prices = benchmark_prices
517
+ self.benchmark_symbol = benchmark_symbol
518
+ self.debug_info = debug_info if debug_info else []
519
+
520
+ if daily_returns is None and len(equity_curve) > 1:
521
+ self.daily_returns = [
522
+ (equity_curve[i] - equity_curve[i-1]) / equity_curve[i-1]
523
+ for i in range(1, len(equity_curve))
524
+ ]
525
+ else:
526
+ self.daily_returns = daily_returns if daily_returns else []
527
+
528
+ self.max_drawdown = self._calculate_max_drawdown()
529
+
530
+ def _calculate_max_drawdown(self):
531
+ if len(self.equity_curve) < 2:
532
+ return 0
533
+ running_max = np.maximum.accumulate(self.equity_curve)
534
+ drawdowns = (np.array(self.equity_curve) - running_max) / running_max * 100
535
+ return abs(np.min(drawdowns))
536
+
537
+
538
+ # ============================================================
539
+ # STOP-LOSS MANAGER (ENHANCED VERSION WITH COMBINED STOP)
540
+ # ============================================================
541
+ class StopLossManager:
542
+ """
543
+ Enhanced stop-loss manager with COMBINED STOP support
544
+
545
+ NEW STOP TYPE:
546
+ - combined: Requires BOTH pl_loss AND directional conditions (from code 2)
547
+ """
548
+
549
+ def __init__(self):
550
+ self.positions = {}
551
+
552
+ def add_position(self, position_id, entry_price, entry_date, stop_type='fixed_pct',
553
+ stop_value=0.05, atr=None, trailing_distance=None, use_pnl_pct=False,
554
+ is_short_bias=False, **kwargs):
555
+ """
556
+ Add position with stop-loss
557
+
558
+ NEW for combined stop:
559
+ stop_type='combined'
560
+ stop_value={'pl_loss': 0.05, 'directional': 0.03}
561
+ """
562
+ self.positions[position_id] = {
563
+ 'entry_price': entry_price,
564
+ 'entry_date': entry_date,
565
+ 'stop_type': stop_type,
566
+ 'stop_value': stop_value,
567
+ 'atr': atr,
568
+ 'trailing_distance': trailing_distance,
569
+ 'highest_price': entry_price if not use_pnl_pct else 0,
570
+ 'lowest_price': entry_price if not use_pnl_pct else 0,
571
+ 'max_profit': 0,
572
+ 'use_pnl_pct': use_pnl_pct,
573
+ 'is_short_bias': is_short_bias,
574
+ **kwargs # Store additional parameters for combined stop
575
+ }
576
+
577
+ def check_stop(self, position_id, current_price, current_date, position_type='LONG', **kwargs):
578
+ """
579
+ Check if stop-loss triggered
580
+
581
+ NEW: Supports 'combined' stop type
582
+ """
583
+ if position_id not in self.positions:
584
+ return False, None, None
585
+
586
+ pos = self.positions[position_id]
587
+ stop_type = pos['stop_type']
588
+ use_pnl_pct = pos.get('use_pnl_pct', False)
589
+
590
+ # Update tracking
591
+ if use_pnl_pct:
592
+ pnl_pct = current_price
593
+ pos['highest_price'] = max(pos['highest_price'], pnl_pct)
594
+ pos['lowest_price'] = min(pos['lowest_price'], pnl_pct)
595
+ pos['max_profit'] = max(pos['max_profit'], pnl_pct)
596
+ else:
597
+ if position_type == 'LONG':
598
+ pos['highest_price'] = max(pos['highest_price'], current_price)
599
+ current_profit = current_price - pos['entry_price']
600
+ else:
601
+ pos['lowest_price'] = min(pos['lowest_price'], current_price)
602
+ current_profit = pos['entry_price'] - current_price
603
+
604
+ pos['max_profit'] = max(pos['max_profit'], current_profit)
605
+
606
+ # Route to appropriate check method
607
+ if stop_type == 'fixed_pct':
608
+ if use_pnl_pct:
609
+ return self._check_fixed_pct_stop_pnl(pos, current_price)
610
+ else:
611
+ return self._check_fixed_pct_stop(pos, current_price, position_type)
612
+
613
+ elif stop_type == 'trailing':
614
+ if use_pnl_pct:
615
+ return self._check_trailing_stop_pnl(pos, current_price)
616
+ else:
617
+ return self._check_trailing_stop(pos, current_price, position_type)
618
+
619
+ elif stop_type == 'time_based':
620
+ return self._check_time_stop(pos, current_date)
621
+
622
+ elif stop_type == 'volatility':
623
+ return self._check_volatility_stop(pos, current_price, position_type)
624
+
625
+ elif stop_type == 'pl_loss':
626
+ return self._check_pl_loss_stop(pos, kwargs)
627
+
628
+ elif stop_type == 'directional':
629
+ return self._check_directional_stop(pos, kwargs)
630
+
631
+ # NEW: COMBINED STOP (requires BOTH conditions)
632
+ elif stop_type == 'combined':
633
+ return self._check_combined_stop(pos, kwargs)
634
+
635
+ else:
636
+ return False, None, None
637
+
638
+ # ========================================================
639
+ # EXISTING STOP METHODS (unchanged)
640
+ # ========================================================
641
+
642
+ def _check_fixed_pct_stop(self, pos, current_price, position_type):
643
+ """Fixed percentage stop-loss (price-based)"""
644
+ entry = pos['entry_price']
645
+ stop_pct = pos['stop_value']
646
+
647
+ if position_type == 'LONG':
648
+ stop_level = entry * (1 - stop_pct)
649
+ triggered = current_price <= stop_level
650
+ else:
651
+ stop_level = entry * (1 + stop_pct)
652
+ triggered = current_price >= stop_level
653
+
654
+ return triggered, stop_level, 'fixed_pct'
655
+
656
+ def _check_fixed_pct_stop_pnl(self, pos, pnl_pct):
657
+ """Fixed percentage stop-loss (P&L%-based for options)"""
658
+ stop_pct = pos['stop_value']
659
+ stop_level = -stop_pct * 100
660
+
661
+ triggered = pnl_pct <= stop_level
662
+
663
+ return triggered, stop_level, 'fixed_pct'
664
+
665
+ def _check_trailing_stop(self, pos, current_price, position_type):
666
+ """Trailing stop-loss (price-based)"""
667
+ if pos['trailing_distance'] is None:
668
+ pos['trailing_distance'] = pos['stop_value']
669
+
670
+ distance = pos['trailing_distance']
671
+
672
+ if position_type == 'LONG':
673
+ stop_level = pos['highest_price'] * (1 - distance)
674
+ triggered = current_price <= stop_level
675
+ else:
676
+ stop_level = pos['lowest_price'] * (1 + distance)
677
+ triggered = current_price >= stop_level
678
+
679
+ return triggered, stop_level, 'trailing'
680
+
681
+ def _check_trailing_stop_pnl(self, pos, pnl_pct):
682
+ """Trailing stop-loss (P&L%-based for options)"""
683
+ if pos['trailing_distance'] is None:
684
+ pos['trailing_distance'] = pos['stop_value']
685
+
686
+ distance = pos['trailing_distance'] * 100
687
+
688
+ stop_level = pos['highest_price'] - distance
689
+
690
+ triggered = pnl_pct <= stop_level
691
+
692
+ return triggered, stop_level, 'trailing'
693
+
694
+ def _check_time_stop(self, pos, current_date):
695
+ """Time-based stop"""
696
+ days_held = (current_date - pos['entry_date']).days
697
+ max_days = pos['stop_value']
698
+
699
+ triggered = days_held >= max_days
700
+ return triggered, None, 'time_based'
701
+
702
+ def _check_volatility_stop(self, pos, current_price, position_type):
703
+ """ATR-based stop"""
704
+ if pos['atr'] is None:
705
+ return False, None, None
706
+
707
+ entry = pos['entry_price']
708
+ atr_multiplier = pos['stop_value']
709
+ stop_distance = pos['atr'] * atr_multiplier
710
+
711
+ if position_type == 'LONG':
712
+ stop_level = entry - stop_distance
713
+ triggered = current_price <= stop_level
714
+ else:
715
+ stop_level = entry + stop_distance
716
+ triggered = current_price >= stop_level
717
+
718
+ return triggered, stop_level, 'volatility'
719
+
720
+ def _check_pl_loss_stop(self, pos, kwargs):
721
+ """Stop-loss based on actual P&L"""
722
+ pnl_pct = kwargs.get('pnl_pct')
723
+
724
+ if pnl_pct is None:
725
+ current_pnl = kwargs.get('current_pnl', 0)
726
+ total_cost = kwargs.get('total_cost', pos.get('total_cost', 1))
727
+
728
+ if total_cost > 0:
729
+ pnl_pct = (current_pnl / total_cost) * 100
730
+ else:
731
+ pnl_pct = 0
732
+
733
+ stop_threshold = -pos['stop_value'] * 100
734
+ triggered = pnl_pct <= stop_threshold
735
+
736
+ return triggered, stop_threshold, 'pl_loss'
737
+
738
+ def _check_directional_stop(self, pos, kwargs):
739
+ """Stop-loss based on underlying price movement"""
740
+ underlying_change_pct = kwargs.get('underlying_change_pct')
741
+
742
+ if underlying_change_pct is None:
743
+ current = kwargs.get('underlying_price')
744
+ entry = kwargs.get('underlying_entry_price', pos.get('underlying_entry_price'))
745
+
746
+ if current is not None and entry is not None and entry != 0:
747
+ underlying_change_pct = ((current - entry) / entry) * 100
748
+ else:
749
+ underlying_change_pct = 0
750
+
751
+ threshold = pos['stop_value'] * 100
752
+ is_short_bias = pos.get('is_short_bias', False)
753
+
754
+ if is_short_bias:
755
+ triggered = underlying_change_pct >= threshold
756
+ else:
757
+ triggered = underlying_change_pct <= -threshold
758
+
759
+ return triggered, threshold, 'directional'
760
+
761
+ # ========================================================
762
+ # NEW: COMBINED STOP (REQUIRES BOTH CONDITIONS)
763
+ # ========================================================
764
+
765
+ def _check_combined_stop(self, pos, kwargs):
766
+ """
767
+ Combined stop: Requires BOTH pl_loss AND directional conditions
768
+
769
+ This is the key feature from code 2:
770
+ - Must have P&L loss > threshold
771
+ - AND underlying must move adversely > threshold
772
+
773
+ Args:
774
+ pos: Position dict with stop_value = {'pl_loss': 0.05, 'directional': 0.03}
775
+ kwargs: Must contain pnl_pct and underlying_change_pct
776
+
777
+ Returns:
778
+ tuple: (triggered, thresholds_dict, 'combined')
779
+ """
780
+ stop_config = pos['stop_value']
781
+
782
+ if not isinstance(stop_config, dict):
783
+ # Fallback: treat as simple fixed stop
784
+ return False, None, 'combined'
785
+
786
+ pl_threshold = stop_config.get('pl_loss', 0.05)
787
+ dir_threshold = stop_config.get('directional', 0.03)
788
+
789
+ # Check P&L condition
790
+ pnl_pct = kwargs.get('pnl_pct', 0)
791
+ is_losing = pnl_pct <= (-pl_threshold * 100)
792
+
793
+ # Check directional condition
794
+ underlying_change_pct = kwargs.get('underlying_change_pct')
795
+
796
+ if underlying_change_pct is None:
797
+ current = kwargs.get('underlying_price')
798
+ entry = kwargs.get('underlying_entry_price', pos.get('underlying_entry_price'))
799
+
800
+ if current is not None and entry is not None and entry != 0:
801
+ underlying_change_pct = ((current - entry) / entry) * 100
802
+ else:
803
+ underlying_change_pct = 0
804
+
805
+ is_short_bias = pos.get('is_short_bias', False)
806
+
807
+ if is_short_bias:
808
+ # Bearish position: adverse move is UP
809
+ adverse_move = underlying_change_pct >= (dir_threshold * 100)
810
+ else:
811
+ # Bullish position: adverse move is DOWN
812
+ adverse_move = underlying_change_pct <= (-dir_threshold * 100)
813
+
814
+ # CRITICAL: Both conditions must be true
815
+ triggered = is_losing and adverse_move
816
+
817
+ # Return detailed thresholds for reporting
818
+ thresholds = {
819
+ 'pl_threshold': -pl_threshold * 100,
820
+ 'dir_threshold': dir_threshold * 100,
821
+ 'actual_pnl_pct': pnl_pct,
822
+ 'actual_underlying_change': underlying_change_pct,
823
+ 'pl_condition': is_losing,
824
+ 'dir_condition': adverse_move
825
+ }
826
+
827
+ return triggered, thresholds, 'combined'
828
+
829
+ # ========================================================
830
+ # UTILITY METHODS
831
+ # ========================================================
832
+
833
+ def remove_position(self, position_id):
834
+ """Remove position from tracking"""
835
+ if position_id in self.positions:
836
+ del self.positions[position_id]
837
+
838
+ def get_position_info(self, position_id):
839
+ """Get position stop-loss info"""
840
+ if position_id not in self.positions:
841
+ return None
842
+
843
+ pos = self.positions[position_id]
844
+ return {
845
+ 'stop_type': pos['stop_type'],
846
+ 'stop_value': pos['stop_value'],
847
+ 'max_profit_before_stop': pos['max_profit']
848
+ }
849
+
850
+
851
+ # ============================================================
852
+ # POSITION MANAGER (unchanged but compatible with combined stop)
853
+ # ============================================================
854
+ class PositionManager:
855
+ """Universal Position Manager with automatic mode detection"""
856
+
857
+ def __init__(self, config, debug=False):
858
+ self.positions = {}
859
+ self.closed_trades = []
860
+ self.config = config
861
+ self.debug = debug
862
+
863
+ # Stop-loss enable logic:
864
+ # 1) Respect explicit flag if provided
865
+ # 2) Otherwise infer from stop_loss_config.enabled for convenience
866
+ explicit_flag = config.get('stop_loss_enabled')
867
+ sl_cfg = config.get('stop_loss_config', {})
868
+ inferred_flag = bool(sl_cfg.get('enabled', False))
869
+
870
+ self.sl_enabled = explicit_flag if explicit_flag is not None else inferred_flag
871
+
872
+ if self.sl_enabled:
873
+ self.sl_config = sl_cfg
874
+ self.sl_manager = StopLossManager()
875
+ else:
876
+ self.sl_config = None
877
+ self.sl_manager = None
878
+
879
+ def open_position(self, position_id, symbol, entry_date, entry_price,
880
+ quantity, position_type='LONG', **kwargs):
881
+ """Open position with automatic stop-loss"""
882
+
883
+ if entry_price == 0 and self.sl_enabled:
884
+ if 'total_cost' not in kwargs or kwargs['total_cost'] == 0:
885
+ raise ValueError(
886
+ f"\n{'='*70}\n"
887
+ f"ERROR: P&L% mode requires 'total_cost' parameter\n"
888
+ f"{'='*70}\n"
889
+ )
890
+
891
+ position = {
892
+ 'id': position_id,
893
+ 'symbol': symbol,
894
+ 'entry_date': entry_date,
895
+ 'entry_price': entry_price,
896
+ 'quantity': quantity,
897
+ 'type': position_type,
898
+ 'highest_price': entry_price,
899
+ 'lowest_price': entry_price,
900
+ **kwargs
901
+ }
902
+
903
+ self.positions[position_id] = position
904
+
905
+ if self.sl_enabled and self.sl_manager:
906
+ sl_type = self.sl_config.get('type', 'fixed_pct')
907
+ sl_value = self.sl_config.get('value', 0.05)
908
+
909
+ use_pnl_pct = (entry_price == 0)
910
+ is_short_bias = kwargs.get('is_short_bias', False)
911
+
912
+ # Pass underlying_entry_price for combined stop
913
+ self.sl_manager.add_position(
914
+ position_id=position_id,
915
+ entry_price=entry_price,
916
+ entry_date=entry_date,
917
+ stop_type=sl_type,
918
+ stop_value=sl_value,
919
+ atr=kwargs.get('atr', None),
920
+ trailing_distance=self.sl_config.get('trailing_distance', None),
921
+ use_pnl_pct=use_pnl_pct,
922
+ is_short_bias=is_short_bias,
923
+ underlying_entry_price=kwargs.get('entry_stock_price') # For combined stop
924
+ )
925
+
926
+ if self.debug:
927
+ mode = "P&L%" if entry_price == 0 else "Price"
928
+ bias = " (SHORT BIAS)" if kwargs.get('is_short_bias') else ""
929
+ print(f"[PositionManager] OPEN {position_id}: {symbol} @ {entry_price} (Mode: {mode}{bias})")
930
+
931
+ return position
932
+
933
+ def check_positions(self, current_date, price_data):
934
+ """Check all positions for stop-loss triggers"""
935
+ if not self.sl_enabled:
936
+ return []
937
+
938
+ to_close = []
939
+
940
+ for position_id, position in self.positions.items():
941
+ if position_id not in price_data:
942
+ continue
943
+
944
+ if isinstance(price_data[position_id], dict):
945
+ data = price_data[position_id]
946
+ current_price = data.get('price', position['entry_price'])
947
+ current_pnl = data.get('pnl', 0)
948
+ current_pnl_pct = data.get('pnl_pct', 0)
949
+
950
+ # NEW: Pass underlying data for combined stop
951
+ underlying_price = data.get('underlying_price')
952
+ underlying_entry_price = data.get('underlying_entry_price')
953
+ underlying_change_pct = data.get('underlying_change_pct')
954
+ else:
955
+ current_price = price_data[position_id]
956
+ current_pnl = (current_price - position['entry_price']) * position['quantity']
957
+ current_pnl_pct = (current_price - position['entry_price']) / position['entry_price'] if position['entry_price'] != 0 else 0
958
+ underlying_price = None
959
+ underlying_entry_price = None
960
+ underlying_change_pct = None
961
+
962
+ position['highest_price'] = max(position['highest_price'], current_price)
963
+ position['lowest_price'] = min(position['lowest_price'], current_price)
964
+
965
+ if position['entry_price'] == 0:
966
+ check_value = current_pnl_pct
967
+ else:
968
+ check_value = current_price
969
+
970
+ # Pass all data to stop manager
971
+ stop_kwargs = {
972
+ 'pnl_pct': current_pnl_pct,
973
+ 'current_pnl': current_pnl,
974
+ 'total_cost': position.get('total_cost', 1),
975
+ 'underlying_price': underlying_price,
976
+ 'underlying_entry_price': underlying_entry_price or position.get('entry_stock_price'),
977
+ 'underlying_change_pct': underlying_change_pct
978
+ }
979
+
980
+ triggered, stop_level, stop_type = self.sl_manager.check_stop(
981
+ position_id=position_id,
982
+ current_price=check_value,
983
+ current_date=current_date,
984
+ position_type=position['type'],
985
+ **stop_kwargs
986
+ )
987
+
988
+ if triggered:
989
+ to_close.append({
990
+ 'position_id': position_id,
991
+ 'symbol': position['symbol'],
992
+ 'stop_type': stop_type,
993
+ 'stop_level': stop_level,
994
+ 'current_price': current_price,
995
+ 'pnl': current_pnl,
996
+ 'pnl_pct': current_pnl_pct
997
+ })
998
+
999
+ if self.debug:
1000
+ mode = "P&L%" if position['entry_price'] == 0 else "Price"
1001
+ print(f"[PositionManager] STOP-LOSS: {position_id} ({stop_type}, {mode}) @ {check_value:.2f}")
1002
+
1003
+ return to_close
1004
+
1005
+ def close_position(self, position_id, exit_date, exit_price,
1006
+ close_reason='manual', pnl=None, **kwargs):
1007
+ """Close position"""
1008
+ if position_id not in self.positions:
1009
+ if self.debug:
1010
+ print(f"[PositionManager] WARNING: Position {position_id} not found")
1011
+ return None
1012
+
1013
+ position = self.positions.pop(position_id)
1014
+
1015
+ if pnl is None:
1016
+ pnl = (exit_price - position['entry_price']) * position['quantity']
1017
+
1018
+ if position['entry_price'] != 0:
1019
+ pnl_pct = (exit_price - position['entry_price']) / position['entry_price'] * 100
1020
+ else:
1021
+ if 'total_cost' in position and position['total_cost'] != 0:
1022
+ pnl_pct = (pnl / position['total_cost']) * 100
1023
+ elif 'total_cost' in kwargs and kwargs['total_cost'] != 0:
1024
+ pnl_pct = (pnl / kwargs['total_cost']) * 100
1025
+ else:
1026
+ pnl_pct = 0.0
1027
+
1028
+ trade = {
1029
+ 'entry_date': position['entry_date'],
1030
+ 'exit_date': exit_date,
1031
+ 'symbol': position['symbol'],
1032
+ 'signal': position['type'],
1033
+ 'entry_price': position['entry_price'],
1034
+ 'exit_price': exit_price,
1035
+ 'quantity': position['quantity'],
1036
+ 'pnl': pnl,
1037
+ 'return_pct': pnl_pct,
1038
+ 'exit_reason': close_reason,
1039
+ 'stop_type': self.sl_config.get('type', 'none') if self.sl_enabled else 'none',
1040
+ **kwargs
1041
+ }
1042
+
1043
+ for key in ['call_strike', 'put_strike', 'expiration', 'contracts',
1044
+ 'short_strike', 'long_strike', 'opt_type', 'spread_type',
1045
+ 'entry_z_score', 'is_short_bias', 'entry_lean', 'exit_lean',
1046
+ 'call_iv_entry', 'put_iv_entry', 'iv_lean_entry']:
1047
+ if key in position:
1048
+ trade[key] = position[key]
1049
+
1050
+ for key in ['short_entry_bid', 'short_entry_ask', 'short_entry_mid',
1051
+ 'long_entry_bid', 'long_entry_ask', 'long_entry_mid',
1052
+ 'underlying_entry_price']:
1053
+ if key in position:
1054
+ trade[key] = position[key]
1055
+
1056
+ for key in ['short_exit_bid', 'short_exit_ask',
1057
+ 'long_exit_bid', 'long_exit_ask',
1058
+ 'underlying_exit_price', 'underlying_change_pct',
1059
+ 'stop_threshold', 'actual_value',
1060
+ 'call_iv_exit', 'put_iv_exit', 'iv_lean_exit',
1061
+ 'spy_intraday_high', 'spy_intraday_low', 'spy_intraday_close',
1062
+ 'spy_stop_trigger_time', 'spy_stop_trigger_price',
1063
+ 'spy_stop_trigger_bid', 'spy_stop_trigger_ask', 'spy_stop_trigger_last',
1064
+ 'intraday_data_points', 'intraday_data_available', 'stop_triggered_by']:
1065
+ if key in kwargs:
1066
+ trade[key] = kwargs[key]
1067
+
1068
+ self.closed_trades.append(trade)
1069
+
1070
+ if self.sl_enabled and self.sl_manager:
1071
+ self.sl_manager.remove_position(position_id)
1072
+
1073
+ if self.debug:
1074
+ print(f"[PositionManager] CLOSE {position_id}: P&L=${pnl:.2f} ({pnl_pct:.2f}%) - {close_reason}")
1075
+
1076
+ return trade
1077
+
1078
+ def get_open_positions(self):
1079
+ return list(self.positions.values())
1080
+
1081
+ def get_closed_trades(self):
1082
+ return self.closed_trades
1083
+
1084
+ def close_all_positions(self, final_date, price_data, reason='end_of_backtest'):
1085
+ """Close all open positions at end of backtest"""
1086
+ for position_id in list(self.positions.keys()):
1087
+ if position_id in price_data:
1088
+ position = self.positions[position_id]
1089
+
1090
+ if isinstance(price_data[position_id], dict):
1091
+ data = price_data[position_id]
1092
+ exit_price = data.get('price', position['entry_price'])
1093
+ pnl = data.get('pnl', None)
1094
+ else:
1095
+ exit_price = price_data[position_id]
1096
+ pnl = None
1097
+
1098
+ if pnl is None and position['entry_price'] == 0:
1099
+ if isinstance(price_data[position_id], dict) and 'pnl' in price_data[position_id]:
1100
+ pnl = price_data[position_id]['pnl']
1101
+
1102
+ self.close_position(
1103
+ position_id=position_id,
1104
+ exit_date=final_date,
1105
+ exit_price=exit_price,
1106
+ close_reason=reason,
1107
+ pnl=pnl
1108
+ )
1109
+
1110
+
1111
+ # ============================================================
1112
+ # BACKTEST ANALYZER (unchanged)
1113
+ # ============================================================
1114
+ class BacktestAnalyzer:
1115
+ """Calculate all metrics from BacktestResults"""
1116
+
1117
+ def __init__(self, results):
1118
+ self.results = results
1119
+ self.metrics = {}
1120
+
1121
+ def calculate_all_metrics(self):
1122
+ r = self.results
1123
+
1124
+ self.metrics['initial_capital'] = r.initial_capital
1125
+ self.metrics['final_equity'] = r.final_capital
1126
+
1127
+ self.metrics['total_pnl'] = r.final_capital - r.initial_capital
1128
+ self.metrics['total_return'] = (self.metrics['total_pnl'] / r.initial_capital) * 100
1129
+
1130
+ if len(r.equity_dates) > 0:
1131
+ start_date = min(r.equity_dates)
1132
+ end_date = max(r.equity_dates)
1133
+ days_diff = (end_date - start_date).days
1134
+
1135
+ if days_diff <= 0:
1136
+ self.metrics['cagr'] = 0
1137
+ self.metrics['show_cagr'] = False
1138
+ else:
1139
+ years = days_diff / 365.25
1140
+ if years >= 1.0:
1141
+ self.metrics['cagr'] = ((r.final_capital / r.initial_capital) ** (1/years) - 1) * 100
1142
+ self.metrics['show_cagr'] = True
1143
+ else:
1144
+ self.metrics['cagr'] = self.metrics['total_return'] * (365.25 / days_diff)
1145
+ self.metrics['show_cagr'] = False
1146
+ else:
1147
+ self.metrics['cagr'] = 0
1148
+ self.metrics['show_cagr'] = False
1149
+
1150
+ self.metrics['sharpe'] = self._sharpe_ratio(r.daily_returns)
1151
+ self.metrics['sortino'] = self._sortino_ratio(r.daily_returns)
1152
+ self.metrics['max_drawdown'] = r.max_drawdown
1153
+ self.metrics['volatility'] = np.std(r.daily_returns) * np.sqrt(252) * 100 if len(r.daily_returns) > 0 else 0
1154
+ self.metrics['calmar'] = abs(self.metrics['total_return'] / r.max_drawdown) if r.max_drawdown > 0 else 0
1155
+ self.metrics['omega'] = self._omega_ratio(r.daily_returns)
1156
+ self.metrics['ulcer'] = self._ulcer_index(r.equity_curve)
1157
+
1158
+ self.metrics['var_95'], self.metrics['var_95_pct'] = self._calculate_var(r.daily_returns, 0.95)
1159
+ self.metrics['var_99'], self.metrics['var_99_pct'] = self._calculate_var(r.daily_returns, 0.99)
1160
+ self.metrics['cvar_95'], self.metrics['cvar_95_pct'] = self._calculate_cvar(r.daily_returns, 0.95)
1161
+
1162
+ avg_equity = np.mean(r.equity_curve) if len(r.equity_curve) > 0 else r.initial_capital
1163
+ self.metrics['var_95_dollar'] = self.metrics['var_95'] * avg_equity
1164
+ self.metrics['var_99_dollar'] = self.metrics['var_99'] * avg_equity
1165
+ self.metrics['cvar_95_dollar'] = self.metrics['cvar_95'] * avg_equity
1166
+
1167
+ self.metrics['tail_ratio'] = self._tail_ratio(r.daily_returns)
1168
+ self.metrics['skewness'], self.metrics['kurtosis'] = self._skewness_kurtosis(r.daily_returns)
1169
+
1170
+ self.metrics['alpha'], self.metrics['beta'], self.metrics['r_squared'] = self._alpha_beta(r)
1171
+
1172
+ if len(r.trades) > 0:
1173
+ self._calculate_trading_stats(r.trades)
1174
+ else:
1175
+ self._set_empty_trading_stats()
1176
+
1177
+ running_max = np.maximum.accumulate(r.equity_curve)
1178
+ max_dd_dollars = np.min(np.array(r.equity_curve) - running_max)
1179
+ self.metrics['recovery_factor'] = self.metrics['total_pnl'] / abs(max_dd_dollars) if max_dd_dollars != 0 else 0
1180
+
1181
+ if len(r.trades) > 0 and 'start_date' in r.config and 'end_date' in r.config:
1182
+ total_days = (pd.to_datetime(r.config['end_date']) - pd.to_datetime(r.config['start_date'])).days
1183
+ self.metrics['exposure_time'] = self._exposure_time(r.trades, total_days)
1184
+ else:
1185
+ self.metrics['exposure_time'] = 0
1186
+
1187
+ return self.metrics
1188
+
1189
+ def _calculate_trading_stats(self, trades):
1190
+ trades_df = pd.DataFrame(trades)
1191
+ winning = trades_df[trades_df['pnl'] > 0]
1192
+ losing = trades_df[trades_df['pnl'] <= 0]
1193
+
1194
+ self.metrics['total_trades'] = len(trades_df)
1195
+ self.metrics['winning_trades'] = len(winning)
1196
+ self.metrics['losing_trades'] = len(losing)
1197
+ self.metrics['win_rate'] = (len(winning) / len(trades_df)) * 100 if len(trades_df) > 0 else 0
1198
+
1199
+ wins_sum = winning['pnl'].sum() if len(winning) > 0 else 0
1200
+ losses_sum = abs(losing['pnl'].sum()) if len(losing) > 0 else 0
1201
+ self.metrics['profit_factor'] = wins_sum / losses_sum if losses_sum > 0 else float('inf')
1202
+
1203
+ self.metrics['avg_win'] = winning['pnl'].mean() if len(winning) > 0 else 0
1204
+ self.metrics['avg_loss'] = losing['pnl'].mean() if len(losing) > 0 else 0
1205
+ self.metrics['best_trade'] = trades_df['pnl'].max()
1206
+ self.metrics['worst_trade'] = trades_df['pnl'].min()
1207
+
1208
+ if len(winning) > 0 and len(losing) > 0 and self.metrics['avg_loss'] != 0:
1209
+ self.metrics['avg_win_loss_ratio'] = abs(self.metrics['avg_win'] / self.metrics['avg_loss'])
1210
+ else:
1211
+ self.metrics['avg_win_loss_ratio'] = 0
1212
+
1213
+ self.metrics['max_win_streak'], self.metrics['max_loss_streak'] = self._win_loss_streaks(trades)
1214
+
1215
+ def _set_empty_trading_stats(self):
1216
+ self.metrics.update({
1217
+ 'total_trades': 0, 'winning_trades': 0, 'losing_trades': 0,
1218
+ 'win_rate': 0, 'profit_factor': 0, 'avg_win': 0, 'avg_loss': 0,
1219
+ 'best_trade': 0, 'worst_trade': 0, 'avg_win_loss_ratio': 0,
1220
+ 'max_win_streak': 0, 'max_loss_streak': 0
1221
+ })
1222
+
1223
+ def _sharpe_ratio(self, returns):
1224
+ if len(returns) < 2:
1225
+ return 0
1226
+ return np.sqrt(252) * np.mean(returns) / np.std(returns) if np.std(returns) > 0 else 0
1227
+
1228
+ def _sortino_ratio(self, returns):
1229
+ if len(returns) < 2:
1230
+ return 0
1231
+ returns_array = np.array(returns)
1232
+ downside = returns_array[returns_array < 0]
1233
+ if len(downside) == 0 or np.std(downside) == 0:
1234
+ return 0
1235
+ return np.sqrt(252) * np.mean(returns_array) / np.std(downside)
1236
+
1237
+ def _omega_ratio(self, returns, threshold=0):
1238
+ if len(returns) < 2:
1239
+ return 0
1240
+ returns_array = np.array(returns)
1241
+ gains = np.sum(np.maximum(returns_array - threshold, 0))
1242
+ losses = np.sum(np.maximum(threshold - returns_array, 0))
1243
+ return gains / losses if losses > 0 else float('inf')
1244
+
1245
+ def _ulcer_index(self, equity_curve):
1246
+ if len(equity_curve) < 2:
1247
+ return 0
1248
+ equity_array = np.array(equity_curve)
1249
+ running_max = np.maximum.accumulate(equity_array)
1250
+ drawdown = (equity_array - running_max) / running_max
1251
+ return np.sqrt(np.mean(drawdown ** 2)) * 100
1252
+
1253
+ def _calculate_var(self, returns, confidence=0.95):
1254
+ if len(returns) < 10:
1255
+ return 0, 0
1256
+ returns_array = np.array(returns)
1257
+ returns_array = returns_array[~np.isnan(returns_array)]
1258
+ if len(returns_array) < 10:
1259
+ return 0, 0
1260
+ var_percentile = (1 - confidence) * 100
1261
+ var_return = np.percentile(returns_array, var_percentile)
1262
+ return var_return, var_return * 100
1263
+
1264
+ def _calculate_cvar(self, returns, confidence=0.95):
1265
+ if len(returns) < 10:
1266
+ return 0, 0
1267
+ returns_array = np.array(returns)
1268
+ returns_array = returns_array[~np.isnan(returns_array)]
1269
+ if len(returns_array) < 10:
1270
+ return 0, 0
1271
+ var_percentile = (1 - confidence) * 100
1272
+ var_threshold = np.percentile(returns_array, var_percentile)
1273
+ tail_losses = returns_array[returns_array <= var_threshold]
1274
+ if len(tail_losses) == 0:
1275
+ return 0, 0
1276
+ cvar_return = np.mean(tail_losses)
1277
+ return cvar_return, cvar_return * 100
1278
+
1279
+ def _tail_ratio(self, returns):
1280
+ if len(returns) < 20:
1281
+ return 0
1282
+ returns_array = np.array(returns)
1283
+ right = np.percentile(returns_array, 95)
1284
+ left = abs(np.percentile(returns_array, 5))
1285
+ return right / left if left > 0 else 0
1286
+
1287
+ def _skewness_kurtosis(self, returns):
1288
+ if len(returns) < 10:
1289
+ return 0, 0
1290
+ returns_array = np.array(returns)
1291
+ mean = np.mean(returns_array)
1292
+ std = np.std(returns_array)
1293
+ if std == 0:
1294
+ return 0, 0
1295
+ skew = np.mean(((returns_array - mean) / std) ** 3)
1296
+ kurt = np.mean(((returns_array - mean) / std) ** 4) - 3
1297
+ return skew, kurt
1298
+
1299
+ def _alpha_beta(self, results):
1300
+ if not hasattr(results, 'benchmark_prices') or not results.benchmark_prices:
1301
+ return 0, 0, 0
1302
+ if len(results.equity_dates) < 10:
1303
+ return 0, 0, 0
1304
+
1305
+ benchmark_returns = []
1306
+ sorted_dates = sorted(results.equity_dates)
1307
+
1308
+ for i in range(1, len(sorted_dates)):
1309
+ prev_date = sorted_dates[i-1]
1310
+ curr_date = sorted_dates[i]
1311
+
1312
+ if prev_date in results.benchmark_prices and curr_date in results.benchmark_prices:
1313
+ prev_price = results.benchmark_prices[prev_date]
1314
+ curr_price = results.benchmark_prices[curr_date]
1315
+ bench_return = (curr_price - prev_price) / prev_price
1316
+ benchmark_returns.append(bench_return)
1317
+ else:
1318
+ benchmark_returns.append(0)
1319
+
1320
+ if len(benchmark_returns) != len(results.daily_returns):
1321
+ return 0, 0, 0
1322
+
1323
+ port_ret = np.array(results.daily_returns)
1324
+ bench_ret = np.array(benchmark_returns)
1325
+
1326
+ bench_mean = np.mean(bench_ret)
1327
+ port_mean = np.mean(port_ret)
1328
+
1329
+ covariance = np.mean((bench_ret - bench_mean) * (port_ret - port_mean))
1330
+ benchmark_variance = np.mean((bench_ret - bench_mean) ** 2)
1331
+
1332
+ if benchmark_variance == 0:
1333
+ return 0, 0, 0
1334
+
1335
+ beta = covariance / benchmark_variance
1336
+ alpha_daily = port_mean - beta * bench_mean
1337
+ alpha_annualized = alpha_daily * 252 * 100
1338
+
1339
+ ss_res = np.sum((port_ret - (alpha_daily + beta * bench_ret)) ** 2)
1340
+ ss_tot = np.sum((port_ret - port_mean) ** 2)
1341
+ r_squared = 1 - (ss_res / ss_tot) if ss_tot > 0 else 0
1342
+
1343
+ return alpha_annualized, beta, r_squared
1344
+
1345
+ def _win_loss_streaks(self, trades):
1346
+ if len(trades) == 0:
1347
+ return 0, 0
1348
+ max_win = max_loss = current_win = current_loss = 0
1349
+ for trade in trades:
1350
+ if trade['pnl'] > 0:
1351
+ current_win += 1
1352
+ current_loss = 0
1353
+ max_win = max(max_win, current_win)
1354
+ else:
1355
+ current_loss += 1
1356
+ current_win = 0
1357
+ max_loss = max(max_loss, current_loss)
1358
+ return max_win, max_loss
1359
+
1360
+ def _exposure_time(self, trades, total_days):
1361
+ if total_days <= 0 or len(trades) == 0:
1362
+ return 0
1363
+ days_with_positions = set()
1364
+ for trade in trades:
1365
+ entry = pd.to_datetime(trade['entry_date'])
1366
+ exit_ = pd.to_datetime(trade['exit_date'])
1367
+ date_range = pd.date_range(start=entry, end=exit_, freq='D')
1368
+ days_with_positions.update(date_range.date)
1369
+ exposure_pct = (len(days_with_positions) / total_days) * 100
1370
+ return min(exposure_pct, 100.0)
1371
+
1372
+
1373
+ # ============================================================
1374
+ # STOP-LOSS METRICS (unchanged)
1375
+ # ============================================================
1376
+ def calculate_stoploss_metrics(analyzer):
1377
+ """Calculate stop-loss specific metrics"""
1378
+ if len(analyzer.results.trades) == 0:
1379
+ _set_empty_stoploss_metrics(analyzer)
1380
+ return analyzer.metrics
1381
+
1382
+ trades_df = pd.DataFrame(analyzer.results.trades)
1383
+
1384
+ if 'exit_reason' not in trades_df.columns:
1385
+ _set_empty_stoploss_metrics(analyzer)
1386
+ return analyzer.metrics
1387
+
1388
+ sl_trades = trades_df[trades_df['exit_reason'].str.contains('stop_loss', na=False)]
1389
+ profit_target_trades = trades_df[trades_df['exit_reason'] == 'profit_target']
1390
+
1391
+ analyzer.metrics['stoploss_count'] = len(sl_trades)
1392
+ analyzer.metrics['stoploss_pct'] = (len(sl_trades) / len(trades_df)) * 100 if len(trades_df) > 0 else 0
1393
+ analyzer.metrics['profit_target_count'] = len(profit_target_trades)
1394
+ analyzer.metrics['profit_target_pct'] = (len(profit_target_trades) / len(trades_df)) * 100 if len(trades_df) > 0 else 0
1395
+
1396
+ if len(sl_trades) > 0:
1397
+ analyzer.metrics['avg_stoploss_pnl'] = sl_trades['pnl'].mean()
1398
+ analyzer.metrics['total_stoploss_loss'] = sl_trades['pnl'].sum()
1399
+ analyzer.metrics['worst_stoploss'] = sl_trades['pnl'].min()
1400
+
1401
+ if 'return_pct' in sl_trades.columns:
1402
+ analyzer.metrics['avg_stoploss_return_pct'] = sl_trades['return_pct'].mean()
1403
+ else:
1404
+ analyzer.metrics['avg_stoploss_return_pct'] = 0
1405
+
1406
+ if 'entry_date' in sl_trades.columns and 'exit_date' in sl_trades.columns:
1407
+ sl_trades_copy = sl_trades.copy()
1408
+ sl_trades_copy['entry_date'] = pd.to_datetime(sl_trades_copy['entry_date'])
1409
+ sl_trades_copy['exit_date'] = pd.to_datetime(sl_trades_copy['exit_date'])
1410
+ sl_trades_copy['days_held'] = (sl_trades_copy['exit_date'] - sl_trades_copy['entry_date']).dt.days
1411
+ analyzer.metrics['avg_days_to_stoploss'] = sl_trades_copy['days_held'].mean()
1412
+ analyzer.metrics['min_days_to_stoploss'] = sl_trades_copy['days_held'].min()
1413
+ analyzer.metrics['max_days_to_stoploss'] = sl_trades_copy['days_held'].max()
1414
+ else:
1415
+ analyzer.metrics['avg_days_to_stoploss'] = 0
1416
+ analyzer.metrics['min_days_to_stoploss'] = 0
1417
+ analyzer.metrics['max_days_to_stoploss'] = 0
1418
+
1419
+ if 'stop_type' in sl_trades.columns:
1420
+ stop_types = sl_trades['stop_type'].value_counts().to_dict()
1421
+ analyzer.metrics['stoploss_by_type'] = stop_types
1422
+ else:
1423
+ analyzer.metrics['stoploss_by_type'] = {}
1424
+ else:
1425
+ analyzer.metrics['avg_stoploss_pnl'] = 0
1426
+ analyzer.metrics['total_stoploss_loss'] = 0
1427
+ analyzer.metrics['worst_stoploss'] = 0
1428
+ analyzer.metrics['avg_stoploss_return_pct'] = 0
1429
+ analyzer.metrics['avg_days_to_stoploss'] = 0
1430
+ analyzer.metrics['min_days_to_stoploss'] = 0
1431
+ analyzer.metrics['max_days_to_stoploss'] = 0
1432
+ analyzer.metrics['stoploss_by_type'] = {}
1433
+
1434
+ if len(profit_target_trades) > 0 and len(sl_trades) > 0:
1435
+ avg_profit_target = profit_target_trades['pnl'].mean()
1436
+ avg_stoploss = abs(sl_trades['pnl'].mean())
1437
+ analyzer.metrics['profit_to_loss_ratio'] = avg_profit_target / avg_stoploss if avg_stoploss > 0 else 0
1438
+ else:
1439
+ analyzer.metrics['profit_to_loss_ratio'] = 0
1440
+
1441
+ if 'max_profit_before_stop' in sl_trades.columns:
1442
+ early_exits = sl_trades[sl_trades['max_profit_before_stop'] > 0]
1443
+ analyzer.metrics['early_exit_count'] = len(early_exits)
1444
+ analyzer.metrics['early_exit_pct'] = (len(early_exits) / len(sl_trades)) * 100 if len(sl_trades) > 0 else 0
1445
+ if len(early_exits) > 0:
1446
+ analyzer.metrics['avg_missed_profit'] = early_exits['max_profit_before_stop'].mean()
1447
+ else:
1448
+ analyzer.metrics['avg_missed_profit'] = 0
1449
+ else:
1450
+ analyzer.metrics['early_exit_count'] = 0
1451
+ analyzer.metrics['early_exit_pct'] = 0
1452
+ analyzer.metrics['avg_missed_profit'] = 0
1453
+
1454
+ exit_reasons = trades_df['exit_reason'].value_counts().to_dict()
1455
+ analyzer.metrics['exit_reasons'] = exit_reasons
1456
+
1457
+ return analyzer.metrics
1458
+
1459
+
1460
+ def _set_empty_stoploss_metrics(analyzer):
1461
+ analyzer.metrics.update({
1462
+ 'stoploss_count': 0, 'stoploss_pct': 0,
1463
+ 'profit_target_count': 0, 'profit_target_pct': 0,
1464
+ 'avg_stoploss_pnl': 0, 'total_stoploss_loss': 0,
1465
+ 'worst_stoploss': 0, 'avg_stoploss_return_pct': 0,
1466
+ 'avg_days_to_stoploss': 0, 'min_days_to_stoploss': 0,
1467
+ 'max_days_to_stoploss': 0, 'stoploss_by_type': {},
1468
+ 'profit_to_loss_ratio': 0, 'early_exit_count': 0,
1469
+ 'early_exit_pct': 0, 'avg_missed_profit': 0,
1470
+ 'exit_reasons': {}
1471
+ })
1472
+
1473
+
1474
+ # ============================================================
1475
+ # RESULTS REPORTER (unchanged)
1476
+ # ============================================================
1477
+ class ResultsReporter:
1478
+ """Print comprehensive metrics report"""
1479
+
1480
+ @staticmethod
1481
+ def print_full_report(analyzer):
1482
+ m = analyzer.metrics
1483
+ r = analyzer.results
1484
+
1485
+ print("="*80)
1486
+ print(" "*25 + "BACKTEST RESULTS")
1487
+ print("="*80)
1488
+ print()
1489
+
1490
+ print("PROFITABILITY METRICS")
1491
+ print("-"*80)
1492
+ print(f"Initial Capital: ${r.initial_capital:>15,.2f}")
1493
+ print(f"Final Equity: ${r.final_capital:>15,.2f}")
1494
+ print(f"Total P&L: ${m['total_pnl']:>15,.2f} (absolute profit/loss)")
1495
+ print(f"Total Return: {m['total_return']:>15.2f}% (% gain/loss)")
1496
+ if m['cagr'] != 0:
1497
+ if m['show_cagr']:
1498
+ print(f"CAGR: {m['cagr']:>15.2f}% (annualized compound growth)")
1499
+ else:
1500
+ print(f"Annualized Return: {m['cagr']:>15.2f}% (extrapolated to 1 year)")
1501
+ print()
1502
+
1503
+ print("RISK METRICS")
1504
+ print("-"*80)
1505
+ print(f"Sharpe Ratio: {m['sharpe']:>15.2f} (>1 good, >2 excellent)")
1506
+ print(f"Sortino Ratio: {m['sortino']:>15.2f} (downside risk, >2 good)")
1507
+ print(f"Calmar Ratio: {m['calmar']:>15.2f} (return/drawdown, >3 good)")
1508
+ if m['omega'] != 0:
1509
+ omega_display = f"{m['omega']:.2f}" if m['omega'] < 999 else "∞"
1510
+ print(f"Omega Ratio: {omega_display:>15s} (gains/losses, >1 good)")
1511
+ print(f"Maximum Drawdown: {m['max_drawdown']:>15.2f}% (peak to trough)")
1512
+ if m['ulcer'] != 0:
1513
+ print(f"Ulcer Index: {m['ulcer']:>15.2f}% (pain of drawdowns, lower better)")
1514
+ print(f"Volatility (ann.): {m['volatility']:>15.2f}% (annualized std dev)")
1515
+
1516
+ if len(r.daily_returns) >= 10:
1517
+ print(f"VaR (95%, 1-day): {m['var_95_pct']:>15.2f}% (${m['var_95_dollar']:>,.0f}) (max loss 95% confidence)")
1518
+ print(f"VaR (99%, 1-day): {m['var_99_pct']:>15.2f}% (${m['var_99_dollar']:>,.0f}) (max loss 99% confidence)")
1519
+ print(f"CVaR (95%, 1-day): {m['cvar_95_pct']:>15.2f}% (${m['cvar_95_dollar']:>,.0f}) (avg loss in worst 5%)")
1520
+
1521
+ if m['tail_ratio'] != 0:
1522
+ print(f"Tail Ratio (95/5): {m['tail_ratio']:>15.2f} (big wins/losses, >1 good)")
1523
+
1524
+ if m['skewness'] != 0 or m['kurtosis'] != 0:
1525
+ print(f"Skewness: {m['skewness']:>15.2f} (>0 positive tail)")
1526
+ print(f"Kurtosis (excess): {m['kurtosis']:>15.2f} (>0 fat tails)")
1527
+
1528
+ if m['beta'] != 0 or m['alpha'] != 0:
1529
+ print(f"Alpha (vs {r.benchmark_symbol}): {m['alpha']:>15.2f}% (excess return)")
1530
+ print(f"Beta (vs {r.benchmark_symbol}): {m['beta']:>15.2f} (<1 defensive, >1 aggressive)")
1531
+ print(f"R² (vs {r.benchmark_symbol}): {m['r_squared']:>15.2f} (market correlation 0-1)")
1532
+
1533
+ if abs(m['total_return']) > 200 or m['volatility'] > 150:
1534
+ print()
1535
+ print("WARNING: UNREALISTIC RESULTS DETECTED")
1536
+ if abs(m['total_return']) > 200:
1537
+ print(f" Total return {m['total_return']:.1f}% is extremely high")
1538
+ if m['volatility'] > 150:
1539
+ print(f" Volatility {m['volatility']:.1f}% is higher than leveraged ETFs")
1540
+ print(" Review configuration before trusting results")
1541
+
1542
+ print()
1543
+
1544
+ print("EFFICIENCY METRICS")
1545
+ print("-"*80)
1546
+ if m['recovery_factor'] != 0:
1547
+ print(f"Recovery Factor: {m['recovery_factor']:>15.2f} (profit/max DD, >3 good)")
1548
+ if m['exposure_time'] != 0:
1549
+ print(f"Exposure Time: {m['exposure_time']:>15.1f}% (time in market)")
1550
+ print()
1551
+
1552
+ print("TRADING STATISTICS")
1553
+ print("-"*80)
1554
+ print(f"Total Trades: {m['total_trades']:>15}")
1555
+ print(f"Winning Trades: {m['winning_trades']:>15}")
1556
+ print(f"Losing Trades: {m['losing_trades']:>15}")
1557
+ print(f"Win Rate: {m['win_rate']:>15.2f}% (% profitable trades)")
1558
+ print(f"Profit Factor: {m['profit_factor']:>15.2f} (gross profit/loss, >1.5 good)")
1559
+ if m['max_win_streak'] > 0 or m['max_loss_streak'] > 0:
1560
+ print(f"Max Win Streak: {m['max_win_streak']:>15} (consecutive wins)")
1561
+ print(f"Max Loss Streak: {m['max_loss_streak']:>15} (consecutive losses)")
1562
+ print(f"Average Win: ${m['avg_win']:>15,.2f}")
1563
+ print(f"Average Loss: ${m['avg_loss']:>15,.2f}")
1564
+ print(f"Best Trade: ${m['best_trade']:>15,.2f}")
1565
+ print(f"Worst Trade: ${m['worst_trade']:>15,.2f}")
1566
+ if m['avg_win_loss_ratio'] != 0:
1567
+ print(f"Avg Win/Loss Ratio: {m['avg_win_loss_ratio']:>15.2f} (avg win / avg loss)")
1568
+ print()
1569
+ print("="*80)
1570
+
1571
+
1572
+ def print_stoploss_section(analyzer):
1573
+ """Print stop-loss analysis section"""
1574
+ m = analyzer.metrics
1575
+
1576
+ if m.get('stoploss_count', 0) == 0:
1577
+ return
1578
+
1579
+ print("STOP-LOSS ANALYSIS")
1580
+ print("-"*80)
1581
+
1582
+ print(f"Stop-Loss Trades: {m['stoploss_count']:>15} ({m['stoploss_pct']:.1f}% of total)")
1583
+ print(f"Profit Target Trades: {m['profit_target_count']:>15} ({m['profit_target_pct']:.1f}% of total)")
1584
+
1585
+ print(f"Avg Stop-Loss P&L: ${m['avg_stoploss_pnl']:>15,.2f}")
1586
+ print(f"Total Loss from SL: ${m['total_stoploss_loss']:>15,.2f}")
1587
+ print(f"Worst Stop-Loss: ${m['worst_stoploss']:>15,.2f}")
1588
+ print(f"Avg SL Return: {m['avg_stoploss_return_pct']:>15.2f}%")
1589
+
1590
+ if m['avg_days_to_stoploss'] > 0:
1591
+ print(f"Avg Days to SL: {m['avg_days_to_stoploss']:>15.1f}")
1592
+ print(f"Min/Max Days to SL: {m['min_days_to_stoploss']:>7} / {m['max_days_to_stoploss']:<7}")
1593
+
1594
+ if m['profit_to_loss_ratio'] > 0:
1595
+ print(f"Profit/Loss Ratio: {m['profit_to_loss_ratio']:>15.2f} (avg profit target / avg stop-loss)")
1596
+
1597
+ if m['early_exit_count'] > 0:
1598
+ print(f"Early Exits: {m['early_exit_count']:>15} ({m['early_exit_pct']:.1f}% of SL trades)")
1599
+ print(f"Avg Missed Profit: ${m['avg_missed_profit']:>15,.2f} (profit before stop triggered)")
1600
+
1601
+ if m['stoploss_by_type']:
1602
+ print(f"\nStop-Loss Types:")
1603
+ for stop_type, count in m['stoploss_by_type'].items():
1604
+ pct = (count / m['stoploss_count']) * 100
1605
+ print(f" {stop_type:20s} {count:>5} trades ({pct:.1f}%)")
1606
+
1607
+ if m.get('exit_reasons'):
1608
+ print(f"\nExit Reasons Distribution:")
1609
+ total_trades = sum(m['exit_reasons'].values())
1610
+ for reason, count in sorted(m['exit_reasons'].items(), key=lambda x: x[1], reverse=True):
1611
+ pct = (count / total_trades) * 100
1612
+ print(f" {reason:20s} {count:>5} trades ({pct:.1f}%)")
1613
+
1614
+ print()
1615
+ print("="*80)
1616
+
1617
+
1618
+ # ============================================================
1619
+ # CHART GENERATOR (only core charts, optimization charts separate)
1620
+ # ============================================================
1621
+ class ChartGenerator:
1622
+ """Generate 6 professional charts"""
1623
+
1624
+ @staticmethod
1625
+ def create_all_charts(analyzer, filename='backtest_results.png', show_plots=True):
1626
+ r = analyzer.results
1627
+
1628
+ if len(r.trades) == 0:
1629
+ print("No trades to visualize")
1630
+ return
1631
+
1632
+ trades_df = pd.DataFrame(r.trades)
1633
+ fig, axes = plt.subplots(3, 2, figsize=(18, 14))
1634
+ fig.suptitle('Backtest Results', fontsize=16, fontweight='bold', y=0.995)
1635
+
1636
+ dates = pd.to_datetime(r.equity_dates)
1637
+ equity_array = np.array(r.equity_curve)
1638
+
1639
+ ax1 = axes[0, 0]
1640
+ ax1.plot(dates, equity_array, linewidth=2.5, color='#2196F3')
1641
+ ax1.axhline(y=r.initial_capital, color='gray', linestyle='--', alpha=0.7)
1642
+ ax1.fill_between(dates, r.initial_capital, equity_array,
1643
+ where=(equity_array >= r.initial_capital),
1644
+ alpha=0.3, color='green', interpolate=True)
1645
+ ax1.fill_between(dates, r.initial_capital, equity_array,
1646
+ where=(equity_array < r.initial_capital),
1647
+ alpha=0.3, color='red', interpolate=True)
1648
+ ax1.set_title('Equity Curve', fontsize=12, fontweight='bold')
1649
+ ax1.set_ylabel('Equity ($)')
1650
+ ax1.grid(True, alpha=0.3)
1651
+
1652
+ ax2 = axes[0, 1]
1653
+ running_max = np.maximum.accumulate(equity_array)
1654
+ drawdown = (equity_array - running_max) / running_max * 100
1655
+ ax2.fill_between(dates, 0, drawdown, alpha=0.6, color='#f44336')
1656
+ ax2.plot(dates, drawdown, color='#d32f2f', linewidth=2)
1657
+ ax2.set_title('Drawdown', fontsize=12, fontweight='bold')
1658
+ ax2.set_ylabel('Drawdown (%)')
1659
+ ax2.grid(True, alpha=0.3)
1660
+
1661
+ ax3 = axes[1, 0]
1662
+ pnl_values = trades_df['pnl'].values
1663
+ ax3.hist(pnl_values, bins=40, color='#4CAF50', alpha=0.7, edgecolor='black')
1664
+ ax3.axvline(x=0, color='red', linestyle='--', linewidth=2)
1665
+ ax3.set_title('P&L Distribution', fontsize=12, fontweight='bold')
1666
+ ax3.set_xlabel('P&L ($)')
1667
+ ax3.grid(True, alpha=0.3, axis='y')
1668
+
1669
+ ax4 = axes[1, 1]
1670
+ if 'signal' in trades_df.columns:
1671
+ signal_pnl = trades_df.groupby('signal')['pnl'].sum()
1672
+ colors = ['#4CAF50' if x > 0 else '#f44336' for x in signal_pnl.values]
1673
+ ax4.bar(signal_pnl.index, signal_pnl.values, color=colors, alpha=0.7)
1674
+ ax4.set_title('P&L by Signal', fontsize=12, fontweight='bold')
1675
+ else:
1676
+ ax4.text(0.5, 0.5, 'No signal data', ha='center', va='center', transform=ax4.transAxes)
1677
+ ax4.axhline(y=0, color='black', linewidth=1)
1678
+ ax4.grid(True, alpha=0.3, axis='y')
1679
+
1680
+ ax5 = axes[2, 0]
1681
+ trades_df['exit_date'] = pd.to_datetime(trades_df['exit_date'])
1682
+ trades_df['month'] = trades_df['exit_date'].dt.to_period('M')
1683
+ monthly_pnl = trades_df.groupby('month')['pnl'].sum()
1684
+ colors = ['#4CAF50' if x > 0 else '#f44336' for x in monthly_pnl.values]
1685
+ ax5.bar(range(len(monthly_pnl)), monthly_pnl.values, color=colors, alpha=0.7)
1686
+ ax5.set_title('Monthly P&L', fontsize=12, fontweight='bold')
1687
+ ax5.set_xticks(range(len(monthly_pnl)))
1688
+ ax5.set_xticklabels([str(m) for m in monthly_pnl.index], rotation=45, ha='right')
1689
+ ax5.axhline(y=0, color='black', linewidth=1)
1690
+ ax5.grid(True, alpha=0.3, axis='y')
1691
+
1692
+ ax6 = axes[2, 1]
1693
+ if 'symbol' in trades_df.columns:
1694
+ symbol_pnl = trades_df.groupby('symbol')['pnl'].sum().sort_values(ascending=True).tail(10)
1695
+ colors = ['#4CAF50' if x > 0 else '#f44336' for x in symbol_pnl.values]
1696
+ ax6.barh(range(len(symbol_pnl)), symbol_pnl.values, color=colors, alpha=0.7)
1697
+ ax6.set_yticks(range(len(symbol_pnl)))
1698
+ ax6.set_yticklabels(symbol_pnl.index, fontsize=9)
1699
+ ax6.set_title('Top Symbols', fontsize=12, fontweight='bold')
1700
+ else:
1701
+ ax6.text(0.5, 0.5, 'No symbol data', ha='center', va='center', transform=ax6.transAxes)
1702
+ ax6.axvline(x=0, color='black', linewidth=1)
1703
+ ax6.grid(True, alpha=0.3, axis='x')
1704
+
1705
+ plt.tight_layout()
1706
+ plt.savefig(filename, dpi=300, bbox_inches='tight')
1707
+
1708
+ if show_plots:
1709
+ plt.show()
1710
+ else:
1711
+ plt.close() # Close without displaying
1712
+
1713
+ print(f"Chart saved: {filename}")
1714
+
1715
+
1716
+ def create_stoploss_charts(analyzer, filename='stoploss_analysis.png', show_plots=True):
1717
+ """Create 4 stop-loss specific charts"""
1718
+ r = analyzer.results
1719
+ m = analyzer.metrics
1720
+
1721
+ if m.get('stoploss_count', 0) == 0:
1722
+ print("No stop-loss trades to visualize")
1723
+ return
1724
+
1725
+ trades_df = pd.DataFrame(r.trades)
1726
+
1727
+ if 'exit_reason' not in trades_df.columns:
1728
+ print("No exit_reason data available")
1729
+ return
1730
+
1731
+ fig, axes = plt.subplots(2, 2, figsize=(16, 12))
1732
+ fig.suptitle('Stop-Loss Analysis', fontsize=16, fontweight='bold', y=0.995)
1733
+
1734
+ ax1 = axes[0, 0]
1735
+ if m.get('exit_reasons'):
1736
+ reasons = pd.Series(m['exit_reasons']).sort_values(ascending=True)
1737
+ colors = ['#f44336' if 'stop_loss' in str(r) else '#4CAF50' if r == 'profit_target' else '#2196F3'
1738
+ for r in reasons.index]
1739
+ ax1.barh(range(len(reasons)), reasons.values, color=colors, alpha=0.7, edgecolor='black')
1740
+ ax1.set_yticks(range(len(reasons)))
1741
+ ax1.set_yticklabels([r.replace('_', ' ').title() for r in reasons.index])
1742
+ ax1.set_title('Exit Reasons Distribution', fontsize=12, fontweight='bold')
1743
+ ax1.set_xlabel('Number of Trades')
1744
+ ax1.grid(True, alpha=0.3, axis='x')
1745
+
1746
+ total = sum(reasons.values)
1747
+ for i, v in enumerate(reasons.values):
1748
+ ax1.text(v, i, f' {(v/total)*100:.1f}%', va='center', fontweight='bold')
1749
+
1750
+ ax2 = axes[0, 1]
1751
+ sl_trades = trades_df[trades_df['exit_reason'].str.contains('stop_loss', na=False)]
1752
+ if len(sl_trades) > 0:
1753
+ ax2.hist(sl_trades['pnl'], bins=30, color='#f44336', alpha=0.7, edgecolor='black')
1754
+ ax2.axvline(x=0, color='black', linestyle='--', linewidth=2)
1755
+ ax2.axvline(x=sl_trades['pnl'].mean(), color='yellow', linestyle='--', linewidth=2, label='Mean')
1756
+ ax2.set_title('Stop-Loss P&L Distribution', fontsize=12, fontweight='bold')
1757
+ ax2.set_xlabel('P&L ($)')
1758
+ ax2.set_ylabel('Frequency')
1759
+ ax2.legend()
1760
+ ax2.grid(True, alpha=0.3, axis='y')
1761
+
1762
+ ax3 = axes[1, 0]
1763
+ if len(sl_trades) > 0 and 'entry_date' in sl_trades.columns and 'exit_date' in sl_trades.columns:
1764
+ sl_trades_copy = sl_trades.copy()
1765
+ sl_trades_copy['entry_date'] = pd.to_datetime(sl_trades_copy['entry_date'])
1766
+ sl_trades_copy['exit_date'] = pd.to_datetime(sl_trades_copy['exit_date'])
1767
+ sl_trades_copy['days_held'] = (sl_trades_copy['exit_date'] - sl_trades_copy['entry_date']).dt.days
1768
+
1769
+ ax3.hist(sl_trades_copy['days_held'], bins=30, color='#FF9800', alpha=0.7, edgecolor='black')
1770
+ ax3.axvline(x=sl_trades_copy['days_held'].mean(), color='red', linestyle='--', linewidth=2, label='Mean')
1771
+ ax3.set_title('Days Until Stop-Loss Triggered', fontsize=12, fontweight='bold')
1772
+ ax3.set_xlabel('Days Held')
1773
+ ax3.set_ylabel('Frequency')
1774
+ ax3.legend()
1775
+ ax3.grid(True, alpha=0.3, axis='y')
1776
+
1777
+ ax4 = axes[1, 1]
1778
+ if 'stop_type' in sl_trades.columns:
1779
+ stop_types = sl_trades['stop_type'].value_counts()
1780
+ colors_types = plt.cm.Set3(range(len(stop_types)))
1781
+ wedges, texts, autotexts = ax4.pie(stop_types.values, labels=stop_types.index,
1782
+ autopct='%1.1f%%', colors=colors_types,
1783
+ startangle=90)
1784
+ for autotext in autotexts:
1785
+ autotext.set_color('black')
1786
+ autotext.set_fontweight('bold')
1787
+ ax4.set_title('Stop-Loss Types', fontsize=12, fontweight='bold')
1788
+ else:
1789
+ ax4.text(0.5, 0.5, 'No stop_type data', ha='center', va='center', transform=ax4.transAxes)
1790
+
1791
+ plt.tight_layout()
1792
+ plt.savefig(filename, dpi=300, bbox_inches='tight')
1793
+
1794
+ if show_plots:
1795
+ plt.show()
1796
+ else:
1797
+ plt.close()
1798
+
1799
+ print(f"Stop-loss charts saved: {filename}")
1800
+
1801
+
1802
+ # ============================================================
1803
+ # RESULTS EXPORTER (unchanged)
1804
+ # ============================================================
1805
+ class ResultsExporter:
1806
+ """Export results to CSV"""
1807
+
1808
+ @staticmethod
1809
+ def export_all(analyzer, prefix='backtest'):
1810
+ r = analyzer.results
1811
+ m = analyzer.metrics
1812
+
1813
+ if len(r.trades) == 0:
1814
+ print("No trades to export")
1815
+ return
1816
+
1817
+ trades_df = pd.DataFrame(r.trades)
1818
+
1819
+ trades_df['entry_date'] = pd.to_datetime(trades_df['entry_date']).dt.strftime('%Y-%m-%d')
1820
+ trades_df['exit_date'] = pd.to_datetime(trades_df['exit_date']).dt.strftime('%Y-%m-%d')
1821
+
1822
+ # Round numeric columns to 5 decimal places
1823
+ numeric_columns = trades_df.select_dtypes(include=[np.number]).columns
1824
+ for col in numeric_columns:
1825
+ trades_df[col] = trades_df[col].round(5)
1826
+
1827
+ core_columns = [
1828
+ 'entry_date', 'exit_date', 'symbol', 'signal',
1829
+ 'pnl', 'return_pct', 'exit_reason', 'stop_type'
1830
+ ]
1831
+
1832
+ options_columns = [
1833
+ 'short_strike', 'long_strike', 'expiration', 'opt_type',
1834
+ 'spread_type', 'contracts'
1835
+ ]
1836
+
1837
+ bidask_columns = [
1838
+ 'short_entry_bid', 'short_entry_ask', 'short_entry_mid',
1839
+ 'long_entry_bid', 'long_entry_ask', 'long_entry_mid',
1840
+ 'short_exit_bid', 'short_exit_ask',
1841
+ 'long_exit_bid', 'long_exit_ask'
1842
+ ]
1843
+
1844
+ underlying_columns = [
1845
+ 'underlying_entry_price', 'underlying_exit_price',
1846
+ 'underlying_change_pct'
1847
+ ]
1848
+
1849
+ stop_columns = [
1850
+ 'stop_threshold', 'actual_value'
1851
+ ]
1852
+
1853
+ strategy_columns = [
1854
+ 'entry_z_score', 'is_short_bias', 'entry_price',
1855
+ 'exit_price', 'quantity', 'entry_lean', 'exit_lean',
1856
+ # IV EOD fields
1857
+ 'call_iv_entry', 'put_iv_entry', 'call_iv_exit', 'put_iv_exit',
1858
+ 'iv_lean_entry', 'iv_lean_exit'
1859
+ ]
1860
+
1861
+ # NEW: Intraday stop-loss columns
1862
+ intraday_columns = [
1863
+ 'spy_intraday_high', 'spy_intraday_low', 'spy_intraday_close',
1864
+ 'spy_stop_trigger_time', 'spy_stop_trigger_price',
1865
+ 'spy_stop_trigger_bid', 'spy_stop_trigger_ask', 'spy_stop_trigger_last',
1866
+ 'intraday_data_points', 'intraday_data_available', 'stop_triggered_by'
1867
+ ]
1868
+
1869
+ ordered_columns = []
1870
+ for col in (core_columns + options_columns + bidask_columns +
1871
+ underlying_columns + stop_columns + strategy_columns + intraday_columns):
1872
+ if col in trades_df.columns:
1873
+ ordered_columns.append(col)
1874
+
1875
+ remaining = [col for col in trades_df.columns if col not in ordered_columns]
1876
+ ordered_columns.extend(remaining)
1877
+
1878
+ trades_df = trades_df[ordered_columns]
1879
+
1880
+ # Round numeric columns to 2 decimals
1881
+ numeric_columns = trades_df.select_dtypes(include=['float64', 'float32', 'float']).columns
1882
+ for col in numeric_columns:
1883
+ trades_df[col] = trades_df[col].round(5)
1884
+
1885
+ trades_df.to_csv(f'{prefix}_trades.csv', index=False)
1886
+ print(f"Exported: {prefix}_trades.csv ({len(ordered_columns)} columns)")
1887
+
1888
+ equity_df = pd.DataFrame({
1889
+ 'date': pd.to_datetime(r.equity_dates).strftime('%Y-%m-%d'),
1890
+ 'equity': r.equity_curve
1891
+ })
1892
+ equity_df['equity'] = equity_df['equity'].round(5)
1893
+ equity_df.to_csv(f'{prefix}_equity.csv', index=False)
1894
+ print(f"Exported: {prefix}_equity.csv")
1895
+
1896
+ with open(f'{prefix}_summary.txt', 'w') as f:
1897
+ f.write("BACKTEST SUMMARY\n")
1898
+ f.write("="*70 + "\n\n")
1899
+ f.write(f"Strategy: {r.config.get('strategy_name', 'Unknown')}\n")
1900
+ f.write(f"Period: {r.config.get('start_date')} to {r.config.get('end_date')}\n\n")
1901
+ f.write("PERFORMANCE\n")
1902
+ f.write("-"*70 + "\n")
1903
+ f.write(f"Total Return: {m['total_return']:.2f}%\n")
1904
+ f.write(f"Sharpe: {m['sharpe']:.2f}\n")
1905
+ f.write(f"Max DD: {m['max_drawdown']:.2f}%\n")
1906
+ f.write(f"Trades: {m['total_trades']}\n")
1907
+
1908
+ print(f"Exported: {prefix}_summary.txt")
1909
+
1910
+ # Export metrics as JSON with rounded values
1911
+ import json
1912
+ metrics_rounded = {}
1913
+ for key, value in m.items():
1914
+ if isinstance(value, (int, float)):
1915
+ metrics_rounded[key] = round(float(value), 5) if isinstance(value, float) else value
1916
+ else:
1917
+ metrics_rounded[key] = value
1918
+
1919
+ with open(f'{prefix}_metrics.json', 'w') as f:
1920
+ json.dump(metrics_rounded, f, indent=2)
1921
+
1922
+ print(f"Exported: {prefix}_metrics.json")
1923
+
1924
+
1925
+ # ============================================================
1926
+ # RUN BACKTEST (unchanged)
1927
+ # ============================================================
1928
+ def run_backtest(strategy_function, config, print_report=True,
1929
+ create_charts=True, export_results=True,
1930
+ chart_filename='backtest_results.png',
1931
+ export_prefix='backtest',
1932
+ progress_context=None):
1933
+ """Run complete backtest"""
1934
+
1935
+ # Check if running inside optimization
1936
+ is_optimization = progress_context and progress_context.get('is_optimization', False)
1937
+
1938
+ if not progress_context and not is_optimization:
1939
+ print("="*80)
1940
+ print(" "*25 + "STARTING BACKTEST")
1941
+ print("="*80)
1942
+ print(f"Strategy: {config.get('strategy_name', 'Unknown')}")
1943
+ print(f"Period: {config.get('start_date')} to {config.get('end_date')}")
1944
+ print(f"Capital: ${config.get('initial_capital', 0):,.0f}")
1945
+ print("="*80 + "\n")
1946
+
1947
+ if progress_context:
1948
+ config['_progress_context'] = progress_context
1949
+
1950
+ results = strategy_function(config)
1951
+
1952
+ if '_progress_context' in config:
1953
+ del config['_progress_context']
1954
+
1955
+ if not is_optimization:
1956
+ print("\n[*] Calculating metrics...")
1957
+ analyzer = BacktestAnalyzer(results)
1958
+ analyzer.calculate_all_metrics()
1959
+
1960
+ if print_report:
1961
+ print("\n" + "="*80)
1962
+ ResultsReporter.print_full_report(analyzer)
1963
+
1964
+ # Export charts during optimization if requested
1965
+ if create_charts and len(results.trades) > 0:
1966
+ if not is_optimization:
1967
+ print(f"\n[*] Creating charts: {chart_filename}")
1968
+ try:
1969
+ # Don't show plots during optimization, just save them
1970
+ ChartGenerator.create_all_charts(analyzer, chart_filename, show_plots=not is_optimization)
1971
+ except Exception as e:
1972
+ if not is_optimization:
1973
+ print(f"[ERROR] Charts failed: {e}")
1974
+
1975
+ # Export results during optimization if requested
1976
+ if export_results and len(results.trades) > 0:
1977
+ if not is_optimization:
1978
+ print(f"\n[*] Exporting: {export_prefix}_*")
1979
+ try:
1980
+ ResultsExporter.export_all(analyzer, export_prefix)
1981
+ except Exception as e:
1982
+ if not is_optimization:
1983
+ print(f"[ERROR] Export failed: {e}")
1984
+
1985
+ return analyzer
1986
+
1987
+
1988
+ def run_backtest_with_stoploss(strategy_function, config, print_report=True,
1989
+ create_charts=True, export_results=True,
1990
+ chart_filename='backtest_results.png',
1991
+ export_prefix='backtest',
1992
+ create_stoploss_report=True,
1993
+ create_stoploss_charts=True,
1994
+ progress_context=None):
1995
+ """Enhanced run_backtest with stop-loss analysis"""
1996
+
1997
+ analyzer = run_backtest(
1998
+ strategy_function, config,
1999
+ print_report=False,
2000
+ create_charts=create_charts,
2001
+ export_results=export_results,
2002
+ chart_filename=chart_filename,
2003
+ export_prefix=export_prefix,
2004
+ progress_context=progress_context
2005
+ )
2006
+
2007
+ calculate_stoploss_metrics(analyzer)
2008
+
2009
+ if print_report:
2010
+ print("\n" + "="*80)
2011
+ ResultsReporter.print_full_report(analyzer)
2012
+
2013
+ if create_stoploss_report and analyzer.metrics.get('stoploss_count', 0) > 0:
2014
+ print_stoploss_section(analyzer)
2015
+
2016
+ if create_stoploss_charts and analyzer.metrics.get('stoploss_count', 0) > 0:
2017
+ print(f"\n[*] Creating stop-loss analysis charts...")
2018
+ try:
2019
+ stoploss_chart_name = chart_filename.replace('.png', '_stoploss.png') if chart_filename else 'stoploss_analysis.png'
2020
+ create_stoploss_charts(analyzer, stoploss_chart_name)
2021
+ except Exception as e:
2022
+ print(f"[ERROR] Stop-loss charts failed: {e}")
2023
+
2024
+ return analyzer
2025
+
2026
+
2027
+ # ============================================================
2028
+ # STOP-LOSS CONFIG (ENHANCED WITH COMBINED)
2029
+ # ============================================================
2030
+ class StopLossConfig:
2031
+ """
2032
+ Universal stop-loss configuration builder (ENHANCED)
2033
+
2034
+ NEW METHOD:
2035
+ - combined(): Requires BOTH pl_loss AND directional conditions
2036
+ """
2037
+
2038
+ @staticmethod
2039
+ def _normalize_pct(value):
2040
+ """Convert any number to decimal (0.30)"""
2041
+ if value >= 1:
2042
+ return value / 100
2043
+ return value
2044
+
2045
+ @staticmethod
2046
+ def _format_pct(value):
2047
+ """Format percentage for display"""
2048
+ if value >= 1:
2049
+ return f"{value:.0f}%"
2050
+ return f"{value*100:.0f}%"
2051
+
2052
+ @staticmethod
2053
+ def none():
2054
+ """No stop-loss"""
2055
+ return {
2056
+ 'enabled': False,
2057
+ 'type': 'none',
2058
+ 'value': 0,
2059
+ 'name': 'No Stop-Loss',
2060
+ 'description': 'No stop-loss protection'
2061
+ }
2062
+
2063
+ @staticmethod
2064
+ def fixed(pct):
2065
+ """Fixed percentage stop-loss"""
2066
+ decimal = StopLossConfig._normalize_pct(pct)
2067
+ display = StopLossConfig._format_pct(pct)
2068
+
2069
+ return {
2070
+ 'enabled': True,
2071
+ 'type': 'fixed_pct',
2072
+ 'value': decimal,
2073
+ 'name': f'Fixed {display}',
2074
+ 'description': f'Fixed stop at {display} loss'
2075
+ }
2076
+
2077
+ @staticmethod
2078
+ def trailing(pct, trailing_distance=None):
2079
+ """Trailing stop-loss"""
2080
+ decimal = StopLossConfig._normalize_pct(pct)
2081
+ display = StopLossConfig._format_pct(pct)
2082
+
2083
+ config = {
2084
+ 'enabled': True,
2085
+ 'type': 'trailing',
2086
+ 'value': decimal,
2087
+ 'name': f'Trailing {display}',
2088
+ 'description': f'Trailing stop at {display} from peak'
2089
+ }
2090
+
2091
+ if trailing_distance is not None:
2092
+ config['trailing_distance'] = StopLossConfig._normalize_pct(trailing_distance)
2093
+
2094
+ return config
2095
+
2096
+ @staticmethod
2097
+ def time_based(days):
2098
+ """Time-based stop"""
2099
+ return {
2100
+ 'enabled': True,
2101
+ 'type': 'time_based',
2102
+ 'value': days,
2103
+ 'name': f'Time {days}d',
2104
+ 'description': f'Exit after {days} days'
2105
+ }
2106
+
2107
+ @staticmethod
2108
+ def volatility(atr_multiplier):
2109
+ """ATR-based stop"""
2110
+ return {
2111
+ 'enabled': True,
2112
+ 'type': 'volatility',
2113
+ 'value': atr_multiplier,
2114
+ 'name': f'ATR {atr_multiplier:.1f}x',
2115
+ 'description': f'Stop at {atr_multiplier:.1f}× ATR',
2116
+ 'requires_atr': True
2117
+ }
2118
+
2119
+ @staticmethod
2120
+ def pl_loss(pct):
2121
+ """P&L-based stop using real bid/ask prices"""
2122
+ decimal = StopLossConfig._normalize_pct(pct)
2123
+ display = StopLossConfig._format_pct(pct)
2124
+
2125
+ return {
2126
+ 'enabled': True,
2127
+ 'type': 'pl_loss',
2128
+ 'value': decimal,
2129
+ 'name': f'P&L Loss {display}',
2130
+ 'description': f'Stop when P&L drops to -{display}'
2131
+ }
2132
+
2133
+ @staticmethod
2134
+ def directional(pct):
2135
+ """Directional stop based on underlying movement"""
2136
+ decimal = StopLossConfig._normalize_pct(pct)
2137
+ display = StopLossConfig._format_pct(pct)
2138
+
2139
+ return {
2140
+ 'enabled': True,
2141
+ 'type': 'directional',
2142
+ 'value': decimal,
2143
+ 'name': f'Directional {display}',
2144
+ 'description': f'Stop when underlying moves {display}'
2145
+ }
2146
+
2147
+ # ========================================================
2148
+ # NEW: COMBINED STOP (REQUIRES BOTH CONDITIONS)
2149
+ # ========================================================
2150
+
2151
+ @staticmethod
2152
+ def combined(pl_loss_pct, directional_pct):
2153
+ """
2154
+ Combined stop: Requires BOTH conditions (from code 2)
2155
+
2156
+ Args:
2157
+ pl_loss_pct: P&L loss threshold (e.g., 5 or 0.05 = -5%)
2158
+ directional_pct: Underlying move threshold (e.g., 3 or 0.03 = 3%)
2159
+
2160
+ Example:
2161
+ StopLossConfig.combined(5, 3)
2162
+ # Triggers only when BOTH:
2163
+ # 1. P&L drops to -5%
2164
+ # 2. Underlying moves 3% adversely
2165
+ """
2166
+ pl_decimal = StopLossConfig._normalize_pct(pl_loss_pct)
2167
+ dir_decimal = StopLossConfig._normalize_pct(directional_pct)
2168
+
2169
+ pl_display = StopLossConfig._format_pct(pl_loss_pct)
2170
+ dir_display = StopLossConfig._format_pct(directional_pct)
2171
+
2172
+ return {
2173
+ 'enabled': True,
2174
+ 'type': 'combined',
2175
+ 'value': {
2176
+ 'pl_loss': pl_decimal,
2177
+ 'directional': dir_decimal
2178
+ },
2179
+ 'name': f'Combined (P&L {pl_display} + Dir {dir_display})',
2180
+ 'description': f'Stop when P&L<-{pl_display} AND underlying moves {dir_display}'
2181
+ }
2182
+
2183
+ # ========================================================
2184
+ # BACKWARD COMPATIBILITY
2185
+ # ========================================================
2186
+
2187
+ @staticmethod
2188
+ def time(days):
2189
+ """Alias for time_based()"""
2190
+ return StopLossConfig.time_based(days)
2191
+
2192
+ @staticmethod
2193
+ def atr(multiplier):
2194
+ """Alias for volatility()"""
2195
+ return StopLossConfig.volatility(multiplier)
2196
+
2197
+ # ========================================================
2198
+ # PRESETS (WITH COMBINED STOPS)
2199
+ # ========================================================
2200
+
2201
+ @staticmethod
2202
+ def presets():
2203
+ """Generate all standard stop-loss presets (UPDATED WITH COMBINED)"""
2204
+ return {
2205
+ 'none': StopLossConfig.none(),
2206
+
2207
+ 'fixed_20': StopLossConfig.fixed(20),
2208
+ 'fixed_30': StopLossConfig.fixed(30),
2209
+ 'fixed_40': StopLossConfig.fixed(40),
2210
+ 'fixed_50': StopLossConfig.fixed(50),
2211
+ 'fixed_70': StopLossConfig.fixed(70),
2212
+
2213
+ 'trailing_20': StopLossConfig.trailing(20),
2214
+ 'trailing_30': StopLossConfig.trailing(30),
2215
+ 'trailing_50': StopLossConfig.trailing(50),
2216
+
2217
+ 'time_5d': StopLossConfig.time(5),
2218
+ 'time_10d': StopLossConfig.time(10),
2219
+ 'time_20d': StopLossConfig.time(20),
2220
+
2221
+ 'atr_2x': StopLossConfig.atr(2.0),
2222
+ 'atr_3x': StopLossConfig.atr(3.0),
2223
+
2224
+ 'pl_loss_5': StopLossConfig.pl_loss(5),
2225
+ 'pl_loss_10': StopLossConfig.pl_loss(10),
2226
+ 'pl_loss_15': StopLossConfig.pl_loss(15),
2227
+
2228
+ 'directional_3': StopLossConfig.directional(3),
2229
+ 'directional_5': StopLossConfig.directional(5),
2230
+ 'directional_7': StopLossConfig.directional(7),
2231
+
2232
+ # NEW: COMBINED STOPS
2233
+ 'combined_5_3': StopLossConfig.combined(5, 3),
2234
+ 'combined_7_5': StopLossConfig.combined(7, 5),
2235
+ 'combined_10_3': StopLossConfig.combined(10, 3),
2236
+ }
2237
+
2238
+ @staticmethod
2239
+ def apply(base_config, stop_config):
2240
+ """Apply stop-loss configuration to base config"""
2241
+ merged = base_config.copy()
2242
+
2243
+ merged['stop_loss_enabled'] = stop_config.get('enabled', False)
2244
+
2245
+ if merged['stop_loss_enabled']:
2246
+ sl_config = {
2247
+ 'type': stop_config['type'],
2248
+ 'value': stop_config['value']
2249
+ }
2250
+
2251
+ if 'trailing_distance' in stop_config:
2252
+ sl_config['trailing_distance'] = stop_config['trailing_distance']
2253
+
2254
+ merged['stop_loss_config'] = sl_config
2255
+
2256
+ return merged
2257
+
2258
+
2259
+ def create_stoploss_comparison_chart(results, filename='stoploss_comparison.png', show_plots=True):
2260
+ """Create comparison chart"""
2261
+ try:
2262
+ fig, axes = plt.subplots(2, 2, figsize=(16, 12))
2263
+ fig.suptitle('Stop-Loss Configuration Comparison', fontsize=16, fontweight='bold')
2264
+
2265
+ names = [r['config']['name'] for r in results.values()]
2266
+ returns = [r['total_return'] for r in results.values()]
2267
+ sharpes = [r['sharpe'] for r in results.values()]
2268
+ drawdowns = [r['max_drawdown'] for r in results.values()]
2269
+ stop_counts = [r['stoploss_count'] for r in results.values()]
2270
+
2271
+ ax1 = axes[0, 0]
2272
+ colors = ['#4CAF50' if r > 0 else '#f44336' for r in returns]
2273
+ ax1.barh(range(len(names)), returns, color=colors, alpha=0.7, edgecolor='black')
2274
+ ax1.set_yticks(range(len(names)))
2275
+ ax1.set_yticklabels(names, fontsize=9)
2276
+ ax1.set_xlabel('Total Return (%)')
2277
+ ax1.set_title('Total Return by Stop-Loss Type', fontsize=12, fontweight='bold')
2278
+ ax1.axvline(x=0, color='black', linestyle='-', linewidth=1)
2279
+ ax1.grid(True, alpha=0.3, axis='x')
2280
+
2281
+ ax2 = axes[0, 1]
2282
+ colors_sharpe = ['#4CAF50' if s > 1 else '#FF9800' if s > 0 else '#f44336' for s in sharpes]
2283
+ ax2.barh(range(len(names)), sharpes, color=colors_sharpe, alpha=0.7, edgecolor='black')
2284
+ ax2.set_yticks(range(len(names)))
2285
+ ax2.set_yticklabels(names, fontsize=9)
2286
+ ax2.set_xlabel('Sharpe Ratio')
2287
+ ax2.set_title('Sharpe Ratio by Stop-Loss Type', fontsize=12, fontweight='bold')
2288
+ ax2.axvline(x=1, color='green', linestyle='--', linewidth=1, label='Good (>1)')
2289
+ ax2.axvline(x=0, color='black', linestyle='-', linewidth=1)
2290
+ ax2.legend()
2291
+ ax2.grid(True, alpha=0.3, axis='x')
2292
+
2293
+ ax3 = axes[1, 0]
2294
+ ax3.barh(range(len(names)), drawdowns, color='#f44336', alpha=0.7, edgecolor='black')
2295
+ ax3.set_yticks(range(len(names)))
2296
+ ax3.set_yticklabels(names, fontsize=9)
2297
+ ax3.set_xlabel('Maximum Drawdown (%)')
2298
+ ax3.set_title('Maximum Drawdown (Lower is Better)', fontsize=12, fontweight='bold')
2299
+ ax3.grid(True, alpha=0.3, axis='x')
2300
+
2301
+ ax4 = axes[1, 1]
2302
+ ax4.barh(range(len(names)), stop_counts, color='#2196F3', alpha=0.7, edgecolor='black')
2303
+ ax4.set_yticks(range(len(names)))
2304
+ ax4.set_yticklabels(names, fontsize=9)
2305
+ ax4.set_xlabel('Number of Stop-Loss Exits')
2306
+ ax4.set_title('Stop-Loss Frequency', fontsize=12, fontweight='bold')
2307
+ ax4.grid(True, alpha=0.3, axis='x')
2308
+
2309
+ plt.tight_layout()
2310
+ plt.savefig(filename, dpi=300, bbox_inches='tight')
2311
+
2312
+ if show_plots:
2313
+ plt.show()
2314
+ else:
2315
+ plt.close()
2316
+
2317
+ print(f"Comparison chart saved: {filename}")
2318
+
2319
+ except Exception as e:
2320
+ print(f"Failed to create comparison chart: {e}")
2321
+
2322
+
2323
+
2324
+ # ============================================================
2325
+ # DATA PRELOADING FUNCTION (FOR OPTIMIZATION)
2326
+ # ============================================================
2327
+ def preload_options_data(config, progress_widgets=None):
2328
+ """
2329
+ Preload options data for optimization.
2330
+ Loads data ONCE and returns cache.
2331
+
2332
+ Returns:
2333
+ tuple: (lean_df, options_cache)
2334
+ - lean_df: DataFrame with IV lean history
2335
+ - options_cache: dict {date: DataFrame} with options data
2336
+ """
2337
+ if progress_widgets:
2338
+ progress_bar, status_label, monitor, start_time = progress_widgets
2339
+ status_label.value = "<b style='color:#0066cc'>🔄 Preloading options data (ONCE)...</b>"
2340
+ progress_bar.value = 5
2341
+
2342
+ # Extract config
2343
+ from datetime import datetime, timedelta
2344
+ import pandas as pd
2345
+ import numpy as np
2346
+ import gc
2347
+
2348
+ start_date = datetime.strptime(config['start_date'], '%Y-%m-%d').date()
2349
+ end_date = datetime.strptime(config['end_date'], '%Y-%m-%d').date()
2350
+ symbol = config['symbol']
2351
+ dte_target = config.get('dte_target', 30)
2352
+ lookback_period = config.get('lookback_period', 60)
2353
+ chunk_months = config.get('chunk_months', 1) # Default 1 month (~30 days), not 3
2354
+
2355
+ # Calculate date chunks
2356
+ data_start = start_date - timedelta(days=lookback_period + 60)
2357
+
2358
+ date_chunks = []
2359
+ current_chunk_start = data_start
2360
+ while current_chunk_start <= end_date:
2361
+ # Use chunk_days_options if available, otherwise chunk_months * 30
2362
+ chunk_days = config.get('chunk_days_options', chunk_months * 30)
2363
+ chunk_end = min(
2364
+ current_chunk_start + timedelta(days=chunk_days),
2365
+ end_date
2366
+ )
2367
+ date_chunks.append((current_chunk_start, chunk_end))
2368
+ current_chunk_start = chunk_end + timedelta(days=1)
2369
+
2370
+ # Store lean calculations
2371
+ lean_history = []
2372
+ all_options_data = [] # List to collect all options DataFrames
2373
+
2374
+ # Track time for ETA
2375
+ preload_start_time = time.time()
2376
+
2377
+ try:
2378
+ # Use api_call with caching instead of direct ivol API
2379
+ cache_config = config.get('cache_config')
2380
+
2381
+ # Process each chunk
2382
+ for chunk_idx, (chunk_start, chunk_end) in enumerate(date_chunks):
2383
+ if progress_widgets:
2384
+ # Use update_progress for full display with ETA, CPU, RAM
2385
+ update_progress(
2386
+ progress_bar, status_label, monitor,
2387
+ current=chunk_idx + 1,
2388
+ total=len(date_chunks),
2389
+ start_time=preload_start_time,
2390
+ message=f"🔄 Loading chunk {chunk_idx+1}/{len(date_chunks)}"
2391
+ )
2392
+
2393
+ # Use api_call with caching (supports disk + memory cache)
2394
+ raw_data = api_call(
2395
+ '/equities/eod/options-rawiv',
2396
+ cache_config,
2397
+ symbol=symbol,
2398
+ from_=chunk_start.strftime('%Y-%m-%d'),
2399
+ to=chunk_end.strftime('%Y-%m-%d'),
2400
+ debug=cache_config.get('debug', False) if cache_config else False
2401
+ )
2402
+
2403
+ if raw_data is None:
2404
+ continue
2405
+
2406
+ # api_call returns dict with 'data' key
2407
+ if isinstance(raw_data, dict) and 'data' in raw_data:
2408
+ df = pd.DataFrame(raw_data['data'])
2409
+ else:
2410
+ df = pd.DataFrame(raw_data)
2411
+
2412
+ if df.empty:
2413
+ continue
2414
+
2415
+ # Essential columns
2416
+ essential_cols = ['date', 'expiration', 'strike', 'Call/Put', 'iv', 'Adjusted close']
2417
+ if 'bid' in df.columns:
2418
+ essential_cols.append('bid')
2419
+ if 'ask' in df.columns:
2420
+ essential_cols.append('ask')
2421
+
2422
+ df = df[essential_cols].copy()
2423
+
2424
+ # Process bid/ask
2425
+ if 'bid' in df.columns:
2426
+ df['bid'] = pd.to_numeric(df['bid'], errors='coerce').astype('float32')
2427
+ else:
2428
+ df['bid'] = np.nan
2429
+
2430
+ if 'ask' in df.columns:
2431
+ df['ask'] = pd.to_numeric(df['ask'], errors='coerce').astype('float32')
2432
+ else:
2433
+ df['ask'] = np.nan
2434
+
2435
+ # Calculate mid price
2436
+ df['mid'] = (df['bid'] + df['ask']) / 2
2437
+ df['mid'] = df['mid'].fillna(df['iv'])
2438
+
2439
+ df['date'] = pd.to_datetime(df['date']).dt.date
2440
+ df['expiration'] = pd.to_datetime(df['expiration']).dt.date
2441
+ df['strike'] = pd.to_numeric(df['strike'], errors='coerce').astype('float32')
2442
+ df['iv'] = pd.to_numeric(df['iv'], errors='coerce').astype('float32')
2443
+ df['Adjusted close'] = pd.to_numeric(df['Adjusted close'], errors='coerce').astype('float32')
2444
+
2445
+ df['dte'] = (pd.to_datetime(df['expiration']) - pd.to_datetime(df['date'])).dt.days
2446
+ df['dte'] = df['dte'].astype('int16')
2447
+
2448
+ df = df.dropna(subset=['strike', 'iv', 'Adjusted close'])
2449
+
2450
+ if df.empty:
2451
+ del df
2452
+ gc.collect()
2453
+ continue
2454
+
2455
+ # Collect all options data
2456
+ all_options_data.append(df.copy())
2457
+
2458
+ # Calculate lean for this chunk
2459
+ trading_dates = sorted(df['date'].unique())
2460
+
2461
+ for current_date in trading_dates:
2462
+ day_data = df[df['date'] == current_date]
2463
+
2464
+ if day_data.empty:
2465
+ continue
2466
+
2467
+ stock_price = float(day_data['Adjusted close'].iloc[0])
2468
+
2469
+ dte_filtered = day_data[
2470
+ (day_data['dte'] >= dte_target - 7) &
2471
+ (day_data['dte'] <= dte_target + 7)
2472
+ ]
2473
+
2474
+ if dte_filtered.empty:
2475
+ continue
2476
+
2477
+ dte_filtered = dte_filtered.copy()
2478
+ dte_filtered['strike_diff'] = abs(dte_filtered['strike'] - stock_price)
2479
+ atm_idx = dte_filtered['strike_diff'].idxmin()
2480
+ atm_strike = float(dte_filtered.loc[atm_idx, 'strike'])
2481
+
2482
+ atm_options = dte_filtered[dte_filtered['strike'] == atm_strike]
2483
+ atm_call = atm_options[atm_options['Call/Put'] == 'C']
2484
+ atm_put = atm_options[atm_options['Call/Put'] == 'P']
2485
+
2486
+ if not atm_call.empty and not atm_put.empty:
2487
+ call_iv = float(atm_call['iv'].iloc[0])
2488
+ put_iv = float(atm_put['iv'].iloc[0])
2489
+
2490
+ if pd.notna(call_iv) and pd.notna(put_iv) and call_iv > 0 and put_iv > 0:
2491
+ iv_lean = call_iv - put_iv
2492
+
2493
+ lean_history.append({
2494
+ 'date': current_date,
2495
+ 'stock_price': stock_price,
2496
+ 'iv_lean': iv_lean
2497
+ })
2498
+
2499
+ del df, raw_data
2500
+ gc.collect()
2501
+
2502
+ lean_df = pd.DataFrame(lean_history)
2503
+ lean_df['stock_price'] = lean_df['stock_price'].astype('float32')
2504
+ lean_df['iv_lean'] = lean_df['iv_lean'].astype('float32')
2505
+
2506
+ # Combine all options data into single DataFrame
2507
+ if all_options_data:
2508
+ options_df = pd.concat(all_options_data, ignore_index=True)
2509
+ # Ensure date column is properly formatted
2510
+ options_df['date'] = pd.to_datetime(options_df['date']).dt.date
2511
+ options_df['expiration'] = pd.to_datetime(options_df['expiration']).dt.date
2512
+ else:
2513
+ options_df = pd.DataFrame()
2514
+
2515
+ del lean_history, all_options_data
2516
+ gc.collect()
2517
+
2518
+ if progress_widgets:
2519
+ status_label.value = f"<b style='color:#00cc00'>✓ Data preloaded: {len(lean_df)} days, {len(options_df)} options records</b>"
2520
+ progress_bar.value = 35
2521
+
2522
+ print(f"✓ Data preloaded: {len(lean_df)} days, {len(options_df)} options records")
2523
+
2524
+ return lean_df, options_df
2525
+
2526
+ except Exception as e:
2527
+ print(f"Error preloading data: {e}")
2528
+ return pd.DataFrame(), {}
2529
+
2530
+
2531
+ # ============================================================
2532
+ # UNIVERSAL DATA PRELOADER V2 (NEW!)
2533
+ # ============================================================
2534
+ def preload_data_universal(config, data_requests=None):
2535
+ """
2536
+ 🚀 TRULY UNIVERSAL DATA PRELOADER - Works with ANY API endpoint!
2537
+
2538
+ Supports:
2539
+ - EOD data: options-rawiv, stock-prices, ivs-by-delta, ivx, etc.
2540
+ - Intraday data: OPTIONS_INTRADAY, stock intraday, etc.
2541
+ - Any custom endpoint with any parameters
2542
+ - Automatic chunking for date ranges
2543
+ - Manual single-date requests
2544
+
2545
+ Args:
2546
+ config: Strategy configuration (start_date, end_date, symbol)
2547
+ data_requests: List of data requests to load. If None, tries auto-detection.
2548
+
2549
+ Format:
2550
+ [
2551
+ {
2552
+ 'name': 'options_data', # Your name for this dataset
2553
+ 'endpoint': '/equities/eod/options-rawiv',
2554
+ 'params': {...}, # Base params (symbol, etc.)
2555
+ 'chunking': { # Optional: for date-range data
2556
+ 'enabled': True,
2557
+ 'date_param': 'from_', # Param name for start date
2558
+ 'date_param_to': 'to', # Param name for end date
2559
+ 'chunk_days': 90 # Chunk size in days
2560
+ },
2561
+ 'post_process': lambda df: df, # Optional: process DataFrame
2562
+ },
2563
+ {
2564
+ 'name': 'ivx_data',
2565
+ 'endpoint': '/equities/eod/ivx',
2566
+ 'params': {
2567
+ 'symbol': config['symbol'],
2568
+ 'from_': config['start_date'],
2569
+ 'to': config['end_date']
2570
+ },
2571
+ 'chunking': {'enabled': False} # Single request
2572
+ },
2573
+ {
2574
+ 'name': 'options_intraday',
2575
+ 'endpoint': '/equities/intraday/options-rawiv',
2576
+ 'params': {
2577
+ 'symbol': config['symbol']
2578
+ },
2579
+ 'date_list': True, # Load for each date separately
2580
+ 'date_param': 'date'
2581
+ }
2582
+ ]
2583
+
2584
+ Returns:
2585
+ dict: Preloaded data with keys like:
2586
+ {
2587
+ '_preloaded_options_data': DataFrame,
2588
+ '_preloaded_ivx_data': DataFrame,
2589
+ '_preloaded_options_intraday': DataFrame,
2590
+ '_stats': {...}
2591
+ }
2592
+
2593
+ Usage in strategy:
2594
+ # Check for ANY preloaded data
2595
+ if any(k.startswith('_preloaded_') for k in config):
2596
+ options_df = config.get('_preloaded_options_data', pd.DataFrame()).copy()
2597
+ ivx_df = config.get('_preloaded_ivx_data', pd.DataFrame()).copy()
2598
+ else:
2599
+ # Load fresh
2600
+ ...
2601
+ """
2602
+
2603
+ print("\n" + "="*80)
2604
+ print("🚀 UNIVERSAL PRELOADER V2 - Supports ANY endpoint (EOD/Intraday/IVX/etc.)")
2605
+ print("="*80)
2606
+ start_time = time.time()
2607
+
2608
+ # Extract common config
2609
+ start_date = datetime.strptime(config['start_date'], '%Y-%m-%d').date()
2610
+ end_date = datetime.strptime(config['end_date'], '%Y-%m-%d').date()
2611
+ symbol = config['symbol']
2612
+ cache_config = config.get('cache_config', get_cache_config())
2613
+
2614
+ # Auto-detection if not specified
2615
+ if data_requests is None:
2616
+ data_requests = _auto_detect_requests(config)
2617
+ print(f"\n🔍 Auto-detected {len(data_requests)} data requests from config")
2618
+
2619
+ preloaded = {}
2620
+ total_rows = 0
2621
+
2622
+ # Process each data request
2623
+ for req_idx, request in enumerate(data_requests, 1):
2624
+ req_name = request['name']
2625
+ endpoint = request['endpoint']
2626
+ base_params = request.get('params', {})
2627
+ chunking = request.get('chunking', {'enabled': False})
2628
+ post_process = request.get('post_process', None)
2629
+ date_list = request.get('date_list', False)
2630
+
2631
+ print(f"\n[{req_idx}/{len(data_requests)}] 📊 Loading: {req_name}")
2632
+ print(f" Endpoint: {endpoint}")
2633
+
2634
+ all_data = []
2635
+
2636
+ # ========================================================
2637
+ # MODE 1: DATE LIST (one request per date, e.g., intraday)
2638
+ # ========================================================
2639
+ if date_list:
2640
+ date_param = request.get('date_param', 'date')
2641
+ trading_days = pd.bdate_range(start_date, end_date).date
2642
+
2643
+ print(f" Mode: Date list ({len(trading_days)} dates)")
2644
+
2645
+ for day_idx, date in enumerate(trading_days):
2646
+ params = base_params.copy()
2647
+ params[date_param] = date.strftime('%Y-%m-%d')
2648
+
2649
+ if day_idx % max(1, len(trading_days) // 10) == 0:
2650
+ print(f" Progress: {day_idx}/{len(trading_days)} dates...")
2651
+
2652
+ response = api_call(endpoint, cache_config, **params)
2653
+ if response and 'data' in response:
2654
+ df = pd.DataFrame(response['data'])
2655
+ if len(df) > 0:
2656
+ all_data.append(df)
2657
+
2658
+ # ========================================================
2659
+ # MODE 2: CHUNKED LOADING (date ranges in chunks)
2660
+ # ========================================================
2661
+ elif chunking.get('enabled', False):
2662
+ date_param_from = chunking.get('date_param', 'from_')
2663
+ date_param_to = chunking.get('date_param_to', 'to')
2664
+ chunk_days = chunking.get('chunk_days', 30)
2665
+ chunk_size = timedelta(days=chunk_days)
2666
+
2667
+ current = start_date
2668
+ chunks = []
2669
+ while current <= end_date:
2670
+ chunk_end = min(current + chunk_size, end_date)
2671
+ chunks.append((current, chunk_end))
2672
+ current = chunk_end + timedelta(days=1)
2673
+
2674
+ print(f" Mode: Chunked ({len(chunks)} chunks of {chunk_days} days)")
2675
+
2676
+ for chunk_idx, (chunk_start, chunk_end) in enumerate(chunks):
2677
+ params = base_params.copy()
2678
+ params[date_param_from] = chunk_start.strftime('%Y-%m-%d')
2679
+ params[date_param_to] = chunk_end.strftime('%Y-%m-%d')
2680
+
2681
+ if chunk_idx % max(1, len(chunks) // 5) == 0:
2682
+ print(f" Progress: {chunk_idx+1}/{len(chunks)} chunks...")
2683
+
2684
+ response = api_call(endpoint, cache_config, **params)
2685
+ if response and 'data' in response:
2686
+ df = pd.DataFrame(response['data'])
2687
+ if len(df) > 0:
2688
+ all_data.append(df)
2689
+
2690
+ # ========================================================
2691
+ # MODE 3: SINGLE REQUEST (no chunking/date list)
2692
+ # ========================================================
2693
+ else:
2694
+ print(f" Mode: Single request")
2695
+
2696
+ params = base_params.copy()
2697
+ response = api_call(endpoint, cache_config, **params)
2698
+ if response and 'data' in response:
2699
+ df = pd.DataFrame(response['data'])
2700
+ if len(df) > 0:
2701
+ all_data.append(df)
2702
+
2703
+ # ========================================================
2704
+ # COMBINE AND STORE
2705
+ # ========================================================
2706
+ if len(all_data) > 0:
2707
+ combined_df = pd.concat(all_data, ignore_index=True)
2708
+
2709
+ # Apply post-processing if provided
2710
+ if post_process is not None:
2711
+ try:
2712
+ combined_df = post_process(combined_df)
2713
+ except Exception as e:
2714
+ print(f" ⚠️ Post-processing failed: {e}")
2715
+
2716
+ # Auto-process common date columns
2717
+ combined_df = _auto_process_dates(combined_df)
2718
+
2719
+ # Store with standardized key
2720
+ key = f"_preloaded_{req_name}"
2721
+ preloaded[key] = combined_df
2722
+ total_rows += len(combined_df)
2723
+
2724
+ print(f" ✓ Loaded: {len(combined_df):,} rows → {key}")
2725
+ else:
2726
+ print(f" ⚠️ No data returned")
2727
+
2728
+ # ========================================================
2729
+ # SUMMARY
2730
+ # ========================================================
2731
+ elapsed = time.time() - start_time
2732
+
2733
+ # Collect detailed stats for each dataset
2734
+ dataset_details = {}
2735
+ for k in preloaded.keys():
2736
+ if k.startswith('_preloaded_'):
2737
+ dataset_name = k.replace('_preloaded_', '')
2738
+ df = preloaded[k]
2739
+ dataset_details[dataset_name] = {
2740
+ 'rows': len(df),
2741
+ 'endpoint': None
2742
+ }
2743
+
2744
+ # Map dataset names to endpoints from data_requests
2745
+ if data_requests:
2746
+ for req in data_requests:
2747
+ req_name = req.get('name', 'unknown')
2748
+ if req_name in dataset_details:
2749
+ dataset_details[req_name]['endpoint'] = req.get('endpoint', 'unknown')
2750
+
2751
+ preloaded['_stats'] = {
2752
+ 'load_time_seconds': int(elapsed),
2753
+ 'total_rows': total_rows,
2754
+ 'data_count': len([k for k in preloaded.keys() if k.startswith('_preloaded_')]),
2755
+ 'datasets': [k.replace('_preloaded_', '') for k in preloaded.keys() if k.startswith('_preloaded_')],
2756
+ 'dataset_details': dataset_details
2757
+ }
2758
+
2759
+ print(f"\n{'='*80}")
2760
+ print(f"✅ PRELOAD COMPLETE:")
2761
+ print(f" • Time: {int(elapsed)}s")
2762
+ print(f" • Total rows: {total_rows:,}")
2763
+ print(f" • Datasets: {preloaded['_stats']['data_count']}")
2764
+ for ds in preloaded['_stats']['datasets']:
2765
+ print(f" - {ds}")
2766
+ print(f" • Cached in RAM for 4-5x speedup! 🚀")
2767
+ print(f"{'='*80}\n")
2768
+
2769
+ return preloaded
2770
+
2771
+
2772
+ def _auto_detect_requests(config):
2773
+ """Auto-detect what data to load based on config keys"""
2774
+ requests = []
2775
+
2776
+ # Always load options data for options strategies
2777
+ requests.append({
2778
+ 'name': 'options',
2779
+ 'endpoint': '/equities/eod/options-rawiv',
2780
+ 'params': {
2781
+ 'symbol': config['symbol']
2782
+ },
2783
+ 'chunking': {
2784
+ 'enabled': True,
2785
+ 'date_param': 'from_',
2786
+ 'date_param_to': 'to',
2787
+ 'chunk_days': config.get('chunk_days_options', 30)
2788
+ },
2789
+ 'post_process': lambda df: _process_options_df(df)
2790
+ })
2791
+
2792
+ # Load IV surface if strategy uses term structure
2793
+ if any(k in config for k in ['short_tenor', 'long_tenor', 'delta_target']):
2794
+ requests.append({
2795
+ 'name': 'ivs_surface',
2796
+ 'endpoint': '/equities/eod/ivs-by-delta',
2797
+ 'params': {
2798
+ 'symbol': config['symbol'],
2799
+ 'deltaFrom': config.get('delta_target', 0.5) - 0.05,
2800
+ 'deltaTo': config.get('delta_target', 0.5) + 0.05,
2801
+ 'periodFrom': config.get('short_tenor', 30) - 7,
2802
+ 'periodTo': config.get('long_tenor', 90) + 7
2803
+ },
2804
+ 'chunking': {
2805
+ 'enabled': True,
2806
+ 'date_param': 'from_',
2807
+ 'date_param_to': 'to',
2808
+ 'chunk_days': config.get('chunk_days_options', 30)
2809
+ }
2810
+ })
2811
+
2812
+ # Load stock prices
2813
+ requests.append({
2814
+ 'name': 'stock',
2815
+ 'endpoint': '/equities/eod/stock-prices',
2816
+ 'params': {
2817
+ 'symbol': config['symbol']
2818
+ },
2819
+ 'chunking': {
2820
+ 'enabled': True,
2821
+ 'date_param': 'from_',
2822
+ 'date_param_to': 'to',
2823
+ 'chunk_days': config.get('chunk_days_stock', 180) # Stock data is lightweight
2824
+ }
2825
+ })
2826
+
2827
+ return requests
2828
+
2829
+
2830
+ def _process_options_df(df):
2831
+ """Process options DataFrame: dates + DTE + OPTIMIZATIONS (5-10x faster!)"""
2832
+ # Basic date processing
2833
+ if 'date' in df.columns:
2834
+ df['date'] = pd.to_datetime(df['date']).dt.date
2835
+ if 'expiration' in df.columns:
2836
+ df['expiration'] = pd.to_datetime(df['expiration']).dt.date
2837
+
2838
+ if 'date' in df.columns and 'expiration' in df.columns:
2839
+ df = df.copy()
2840
+ df['dte'] = (pd.to_datetime(df['expiration']) -
2841
+ pd.to_datetime(df['date'])).dt.days
2842
+
2843
+ # ========================================================
2844
+ # CRITICAL: SORT BY DATE FIRST! (Required for time-series)
2845
+ # ========================================================
2846
+ if 'date' in df.columns:
2847
+ # Check if already sorted (skip if yes, fast!)
2848
+ if not df['date'].is_monotonic_increasing:
2849
+ df = df.sort_values('date') # ✅ Sort only if needed
2850
+
2851
+ # ========================================================
2852
+ # AUTOMATIC OPTIMIZATIONS (applied by library)
2853
+ # ========================================================
2854
+
2855
+ # These optimizations are SAFE to apply automatically:
2856
+ # - Categorical types for low-cardinality columns
2857
+ # - Optimized numeric types (float32/int16 instead of float64/int64)
2858
+ #
2859
+ # NOTE: We do NOT set index on 'date' in library functions because:
2860
+ # - It breaks existing code that uses .loc with non-date indices
2861
+ # - Requires all strategies to handle Series vs scalar results
2862
+
2863
+ # Convert Call/Put to categorical (60% less RAM, 2x faster filtering)
2864
+ if 'Call/Put' in df.columns:
2865
+ df['Call/Put'] = df['Call/Put'].astype('category')
2866
+
2867
+ # Optimize data types (50% less RAM)
2868
+ # float32 for prices (4 bytes instead of 8, enough precision)
2869
+ float32_cols = ['strike', 'bid', 'ask', 'iv', 'price', 'mid', 'delta', 'gamma', 'vega', 'theta']
2870
+ for col in float32_cols:
2871
+ if col in df.columns:
2872
+ df[col] = pd.to_numeric(df[col], errors='coerce').astype('float32')
2873
+
2874
+ # int16 for DTE (2 bytes instead of 8, max 32767 days)
2875
+ if 'dte' in df.columns:
2876
+ df['dte'] = df['dte'].astype('int16')
2877
+
2878
+ return df
2879
+
2880
+
2881
+ def _auto_process_dates(df):
2882
+ """Auto-process common date columns + SORT BY DATE"""
2883
+ date_columns = ['date', 'expiration', 'trade_date', 'time']
2884
+
2885
+ for col in date_columns:
2886
+ if col in df.columns:
2887
+ try:
2888
+ if col == 'time':
2889
+ # Keep time as string or datetime
2890
+ pass
2891
+ else:
2892
+ df[col] = pd.to_datetime(df[col]).dt.date
2893
+ except:
2894
+ pass # Already in correct format or not a date
2895
+
2896
+ # ========================================================
2897
+ # CRITICAL: SORT BY DATE! (Required for time-series)
2898
+ # ========================================================
2899
+ if 'date' in df.columns:
2900
+ # Check if already sorted (O(1) check vs O(N log N) sort)
2901
+ if not df['date'].is_monotonic_increasing:
2902
+ df = df.sort_values('date') # ✅ Sort only if needed
2903
+ elif 'trade_date' in df.columns:
2904
+ if not df['trade_date'].is_monotonic_increasing:
2905
+ df = df.sort_values('trade_date') # Alternative date column
2906
+
2907
+ return df
2908
+
2909
+
2910
+ # ============================================================
2911
+ # NEW: OPTIMIZATION FRAMEWORK
2912
+ # ============================================================
2913
+ def optimize_parameters(base_config, param_grid, strategy_function,
2914
+ optimization_metric='sharpe', min_trades=5,
2915
+ max_drawdown_limit=None, parallel=False,
2916
+ export_each_combo=True, # ← NEW PARAMETER
2917
+ optimization_config=None, # ← NEW PARAMETER FOR PRESETS
2918
+ results_folder=None # ← NEW: Use existing folder or create new
2919
+ ):
2920
+ """
2921
+ Optimize strategy parameters across multiple combinations
2922
+
2923
+ Args:
2924
+ base_config: Base configuration dict
2925
+ param_grid: Dict of parameters to optimize
2926
+ Example: {'z_score_entry': [1.0, 1.5, 2.0], 'z_score_exit': [0.1, 0.3, 0.5]}
2927
+ strategy_function: Strategy function to run
2928
+ optimization_metric: Metric to optimize ('sharpe', 'total_return', 'total_pnl', 'profit_factor', 'calmar')
2929
+ min_trades: Minimum number of trades required
2930
+ max_drawdown_limit: Maximum acceptable drawdown (e.g., 0.10 for 10%)
2931
+ parallel: Use parallel processing (not implemented yet)
2932
+ export_each_combo: If True, exports files for each combination # ←
2933
+
2934
+ Returns:
2935
+ tuple: (results_df, best_params, results_folder)
2936
+ """
2937
+
2938
+ # Check if optimization_config has preset and apply it automatically
2939
+ if optimization_config and isinstance(optimization_config, dict) and 'preset' in optimization_config:
2940
+ preset = optimization_config['preset']
2941
+ print(f"🔄 Auto-applying preset: {preset}")
2942
+ apply_optimization_preset(optimization_config, preset)
2943
+ print_preset_info(optimization_config)
2944
+
2945
+ # Use preset parameters for grid and validation criteria
2946
+ param_grid = optimization_config['param_grid']
2947
+ min_trades = optimization_config['min_trades']
2948
+ max_drawdown_limit = optimization_config['max_drawdown_limit']
2949
+
2950
+ # Use optimization_config for optimization_metric if available
2951
+ if 'optimization_metric' in optimization_config:
2952
+ optimization_metric = optimization_config['optimization_metric']
2953
+
2954
+ # Use optimization_config for execution settings if available
2955
+ if 'parallel' in optimization_config:
2956
+ parallel = optimization_config['parallel']
2957
+ if 'export_each_combo' in optimization_config:
2958
+ export_each_combo = optimization_config['export_each_combo']
2959
+
2960
+ # ═══ ADD AT THE BEGINNING OF FUNCTION ═══
2961
+ # Create results folder (or use provided one)
2962
+ if results_folder is None:
2963
+ results_folder = create_optimization_folder()
2964
+ print(f"📊 Results will be saved to: {results_folder}\n")
2965
+ else:
2966
+ print(f"📊 Using existing results folder: {results_folder}\n")
2967
+
2968
+ # Record start time
2969
+ optimization_start_time = datetime.now()
2970
+ start_time_str = optimization_start_time.strftime('%Y-%m-%d %H:%M:%S')
2971
+
2972
+ print("\n" + "="*80)
2973
+ print(" "*20 + "PARAMETER OPTIMIZATION")
2974
+ print("="*80)
2975
+ print(f"Strategy: {base_config.get('strategy_name', 'Unknown')}")
2976
+ print(f"Period: {base_config.get('start_date')} to {base_config.get('end_date')}")
2977
+ print(f"Optimization Metric: {optimization_metric}")
2978
+ print(f"Min Trades: {min_trades}")
2979
+ print(f"🕐 Started: {start_time_str}")
2980
+ if max_drawdown_limit:
2981
+ print(f"Max Drawdown Limit: {max_drawdown_limit*100:.0f}%")
2982
+ print("="*80 + "\n")
2983
+
2984
+ # Generate all combinations
2985
+ param_names = list(param_grid.keys())
2986
+ param_values = list(param_grid.values())
2987
+ all_combinations = list(product(*param_values))
2988
+
2989
+ total_combinations = len(all_combinations)
2990
+ print(f"Testing {total_combinations} parameter combinations...")
2991
+ print(f"Parameters: {param_names}")
2992
+ print(f"Grid: {param_grid}\n")
2993
+
2994
+ # Create SHARED progress context for all backtests
2995
+ try:
2996
+ from IPython.display import display
2997
+ import ipywidgets as widgets
2998
+
2999
+ progress_bar = widgets.FloatProgress(
3000
+ value=0, min=0, max=100,
3001
+ description='Optimizing:',
3002
+ bar_style='info',
3003
+ layout=widgets.Layout(width='100%', height='30px')
3004
+ )
3005
+
3006
+ status_label = widgets.HTML(value="<b>Starting optimization...</b>")
3007
+ display(widgets.VBox([progress_bar, status_label]))
3008
+
3009
+ monitor = ResourceMonitor()
3010
+ opt_start_time = time.time()
3011
+
3012
+ # Create shared progress context (will suppress individual backtest progress)
3013
+ shared_progress = {
3014
+ 'progress_widgets': (progress_bar, status_label, monitor, opt_start_time),
3015
+ 'is_optimization': True
3016
+ }
3017
+ has_widgets = True
3018
+ except:
3019
+ shared_progress = None
3020
+ has_widgets = False
3021
+ print("Running optimization (no progress bar)...")
3022
+
3023
+ # ═══════════════════════════════════════════════════════════════════════════
3024
+ # DEPRECATED: optimize_parameters should NOT preload data internally!
3025
+ # Data should be preloaded BEFORE calling optimize_parameters using preload_data_universal()
3026
+ # ═══════════════════════════════════════════════════════════════════════════
3027
+ # Check if data is already preloaded
3028
+ preloaded_keys = [k for k in base_config.keys() if k.startswith('_preloaded_')]
3029
+
3030
+ # Initialize these variables for backward compatibility
3031
+ preloaded_lean_df = None
3032
+ preloaded_options_df = None
3033
+ use_legacy_preload = False
3034
+
3035
+ if not preloaded_keys:
3036
+ # Fallback: use old preload_options_data (for backward compatibility)
3037
+ print("\n" + "="*80)
3038
+ print("📥 PRELOADING OPTIONS DATA (loads ONCE, reused for all combinations)")
3039
+ print("="*80)
3040
+ print("⚠️ WARNING: Data not preloaded! Using deprecated preload_options_data()")
3041
+ print("⚠️ Recommendation: Use preload_data_universal() before calling optimize_parameters()")
3042
+ print("="*80)
3043
+
3044
+ preloaded_lean_df, preloaded_options_df = preload_options_data(
3045
+ base_config,
3046
+ progress_widgets=shared_progress['progress_widgets'] if shared_progress else None
3047
+ )
3048
+
3049
+ if preloaded_lean_df.empty:
3050
+ print("\n❌ ERROR: Failed to preload data. Cannot proceed with optimization.")
3051
+ return pd.DataFrame(), None
3052
+
3053
+ use_legacy_preload = True
3054
+ print(f"✓ Preloading complete! Data will be reused for all {total_combinations} combinations")
3055
+ print("="*80 + "\n")
3056
+ else:
3057
+ print("\n✓ Using preloaded data from preload_data_universal() (recommended method)\n")
3058
+
3059
+ # ═══════════════════════════════════════════════════════════════════════════
3060
+ # RESET PROGRESS BAR FOR OPTIMIZATION LOOP
3061
+ # ═══════════════════════════════════════════════════════════════════════════
3062
+ if has_widgets:
3063
+ progress_bar.value = 0
3064
+ progress_bar.bar_style = 'info'
3065
+ status_label.value = "<b style='color:#0066cc'>Starting optimization loop...</b>"
3066
+
3067
+ # Run backtests
3068
+ results = []
3069
+ start_time = time.time()
3070
+
3071
+ for idx, param_combo in enumerate(all_combinations, 1):
3072
+ # Create test config
3073
+ test_config = base_config.copy()
3074
+
3075
+ # Update parameters
3076
+ for param_name, param_value in zip(param_names, param_combo):
3077
+ test_config[param_name] = param_value
3078
+
3079
+ # Update name
3080
+ param_str = "_".join([f"{k}={v}" for k, v in zip(param_names, param_combo)])
3081
+ test_config['strategy_name'] = f"{base_config.get('strategy_name', 'Strategy')} [{param_str}]"
3082
+
3083
+ # ═══ ADD PRELOADED DATA TO CONFIG ═══
3084
+ # Only add legacy preloaded data if it was loaded by preload_options_data
3085
+ if use_legacy_preload:
3086
+ test_config['_preloaded_lean_df'] = preloaded_lean_df
3087
+ test_config['_preloaded_options_cache'] = preloaded_options_df
3088
+ # Otherwise, data is already in base_config from preload_data_universal
3089
+
3090
+ # Update progress
3091
+ if has_widgets:
3092
+ # Use update_progress for full display with ETA, CPU, RAM
3093
+ update_progress(
3094
+ progress_bar, status_label, monitor,
3095
+ current=idx,
3096
+ total=total_combinations,
3097
+ start_time=start_time,
3098
+ message=f"Testing: {param_str}"
3099
+ )
3100
+ else:
3101
+ if idx % max(1, total_combinations // 10) == 0:
3102
+ print(f"[{idx}/{total_combinations}] {param_str}")
3103
+
3104
+ # ═══ MODIFY run_backtest CALL (lines ~2240-2248) ═══
3105
+ try:
3106
+ # Create compact parameter string (e.g., Z1.0_E0.1_PT20)
3107
+ param_parts = []
3108
+ for name, value in zip(param_names, param_combo):
3109
+ if 'z_score_entry' in name:
3110
+ param_parts.append(f"Z{value}")
3111
+ elif 'z_score_exit' in name:
3112
+ param_parts.append(f"E{value}")
3113
+ elif 'profit_target' in name:
3114
+ if value is None:
3115
+ param_parts.append("PTNo")
3116
+ else:
3117
+ param_parts.append(f"PT{int(value*100)}")
3118
+ elif 'min_days' in name:
3119
+ param_parts.append(f"D{value}")
3120
+ else:
3121
+ # Generic short name for other params
3122
+ short_name = ''.join([c for c in name if c.isupper() or c.isdigit()])[:3]
3123
+ param_parts.append(f"{short_name}{value}")
3124
+
3125
+ compact_params = "_".join(param_parts)
3126
+
3127
+ # Create combo folder: c01_Z1.0_E0.1_PT20
3128
+ combo_folder = os.path.join(results_folder, f'c{idx:02d}_{compact_params}')
3129
+ os.makedirs(combo_folder, exist_ok=True)
3130
+
3131
+ # File prefix: c01_Z1.0_E0.1_PT20
3132
+ combo_prefix = f"c{idx:02d}_{compact_params}"
3133
+
3134
+ # Run backtest WITH EXPORT AND CHARTS (saved but not displayed)
3135
+ analyzer = run_backtest(
3136
+ strategy_function,
3137
+ test_config,
3138
+ print_report=False,
3139
+ create_charts=export_each_combo, # ← CREATE CHARTS (saved but not displayed)
3140
+ export_results=export_each_combo, # ← MODIFIED
3141
+ progress_context=shared_progress,
3142
+ chart_filename=os.path.join(combo_folder, 'equity_curve.png') if export_each_combo else None, # ← CHARTS SAVED
3143
+ export_prefix=os.path.join(combo_folder, combo_prefix) if export_each_combo else None # ← ADDED
3144
+ )
3145
+
3146
+ # Check validity
3147
+ is_valid = True
3148
+ invalid_reason = ""
3149
+
3150
+ if analyzer.metrics['total_trades'] < min_trades:
3151
+ is_valid = False
3152
+ invalid_reason = f"Too few trades ({analyzer.metrics['total_trades']})"
3153
+
3154
+ if max_drawdown_limit and analyzer.metrics['max_drawdown'] > (max_drawdown_limit * 100):
3155
+ is_valid = False
3156
+ invalid_reason = f"Excessive drawdown ({analyzer.metrics['max_drawdown']:.1f}%)"
3157
+
3158
+ # Print compact statistics for this combination
3159
+ status_symbol = "✓" if is_valid else "✗"
3160
+ status_color = "#00cc00" if is_valid else "#ff6666"
3161
+
3162
+ print(f"\n[{idx}/{total_combinations}] {param_str}")
3163
+ print("-" * 80)
3164
+ if is_valid:
3165
+ print(f" {status_symbol} Return: {analyzer.metrics['total_return']:>7.2f}% | "
3166
+ f"Sharpe: {analyzer.metrics['sharpe']:>6.2f} | "
3167
+ f"Max DD: {analyzer.metrics['max_drawdown']:>6.2f}% | "
3168
+ f"Trades: {analyzer.metrics['total_trades']:>3} | "
3169
+ f"Win Rate: {analyzer.metrics['win_rate']:>5.1f}% | "
3170
+ f"PF: {analyzer.metrics['profit_factor']:>5.2f}")
3171
+ else:
3172
+ print(f" {status_symbol} INVALID: {invalid_reason}")
3173
+
3174
+ # Update widget status with last result
3175
+ if has_widgets:
3176
+ result_text = f"Return: {analyzer.metrics['total_return']:.1f}% | Sharpe: {analyzer.metrics['sharpe']:.2f}" if is_valid else invalid_reason
3177
+
3178
+ # Get resource usage
3179
+ cpu_pct = monitor.get_cpu_percent()
3180
+ mem_info = monitor.get_memory_info()
3181
+ ram_mb = mem_info[0] # process_mb
3182
+ resource_text = f"CPU: {cpu_pct:.0f}% | RAM: {ram_mb:.0f}MB"
3183
+
3184
+ status_label.value = (
3185
+ f"<b style='color:{status_color}'>[{idx}/{total_combinations}] {param_str}</b><br>"
3186
+ f"<span style='color:#666'>{result_text}</span><br>"
3187
+ f"<span style='color:#999;font-size:10px'>{resource_text}</span>"
3188
+ )
3189
+
3190
+ # Store results
3191
+ result = {
3192
+ 'combination_id': idx,
3193
+ 'is_valid': is_valid,
3194
+ 'invalid_reason': invalid_reason,
3195
+ **{name: value for name, value in zip(param_names, param_combo)},
3196
+ 'total_return': analyzer.metrics['total_return'],
3197
+ 'sharpe': analyzer.metrics['sharpe'],
3198
+ 'sortino': analyzer.metrics['sortino'],
3199
+ 'calmar': analyzer.metrics['calmar'],
3200
+ 'max_drawdown': analyzer.metrics['max_drawdown'],
3201
+ 'win_rate': analyzer.metrics['win_rate'],
3202
+ 'profit_factor': analyzer.metrics['profit_factor'],
3203
+ 'total_trades': analyzer.metrics['total_trades'],
3204
+ 'avg_win': analyzer.metrics['avg_win'],
3205
+ 'avg_loss': analyzer.metrics['avg_loss'],
3206
+ 'volatility': analyzer.metrics['volatility'],
3207
+ }
3208
+
3209
+ results.append(result)
3210
+
3211
+ # Show intermediate summary every 10 combinations (or at end)
3212
+ if idx % 10 == 0 or idx == total_combinations:
3213
+ valid_so_far = [r for r in results if r['is_valid']]
3214
+ if valid_so_far:
3215
+ print("\n" + "="*80)
3216
+ print(f"INTERMEDIATE SUMMARY ({idx}/{total_combinations} tested)")
3217
+ print("="*80)
3218
+
3219
+ # Sort by optimization metric
3220
+ if optimization_metric == 'sharpe':
3221
+ valid_so_far.sort(key=lambda x: x['sharpe'], reverse=True)
3222
+ elif optimization_metric == 'total_return':
3223
+ valid_so_far.sort(key=lambda x: x['total_return'], reverse=True)
3224
+ elif optimization_metric == 'total_pnl':
3225
+ valid_so_far.sort(key=lambda x: x['total_pnl'], reverse=True)
3226
+ elif optimization_metric == 'profit_factor':
3227
+ valid_so_far.sort(key=lambda x: x['profit_factor'], reverse=True)
3228
+ elif optimization_metric == 'calmar':
3229
+ valid_so_far.sort(key=lambda x: x['calmar'], reverse=True)
3230
+
3231
+ # Show top 3
3232
+ print(f"\n🏆 TOP 3 BY {optimization_metric.upper()}:")
3233
+ print("-"*80)
3234
+ for rank, res in enumerate(valid_so_far[:3], 1):
3235
+ params_display = ", ".join([f"{name}={res[name]}" for name in param_names])
3236
+ print(f" {rank}. [{params_display}]")
3237
+ print(f" Return: {res['total_return']:>7.2f}% | "
3238
+ f"Sharpe: {res['sharpe']:>6.2f} | "
3239
+ f"Max DD: {res['max_drawdown']:>6.2f}% | "
3240
+ f"Trades: {res['total_trades']:>3}")
3241
+
3242
+ print(f"\nValid: {len(valid_so_far)}/{idx} | "
3243
+ f"Invalid: {idx - len(valid_so_far)}/{idx}")
3244
+ print("="*80 + "\n")
3245
+
3246
+ except Exception as e:
3247
+ print(f"\n[{idx}/{total_combinations}] {param_str}")
3248
+ print("-" * 80)
3249
+ print(f" ✗ ERROR: {str(e)}")
3250
+ import traceback
3251
+ print(" Full traceback:")
3252
+ traceback.print_exc()
3253
+
3254
+ result = {
3255
+ 'combination_id': idx,
3256
+ 'is_valid': False,
3257
+ 'invalid_reason': f"Error: {str(e)[:50]}",
3258
+ **{name: value for name, value in zip(param_names, param_combo)},
3259
+ 'total_return': 0, 'sharpe': 0, 'sortino': 0, 'calmar': 0,
3260
+ 'max_drawdown': 0, 'win_rate': 0, 'profit_factor': 0,
3261
+ 'total_trades': 0, 'avg_win': 0, 'avg_loss': 0, 'volatility': 0
3262
+ }
3263
+ results.append(result)
3264
+
3265
+ elapsed = time.time() - start_time
3266
+
3267
+ if has_widgets:
3268
+ progress_bar.value = 100
3269
+ progress_bar.bar_style = 'success'
3270
+ status_label.value = f"<b style='color:#00cc00'>✓ Optimization complete in {int(elapsed)}s</b>"
3271
+
3272
+ # Create results DataFrame
3273
+ results_df = pd.DataFrame(results)
3274
+
3275
+ # Round numeric columns to 2 decimals
3276
+ numeric_columns = results_df.select_dtypes(include=['float64', 'float32', 'float']).columns
3277
+ for col in numeric_columns:
3278
+ results_df[col] = results_df[col].round(5)
3279
+
3280
+ # ═══ ADD SUMMARY SAVE TO FOLDER ═══
3281
+ summary_path = os.path.join(results_folder, 'optimization_summary.csv')
3282
+ results_df.to_csv(summary_path, index=False)
3283
+ print(f"\n✓ Summary saved: {summary_path}")
3284
+
3285
+ # Find best parameters
3286
+ valid_results = results_df[results_df['is_valid'] == True].copy()
3287
+
3288
+ if len(valid_results) == 0:
3289
+ print("\n" + "="*80)
3290
+ print("WARNING: No valid combinations found!")
3291
+ print("Try relaxing constraints or checking parameter ranges")
3292
+ print("="*80)
3293
+ return results_df, None, results_folder
3294
+
3295
+ # Select best based on metric
3296
+ if optimization_metric == 'sharpe':
3297
+ best_idx = valid_results['sharpe'].idxmax()
3298
+ elif optimization_metric == 'total_return':
3299
+ best_idx = valid_results['total_return'].idxmax()
3300
+ elif optimization_metric == 'total_pnl':
3301
+ best_idx = valid_results['total_pnl'].idxmax()
3302
+ elif optimization_metric == 'profit_factor':
3303
+ best_idx = valid_results['profit_factor'].idxmax()
3304
+ elif optimization_metric == 'calmar':
3305
+ best_idx = valid_results['calmar'].idxmax()
3306
+ else:
3307
+ best_idx = valid_results['sharpe'].idxmax()
3308
+
3309
+ best_result = valid_results.loc[best_idx]
3310
+
3311
+ # Extract best parameters
3312
+ best_params = {name: best_result[name] for name in param_names}
3313
+
3314
+ # Add stop_loss_pct if it exists in config (it's handled separately in notebook)
3315
+ if 'stop_loss_config' in base_config and base_config['stop_loss_config']:
3316
+ stop_loss_value = base_config['stop_loss_config'].get('value')
3317
+ if stop_loss_value is not None:
3318
+ best_params['stop_loss_pct'] = stop_loss_value
3319
+
3320
+ # Calculate total time
3321
+ optimization_end_time = datetime.now()
3322
+ total_duration = optimization_end_time - optimization_start_time
3323
+ end_time_str = optimization_end_time.strftime('%Y-%m-%d %H:%M:%S')
3324
+ duration_str = format_time(total_duration.total_seconds())
3325
+
3326
+ # Print summary
3327
+ print("\n" + "="*120)
3328
+ print(" "*31 + "🏆 OPTIMIZATION COMPLETE 🏆")
3329
+ print(" "*31 + "=========================")
3330
+ print(f" • Started : {start_time_str}")
3331
+ print(f" • Finished : {end_time_str}")
3332
+ print(f" • Total Duration : {duration_str} ({int(total_duration.total_seconds())} seconds)")
3333
+ print(f" • Average per run : {total_duration.total_seconds() / total_combinations:.1f} seconds")
3334
+ print(f" • Total combinations : {total_combinations}")
3335
+ print(f" • Valid combinations : {len(valid_results)}")
3336
+ print(f" • Invalid combinations : {len(results_df) - len(valid_results)}")
3337
+
3338
+ print(f"\n📈 OPTIMIZATION METRIC:")
3339
+ print(f" • Metric optimized : {optimization_metric.upper()}")
3340
+
3341
+ # Format best parameters in one line (with special formatting for stop_loss_pct)
3342
+ param_parts = []
3343
+ for name, value in best_params.items():
3344
+ if name == 'stop_loss_pct':
3345
+ param_parts.append(f"stop_loss={value*100:.0f}%")
3346
+ else:
3347
+ param_parts.append(f"{name}={value}")
3348
+ param_str = ", ".join(param_parts)
3349
+ print(f" • Best parameters : {param_str}")
3350
+
3351
+ # Add intraday stop-loss info if enabled
3352
+ intraday_stops = base_config.get('intraday_stops', {})
3353
+ if intraday_stops.get('enabled', False):
3354
+ intraday_pct = intraday_stops.get('stop_pct', 0.03) * 100
3355
+ intraday_days = intraday_stops.get('min_days_before_intraday', 3)
3356
+ print(f" • Intraday stop-loss : Enabled ({intraday_pct:.0f}% after {intraday_days} days)")
3357
+
3358
+ print(f"\n🏆 BEST PERFORMANCE:")
3359
+ print(f" • Total Return : {best_result['total_return']:>10.2f}%")
3360
+ print(f" • Sharpe Ratio : {best_result['sharpe']:>10.2f}")
3361
+ print(f" • Max Drawdown : {best_result['max_drawdown']:>10.2f}%")
3362
+ print(f" • Win Rate : {best_result['win_rate']:>10.1f}%")
3363
+ print(f" • Profit Factor : {best_result['profit_factor']:>10.2f}")
3364
+ print(f" • Total Trades : {best_result['total_trades']:>10.0f}")
3365
+
3366
+ print(f"\n🔌 API ENDPOINTS:")
3367
+ # Extract real endpoints from preloaded data stats
3368
+ endpoints_info = []
3369
+
3370
+ if '_stats' in base_config and 'dataset_details' in base_config['_stats']:
3371
+ dataset_details = base_config['_stats']['dataset_details']
3372
+ for dataset_name, info in dataset_details.items():
3373
+ endpoint = info.get('endpoint')
3374
+ rows = info.get('rows', 0)
3375
+ if endpoint:
3376
+ endpoints_info.append((endpoint, rows))
3377
+
3378
+ # Check if intraday stops are enabled
3379
+ intraday_stops = base_config.get('intraday_stops', {})
3380
+ if intraday_stops.get('enabled', False):
3381
+ intraday_endpoint = "/equities/intraday/stock-prices"
3382
+ if not any(ep[0] == intraday_endpoint for ep in endpoints_info):
3383
+ endpoints_info.append((intraday_endpoint, "on-demand"))
3384
+
3385
+ if endpoints_info:
3386
+ for idx, (endpoint, rows) in enumerate(endpoints_info, 1):
3387
+ if isinstance(rows, int):
3388
+ print(f" {idx}. {endpoint:<45} ({rows:>10,} rows)")
3389
+ else:
3390
+ print(f" {idx}. {endpoint:<45} ({rows})")
3391
+ else:
3392
+ # Fallback to static list if no stats available
3393
+ print(f" 1. /equities/eod/options-rawiv")
3394
+ print(f" 2. /equities/eod/stock-prices")
3395
+ if intraday_stops.get('enabled', False):
3396
+ print(f" 3. /equities/intraday/stock-prices")
3397
+
3398
+ print("="*120)
3399
+
3400
+ # ═══════════════════════════════════════════════════════════════════════════
3401
+ # NEW! FULL BACKTEST OF BEST COMBINATION WITH ALL CHARTS
3402
+ # ═══════════════════════════════════════════════════════════════════════════
3403
+ print("\n" + "="*80)
3404
+ print(" "*15 + "RUNNING FULL BACKTEST FOR BEST COMBINATION")
3405
+ print("="*80)
3406
+ print("\n📊 Creating detailed report for best combination...")
3407
+ print(f"Parameters: {', '.join([f'{k}={v}' for k, v in best_params.items()])}\n")
3408
+
3409
+ # Create config for best combination
3410
+ best_config = base_config.copy()
3411
+ best_config.update(best_params)
3412
+ if use_legacy_preload:
3413
+ best_config['_preloaded_lean_df'] = preloaded_lean_df
3414
+ best_config['_preloaded_options_cache'] = preloaded_options_df
3415
+
3416
+ # Create folder for best combination
3417
+ best_combo_folder = os.path.join(results_folder, 'best_combination')
3418
+ os.makedirs(best_combo_folder, exist_ok=True)
3419
+
3420
+ # Run FULL backtest with ALL charts and exports
3421
+ # Note: progress_context=None, so plt.show() will be called but fail due to renderer
3422
+ # We'll display charts explicitly afterwards using IPython.display.Image
3423
+ best_analyzer = run_backtest(
3424
+ strategy_function,
3425
+ best_config,
3426
+ print_report=True, # ← SHOW FULL REPORT
3427
+ create_charts=True, # ← CREATE ALL CHARTS
3428
+ export_results=True, # ← EXPORT ALL FILES
3429
+ progress_context=None, # ← Normal mode
3430
+ chart_filename=os.path.join(best_combo_folder, 'equity_curve.png'),
3431
+ export_prefix=os.path.join(best_combo_folder, 'best')
3432
+ )
3433
+
3434
+ # Save detailed metrics to optimization_metrics.csv
3435
+ metrics_data = {
3436
+ 'metric': list(best_analyzer.metrics.keys()),
3437
+ 'value': list(best_analyzer.metrics.values())
3438
+ }
3439
+ metrics_df = pd.DataFrame(metrics_data)
3440
+ metrics_path = os.path.join(results_folder, 'optimization_metrics.csv')
3441
+ metrics_df.to_csv(metrics_path, index=False)
3442
+
3443
+ print(f"\n✓ Detailed metrics saved: {metrics_path}")
3444
+ print(f"✓ Best combination results saved to: {best_combo_folder}/")
3445
+
3446
+ # ═══════════════════════════════════════════════════════════════════════════
3447
+ # DISPLAY CHARTS FOR BEST COMBINATION IN NOTEBOOK
3448
+ # ═══════════════════════════════════════════════════════════════════════════
3449
+ try:
3450
+ # Charts are displayed in the notebook, not here
3451
+ chart_file = os.path.join(best_combo_folder, 'equity_curve.png')
3452
+ if os.path.exists(chart_file):
3453
+ print(f"\n📈 Best combination charts saved to: {chart_file}")
3454
+ except Exception as e:
3455
+ print(f"\n⚠ Could not display charts (saved to {best_combo_folder}/): {e}")
3456
+
3457
+ # ═══════════════════════════════════════════════════════════════════════════
3458
+ # CREATE OPTIMIZATION COMPARISON CHARTS (save only, display in notebook manually)
3459
+ # ═══════════════════════════════════════════════════════════════════════════
3460
+ print("\n" + "="*80)
3461
+ print(" "*15 + "CREATING OPTIMIZATION COMPARISON CHARTS")
3462
+ print("="*80)
3463
+ try:
3464
+ optimization_chart_path = os.path.join(results_folder, 'optimization_results.png')
3465
+ # Save chart but don't display (show_plot=False) - display will be done in notebook for combined results
3466
+ plot_optimization_results(
3467
+ results_df,
3468
+ param_names,
3469
+ filename=optimization_chart_path,
3470
+ show_plot=False # Don't display here - will be shown in notebook for combined results
3471
+ )
3472
+ print(f"✓ Optimization comparison charts saved to: {optimization_chart_path}")
3473
+ print(" (Chart will be displayed in notebook for combined results)")
3474
+ except Exception as e:
3475
+ print(f"⚠ Could not create optimization charts: {e}")
3476
+ import traceback
3477
+ traceback.print_exc()
3478
+
3479
+ print("="*80 + "\n")
3480
+
3481
+ return results_df, best_params, results_folder
3482
+
3483
+
3484
+ def plot_optimization_results(results_df, param_names, filename='optimization_results.png', show_plot=True):
3485
+ """
3486
+ Create visualization of optimization results
3487
+
3488
+ Args:
3489
+ results_df: Results DataFrame from optimize_parameters()
3490
+ param_names: List of parameter names
3491
+ filename: Output filename
3492
+ show_plot: If True, display plot in Jupyter notebook (default: True)
3493
+ """
3494
+ import matplotlib.pyplot as plt
3495
+ import seaborn as sns
3496
+
3497
+ # Handle missing is_valid column (for combined results from multiple optimizations)
3498
+ if 'is_valid' not in results_df.columns:
3499
+ results_df = results_df.copy()
3500
+ results_df['is_valid'] = True
3501
+
3502
+ valid_results = results_df[results_df['is_valid'] == True].copy()
3503
+
3504
+ if valid_results.empty:
3505
+ print("No valid results to plot")
3506
+ return
3507
+
3508
+ sns.set_style("whitegrid")
3509
+
3510
+ fig = plt.figure(figsize=(18, 12))
3511
+
3512
+ # 1. Sharpe vs Total Return scatter
3513
+ ax1 = plt.subplot(2, 3, 1)
3514
+ scatter = ax1.scatter(
3515
+ valid_results['total_return'],
3516
+ valid_results['sharpe'],
3517
+ c=valid_results['max_drawdown'],
3518
+ s=valid_results['total_trades']*10,
3519
+ alpha=0.6,
3520
+ cmap='RdYlGn_r'
3521
+ )
3522
+ ax1.set_xlabel('Total Return (%)', fontsize=10)
3523
+ ax1.set_ylabel('Sharpe Ratio', fontsize=10)
3524
+ ax1.set_title('Sharpe vs Return (size=trades, color=drawdown)', fontsize=11, fontweight='bold')
3525
+ plt.colorbar(scatter, ax=ax1, label='Max Drawdown (%)')
3526
+ ax1.grid(True, alpha=0.3)
3527
+
3528
+ # 2. Parameter heatmap (if 2 parameters)
3529
+ if len(param_names) == 2:
3530
+ ax2 = plt.subplot(2, 3, 2)
3531
+ pivot_data = valid_results.pivot_table(
3532
+ values='sharpe',
3533
+ index=param_names[0],
3534
+ columns=param_names[1],
3535
+ aggfunc='mean'
3536
+ )
3537
+ sns.heatmap(pivot_data, annot=True, fmt='.2f', cmap='RdYlGn', ax=ax2)
3538
+ ax2.set_title(f'Sharpe Ratio Heatmap', fontsize=11, fontweight='bold')
3539
+ else:
3540
+ ax2 = plt.subplot(2, 3, 2)
3541
+ ax2.text(0.5, 0.5, 'Heatmap requires\nexactly 2 parameters',
3542
+ ha='center', va='center', fontsize=12)
3543
+ ax2.axis('off')
3544
+
3545
+ # 3. Win Rate vs Profit Factor
3546
+ ax3 = plt.subplot(2, 3, 3)
3547
+ scatter3 = ax3.scatter(
3548
+ valid_results['win_rate'],
3549
+ valid_results['profit_factor'],
3550
+ c=valid_results['sharpe'],
3551
+ s=100,
3552
+ alpha=0.6,
3553
+ cmap='viridis'
3554
+ )
3555
+ ax3.set_xlabel('Win Rate (%)', fontsize=10)
3556
+ ax3.set_ylabel('Profit Factor', fontsize=10)
3557
+ ax3.set_title('Win Rate vs Profit Factor (color=Sharpe)', fontsize=11, fontweight='bold')
3558
+ plt.colorbar(scatter3, ax=ax3, label='Sharpe Ratio')
3559
+ ax3.grid(True, alpha=0.3)
3560
+
3561
+ # 4. Distribution of Sharpe Ratios
3562
+ ax4 = plt.subplot(2, 3, 4)
3563
+ ax4.hist(valid_results['sharpe'], bins=20, color='steelblue', alpha=0.7, edgecolor='black')
3564
+ ax4.axvline(valid_results['sharpe'].mean(), color='red', linestyle='--', linewidth=2, label='Mean')
3565
+ ax4.axvline(valid_results['sharpe'].median(), color='green', linestyle='--', linewidth=2, label='Median')
3566
+ ax4.set_xlabel('Sharpe Ratio', fontsize=10)
3567
+ ax4.set_ylabel('Frequency', fontsize=10)
3568
+ ax4.set_title('Distribution of Sharpe Ratios', fontsize=11, fontweight='bold')
3569
+ ax4.legend()
3570
+ ax4.grid(True, alpha=0.3, axis='y')
3571
+
3572
+ # 5. Total Trades distribution
3573
+ ax5 = plt.subplot(2, 3, 5)
3574
+ ax5.hist(valid_results['total_trades'], bins=15, color='coral', alpha=0.7, edgecolor='black')
3575
+ ax5.set_xlabel('Total Trades', fontsize=10)
3576
+ ax5.set_ylabel('Frequency', fontsize=10)
3577
+ ax5.set_title('Distribution of Trade Counts', fontsize=11, fontweight='bold')
3578
+ ax5.grid(True, alpha=0.3, axis='y')
3579
+
3580
+ # 6. Top 10 combinations
3581
+ ax6 = plt.subplot(2, 3, 6)
3582
+ if 'combination_id' in valid_results.columns:
3583
+ top_10 = valid_results.nlargest(10, 'sharpe')[['combination_id', 'sharpe']].sort_values('sharpe')
3584
+ ax6.barh(range(len(top_10)), top_10['sharpe'], color='green', alpha=0.7)
3585
+ ax6.set_yticks(range(len(top_10)))
3586
+ ax6.set_yticklabels([f"#{int(x)}" for x in top_10['combination_id']])
3587
+ ax6.set_xlabel('Sharpe Ratio', fontsize=10)
3588
+ ax6.set_title('Top 10 Combinations by Sharpe', fontsize=11, fontweight='bold')
3589
+ else:
3590
+ # Fallback: use index as combination ID
3591
+ top_10 = valid_results.nlargest(10, 'sharpe')['sharpe'].sort_values()
3592
+ ax6.barh(range(len(top_10)), top_10.values, color='green', alpha=0.7)
3593
+ ax6.set_yticks(range(len(top_10)))
3594
+ ax6.set_yticklabels([f"#{i+1}" for i in range(len(top_10))])
3595
+ ax6.set_xlabel('Sharpe Ratio', fontsize=10)
3596
+ ax6.set_title('Top 10 Combinations by Sharpe', fontsize=11, fontweight='bold')
3597
+ ax6.grid(True, alpha=0.3, axis='x')
3598
+
3599
+ plt.tight_layout()
3600
+ plt.savefig(filename, dpi=150, bbox_inches='tight')
3601
+ print(f"\nVisualization saved: {filename}")
3602
+
3603
+ # Display plot if requested
3604
+ if show_plot:
3605
+ try:
3606
+ # First try to use IPython.display.Image (most reliable in Jupyter)
3607
+ from IPython.display import display, Image
3608
+ import os
3609
+ if os.path.exists(filename):
3610
+ display(Image(filename))
3611
+ else:
3612
+ # If file doesn't exist yet, try plt.show()
3613
+ plt.show()
3614
+ except (ImportError, NameError):
3615
+ # Not in Jupyter or IPython not available - try plt.show()
3616
+ try:
3617
+ plt.show()
3618
+ except:
3619
+ plt.close()
3620
+ except Exception:
3621
+ # Any other error - try plt.show() as fallback
3622
+ try:
3623
+ plt.show()
3624
+ except:
3625
+ plt.close()
3626
+ else:
3627
+ plt.close() # Close without displaying
3628
+
3629
+
3630
+ # ============================================================
3631
+ # CACHE CONFIGURATION (integrated from universal_backend_system.py)
3632
+ # ============================================================
3633
+ def get_cache_config(disk_enabled: bool = True, memory_enabled: bool = True,
3634
+ memory_percent: int = 10, max_age_days: int = 7,
3635
+ debug: bool = False, cache_dir: str = 'cache',
3636
+ compression: bool = True, auto_cleanup: bool = True) -> Dict[str, Any]:
3637
+ """
3638
+ Get cache configuration
3639
+
3640
+ Args:
3641
+ disk_enabled: Enable disk cache
3642
+ memory_enabled: Enable memory cache
3643
+ memory_percent: RAM percentage for cache (default 10%)
3644
+ max_age_days: Maximum cache age in days
3645
+ debug: Debug mode
3646
+ cache_dir: Cache directory
3647
+ compression: Use compression (Parquet + Snappy)
3648
+ auto_cleanup: Automatic cleanup of old cache
3649
+
3650
+ Returns:
3651
+ Dict with cache configuration
3652
+ """
3653
+ return {
3654
+ 'disk_enabled': disk_enabled,
3655
+ 'memory_enabled': memory_enabled,
3656
+ 'memory_percent': memory_percent,
3657
+ 'max_age_days': max_age_days,
3658
+ 'debug': debug,
3659
+ 'cache_dir': cache_dir,
3660
+ 'compression': compression,
3661
+ 'auto_cleanup': auto_cleanup
3662
+ }
3663
+
3664
+
3665
+ # ============================================================
3666
+ # UNIVERSAL CACHE MANAGER (integrated from universal_backend_system.py)
3667
+ # ============================================================
3668
+ class UniversalCacheManager:
3669
+ """Universal cache manager for any data types"""
3670
+
3671
+ # Mapping data types to cache directories
3672
+ DATA_TYPE_MAP = {
3673
+ 'stock_eod': 'STOCK_EOD',
3674
+ 'stock_intraday': 'STOCK_INTRADAY',
3675
+ 'options_eod': 'OPTIONS_EOD',
3676
+ 'options_intraday': 'OPTIONS_INTRADAY',
3677
+ # Backward compatibility (old naming):
3678
+ 'stock': 'STOCK_EOD',
3679
+ 'options': 'OPTIONS_EOD',
3680
+ 'intraday': 'OPTIONS_INTRADAY', # Default intraday = options
3681
+ }
3682
+
3683
+ def __init__(self, cache_config: Dict[str, Any]):
3684
+ self.cache_config = cache_config
3685
+ self.disk_enabled = cache_config.get('disk_enabled', True)
3686
+ self.memory_enabled = cache_config.get('memory_enabled', True)
3687
+ self.memory_percent = cache_config.get('memory_percent', 10)
3688
+ self.max_age_days = cache_config.get('max_age_days', 7)
3689
+ self.debug = cache_config.get('debug', False)
3690
+ self.cache_dir = cache_config.get('cache_dir', 'cache')
3691
+ self.compression = cache_config.get('compression', True)
3692
+ self.auto_cleanup = cache_config.get('auto_cleanup', True)
3693
+
3694
+ # Calculate cache size in RAM
3695
+ if self.memory_enabled:
3696
+ total_memory = psutil.virtual_memory().total
3697
+ self.max_memory_bytes = int(total_memory * self.memory_percent / 100)
3698
+ self.memory_cache = {}
3699
+ self.cache_order = []
3700
+ else:
3701
+ self.max_memory_bytes = 0
3702
+ self.memory_cache = {}
3703
+ self.cache_order = []
3704
+
3705
+ # Create cache directories
3706
+ if self.disk_enabled:
3707
+ os.makedirs(self.cache_dir, exist_ok=True)
3708
+
3709
+ def get(self, key: str, data_type: str = 'default') -> Optional[Any]:
3710
+ """Get data from cache"""
3711
+ try:
3712
+ # Check memory
3713
+ if self.memory_enabled and key in self.memory_cache:
3714
+ if self.debug:
3715
+ print(f"[CACHE] 🧠 Memory hit: {key}")
3716
+ return self.memory_cache[key]
3717
+
3718
+ # Check disk
3719
+ if self.disk_enabled:
3720
+ # Map data_type to proper directory structure using DATA_TYPE_MAP
3721
+ dir_name = self.DATA_TYPE_MAP.get(data_type, data_type.upper())
3722
+ data_dir = f"{self.cache_dir}/{dir_name}"
3723
+
3724
+ cache_file = os.path.join(data_dir, f"{key}.parquet")
3725
+ if os.path.exists(cache_file):
3726
+ if self._is_cache_valid(cache_file):
3727
+ data = self._load_from_disk(cache_file)
3728
+ if data is not None:
3729
+ # Save to memory
3730
+ if self.memory_enabled:
3731
+ self._save_to_memory(key, data)
3732
+ if self.debug:
3733
+ print(f"[CACHE] 💾 Disk hit: {key}")
3734
+ return data
3735
+
3736
+ # NEW: If exact match not found, search for overlapping cache
3737
+ # Only for date-range based cache types
3738
+ if data_type in ['stock_eod', 'options_eod', 'stock_intraday', 'options_intraday']:
3739
+ overlapping_data = self._find_overlapping_cache(key, data_type, data_dir)
3740
+ if overlapping_data is not None:
3741
+ # Save to memory for fast access
3742
+ if self.memory_enabled:
3743
+ self._save_to_memory(key, overlapping_data)
3744
+ return overlapping_data
3745
+
3746
+ if self.debug:
3747
+ print(f"[CACHE] ❌ Cache miss: {key}")
3748
+ return None
3749
+
3750
+ except Exception as e:
3751
+ if self.debug:
3752
+ print(f"[CACHE] ❌ Error getting {key}: {e}")
3753
+ return None
3754
+
3755
+ def set(self, key: str, data: Any, data_type: str = 'default') -> bool:
3756
+ """Save data to cache"""
3757
+ try:
3758
+ # Save to memory
3759
+ if self.memory_enabled:
3760
+ self._save_to_memory(key, data)
3761
+
3762
+ # Save to disk
3763
+ if self.disk_enabled:
3764
+ # Map data_type to proper directory structure using DATA_TYPE_MAP
3765
+ dir_name = self.DATA_TYPE_MAP.get(data_type, data_type.upper())
3766
+ data_dir = f"{self.cache_dir}/{dir_name}"
3767
+
3768
+ # Create directory if it doesn't exist
3769
+ os.makedirs(data_dir, exist_ok=True)
3770
+
3771
+ cache_file = os.path.join(data_dir, f"{key}.parquet")
3772
+ self._save_to_disk(cache_file, data)
3773
+
3774
+ if self.debug:
3775
+ # Count records for reporting
3776
+ record_count = len(data) if hasattr(data, '__len__') else '?'
3777
+ print(f"[CACHE] 💾 Saved: {key}")
3778
+ print(f"[CACHE] 💾 Saved to cache: {data_type.upper()} ({record_count} records)")
3779
+ return True
3780
+
3781
+ except Exception as e:
3782
+ if self.debug:
3783
+ print(f"[CACHE] ❌ Error saving {key}: {e}")
3784
+ return False
3785
+
3786
+ def _save_to_memory(self, key: str, data: Any):
3787
+ """Save to memory with LRU logic"""
3788
+ if key in self.memory_cache:
3789
+ self.cache_order.remove(key)
3790
+ else:
3791
+ # Check cache size
3792
+ while len(self.memory_cache) > 0 and self._get_memory_usage() > self.max_memory_bytes:
3793
+ oldest_key = self.cache_order.pop(0)
3794
+ del self.memory_cache[oldest_key]
3795
+
3796
+ self.memory_cache[key] = data
3797
+ self.cache_order.append(key)
3798
+
3799
+ def _save_to_disk(self, file_path: str, data: Any):
3800
+ """Save to disk"""
3801
+ try:
3802
+ # Ensure directory exists
3803
+ file_dir = os.path.dirname(file_path)
3804
+ if file_dir and not os.path.exists(file_dir):
3805
+ os.makedirs(file_dir, exist_ok=True)
3806
+
3807
+ if isinstance(data, pd.DataFrame):
3808
+ if self.compression:
3809
+ data.to_parquet(file_path, compression='snappy')
3810
+ else:
3811
+ data.to_parquet(file_path)
3812
+ elif isinstance(data, dict):
3813
+ # Convert dict to DataFrame
3814
+ df = pd.DataFrame([data])
3815
+ if self.compression:
3816
+ df.to_parquet(file_path, compression='snappy')
3817
+ else:
3818
+ df.to_parquet(file_path)
3819
+ else:
3820
+ # Try to convert to DataFrame
3821
+ df = pd.DataFrame(data)
3822
+ if self.compression:
3823
+ df.to_parquet(file_path, compression='snappy')
3824
+ else:
3825
+ df.to_parquet(file_path)
3826
+ except Exception as e:
3827
+ if self.debug:
3828
+ print(f"[CACHE] ❌ Error saving to disk: {e}")
3829
+
3830
+ def _load_from_disk(self, file_path: str) -> Optional[Any]:
3831
+ """Load from disk"""
3832
+ try:
3833
+ return pd.read_parquet(file_path)
3834
+ except Exception as e:
3835
+ if self.debug:
3836
+ print(f"[CACHE] ❌ Error loading from disk: {e}")
3837
+ return None
3838
+
3839
+ def _is_cache_valid(self, file_path: str) -> bool:
3840
+ """Check cache validity"""
3841
+ if not os.path.exists(file_path):
3842
+ return False
3843
+
3844
+ file_age = time.time() - os.path.getmtime(file_path)
3845
+ max_age_seconds = self.max_age_days * 24 * 3600
3846
+
3847
+ return file_age < max_age_seconds
3848
+
3849
+ def _get_memory_usage(self) -> int:
3850
+ """Get memory usage"""
3851
+ total_size = 0
3852
+ for key, value in self.memory_cache.items():
3853
+ try:
3854
+ if hasattr(value, 'memory_usage'):
3855
+ total_size += value.memory_usage(deep=True).sum()
3856
+ else:
3857
+ total_size += sys.getsizeof(value)
3858
+ except:
3859
+ total_size += sys.getsizeof(value)
3860
+ return total_size
3861
+
3862
+ def _find_overlapping_cache(self, key: str, data_type: str, data_dir: str) -> Optional[Any]:
3863
+ """
3864
+ Find cache files with overlapping date ranges
3865
+
3866
+ Args:
3867
+ key: Cache key (format: SYMBOL_START_END or SYMBOL_DATE)
3868
+ data_type: Data type (stock_eod, options_eod, etc.)
3869
+ data_dir: Cache directory
3870
+
3871
+ Returns:
3872
+ Filtered data if overlapping cache found, None otherwise
3873
+ """
3874
+ try:
3875
+ import re
3876
+ import glob
3877
+ from datetime import datetime
3878
+
3879
+ # Parse symbol and dates from key
3880
+ # Format: "SPY_2024-07-01_2025-10-29" or "SPY_2024-07-01"
3881
+ match = re.search(r'^([A-Z]+)_(\d{4}-\d{2}-\d{2})(?:_(\d{4}-\d{2}-\d{2}))?$', key)
3882
+ if not match:
3883
+ return None
3884
+
3885
+ symbol = match.group(1)
3886
+ start_date_str = match.group(2)
3887
+ end_date_str = match.group(3) if match.group(3) else start_date_str
3888
+
3889
+ # Parse dates
3890
+ start_date = datetime.strptime(start_date_str, '%Y-%m-%d').date()
3891
+ end_date = datetime.strptime(end_date_str, '%Y-%m-%d').date()
3892
+
3893
+ # Find all cache files for this symbol
3894
+ if not os.path.exists(data_dir):
3895
+ return None
3896
+
3897
+ pattern = os.path.join(data_dir, f"{symbol}_*.parquet")
3898
+ cache_files = glob.glob(pattern)
3899
+
3900
+ if not cache_files:
3901
+ return None
3902
+
3903
+ # Search for best overlapping cache
3904
+ best_match = None
3905
+ best_size = float('inf') # Prefer smallest file that covers range
3906
+
3907
+ for cache_file in cache_files:
3908
+ # Skip if cache is not valid
3909
+ if not self._is_cache_valid(cache_file):
3910
+ continue
3911
+
3912
+ # Parse dates from filename
3913
+ filename = os.path.basename(cache_file)
3914
+ file_match = re.search(r'(\d{4}-\d{2}-\d{2})(?:_(\d{4}-\d{2}-\d{2}))?', filename)
3915
+
3916
+ if not file_match:
3917
+ continue
3918
+
3919
+ cached_start_str = file_match.group(1)
3920
+ cached_end_str = file_match.group(2) if file_match.group(2) else cached_start_str
3921
+
3922
+ cached_start = datetime.strptime(cached_start_str, '%Y-%m-%d').date()
3923
+ cached_end = datetime.strptime(cached_end_str, '%Y-%m-%d').date()
3924
+
3925
+ # Check if cached range CONTAINS requested range
3926
+ if cached_start <= start_date and cached_end >= end_date:
3927
+ # Calculate file size (prefer smaller files)
3928
+ file_size = os.path.getsize(cache_file)
3929
+
3930
+ if file_size < best_size:
3931
+ best_match = cache_file
3932
+ best_size = file_size
3933
+
3934
+ if best_match:
3935
+ if self.debug:
3936
+ print(f"[CACHE] 🔍 Found overlapping cache: {os.path.basename(best_match)}")
3937
+ print(f"[CACHE] Requested: {start_date_str} → {end_date_str}")
3938
+ print(f"[CACHE] Filtering and loading...")
3939
+
3940
+ # Load and filter data
3941
+ df = pd.read_parquet(best_match)
3942
+
3943
+ # Ensure date column is in correct format
3944
+ if 'date' in df.columns:
3945
+ if df['date'].dtype == 'object':
3946
+ df['date'] = pd.to_datetime(df['date']).dt.date
3947
+ elif pd.api.types.is_datetime64_any_dtype(df['date']):
3948
+ df['date'] = df['date'].dt.date
3949
+
3950
+ # Filter by date range
3951
+ filtered = df[(df['date'] >= start_date) & (df['date'] <= end_date)].copy()
3952
+
3953
+ if self.debug:
3954
+ print(f"[CACHE] ✓ Overlapping cache hit: {len(filtered)} records (filtered from {len(df)})")
3955
+
3956
+ return filtered
3957
+ else:
3958
+ # No date column to filter - return as is
3959
+ if self.debug:
3960
+ print(f"[CACHE] ✓ Overlapping cache hit: {len(df)} records (no date filtering)")
3961
+ return df
3962
+
3963
+ return None
3964
+
3965
+ except Exception as e:
3966
+ if self.debug:
3967
+ print(f"[CACHE] ⚠️ Error searching for overlapping cache: {e}")
3968
+ return None
3969
+
3970
+
3971
+ # Export all
3972
+ __all__ = [
3973
+ 'BacktestResults', 'BacktestAnalyzer', 'ResultsReporter',
3974
+ 'ChartGenerator', 'ResultsExporter', 'run_backtest', 'run_backtest_with_stoploss',
3975
+ 'init_api', 'api_call', 'APIHelper', 'APIManager',
3976
+ 'ResourceMonitor', 'create_progress_bar', 'update_progress', 'format_time',
3977
+ 'StopLossManager', 'PositionManager', 'StopLossConfig',
3978
+ 'calculate_stoploss_metrics', 'print_stoploss_section', 'create_stoploss_charts',
3979
+ 'create_stoploss_comparison_chart',
3980
+ 'optimize_parameters', 'plot_optimization_results',
3981
+ 'create_optimization_folder',
3982
+ 'preload_options_data',
3983
+ 'preload_data_universal', # NEW: Universal preloader V2
3984
+ # New caching functions
3985
+ # Optimization preset functions
3986
+ 'apply_optimization_preset', 'list_optimization_presets',
3987
+ 'calculate_combinations_count', 'print_preset_info',
3988
+ 'get_cache_config', 'UniversalCacheManager'
3989
+ ]
3990
+
3991
+
3992
+ # ============================================================
3993
+ # OPTIMIZATION PRESET FUNCTIONS
3994
+ # ============================================================
3995
+
3996
+ def apply_optimization_preset(config, preset='default'):
3997
+ """
3998
+ Apply built-in optimization preset to config
3999
+
4000
+ Args:
4001
+ config: Configuration dictionary (will be updated)
4002
+ preset: Preset name ('default', 'quick_test', 'aggressive', 'conservative')
4003
+
4004
+ Returns:
4005
+ dict: Updated configuration
4006
+ """
4007
+ presets = {
4008
+ 'default': {
4009
+ 'param_grid': {
4010
+ 'z_score_entry': [0.8, 1.0, 1.2, 1.5],
4011
+ 'z_score_exit': [0.05, 0.1, 0.15],
4012
+ 'lookback_period': [45, 60, 90],
4013
+ 'dte_target': [30, 45, 60]
4014
+ },
4015
+ 'optimization_metric': 'sharpe',
4016
+ 'min_trades': 5,
4017
+ 'max_drawdown_limit': 0.50,
4018
+ 'parallel': False,
4019
+ # 'export_each_combo': True, # ← Убрано, будет использоваться из основного конфига
4020
+ 'results_folder_prefix': 'optimization',
4021
+ 'chart_filename': 'optimization_analysis.png',
4022
+ 'show_progress': True,
4023
+ 'verbose': True
4024
+ },
4025
+ 'quick_test': {
4026
+ 'param_grid': {
4027
+ 'z_score_entry': [1.0, 1.5],
4028
+ 'z_score_exit': [0.1],
4029
+ 'lookback_period': [60],
4030
+ 'dte_target': [45]
4031
+ },
4032
+ 'optimization_metric': 'sharpe',
4033
+ 'min_trades': 3,
4034
+ 'max_drawdown_limit': 0.40,
4035
+ 'parallel': False,
4036
+ # 'export_each_combo': False, # ← Убрано, будет использоваться из основного конфига
4037
+ 'results_folder_prefix': 'quick_test',
4038
+ 'chart_filename': 'quick_test_analysis.png',
4039
+ 'show_progress': True,
4040
+ 'verbose': False
4041
+ },
4042
+ 'aggressive': {
4043
+ 'param_grid': {
4044
+ 'z_score_entry': [1.5, 2.0, 2.5],
4045
+ 'z_score_exit': [0.05, 0.1],
4046
+ 'lookback_period': [30, 45, 60],
4047
+ 'dte_target': [30, 45]
4048
+ },
4049
+ 'optimization_metric': 'total_return',
4050
+ 'min_trades': 10,
4051
+ 'max_drawdown_limit': 0.60,
4052
+ 'parallel': False,
4053
+ # 'export_each_combo': True, # ← Убрано, будет использоваться из основного конфига
4054
+ 'results_folder_prefix': 'aggressive',
4055
+ 'chart_filename': 'aggressive_analysis.png',
4056
+ 'show_progress': True,
4057
+ 'verbose': True
4058
+ },
4059
+ 'conservative': {
4060
+ 'param_grid': {
4061
+ 'z_score_entry': [0.8, 1.0],
4062
+ 'z_score_exit': [0.1, 0.15, 0.2],
4063
+ 'lookback_period': [60, 90, 120],
4064
+ 'dte_target': [45, 60, 90]
4065
+ },
4066
+ 'optimization_metric': 'calmar',
4067
+ 'min_trades': 8,
4068
+ 'max_drawdown_limit': 0.25,
4069
+ 'parallel': False,
4070
+ # 'export_each_combo': True, # ← Убрано, будет использоваться из основного конфига
4071
+ 'results_folder_prefix': 'conservative',
4072
+ 'chart_filename': 'conservative_analysis.png',
4073
+ 'show_progress': True,
4074
+ 'verbose': True
4075
+ }
4076
+ }
4077
+
4078
+ if preset not in presets:
4079
+ available = list(presets.keys())
4080
+ raise ValueError(f"Preset '{preset}' not found. Available: {available}")
4081
+
4082
+ # Update only specific fields from preset
4083
+ preset_data = presets[preset]
4084
+
4085
+ # CRITICAL LOGIC:
4086
+ # - If preset == 'default' → use param_grid from config (if exists)
4087
+ # - If preset != 'default' → use param_grid from preset (override config)
4088
+ user_param_grid = config.get('param_grid')
4089
+
4090
+ fields_to_update = [
4091
+ 'param_grid', 'min_trades', 'max_drawdown_limit',
4092
+ 'optimization_metric', 'parallel', 'export_each_combo',
4093
+ 'results_folder_prefix', 'chart_filename',
4094
+ 'show_progress', 'verbose'
4095
+ ]
4096
+
4097
+ for field in fields_to_update:
4098
+ if field in preset_data:
4099
+ # Special handling for param_grid based on preset type
4100
+ if field == 'param_grid':
4101
+ if preset == 'default' and user_param_grid is not None:
4102
+ # 'default' preset → preserve user's param_grid
4103
+ continue
4104
+ else:
4105
+ # Non-default preset (quick_test, aggressive, etc.) → use preset's param_grid
4106
+ config[field] = preset_data[field]
4107
+ else:
4108
+ config[field] = preset_data[field]
4109
+
4110
+ print(f"✓ Applied preset: {preset}")
4111
+ if preset == 'default' and user_param_grid is not None:
4112
+ print(f" (Using user-defined param_grid from config)")
4113
+ elif preset != 'default':
4114
+ print(f" (Using param_grid from preset, ignoring config)")
4115
+
4116
+ return config
4117
+
4118
+
4119
+ def calculate_combinations_count(param_grid):
4120
+ """
4121
+ Calculate total number of parameter combinations
4122
+
4123
+ Args:
4124
+ param_grid: Dictionary with parameter lists
4125
+
4126
+ Returns:
4127
+ int: Total number of combinations
4128
+ """
4129
+ import math
4130
+ return math.prod(len(values) for values in param_grid.values())
4131
+
4132
+
4133
+ def print_preset_info(config):
4134
+ """
4135
+ Print preset information and combination count
4136
+
4137
+ Args:
4138
+ config: Configuration dictionary with preset applied
4139
+ """
4140
+ preset = config.get('preset', 'unknown')
4141
+ combinations = calculate_combinations_count(config['param_grid'])
4142
+
4143
+ print(f"\n{'='*60}")
4144
+ print(f"OPTIMIZATION PRESET: {preset.upper()}")
4145
+ print(f"{'='*60}")
4146
+ print(f"Total combinations: {combinations}")
4147
+ print(f"Optimization metric: {config.get('optimization_metric', 'sharpe')}")
4148
+ print(f"Min trades required: {config.get('min_trades', 10)}")
4149
+ print(f"Max drawdown limit: {config.get('max_drawdown_limit', 0.50)}")
4150
+ print(f"Parallel execution: {config.get('parallel', True)}")
4151
+ print(f"Export each combo: {config.get('export_each_combo', False)}")
4152
+ print(f"{'='*60}\n")
4153
+
4154
+
4155
+ def list_optimization_presets():
4156
+ """Show available built-in presets"""
4157
+ presets = {
4158
+ 'default': 'Standard configuration (4×3×3×3 = 108 combinations)',
4159
+ 'quick_test': 'Quick test (2×1×1×1 = 2 combinations)',
4160
+ 'aggressive': 'Aggressive strategy (3×2×3×2 = 36 combinations)',
4161
+ 'conservative': 'Conservative strategy (2×3×3×3 = 54 combinations)'
4162
+ }
4163
+
4164
+ print("\n📋 AVAILABLE OPTIMIZATION PRESETS:")
4165
+ print("-" * 60)
4166
+ for name, desc in presets.items():
4167
+ print(f" {name:<12} | {desc}")
4168
+ print("-" * 60)
4169
+
4170
+
4171
+
4172
+
4173
+
4174
+
4175
+