Quant Buffet API

Sandbox rules

Allowed imports, the exact builtins you get, blocked calls, and hard runtime limits.

Lab runs execute your code in `backtest/sandbox_runner.py`. The source is parsed with `ast`, stripped of bootstrap boilerplate by lab_sanitize, then exec'd in a restricted namespace. Understanding these limits explains most confusing failures.

Allowed import roots

CategoryRoots
Quant Buffetbacktest (data, engine, metrics, templates, universes, lab_sanitize)
Numericsnumpy, np, pandas, pd
Standard librarymath, json, typing, collections, dataclasses, functools, itertools, datetime, re, statistics, decimal
Language__future__

The check is on the root module, so from backtest.templates import make_sma_trend and import collections.abc both pass. Anything else fails at validation time with *Import blocked: …*.

Blocked

  • Standard-library modules outside the allow-list: os, sys, pathlib, subprocess, socket, pickle, random, time, …
  • All third-party packages: requests, sklearn, scipy, matplotlib, yfinance, statsmodels, …
  • Direct calls to eval, exec, compile, open, __import__, input, breakpoint — rejected by the AST walk.
  • async / await / async for / async with — *Async code is not supported in the sandbox.*
  • Source files larger than 80 KB — *Code too long (max 80KB).*

Builtins you get

GroupAvailable
Numericabs, min, max, sum, round, float, int, bool
Sequencelen, range, enumerate, zip, map, filter, sorted
Containerslist, dict, set, tuple, str
Introspectionisinstance, issubclass, hasattr, type, print
ExceptionsException, ValueError, TypeError, KeyError, RuntimeError, StopIteration

Builtins you do NOT get

MissingUse instead
any(...), all(...)series.any(), series.all(), or sum(1 for … ) > 0
reversed(...)sorted(x, reverse=True) or x[::-1]
getattr, setattr, vars, globals, localsRestructure — attribute plumbing is not needed here
next, iter, frozenset, divmod, powComprehensions, set, // and %, **
IndexError, AttributeError, ZeroDivisionErrorCatch Exception, or guard the condition instead

Runtime limits

LimitValueError when exceeded
Source size80 KB*Code too long (max 80KB).*
Symbols in ASSETS15*Too many symbols (max 15).*
Minimum price rows30 after load*Not enough price history for the selected assets/start date.*
Minimum equity points20 after the run*Equity curve too short — strategy may never trade.*
Equity chart points90 (downsampled)n/a — the full curve still drives the metrics
Default start date2000-01-01n/a

Source sanitisation

backtest/lab_sanitize.py runs before validation and removes the scaffolding that catalog-generated files carry, so the same file works both as a CLI script and as lab input. It strips:

  • everything from def main( onwards, including the __main__ guard;
  • import sys, from pathlib …, ROOT = …, and any line touching sys.path;
  • the leading module docstring and from __future__ line;
  • generator metadata assignments such as SLUG, LOCALE, TITLE, TEMPLATE, FIDELITY, DATA_SOURCE.