Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(journal): parse parenthesised accounting negatives instead of zeroing them

Rudy Celeklid80249bba25c
diff --git a/agent/src/tools/trade_journal_parsers.py b/agent/src/tools/trade_journal_parsers.py
index bc4eede6c..70159c092 100644
--- a/agent/src/tools/trade_journal_parsers.py
+++ b/agent/src/tools/trade_journal_parsers.py
@@ -318,7 +318,16 @@ def _to_float(val: Any, default: float = 0.0) -> float:
s = _CURRENCY_TOKEN_RE.sub("", s).replace(",", "").strip()
if not s:
return default
+ # Accounting exports write a negative as "(1,234.50)" (optionally
+ # "$(1,234.50)"); without this float() fails and the cell becomes 0.0.
+ negate = s.startswith("(") and s.endswith(")")
+ if negate:
+ s = s[1:-1].strip()
+ if not s or s[0] in "+-":
+ return default
parsed = float(s)
+ if negate:
+ parsed = -parsed
return parsed if math.isfinite(parsed) else default
except (ValueError, TypeError):
return default
diff --git a/agent/tests/test_trade_journal_parser_accounting_negatives.py b/agent/tests/test_trade_journal_parser_accounting_negatives.py
new file mode 100644
index 000000000..e6fb510b7
--- /dev/null
+++ b/agent/tests/test_trade_journal_parser_accounting_negatives.py
@@ -0,0 +1,32 @@
+"""Accounting-style negatives such as ``(1,234.50)`` must not collapse to 0.0."""
+
+from __future__ import annotations
+
+import pytest
+
+from src.tools.trade_journal_parsers import _to_float
+
+
+ ("raw", "expected"),
+ [
+ ("(1,234.50)", -1234.5),
+ ("($5.00)", -5.0),
+ ("$(5)", -5.0),
+ (" ( 12 ) ", -12.0),
+ ("(USD 7.25)", -7.25),
+ ],
+)
+def test_parenthesised_numbers_are_negative(raw: str, expected: float) -> None:
+ assert _to_float(raw) == expected
+
+
[email protected]("raw", ["()", "(-5)", "(+5)", "(abc)", "(1", "1)", "(nan)", "(inf)"])
+def test_malformed_parentheses_still_fall_back_to_default(raw: str) -> None:
+ assert _to_float(raw) == 0.0
+ assert _to_float(raw, default=-1.0) == -1.0
+
+
[email protected](("raw", "expected"), [("1,234.50", 1234.5), ("-5", -5.0), ("−5", -5.0), ("$12", 12.0)])
+def test_plain_numbers_are_unchanged(raw: str, expected: float) -> None:
+ assert _to_float(raw) == expected