#!/usr/bin/env python3
"""
Extract key metrics from backtest progress log for analysis.
Run after backtest completes to generate detailed analysis.
"""

import re
import sys

def parse_progress_line(line):
    """Parse a progress line and extract metrics."""
    # Pattern: 159,600,000/330,779,360]  48.2% | ... | Bal: $ 24,891.36 | Eq: $-28,373.89 | DD: $53,312.56 (213.77%) | MaxDD: $57,749.61 (231.57%) | Pos: 13 | AvgL: 12
    # Note: Opening bracket may or may not be present

    pattern = r'\[?\s*([\d,]+)/([\d,]+)\]\s*([\d.]+)%.*Bal: \$\s*([\d,.-]+).*Eq: \$\s*([\d,.-]+).*DD: \$\s*([\d,.-]+)\s*\(\s*([\d.]+)%\).*MaxDD: \$\s*([\d,.-]+)\s*\(\s*([\d.]+)%\).*Pos:\s*(\d+).*AvgL:\s*(\d+)'

    match = re.search(pattern, line)
    if match:
        return {
            'ticks_processed': int(match.group(1).replace(',', '')),
            'total_ticks': int(match.group(2).replace(',', '')),
            'progress_pct': float(match.group(3)),
            'balance': float(match.group(4).replace(',', '')),
            'equity': float(match.group(5).replace(',', '')),
            'drawdown': float(match.group(6).replace(',', '')),
            'drawdown_pct': float(match.group(7)),
            'max_drawdown': float(match.group(8).replace(',', '')),
            'max_drawdown_pct': float(match.group(9)),
            'positions': int(match.group(10)),
            'avg_level': int(match.group(11))
        }
    return None

def analyze_log(filepath):
    """Analyze backtest log and extract key events."""

    all_data = []
    max_dd_events = []  # Track when max DD increases
    negative_equity_periods = []
    deep_avg_levels = []  # Track averaging level 10+

    prev_max_dd = 0
    in_negative = False
    negative_start = None

    with open(filepath, 'r') as f:
        for line in f:
            data = parse_progress_line(line)
            if data:
                all_data.append(data)

                # Track max DD increases
                if data['max_drawdown'] > prev_max_dd + 100:  # Significant increase
                    max_dd_events.append({
                        'progress': data['progress_pct'],
                        'max_dd': data['max_drawdown'],
                        'max_dd_pct': data['max_drawdown_pct'],
                        'equity': data['equity'],
                        'avg_level': data['avg_level']
                    })
                    prev_max_dd = data['max_drawdown']

                # Track negative equity periods
                if data['equity'] < 0 and not in_negative:
                    in_negative = True
                    negative_start = data['progress_pct']
                elif data['equity'] >= 0 and in_negative:
                    in_negative = False
                    negative_equity_periods.append({
                        'start': negative_start,
                        'end': data['progress_pct'],
                        'lowest_equity': min(d['equity'] for d in all_data if d['progress_pct'] >= negative_start and d['progress_pct'] <= data['progress_pct'])
                    })

                # Track deep averaging
                if data['avg_level'] >= 10:
                    deep_avg_levels.append({
                        'progress': data['progress_pct'],
                        'level': data['avg_level'],
                        'equity': data['equity'],
                        'drawdown_pct': data['drawdown_pct']
                    })

    return {
        'total_samples': len(all_data),
        'final_data': all_data[-1] if all_data else None,
        'max_dd_events': max_dd_events,
        'negative_equity_periods': negative_equity_periods,
        'deep_averaging_count': len(deep_avg_levels),
        'max_averaging_level': max((d['avg_level'] for d in all_data), default=0),
        'min_equity': min((d['equity'] for d in all_data), default=0),
        'max_equity': max((d['equity'] for d in all_data), default=0),
    }

def print_report(analysis):
    """Print analysis report."""
    print("="*60)
    print("BACKTEST ANALYSIS REPORT")
    print("="*60)

    if analysis['final_data']:
        fd = analysis['final_data']
        print(f"\nFinal Status (at {fd['progress_pct']:.1f}% completion):")
        print(f"  Balance:      ${fd['balance']:,.2f}")
        print(f"  Equity:       ${fd['equity']:,.2f}")
        print(f"  Max Drawdown: ${fd['max_drawdown']:,.2f} ({fd['max_drawdown_pct']:.2f}%)")
        print(f"  Positions:    {fd['positions']}")
        print(f"  Avg Level:    {fd['avg_level']}")

    print(f"\nKey Metrics:")
    print(f"  Min Equity:   ${analysis['min_equity']:,.2f}")
    print(f"  Max Equity:   ${analysis['max_equity']:,.2f}")
    print(f"  Max Avg Level: {analysis['max_averaging_level']}")
    print(f"  Deep Avg Events (10+): {analysis['deep_averaging_count']}")

    print(f"\nMax Drawdown Events (significant increases):")
    for event in analysis['max_dd_events'][:10]:  # First 10
        print(f"  {event['progress']:.1f}%: MaxDD ${event['max_dd']:,.0f} ({event['max_dd_pct']:.1f}%), Eq ${event['equity']:,.0f}, AvgL {event['avg_level']}")

    if analysis['negative_equity_periods']:
        print(f"\nNegative Equity Periods:")
        for period in analysis['negative_equity_periods']:
            print(f"  {period['start']:.1f}% - {period['end']:.1f}%: Lowest ${period['lowest_equity']:,.0f}")

    print("="*60)

if __name__ == "__main__":
    log_file = sys.argv[1] if len(sys.argv) > 1 else "backtest_5year_progress.log"
    analysis = analyze_log(log_file)
    print_report(analysis)

    # Save to JSON
    import json
    with open('backtest_analysis.json', 'w') as f:
        json.dump(analysis, f, indent=2, default=str)
    print(f"\nAnalysis saved to backtest_analysis.json")
