Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(journal): preserve source order for equal timestamps (#1941)

Rix75757fad5056
diff --git a/README.md b/README.md
index 35692df02..5e1df22ee 100644
--- a/README.md
+++ b/README.md
@@ -1490,6 +1490,8 @@ Generic journals accept a complete `datetime` column or separate `date` and
`time` columns (case-insensitive). Split columns are combined before sorting
and FIFO matching; Excel date serials and time fractions are supported.
A complete `datetime` column takes precedence when all three are present.
+Rows with equal timestamps retain their source-file order for FIFO matching.
+This preserves the supplied sequence; it does not reconstruct missing fill order.
### Market Research
diff --git a/agent/src/tools/trade_journal_parsers.py b/agent/src/tools/trade_journal_parsers.py
index cddd3a06d..8a2001c2f 100644
--- a/agent/src/tools/trade_journal_parsers.py
+++ b/agent/src/tools/trade_journal_parsers.py
@@ -762,9 +762,9 @@ def parse_file(path: str | Path) -> tuple[FormatName, list[TradeRecord]]:
def records_to_dataframe(records: list[TradeRecord]) -> pd.DataFrame:
- """Convert records to a standardized DataFrame (datetime column parsed)."""
+ """Parse and sort datetimes, retaining source order for equal timestamps."""
if not records:
return pd.DataFrame(columns=[f.name for f in TradeRecord.__dataclass_fields__.values()])
df = pd.DataFrame([asdict(r) for r in records])
df["datetime"] = pd.to_datetime(df["datetime"], errors="coerce")
- return df.sort_values("datetime").reset_index(drop=True)
+ return df.sort_values("datetime", kind="stable").reset_index(drop=True)
diff --git a/agent/tests/test_trade_journal_timestamp_order.py b/agent/tests/test_trade_journal_timestamp_order.py
new file mode 100644
index 000000000..33b126441
--- /dev/null
+++ b/agent/tests/test_trade_journal_timestamp_order.py
@@ -0,0 +1,76 @@
+"""Equal timestamps must retain source order before FIFO pairing."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+from src.tools.trade_journal_parsers import TradeRecord, records_to_dataframe
+from src.tools.trade_journal_tool import analyze_trade_journal
+
+
[email protected]("pattern", ["equal", "groups", "interleaved"])
+def test_timestamp_sort_keeps_each_groups_source_order(pattern: str) -> None:
+ records = []
+ for i in range(20):
+ day = (
+ 1 if pattern == "equal" else (i // 10 if pattern == "groups" else i % 2) + 1
+ )
+ records.append(
+ TradeRecord(
+ datetime=f"2026-01-{day:02d} 10:00:00",
+ symbol="AAPL",
+ name=str(i),
+ side="buy",
+ quantity=1,
+ price=100 + i,
+ amount=100 + i,
+ fee=1,
+ market="us",
+ )
+ )
+
+ # Python's stable sort supplies an independent ordering oracle.
+ expected = [r.name for r in sorted(records, key=lambda r: r.datetime)]
+ frame = records_to_dataframe(records)
+ assert frame["name"].tolist() == expected
+ assert frame["datetime"].is_monotonic_increasing
+ assert frame.index.tolist() == list(range(20))
+
+
[email protected]("short_first", [False, True])
[email protected]("date_only", [False, True])
+def test_csv_ties_do_not_turn_winning_pairs_into_losers(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, short_first: bool, date_only: bool
+) -> None:
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_FILE_ROOTS", str(tmp_path))
+ legs = (
+ [("sell", 100), ("buy", 95), ("sell", 90), ("buy", 80)]
+ if short_first
+ else [("buy", 100), ("sell", 105), ("buy", 110), ("sell", 120)]
+ )
+ timestamp = "2026-01-01" if date_only else "2026-01-01 10:00:00"
+ rows = [f"{timestamp},AAPL,{side},1,{price},1" for side, price in legs * 5]
+ path = tmp_path / "journal.csv"
+ path.write_text(
+ ("date" if date_only else "datetime")
+ + ",symbol,side,quantity,price,fee\n"
+ + "\n".join(rows)
+ + "\n",
+ encoding="utf-8",
+ )
+
+ result = json.loads(analyze_trade_journal(str(path), "profile"))
+ assert result["status"] == "ok"
+ profile = result["profile"]
+ # Each source pair earns (5 - 2)=3 or (10 - 2)=8, repeated five times.
+ assert profile["total_roundtrips"] == 10
+ assert profile["total_pnl"] == 55
+ assert profile["win_rate"] == 1.0
+ assert profile["profit_loss_ratio"] is None # No losses; strict JSON.
+ assert [rt["pnl"] for rt in profile["roundtrips_sample"]] == [3, 8, 3, 8, 3]
+ assert {rt["side"] for rt in profile["roundtrips_sample"]} == {
+ "short" if short_first else "long"
+ }