Quant BuffetRelax, Not Over Thinking

Quant Buffet API

Overview

What the Quant Buffet backtest API is and how lab strategies are structured.

Quant Buffet strategies run inside a Python sandbox that uses the in-house backtest.* package. You write daily-rebalance logic; the engine simulates fills, tracks equity, and computes performance metrics.

What you write

  • Module-level `ASSETS` — list of ETF/crypto tickers (max 15, whitelist only).
  • `make_on_day(prices)` — builds signals from a price panel and returns (on_day, ready).
  • `on_day(engine, dt)` — called each trading day; call engine.set_target_weights() to rebalance.
  • `ready` — first date when signals are valid (usually after your longest lookback).

Execution flow

  1. Sandbox validates imports and AST (no file I/O, no network from your code).
  2. load_daily_prices(ASSETS, start=…) downloads or reads cached OHLCV.
  3. make_on_day(prices) returns your daily callback and warmup date.
  4. PortfolioEngine.run(on_day, start=ready) walks the calendar and records trades.
  5. compute_metrics(equity, …) produces CAGR, Sharpe, drawdown, etc.

Minimal working strategy

from __future__ import annotations

import numpy as np
import pandas as pd

from backtest.data import load_daily_prices
from backtest.engine import EngineConfig, PortfolioEngine
from backtest.metrics import compute_metrics

ASSETS = ["SPY", "QQQ", "TLT", "GLD", "BIL"]


def make_on_day(prices: pd.DataFrame):
    cols = [c for c in ASSETS if c in prices.columns]
    sma = prices[cols].rolling(200, min_periods=200).mean()
    state = {"last": None}

    def on_day(engine: PortfolioEngine, dt: pd.Timestamp) -> None:
        if sma.loc[dt].isna().all():
            return
        key = (dt.year, dt.month)
        if state["last"] == key:
            return
        state["last"] = key
        long = [s for s in cols if prices.at[dt, s] > sma.at[dt, s]]
        w = 1.0 / len(long) if long else 0.0
        engine.set_target_weights(dt, {s: w for s in long})

    ready = sma.dropna(how="all").index.min()
    return on_day, ready

Documentation map

  • Lab contract — required functions, return types, common mistakes.
  • Data APIload_daily_prices, caching, panel shape.
  • Engine APIPortfolioEngine, EngineConfig, set_target_weights.
  • Metrics APIcompute_metrics output fields.
  • Sandbox rules — allowed imports, size limits, blocked builtins.
  • Universes — ETF whitelist and named books.
  • Templates — pre-built make_* factories in backtest.templates.
  • Examples — momentum, mean reversion, and template-based patterns.