From 7044c19a60f7602da032b521d28949985e84de98 Mon Sep 17 00:00:00 2001 From: cbazza Date: Tue, 27 Jan 2026 12:35:24 +0100 Subject: [PATCH] fix: Handle empty stats in check_go_live_readiness Added default values using .get() for all stats fields to prevent KeyError when no trades have been logged yet. Co-Authored-By: Claude Opus 4.5 --- demo_test_tracker.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/demo_test_tracker.py b/demo_test_tracker.py index 1536772..4f6a7df 100644 --- a/demo_test_tracker.py +++ b/demo_test_tracker.py @@ -358,24 +358,31 @@ class DemoTestTracker: checks = {} + # Get values with defaults for when no trades exist yet + total_trades = stats.get('total_trades', 0) + win_rate = stats.get('win_rate', 0) + profit_factor = stats.get('profit_factor', 0) + max_drawdown = stats.get('max_drawdown', 0) + sessions_tested = stats.get('sessions_tested', []) + # 1. Minimum Trades checks['min_trades'] = { - 'passed': stats['total_trades'] >= self.criteria['min_trades'], - 'current': stats['total_trades'], + 'passed': total_trades >= self.criteria['min_trades'], + 'current': total_trades, 'required': self.criteria['min_trades'], - 'label': f"Trades: {stats['total_trades']}/{self.criteria['min_trades']}" + 'label': f"Trades: {total_trades}/{self.criteria['min_trades']}" } # 2. Win Rate checks['win_rate'] = { - 'passed': stats['win_rate'] >= self.criteria['min_win_rate'], - 'current': stats['win_rate'], + 'passed': win_rate >= self.criteria['min_win_rate'], + 'current': win_rate, 'required': self.criteria['min_win_rate'], - 'label': f"Win Rate: {stats['win_rate']*100:.1f}% (min {self.criteria['min_win_rate']*100:.0f}%)" + 'label': f"Win Rate: {win_rate*100:.1f}% (min {self.criteria['min_win_rate']*100:.0f}%)" } # 3. Profit Factor - pf = stats['profit_factor'] if stats['profit_factor'] != float('inf') else 999 + pf = profit_factor if profit_factor != float('inf') else 999 checks['profit_factor'] = { 'passed': pf >= self.criteria['min_profit_factor'], 'current': pf, @@ -385,10 +392,10 @@ class DemoTestTracker: # 4. Max Drawdown checks['max_drawdown'] = { - 'passed': stats['max_drawdown'] <= self.criteria['max_drawdown'], - 'current': stats['max_drawdown'], + 'passed': max_drawdown <= self.criteria['max_drawdown'], + 'current': max_drawdown, 'required': self.criteria['max_drawdown'], - 'label': f"Max Drawdown: {stats['max_drawdown']*100:.1f}% (max {self.criteria['max_drawdown']*100:.0f}%)" + 'label': f"Max Drawdown: {max_drawdown*100:.1f}% (max {self.criteria['max_drawdown']*100:.0f}%)" } # 5. Days Running