Skip to content

"Vibe-Trading: Your Personal Trading Agent"

Merge PR #1311: model short lots in FIFO pairing and restate legs across corporate actions

Haozhe Wu953930a7d50c
diff --cc agent/src/shadow_account/backtester.py
index e4ad50e90,9473681a9..7ec69ca6c
--- a/agent/src/shadow_account/backtester.py
+++ b/agent/src/shadow_account/backtester.py
@@@ -272,7 -281,7 +282,8 @@@ def run_shadow_backtest
journal_path=journal_path,
combined=combined,
initial_capital=initial_capital,
+ pool_currency=headline_currency,
+ adjust=build_frame_adjust(adjust_frames) if adjust_frames else None,
)
result = ShadowBacktestResult(
@@@ -525,7 -505,7 +536,8 @@@ def _attribution_or_zero
journal_path: str | Path | None,
combined: dict[str, float],
initial_capital: float,
+ pool_currency: str | None = None,
+ adjust=None,
) -> tuple[AttributionBreakdown, float | None, float]:
"""Compute attribution if the journal is available, else return zeros."""
shadow_pnl = _shadow_pnl_from_metrics(combined, initial_capital)
diff --cc agent/src/tools/trade_journal_tool.py
index 70bb6d516,ffa55aab0..8670e0954
--- a/agent/src/tools/trade_journal_tool.py
+++ b/agent/src/tools/trade_journal_tool.py
@@@ -41,34 -96,90 +96,98 @@@ def pair_trades_fifo
List of dicts: symbol, buy_dt, sell_dt, qty, buy_price, sell_price,
hold_days, pnl, pnl_pct. Unmatched positions are ignored.
"""
- queues: dict[str, deque] = defaultdict(deque)
+ longs: dict[str, deque] = defaultdict(deque)
+ shorts: dict[str, deque] = defaultdict(deque)
roundtrips: list[dict[str, Any]] = []
+ def _factor(symbol: str, from_dt: Any, to_dt: Any) -> float | None:
+ if adjust is None:
+ return None
+ try:
+ factor = adjust(symbol, from_dt, to_dt)
+ except Exception: # an adjuster miss must not break pairing
+ return None
+ if factor is None or factor <= 0:
+ return None
+ return float(factor)
+
for row in df.itertuples(index=False):
if row.side == "buy":
- queues[row.symbol].append({
- "dt": row.datetime,
- "qty": row.quantity,
- "price": row.price,
- "fee": row.fee,
- })
+ # Cover open shorts first, then queue any remainder as a long lot.
+ remaining = row.quantity
+ sq = shorts[row.symbol]
+ while remaining > 1e-9 and sq:
+ lot = sq[0]
- take = min(lot["qty"], remaining)
+ cover_dt = pd.to_datetime(row.datetime)
+ entry_dt = pd.to_datetime(lot["dt"])
+ hold = (cover_dt - entry_dt).total_seconds() / 86400.0 if pd.notna(cover_dt) and pd.notna(entry_dt) else 0.0
- factor = _factor(row.symbol, entry_dt, cover_dt)
- entry_price = lot["price"] * (1.0 / factor) if factor else lot["price"]
++ # Restate the short lot into the cover date's caliber exactly as
++ # the long branch below does: the factor turns an entry-date
++ # price into the cover date's, so price is scaled BY it and the
++ # share count by its inverse. Dividing the price instead would
++ # book a 1:2 split as a large fake short profit.
++ factor = _factor(row.symbol, entry_dt, cover_dt) or 1.0
++ lot_qty_here = lot["qty"] / factor
++ entry_price = lot["price"] * factor
++ take = min(lot_qty_here, remaining)
+ gross = (entry_price - row.price) * take
- entry_fee = lot["fee"] * (take / lot["qty"]) if lot["qty"] else 0.0
++ # Fees were paid in original-caliber shares.
++ consumed = take * factor
++ entry_fee = lot["fee"] * (consumed / lot["qty"]) if lot["qty"] else 0.0
+ cover_fee = row.fee * (take / row.quantity) if row.quantity else 0.0
+ pnl = gross - entry_fee - cover_fee
+ cost = entry_price * take
+ pnl_pct = pnl / cost if cost else 0.0
+ roundtrips.append({
+ "symbol": row.symbol,
+ "buy_dt": lot["dt"],
+ "sell_dt": row.datetime,
+ "qty": take,
+ "buy_price": lot["price"],
+ "sell_price": row.price,
+ "hold_days": round(hold, 2),
+ "pnl": round(pnl, 2),
+ "pnl_pct": round(pnl_pct, 4),
+ "side": "short",
+ })
+ lot["fee"] -= entry_fee
- lot["qty"] -= take
++ lot["qty"] -= consumed
+ remaining -= take
+ if lot["qty"] <= 1e-9:
+ sq.popleft()
+ if remaining > 1e-9:
+ longs[row.symbol].append({
+ "dt": row.datetime,
+ "qty": remaining,
+ "price": row.price,
+ "fee": row.fee,
+ })
continue
- # sell: match against oldest buys
+ # sell: match against oldest longs; anything left opens a short lot.
remaining = row.quantity
- q = queues[row.symbol]
+ q = longs[row.symbol]
while remaining > 1e-9 and q:
lot = q[0]
- take = min(lot["qty"], remaining)
sell_dt = pd.to_datetime(row.datetime)
buy_dt = pd.to_datetime(lot["dt"])
+ factor = _factor(row.symbol, buy_dt, sell_dt)
+ # Restate the lot to the sell date's caliber: a 1:2 split turns a
+ # pre-split lot of 10@100 into 20@50, so the post-split sell
+ # matches the whole position instead of half plus a phantom
+ # short (#15).
+ factor = factor or 1.0
+ lot_qty_here = lot["qty"] / factor
+ lot_price_here = lot["price"] * factor
+ take = min(lot_qty_here, remaining)
hold = (sell_dt - buy_dt).total_seconds() / 86400.0 if pd.notna(sell_dt) and pd.notna(buy_dt) else 0.0
- gross = (row.price - lot["price"]) * take
- # Proportional fee allocation
- buy_fee = lot["fee"] * (take / lot["qty"]) if lot["qty"] else 0.0
+ gross = (row.price - lot_price_here) * take
+ # Proportional fee allocation (in original-caliber shares)
+ consumed = take * factor
+ buy_fee = lot["fee"] * (consumed / lot["qty"]) if lot["qty"] else 0.0
sell_fee = row.fee * (take / row.quantity) if row.quantity else 0.0
pnl = gross - buy_fee - sell_fee
- cost = lot["price"] * take
+ cost = lot_price_here * take
pnl_pct = pnl / cost if cost else 0.0
roundtrips.append({
"symbol": row.symbol,
diff --cc agent/tests/test_trade_journal_fifo.py
index 000000000,5e96ed36b..dfc985744
mode 000000,100644..100644
--- a/agent/tests/test_trade_journal_fifo.py
+++ b/agent/tests/test_trade_journal_fifo.py
@@@ -1,0 -1,77 +1,116 @@@
+ """FIFO short lots (#16) and corporate-action caliber restatement (#15).
+
+ Short sales used to vanish: the queue only modelled buy-first, so a sell
+ with no open long was skipped and the later cover queued as a phantom long
+ lot that poisoned every later match. And a split between buy and sell
+ fabricated a large fake loss, with cash dividends never entering fills.
+ """
+
+ from __future__ import annotations
+
+ import pandas as pd
+
+ from src.tools.trade_journal_tool import build_frame_adjust, pair_trades_fifo
+
+
+ def _df(rows: list[tuple]) -> pd.DataFrame:
+ frame = pd.DataFrame(
+ rows,
+ columns=["datetime", "symbol", "name", "side", "quantity", "price", "amount", "fee"],
+ )
+ return frame
+
+
+ def test_short_sale_and_cover_is_a_roundtrip_not_a_phantom_long() -> None:
+ df = _df([
+ ("2026-01-02 09:30:00", "AAPL.US", "Apple", "sell", 10.0, 100.0, 1000.0, 1.0),
+ ("2026-01-05 09:30:00", "AAPL.US", "Apple", "buy", 10.0, 90.0, 900.0, 1.0),
+ ])
+ rts = pair_trades_fifo(df)
+ assert len(rts) == 1
+ rt = rts[0]
+ assert rt["side"] == "short"
+ # (100 - 90) * 10 - fees
+ assert rt["pnl"] == 98.0
+ assert rt["hold_days"] == 3.0
+
+
+ def test_cover_after_long_and_short_keeps_books_separate() -> None:
+ df = _df([
+ ("2026-01-02 09:30:00", "AAPL.US", "Apple", "buy", 5.0, 50.0, 250.0, 0.0),
+ ("2026-01-03 09:30:00", "AAPL.US", "Apple", "sell", 10.0, 100.0, 1000.0, 0.0),
+ ("2026-01-04 09:30:00", "AAPL.US", "Apple", "buy", 10.0, 90.0, 900.0, 0.0),
+ ])
+ rts = pair_trades_fifo(df)
+ # 5-share long closed at 100, then a 5-share short covered at 90.
+ assert len(rts) == 2
+ assert rts[0]["side"] == "long" and rts[0]["pnl"] == 250.0
+ assert rts[1]["side"] == "short" and rts[1]["pnl"] == 50.0
+
+
+ def _frames() -> dict[str, pd.DataFrame]:
+ # 1:2 split between 2026-01-03 and 2026-01-06: the adjusted close halves
+ # on the first post-split bar, so the factor from pre to post is 0.5.
+ idx = pd.to_datetime(["2026-01-02", "2026-01-03", "2026-01-06"])
+ return {"AAPL.US": pd.DataFrame({"close": [100.0, 100.0, 50.0]}, index=idx)}
+
+
+ def test_split_between_legs_no_longer_fabricates_a_loss() -> None:
+ df = _df([
+ ("2026-01-03 09:30:00", "AAPL.US", "Apple", "buy", 10.0, 100.0, 1000.0, 0.0),
+ ("2026-01-06 09:30:00", "AAPL.US", "Apple", "sell", 20.0, 50.0, 1000.0, 0.0),
+ ])
+ raw = pair_trades_fifo(df)
+ assert raw[0]["pnl"] == -500.0 # the fake loss this issue is about
+
+ adjusted = pair_trades_fifo(df, adjust=build_frame_adjust(_frames()))
+ # Buy leg restated to the post-split caliber: 100 * 0.5 = 50, pnl = 0.
+ assert adjusted[0]["pnl"] == 0.0
+
+
+ def test_uncovered_symbol_stays_raw() -> None:
+ df = _df([
+ ("2026-01-03 09:30:00", "MSFT.US", "Microsoft", "buy", 10.0, 100.0, 1000.0, 0.0),
+ ("2026-01-06 09:30:00", "MSFT.US", "Microsoft", "sell", 10.0, 50.0, 500.0, 0.0),
+ ])
+ rts = pair_trades_fifo(df, adjust=build_frame_adjust({}))
+ assert rts[0]["pnl"] == -500.0
++
++
++def test_split_between_legs_of_a_short_mirrors_the_long_case() -> None:
++ """A short across a 1:2 split must net to zero, exactly like the long.
++
++ The original short branch divided the entry price by the factor instead of
++ multiplying (and never restated the share count), so the same split that
++ nets a long to zero booked the short at +1500.
++ """
++ short_df = _df([
++ ("2026-01-03 09:30:00", "AAPL.US", "Apple", "sell", 10.0, 100.0, 1000.0, 0.0),
++ ("2026-01-06 09:30:00", "AAPL.US", "Apple", "buy", 20.0, 50.0, 1000.0, 0.0),
++ ])
++ long_df = _df([
++ ("2026-01-03 09:30:00", "AAPL.US", "Apple", "buy", 10.0, 100.0, 1000.0, 0.0),
++ ("2026-01-06 09:30:00", "AAPL.US", "Apple", "sell", 20.0, 50.0, 1000.0, 0.0),
++ ])
++ adjust = build_frame_adjust(_frames())
++
++ short_rt = pair_trades_fifo(short_df, adjust=adjust)[0]
++ long_rt = pair_trades_fifo(long_df, adjust=adjust)[0]
++
++ assert short_rt["side"] == "short"
++ assert short_rt["pnl"] == 0.0
++ assert short_rt["pnl"] == long_rt["pnl"]
++ # The cover consumes the whole restated position, not half of it.
++ assert short_rt["qty"] == 20.0
++ assert short_rt["qty"] == long_rt["qty"]
++
++
++def test_short_without_adjust_is_unchanged() -> None:
++ """No adjuster: the short stays on raw prices, byte-for-byte legacy."""
++ df = _df([
++ ("2026-01-03 09:30:00", "AAPL.US", "Apple", "sell", 10.0, 100.0, 1000.0, 0.0),
++ ("2026-01-06 09:30:00", "AAPL.US", "Apple", "buy", 10.0, 80.0, 800.0, 0.0),
++ ])
++ rt = pair_trades_fifo(df)[0]
++ assert rt["side"] == "short"
++ assert rt["pnl"] == 200.0 # (100 - 80) * 10