"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(report-audit): read accounting parentheses as negatives
Yufeng Heed039a3fdb24
diff --git a/agent/src/tools/report_audit_tool.py b/agent/src/tools/report_audit_tool.pyindex 7ecba1d39..c647f2b7d 100644--- a/agent/src/tools/report_audit_tool.py+++ b/agent/src/tools/report_audit_tool.py@@ -34,11 +34,13 @@ from src.agent.tools import BaseTool_KV_LABEL_RE = re.compile(r"(?P<label>[一-龥A-Za-z][^|\n::*]{1,30})[::]\s*[~约]?\$?"- r"(?P<num>[\d,,.]+)\s*"+ r"(?P<open>[((])?(?P<num>[\d,,.]+)(?(open)[))])\s*"r"(?P<unit>亿[元美港]?元?|万亿|[xX倍]|%|[BMT])?")-_NUMUNIT_RE = re.compile(r"[~约]?\$?([\d,,.]+)\s*(亿[元美港]?元?|万亿|[xX倍]|%|[BMT])?")+_NUMUNIT_RE = re.compile(+ r"[~约]?\$?([((])?([\d,,.]+)(?(1)[))])\s*(亿[元美港]?元?|万亿|[xX倍]|%|[BMT])?"+)_TABLE_SEP_RE = re.compile(r"^\|[\-\s|:]+\|$")_SKIP_LABELS = {@@ -119,8 +121,12 @@ def _parse_md_tables(lines: list[str]) -> list[tuple[str, str, float, str, int,)m = _NUMUNIT_RE.search(cell)if m:- val = _clean_num(m.group(1))- unit = (m.group(2) or "").strip()+ val = _clean_num(m.group(2))+ # Tables follow accounting convention: a+ # parenthesized number is a negative.+ if val is not None and m.group(1):+ val = -val+ unit = (m.group(3) or "").strip()if val and val != 0 and val < 1e15:results.append((row_label, col_header, val, unit, i + 1, dline))i += 1@@ -190,9 +196,16 @@ def extract_data_points(md_text: str) -> list[dict[str, Any]]:if "|" in stripped:continue # handled as a table abovefor m in _KV_LABEL_RE.finditer(stripped):+ if m.group("open") and not m.group("unit"):+ # A parenthesized bare number in prose is a year ("(2024)"),+ # not a negative. Only a trailing unit makes it accounting.+ continue+ val = _clean_num(m.group("num"))+ if val is not None and m.group("open"):+ val = -val_add(m.group("label"),- _clean_num(m.group("num")),+ val,(m.group("unit") or "").strip(),lineno,stripped,diff --git a/agent/tests/test_report_audit_accounting_parens.py b/agent/tests/test_report_audit_accounting_parens.pynew file mode 100644index 000000000..3247737bd--- /dev/null+++ b/agent/tests/test_report_audit_accounting_parens.py@@ -0,0 +1,82 @@+"""Accounting-parentheses negatives in report_audit extraction.++Financial reports write negatives as ``(25.3)`` / ``(25.3)``. The extractor+used to read the bare digits out of the parentheses, so a loss came out+positive and the closing paren swallowed the unit. Issue #1660.+"""++from src.tools.report_audit_tool import extract_data_points+++def _by_label(points, label_part):+ return [p for p in points if label_part in p["label"]]+++def test_table_parenthesized_negative_keeps_sign_and_unit():+ md = (+ "| 项目 | 2024 |\n"+ "|---|---|\n"+ "| 净利润 | (25.3)亿 |\n"+ )+ pts = extract_data_points(md)+ hits = _by_label(pts, "净利润")+ assert len(hits) == 1+ assert hits[0]["reported_value"] == -25.3+ assert hits[0]["unit"] == "亿"+++def test_table_fullwidth_parens_negative():+ md = (+ "| 项目 | 2024 |\n"+ "|---|---|\n"+ "| 归母净利润 | (3.20)亿元 |\n"+ )+ pts = extract_data_points(md)+ hits = _by_label(pts, "归母净利润")+ assert len(hits) == 1+ assert hits[0]["reported_value"] == -3.2+ assert hits[0]["unit"] == "亿元"+++def test_table_positive_control_unchanged():+ md = (+ "| 项目 | 2024 |\n"+ "|---|---|\n"+ "| 营收 | 100.5亿 |\n"+ )+ pts = extract_data_points(md)+ hits = _by_label(pts, "营收")+ assert len(hits) == 1+ assert hits[0]["reported_value"] == 100.5+ assert hits[0]["unit"] == "亿"+++def test_table_placeholder_cell_still_skipped():+ md = (+ "| 项目 | 2024 |\n"+ "|---|---|\n"+ "| 净利润 | (-) |\n"+ )+ assert extract_data_points(md) == []+++def test_kv_line_parenthesized_negative_with_unit():+ pts = extract_data_points("净利润:(25.3)亿元,同比转亏")+ hits = _by_label(pts, "净利润")+ assert len(hits) == 1+ assert hits[0]["reported_value"] == -25.3+ assert hits[0]["unit"] == "亿元"+++def test_kv_line_bare_paren_year_stays_dropped():+ # "(2024)" in prose is a year, not a negative; main drops it and the+ # fix must not start extracting it.+ assert extract_data_points("规划:(2024) 年投产") == []+++def test_kv_line_positive_control_unchanged():+ pts = extract_data_points("营收:100.5亿元")+ hits = _by_label(pts, "营收")+ assert len(hits) == 1+ assert hits[0]["reported_value"] == 100.5+ assert hits[0]["unit"] == "亿元"