#!/usr/bin/env python3 """ Import CSV results from workers into exploration.db """ import sqlite3 import csv import json import sys from datetime import datetime def import_results(csv_file, chunk_id, db_path='cluster/exploration.db'): """Import results CSV into strategies table""" conn = sqlite3.connect(db_path) cursor = conn.cursor() # Read CSV and insert rows imported = 0 skipped = 0 with open(csv_file, 'r') as f: reader = csv.DictReader(f) for row in reader: # Skip header or invalid rows if row.get('trades') == '0': skipped += 1 continue # Build params JSON params_json = json.dumps({ 'flip_threshold': float(row['flip_threshold']), 'ma_gap': float(row['ma_gap']), 'adx_min': int(row['adx_min']), 'long_pos_max': int(row['long_pos_max']), 'short_pos_min': int(row['short_pos_min']), 'cooldown': int(row['cooldown']), 'position_size': int(row['position_size']), 'tp1_mult': float(row['tp1_mult']), 'tp2_mult': float(row['tp2_mult']), 'sl_mult': float(row['sl_mult']), 'tp1_close_pct': int(row['tp1_close_pct']), 'trailing_mult': float(row['trailing_mult']), 'vol_min': float(row['vol_min']), 'max_bars': int(row['max_bars']), }) try: cursor.execute(''' INSERT INTO strategies ( chunk_id, params_json, trades, win_rate, total_pnl, pnl_per_1k, profit_factor, max_drawdown, sharpe_ratio ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ''', ( chunk_id, params_json, int(row['trades']), float(row['win_rate']), float(row['total_pnl']), float(row['pnl_per_1k']), float(row['profit_factor']), float(row['max_drawdown']), float(row['sharpe_ratio']) )) imported += 1 except Exception as e: print(f"Error importing row: {e}") print(f"Row data: {row}") skipped += 1 conn.commit() conn.close() print(f"āœ… Imported {imported} strategies from {csv_file}") print(f"ā­ļø Skipped {skipped} rows (0 trades or errors)") return imported def update_chunk_status(chunk_id, db_path='cluster/exploration.db'): """Mark chunk as completed""" conn = sqlite3.connect(db_path) cursor = conn.cursor() # Get completion time from file system or use now now = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') cursor.execute(''' UPDATE chunks SET status='completed', completed_at=? WHERE id=? ''', (now, chunk_id)) conn.commit() conn.close() print(f"āœ… Updated chunk {chunk_id} status to completed") if __name__ == '__main__': if len(sys.argv) < 2: print("Usage: python3 import_results.py [chunk_id]") sys.exit(1) csv_file = sys.argv[1] # Extract chunk_id from filename if not provided if len(sys.argv) >= 3: chunk_id = sys.argv[2] else: # Parse from filename: chunk_v9_chunk_000000_results.csv -> v9_chunk_000000 import os basename = os.path.basename(csv_file) chunk_id = basename.replace('chunk_', '').replace('_results.csv', '') print(f"šŸ“„ Importing {csv_file} as chunk {chunk_id}") count = import_results(csv_file, chunk_id) if count > 0: update_chunk_status(chunk_id) print(f"\nšŸŽ‰ Import complete! {count} strategies added to database") else: print(f"\nāš ļø No strategies imported from {csv_file}")