"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(journal): allocate reversal fees to remaining quantity
Rix0ae1b7883b81
diff --git a/README.md b/README.mdindex ef4760b95..e554975c9 100644--- a/README.md+++ b/README.md@@ -1458,6 +1458,13 @@ signal = engine.compute_signal(panel)Correlation market inference recognizes explicit crypto pairs; bare equity tickers such as `ABNB` and `SOL` use the US equity data chain.+### Trade journal fees++Journal PnL includes recorded fees. When one fill closes a position and opens+an opposite position, its fee is split by quantity between the two legs.+Profile win rate and the average-win/average-loss ratio use the resulting net+PnL of FIFO matched portions; those portions are not necessarily whole orders.+### Market Research```bashdiff --git a/agent/src/tools/trade_journal_tool.py b/agent/src/tools/trade_journal_tool.pyindex 2307e9535..c52b332bc 100644--- a/agent/src/tools/trade_journal_tool.py+++ b/agent/src/tools/trade_journal_tool.py@@ -77,6 +77,9 @@ def pair_trades_fifo() -> list[dict[str, Any]]:"""Pair buys and sells per symbol using FIFO to compute per-roundtrip PnL.+ Reversal fees are shared proportionally between the closing quantity and+ the remaining opening quantity, so each fee is counted exactly once.+Long lots and short lots are both modelled: a sell that finds no openlong lot opens a short lot, and a buy first covers open shorts beforequeueing as a long (#16). Short PnL is (entry - cover) x qty.@@ -168,7 +171,7 @@ def pair_trades_fifo("dt": row.datetime,"qty": remaining,"price": row.price,- "fee": row.fee,+ "fee": row.fee * (remaining / row.quantity),})continue@@ -220,7 +223,7 @@ def pair_trades_fifo("dt": row.datetime,"qty": remaining,"price": row.price,- "fee": row.fee,+ "fee": row.fee * (remaining / row.quantity),})return roundtripsdiff --git a/agent/tests/test_trade_journal_reversal_fees.py b/agent/tests/test_trade_journal_reversal_fees.pynew file mode 100644index 000000000..270c79e5b--- /dev/null+++ b/agent/tests/test_trade_journal_reversal_fees.py@@ -0,0 +1,98 @@+"""A reversal's fee is shared by its closing and opening quantities."""++from decimal import Decimal++import pandas as pd+import pytest++from src.tools.trade_journal_tool import _compute_profile, pair_trades_fifo+++def _frame(rows):+ dates = pd.date_range("2026-01-02 10:00", periods=len(rows), freq="h")+ return pd.DataFrame(+ [+ {+ "datetime": dt,+ "symbol": "AAPL.US",+ "name": "Fixture",+ "side": side,+ "quantity": qty,+ "price": price,+ "amount": qty * price,+ "fee": fee,+ "market": "us",+ }+ for dt, (side, qty, price, fee) in zip(dates, rows)+ ]+ )+++def _net_cash(rows):+ assert sum((1 if side == "buy" else -1) * qty for side, qty, _, _ in rows) == 0+ return sum(+ (Decimal(-1) if side == "buy" else Decimal(1))+ * Decimal(str(qty))+ * Decimal(str(price))+ - Decimal(str(fee))+ for side, qty, price, fee in rows+ )+++def _reversal_rows(short_first, fee, shape):+ opening, reversal, closing = (+ ("sell", "buy", "sell") if short_first else ("buy", "sell", "buy")+ )+ reversal_price, close_price = (90, 95) if short_first else (110, 105)+ entries = (+ [(opening, 10, 100, 2)]+ if shape != "multiple_lots"+ else [(opening, 4, 100, 0.8), (opening, 6, 100, 1.2)]+ )+ exits = (+ [(closing, 5, close_price, 1)]+ if shape != "partial_exits"+ else [(closing, 2, close_price, 0.4), (closing, 3, close_price, 0.6)]+ )+ return entries + [(reversal, 15, reversal_price, fee)] + exits++[email protected]("short_first", [False, True])[email protected]("fee", [3, 0])[email protected]("shape", ["single_lot", "multiple_lots", "partial_exits"])+def test_flat_reversal_pnl_matches_net_cash(short_first, fee, shape):+ rows = _reversal_rows(short_first, fee, shape)++ trips = pair_trades_fifo(_frame(rows))++ expected_first = "short" if short_first else "long"+ assert trips[0]["side"] == expected_first+ assert trips[-1]["side"] != expected_first+ assert all(trip["currency"] == "USD" for trip in trips)+ assert sum(Decimal(str(trip["pnl"])) for trip in trips) == _net_cash(rows)++[email protected]("short_first", [False, True])+def test_reversal_rebate_is_allocated_once(short_first):+ rows = _reversal_rows(short_first, -3, "single_lot")++ trips = pair_trades_fifo(_frame(rows))++ assert sum(Decimal(str(trip["pnl"])) for trip in trips) == _net_cash(rows)++[email protected]("short_first", [False, True])+def test_correct_fee_preserves_small_net_winner_in_profile(short_first):+ rows = _reversal_rows(short_first, 3, "single_lot")+ # The residual lot earns 2.50 gross: its 1.00 entry fee and 1.00 exit+ # fee leave +0.50. Reusing the whole 3.00 reversal fee makes it -1.50.+ side, qty, _, fee = rows[-1]+ rows[-1] = (side, qty, 90.5 if short_first else 109.5, fee)+ rows += [("buy", 2, 100, 1), ("sell", 2, 97, 1)]++ profile = _compute_profile(_frame(rows))++ assert [t["pnl"] for t in profile["roundtrips_sample"]] == [96.0, 0.5, -8.0]+ assert profile["total_pnl"] == float(_net_cash(rows)) == 88.5+ assert profile["win_rate"] == 0.6667+ assert profile["profit_loss_ratio"] == 6.03 # ((96 + .5) / 2) / 8