ARKAISW commited on
Commit
d4d6ac3
·
1 Parent(s): 42a3a92

Fix: Hardened simulation engine with error safety checks and data validation

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -129,7 +129,7 @@ def build_main_chart(ticks_data):
129
 
130
  # Layout
131
  # Add minimal range to avoid the "zoomed in on noise" look
132
- prices_only = [t["price"] for t in ticks_data]
133
  min_p, max_p = min(prices_only), max(prices_only)
134
  if max_p - min_p < 1.0:
135
  center = (max_p + min_p) / 2
@@ -334,36 +334,44 @@ def run_simulation(n_mom, n_mr, n_fund, n_noise, n_mm,
334
  api_key=hf_token,
335
  )
336
 
337
- t0 = time.time()
338
-
339
- # Ensure output directory exists for CSV generation
340
- os.makedirs(config.output_dir, exist_ok=True)
341
-
342
- # Run the simulation fully without yielding intermediate plots to prevent UI flickering
343
- print("DEBUG: Executing simulation loop...")
344
- for tick in engine.run_generator():
345
- progress(tick / int(num_ticks), desc=f"Simulating market dynamics... {tick}/{num_ticks}")
346
-
347
- print(f"DEBUG: Simulation complete in {time.time()-t0:.2f}s")
348
-
349
- # Build final results
350
- ticks_data = engine.csv_rows
351
- pnl_data = engine.agent_pnl_rows
352
-
353
- main_chart = build_main_chart(ticks_data)
354
- pnl_chart = build_pnl_chart(pnl_data, agents)
355
- leaderboard = build_leaderboard(pnl_data, ticks_data)
356
- stats_html = build_stats_html(ticks_data, pnl_data, time.time() - t0)
357
-
358
- # Create temporary export file
359
- export_path = "marketmind_simulation.csv"
360
- pd.DataFrame(ticks_data).to_csv(export_path, index=False)
361
-
362
- # After simulation is done, write CSVs
363
- engine._write_csvs()
364
-
365
- # Final yield
366
- yield main_chart, pnl_chart, leaderboard, stats_html, export_path
 
 
 
 
 
 
 
 
367
 
368
 
369
  # ─── CUSTOM CSS ───────────────────────────────────────────────────
 
129
 
130
  # Layout
131
  # Add minimal range to avoid the "zoomed in on noise" look
132
+ prices_only = [t["price"] for t in ticks_data] if ticks_data else [100.0]
133
  min_p, max_p = min(prices_only), max(prices_only)
134
  if max_p - min_p < 1.0:
135
  center = (max_p + min_p) / 2
 
334
  api_key=hf_token,
335
  )
336
 
337
+ try:
338
+ t0 = time.time()
339
+
340
+ # Ensure output directory exists for CSV generation
341
+ os.makedirs(config.output_dir, exist_ok=True)
342
+
343
+ # Run the simulation fully without yielding intermediate plots to prevent UI flickering
344
+ print("DEBUG: Executing simulation loop...")
345
+ for tick in engine.run_generator():
346
+ progress(tick / int(num_ticks), desc=f"Simulating market dynamics... {tick}/{num_ticks}")
347
+
348
+ print(f"DEBUG: Simulation complete in {time.time()-t0:.2f}s")
349
+
350
+ # Build final results
351
+ ticks_data = engine.csv_rows
352
+ pnl_data = engine.agent_pnl_rows
353
+
354
+ if not ticks_data:
355
+ raise ValueError("No data produced during simulation.")
356
+
357
+ main_chart = build_main_chart(ticks_data)
358
+ pnl_chart = build_pnl_chart(pnl_data, agents)
359
+ leaderboard = build_leaderboard(pnl_data, ticks_data)
360
+ stats_html = build_stats_html(ticks_data, pnl_data, time.time() - t0)
361
+
362
+ # Create temporary export file
363
+ export_path = "marketmind_simulation.csv"
364
+ pd.DataFrame(ticks_data).to_csv(export_path, index=False)
365
+
366
+ # After simulation is done, write CSVs
367
+ engine._write_csvs()
368
+
369
+ yield main_chart, pnl_chart, leaderboard, stats_html, export_path
370
+ except Exception as e:
371
+ print(f"CRITICAL ERROR in run_simulation: {str(e)}")
372
+ import traceback
373
+ traceback.print_exc()
374
+ raise gr.Error(f"Simulation Failed: {str(e)}")
375
 
376
 
377
  # ─── CUSTOM CSS ───────────────────────────────────────────────────