> For the complete documentation index, see [llms.txt](https://docs.vibetrading.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.vibetrading.dev/library/backtest-analysis.md).

# Backtest Analysis

Use an LLM to evaluate backtest results — get a performance score (1-10), strengths, weaknesses, risk assessment, and actionable improvement suggestions.

## Quick Usage

```python
import vibetrading.strategy
import vibetrading.backtest

results = vibetrading.backtest.run(strategy_code, interval="1h", data=data)

report = vibetrading.strategy.analyze(
    results,
    strategy_code=strategy_code,
    model="gpt-4o",
)

print(f"Score: {report.score}/10")
print(report.summary)
```

## `BacktestAnalysisResult`

The analysis returns a `BacktestAnalysisResult` with the following fields:

| Field              | Type       | Description                            |
| ------------------ | ---------- | -------------------------------------- |
| score              | int        | Overall score (1-10)                   |
| summary            | str        | 2-3 sentence assessment                |
| strengths          | list\[str] | What the strategy does well            |
| weaknesses         | list\[str] | Problems to address                    |
| risk\_assessment   | str        | Risk evaluation                        |
| suggestions        | list\[str] | Actionable improvement recommendations |
| detailed\_analysis | str        | Multi-paragraph deep analysis          |
| raw\_metrics       | dict       | The metrics dict that was analyzed     |

### Scoring Guidelines

| Score | Meaning     | Characteristics                                      |
| ----- | ----------- | ---------------------------------------------------- |
| 9-10  | Exceptional | Sharpe > 2.0, drawdown < 10%, strong win rate        |
| 7-8   | Good        | Positive risk-adjusted returns, manageable drawdowns |
| 5-6   | Mediocre    | Marginal returns or concerning risk metrics          |
| 3-4   | Poor        | Negative returns or extreme drawdowns                |
| 1-2   | Failing     | Liquidation, massive losses, or non-functional       |

## Reading the Report

```python
report = vibetrading.strategy.analyze(results, strategy_code=code)

# Score & summary
print(f"Score: {report.score}/10")
print(report.summary)

# Strengths
for s in report.strengths:
    print(f"  + {s}")

# Weaknesses
for w in report.weaknesses:
    print(f"  - {w}")

# Risk assessment
print(report.risk_assessment)

# Actionable suggestions
for i, s in enumerate(report.suggestions, 1):
    print(f"  {i}. {s}")

# Full analysis text
print(report.detailed_analysis)
```

## Feeding Analysis Back to the Generator

The analysis result includes `format_for_llm()` — a method that converts the report into structured feedback suitable for a follow-up generation call:

```python
import vibetrading.strategy

# First generate and backtest
code = vibetrading.strategy.generate("BTC momentum with RSI", model="gpt-4o")
results = vibetrading.backtest.run(code, interval="1h", data=data)

# Analyze
report = vibetrading.strategy.analyze(results, strategy_code=code)

# Feed back into generator for improvement
feedback = report.format_for_llm()
improved_code = vibetrading.strategy.generate(
    "BTC momentum with RSI",
    model="gpt-4o",
    feedback=feedback,
)
```

Or use `vibetrading.strategy.generate()` and `vibetrading.strategy.analyze()` in a loop to iterate manually.

## Detail Levels

Control how much data is sent to the LLM:

```python
# Metrics only (fast, cheap)
report = vibetrading.strategy.analyze(results, detail_level="brief")

# Metrics + trade summary (default)
report = vibetrading.strategy.analyze(results, detail_level="standard")

# Metrics + trades + equity curve (most thorough)
report = vibetrading.strategy.analyze(results, detail_level="detailed")
```

## Using `BacktestAnalyzer` Directly

For repeated analysis or custom configuration:

```python
from vibetrading.strategy import BacktestAnalyzer

analyzer = BacktestAnalyzer(
    model="gpt-4o",
    temperature=0.3,
)

report1 = analyzer.analyze(results1, strategy_code=code1)
report2 = analyzer.analyze(results2, strategy_code=code2)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.vibetrading.dev/library/backtest-analysis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
