Skip to content

"Vibe-Trading: Your Personal Trading Agent"

Merge remote-tracking branch 'origin/main' into codex/pr-531-followup

Haozhe Wu9fa3a641c08c
diff --cc CHANGELOG.md
index 0d4183e32,774bd5248..354c2f7a4
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@@ -52,8 -52,8 +52,10 @@@ This project adheres to [Semantic Versi
@roberttidball).
- Portfolio optimizers no longer include the decision bar's close-to-close
return in weights executed at that bar's open (#487, thanks @YZY0108).
+- End-of-backtest liquidations now apply exit slippage and include their
+ commission in the final reported equity.
+ - Open-price rebalances no longer use the decision bar's close for sizing or
+ depend on whether a replacement symbol sorts before the position it closes.
- Preflight (`vibe-trading run`) no longer resolves provider/model against a
stale `EnvConfig` snapshot cached before dotenv loads (#479, thanks
@ananaymital, closes #477).
diff --cc agent/backtest/engines/base.py
index 7856ca109,c8ec5bfcc..0adc8da84
--- a/agent/backtest/engines/base.py
+++ b/agent/backtest/engines/base.py
@@@ -558,24 -654,45 +654,60 @@@ class BaseEngine(ABC)
if len(dates) > 0:
last_ts = dates[-1]
for c in list(self.positions.keys()):
- price = self._safe_price(close_df, last_ts, c, self.positions[c].entry_price)
- self._close_position(c, price, last_ts, "end_of_backtest")
+ pos = self.positions[c]
+ mark_price = self._safe_price(close_df, last_ts, c, pos.entry_price)
+ self._active_symbol = c
+ exit_price = self.apply_slippage(mark_price, -pos.direction)
+ self._close_position(c, exit_price, last_ts, "end_of_backtest")
+
+ # The final snapshot feeds metrics and artifacts. Replace its
+ # pre-liquidation mark with post-liquidation cash so terminal
+ # slippage and exit commission are reflected in reported equity.
+ if self.equity_snapshots:
+ self.equity_snapshots[-1] = EquitySnapshot(
+ timestamp=last_ts,
+ capital=self.capital,
+ unrealized=0.0,
+ equity=self.capital,
+ positions=0,
+ )
+ def _calc_open_equity(
+ self,
+ data_map: Dict[str, pd.DataFrame],
+ close_df: pd.DataFrame,
+ ts: pd.Timestamp,
+ ) -> float:
+ """Value current positions at the execution bar's observable open.
+
+ For a symbol that has a bar at ``ts``, its open is the mark available
+ when next-bar-open orders execute. Symbols without a bar on the
+ unified calendar retain the aligned close fallback, which is the most
+ recent price carried by ``_align``.
+ """
+ if not self.positions:
+ return self.capital
+
+ equity = self.capital
+ for sym, pos in self.positions.items():
+ current_price = self._safe_price(close_df, ts, sym, pos.entry_price)
+ frame = data_map.get(sym)
+ if frame is not None and ts in frame.index:
+ open_price = frame.loc[ts].get("open")
+ if (
+ open_price is not None
+ and pd.notna(open_price)
+ and float(open_price) > 0
+ ):
+ current_price = float(open_price)
+
+ margin = self._calc_margin(sym, pos.size, pos.entry_price, pos.leverage)
+ unrealized = self._calc_pnl(
+ sym, pos.direction, pos.size, pos.entry_price, current_price
+ )
+ equity += margin + unrealized
+ return equity
+
def _calc_equity(self, close_df: pd.DataFrame, ts: pd.Timestamp) -> float:
"""Total equity = free cash + sum(margin + unrealised) per position.