Source code for trader.chat.backtest_format
"""Formatting a `BacktestResult` for the chat CLI.
Deliberately **not** run through the model at all: every number here already
came out of `run_backtest`, so there is nothing for a second LLM call to add
except a chance to misstate one of them. Issue #15 asks for "the same results
summary the backtest CLI command produces" — this mirrors
`trader.cli.main.backtest`'s own `typer.echo` lines line for line rather than
re-deriving a second copy of that formatting.
"""
from trader.backtest.engine import BacktestResult
__all__ = ["format_backtest_result"]
[docs]
def format_backtest_result(result: BacktestResult, bar_count: int) -> str:
"""The same summary `trader backtest` prints, as one block of text."""
lines = [
f"{result.strategy_id} on {result.symbol} ({bar_count} bars)",
f" starting value: {result.starting_value}",
f" ending value: {result.ending_value}",
f" total return: {result.total_return_pct:.2f}%",
f" max drawdown: {result.max_drawdown_pct:.2f}%",
f" wins / losses: {result.win_count} / {result.loss_count}",
f" trades: {len(result.trades)}",
"",
f" buy & hold: {result.benchmark_return_pct:.2f}%",
f" buy & hold value:{result.benchmark_ending_value:>12.2f}",
f" excess vs hold: {result.excess_return_pct:+.2f}%",
]
return "\n".join(lines)