Fix loading of new accounts without trades on live mt5 eas page
This commit is contained in:
+14
-7
@@ -9,9 +9,13 @@ Parsers for three MT5 trade report formats:
|
|||||||
All normalise to a common DataFrame schema.
|
All normalise to a common DataFrame schema.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from curses import raw
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from streamlit import text
|
||||||
|
|
||||||
|
|
||||||
# ── Common schema ─────────────────────────────────────────────────────────────
|
# ── Common schema ─────────────────────────────────────────────────────────────
|
||||||
# open_time, close_time, symbol, type, volume, open_price, close_price,
|
# 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).
|
Returns (df, format_name) or (None, None).
|
||||||
"""
|
"""
|
||||||
fname = filename.lower()
|
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
|
fallback = _strategy_from_filename(filename) if filename else None
|
||||||
|
|
||||||
if fname.endswith('.csv'):
|
if fname.endswith('.csv'):
|
||||||
df = parse_quant_csv(file_bytes, fallback_strategy=fallback)
|
df = parse_quant_csv(file_bytes, fallback_strategy=fallback)
|
||||||
return df, 'Quant Analyzer CSV'
|
return df, 'Quant Analyzer CSV'
|
||||||
|
|
||||||
# HTML/HTM — detect backtest vs real account
|
# HTML/HTM — decode with UTF-16 support first
|
||||||
try:
|
text = None
|
||||||
text = _decode(file_bytes)
|
for enc in ['utf-16', 'utf-8', 'latin-1']:
|
||||||
except:
|
try:
|
||||||
|
text = file_bytes.decode(enc)
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if text is None:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
if 'Strategy Tester Report' in text or 'strategy tester' in text.lower():
|
if 'Strategy Tester Report' in text or 'strategy tester' in text.lower():
|
||||||
|
|||||||
@@ -116,13 +116,13 @@ def refresh_account(cfg: dict, account_folder: str, label: str = "") -> dict:
|
|||||||
if raw is None:
|
if raw is None:
|
||||||
return {"error": f"No report found for {account_folder}"}
|
return {"error": f"No report found for {account_folder}"}
|
||||||
df, fmt = detect_and_parse(raw, f"{account_folder}.htm")
|
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}"}
|
return {"error": f"Could not parse report for {account_folder}"}
|
||||||
stats = calc_stats(df)
|
# df.empty is valid for a new account with no closed trades yet
|
||||||
# Also parse open positions if present
|
stats = calc_stats(df) if not df.empty else {}
|
||||||
from mt5_parser import parse_open_positions
|
from mt5_parser import parse_open_positions
|
||||||
df_open = parse_open_positions(raw)
|
df_open = parse_open_positions(raw)
|
||||||
data = {
|
data = {
|
||||||
"account_folder": account_folder,
|
"account_folder": account_folder,
|
||||||
"label" : label or account_folder,
|
"label" : label or account_folder,
|
||||||
"df" : df,
|
"df" : df,
|
||||||
|
|||||||
Reference in New Issue
Block a user