Strategy Evolution

One call to iteratively improve a strategy through generate → backtest → analyze → regenerate feedback loops.

┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────┐
│ Generate │ ──▶ │ Backtest │ ──▶ │ Analyze  │ ──▶ │ Feedback │
└──────────┘     └──────────┘     └──────────┘     └─────┬────┘
      ▲                                                  │
      └──────────────────────────────────────────────────┘

Quick Usage

import vibetrading

result = vibetrading.evolve(
    "BTC momentum strategy with RSI and SMA crossover, 3x leverage",
    iterations=3,
    model="gpt-4o",
    interval="1h",
)

print(f"Best score: {result.best_score}/10")
print(f"Improved: {result.improved}")
print(result.best_code)

How It Works

Each iteration:

  1. Generate — LLM produces strategy code from the prompt (or regenerates from prior feedback).

  2. Validate — Static analysis catches structural errors before execution.

  3. Backtest — Runs the strategy against historical data.

  4. Analyze — LLM evaluates backtest results: score (1-10), strengths, weaknesses, suggestions.

  5. Feedback — Analysis is formatted and fed back to the generator as context for the next iteration.

The loop stops when iterations is reached or when the score meets score_threshold (default: 8).

EvolutionResult

Field
Type
Description

best_code

str

Strategy code from the highest-scoring iteration

best_analysis

BacktestAnalysisResult

Analysis from the best iteration

best_backtest

dict

Backtest results from the best iteration

best_iteration

int

Index of the best iteration

history

list[EvolutionStep]

Full record of all iterations

total_iterations

int

Number of iterations executed

prompt

str

Original natural language prompt

Properties

EvolutionStep

Each iteration produces an EvolutionStep:

Field
Type
Description

iteration

int

0-based iteration index

code

str

Generated strategy code

validation_passed

bool

Whether static validation passed

backtest_results

dict | None

Raw backtest output

analysis

BacktestAnalysisResult | None

LLM analysis result

error

str | None

Error message if iteration failed

score

int (property)

Score from analysis, or 0

Parameters

Tracking Progress with Callbacks

Use on_iteration to monitor each step:

Using StrategyEvolver Directly

For advanced control (separate models for generation vs analysis, custom kwargs):

Pre-loading Data

Avoid re-downloading data on every iteration by passing data:

Last updated