diff --git a/mt5_parser.py b/mt5_parser.py index 445af57..d2fc188 100644 --- a/mt5_parser.py +++ b/mt5_parser.py @@ -9,9 +9,13 @@ Parsers for three MT5 trade report formats: All normalise to a common DataFrame schema. """ +from curses import raw + import pandas as pd import re +from streamlit import text + # ── Common schema ───────────────────────────────────────────────────────────── # open_time, close_time, symbol, type, volume, open_price, close_price, @@ -398,19 +402,22 @@ def detect_and_parse(file_bytes, filename=''): Returns (df, format_name) or (None, None). """ fname = filename.lower() - # Derive a fallback strategy name from the filename for files where - # all comments are MT5 close-reason tags (sl/tp/so) and no strategy - # name is embedded in the comment field. fallback = _strategy_from_filename(filename) if filename else None if fname.endswith('.csv'): df = parse_quant_csv(file_bytes, fallback_strategy=fallback) return df, 'Quant Analyzer CSV' - # HTML/HTM — detect backtest vs real account - try: - text = _decode(file_bytes) - except: + # HTML/HTM — decode with UTF-16 support first + text = None + for enc in ['utf-16', 'utf-8', 'latin-1']: + try: + text = file_bytes.decode(enc) + break + except Exception: + continue + + if text is None: return None, None if 'Strategy Tester Report' in text or 'strategy tester' in text.lower(): diff --git a/view_live_mt5_eas.py b/view_live_mt5_eas.py index 4079f48..60d4c21 100644 --- a/view_live_mt5_eas.py +++ b/view_live_mt5_eas.py @@ -116,13 +116,13 @@ def refresh_account(cfg: dict, account_folder: str, label: str = "") -> dict: if raw is None: return {"error": f"No report found for {account_folder}"} df, fmt = detect_and_parse(raw, f"{account_folder}.htm") - if df is None or df.empty: + if df is None: return {"error": f"Could not parse report for {account_folder}"} - stats = calc_stats(df) - # Also parse open positions if present + # df.empty is valid for a new account with no closed trades yet + stats = calc_stats(df) if not df.empty else {} from mt5_parser import parse_open_positions df_open = parse_open_positions(raw) - data = { + data = { "account_folder": account_folder, "label" : label or account_folder, "df" : df,