#!/usr/bin/env python3 """Export MT5 HTML report to a clean, browser-viewable HTML file""" import re import sys import os from datetime import datetime def parse_report(html_file): """Parse MT5 HTML report and extract data""" try: with open(html_file, 'rb') as f: content = f.read().decode('utf-16-le', errors='ignore') except Exception as e: print(f"Error reading file: {e}") return None # Clean text for parsing text = re.sub('<[^<]+?>', ' ', content) text = text.replace(' ', ' ') def extract_value(pattern, text, group=1): match = re.search(pattern, text) return match.group(group).strip() if match else "N/A" def parse_num(s): if not s or s == "N/A": return 0 try: s = str(s).replace(' ', '').replace(',', '.') parts = s.split('.') if len(parts) > 2: s = ''.join(parts[:-1]) + '.' + parts[-1] return float(s) except: return 0 data = { 'filename': os.path.basename(html_file), 'account': extract_value(r'Account:\s*(\d+)', text), 'name': extract_value(r'Name:\s*([^\d]+?)(?:\s+Account|\s*$)', text), 'company': extract_value(r'Company:\s*([^\d]+?)(?:\s+Date|\s*$)', text), 'net_profit': extract_value(r'Total Net Profit[^\d\-]*([\-\d\s,\.]+)', text), 'gross_profit': extract_value(r'Gross Profit[^\d]*([\d\s,\.]+)', text), 'gross_loss': extract_value(r'Gross Loss[^\d\-]*([\-\d\s,\.]+)', text), 'profit_factor': extract_value(r'Profit Factor[^\d]*([\d\.]+)', text), 'expected_payoff': extract_value(r'Expected Payoff[^\d]*([\d\s,\.]+)', text), 'total_trades': extract_value(r'Total Trades[^\d]*(\d+)', text), 'profit_trades': extract_value(r'Profit Trades \(%[^)]*\)[^\d]*(\d+)', text), 'loss_trades': extract_value(r'Loss Trades \(%[^)]*\)[^\d]*(\d+)', text), 'win_pct': extract_value(r'Profit Trades \(%[^)]*\)[^\d]*\d+[^\d]*\((\d+\.?\d*)%', text), 'largest_profit': extract_value(r'Largest profit trade[^\d]*([\d\s,\.]+)', text), 'largest_loss': extract_value(r'Largest loss trade[^\d\-]*([\-\d\s,\.]+)', text), 'max_drawdown': extract_value(r'Balance Drawdown Maximal[^\d]*([\d\s,\.]+)', text), } # Calculate values net = parse_num(data['net_profit']) data['starting_balance'] = 100000.00 data['current_balance'] = 100000.00 + net data['return_pct'] = (net / 100000.00) * 100 return data def generate_html(data, output_file): """Generate clean HTML report""" html_content = f'''
Account {data['account']} | {data['name']} | Generated on {datetime.now().strftime('%Y-%m-%d %H:%M')}