Backtesting

Run a Backtest

from datetime import datetime, timezone

import vibetrading.backtest
import vibetrading.tools

start = datetime(2025, 1, 1, tzinfo=timezone.utc)
end = datetime(2025, 7, 1, tzinfo=timezone.utc)

data = vibetrading.tools.download_data(
    ["BTC"],
    exchange="binance",
    start_time=start,
    end_time=end,
    interval="1h",
)

engine = vibetrading.backtest.BacktestEngine(
    start_time=start,
    end_time=end,
    interval="1h",
    exchange="binance",
    initial_balances={"USDC": 10000},
    data=data,
)

results = engine.run(strategy_code)

print(results["metrics"])

Quick Backtest with run()

For a simpler interface, use the run() shortcut:

Backtest Results

engine.run() returns a dictionary containing:

Metrics

Metric
Description

total_return

Total portfolio return (decimal)

max_drawdown

Maximum peak-to-trough drawdown

sharpe_ratio

Annualized Sharpe ratio

win_rate

Percentage of profitable closed trades

number_of_trades

Total number of trades executed

funding_revenue

Net funding payments received/paid

total_tx_fees

Total transaction fees paid

average_trade_duration_hours

Mean holding period

Supported Intervals

1s, 1m, 5m, 15m, 30m, 1h, 6h, 1d

Supported Exchanges

Data is fetched from exchanges via CCXT. Download data first, then pass it to the backtest engine:

Next Steps

After backtesting, you can:

  • Analyze results — Use an LLM to score performance and get actionable improvement suggestions.

  • Iterate manually — Use vibetrading.strategy.generate() and vibetrading.strategy.analyze() in a loop to refine based on feedback.

Last updated