Skip to main content

sf-backtester

SLURM-based parallel backtesting for quantitative finance. Distributes MVO optimization across compute nodes, processing one year per task.

Installation

pip install sf-backtester

Usage

CLI

# Run backtest
sf_backtester run config.yml

# Run dynamic backtest
sf_backtester run-dynamic config.yml

# Preview sbatch script without submitting
sf_backtester run config.yml --dry-run

Python API

from sf_backtester import BacktestRunner, BacktestConfig, SlurmConfig

slurm_config = SlurmConfig(
    n_cpus=8,
    mem="32G",
    time="03:00:00",
    mail_type="BEGIN,END,FAIL",
    max_concurrent_jobs=30,
)

config = BacktestConfig(
    signal_name="momentum",
    gamma=50,
    data_path="/path/to/alphas.parquet",
    project_root="/path/to/project",
    byu_email="you@byu.edu",
    constraints=["ZeroBeta", "ZeroInvestment"],
    slurm=slurm_config,
)

runner = BacktestRunner(config)

# Submit to SLURM
runner.submit()

Or load from YAML:

from sf_backtester import BacktestRunner

runner = BacktestRunner.from_yaml("config.yml")

runner.submit()

You can also pass a DataFrame directly:

from sf_backtester import BacktestRunner
import polars as pl

runner = BacktestRunner.from_yaml("config.yml")

data = pl.read_parquet("alphas.parquet")

runner.submit(data=data)

Configuration

Standard backtest (YAML)

signal_name: momentum
gamma: 500
data_path: /path/to/alphas.parquet
project_root: /path/to/project
byu_email: you@byu.edu

constraints:
  - ZeroBeta
  - ZeroInvestment

slurm:
  n_cpus: 8
  mem: 32G
  time: "03:00:00"
  mail_type: BEGIN,END,FAIL
  max_concurrent_jobs: 31

Dynamic backtest (YAML)

The dynamic backtest maximizes expected alpha in active weight space subject to an annualized tracking-error cap and the exact constraints supplied. The cap need not bind when other constraints limit the portfolio. Specify every intended mandate explicitly; for a long-only portfolio with zero active investment and beta, use the following configuration:

signal_name: momentum
target_active_risk: 0.05
active_weights: true
data_path: /path/to/alphas.parquet
project_root: /path/to/project
byu_email: you@byu.edu

constraints:
  - ZeroInvestment
  - LongOnly
  - ZeroBeta

slurm:
  n_cpus: 8
  mem: 32G
  time: "06:00:00"
  mail_type: BEGIN,END,FAIL
  max_concurrent_jobs: 31

Set active_weights: true to save active weights h, or false to save total weights h + b. This changes the output representation, not the optimization constraints. Beta neutrality is controlled exclusively by including ZeroBeta in constraints; the former boolean beta flag has been removed from the configuration and worker interfaces.

Constraint specifications

Both standard and dynamic configurations accept constraint-name strings, parameterized mappings, or a mixture of the two. For example, an active extension with normalized benchmark weights can use:

constraints:
  - ZeroInvestment
  - name: GrossLeverage
    max_leverage: 1.6

The leverage limit must be a finite, positive real number; booleans are invalid. The name-only entry GrossLeverage uses its default limit of 1.0. With net total investment of 1, a gross cap of 1.6 permits up to 130% long and 30% short exposure; the cap alone does not set net investment or require the portfolio to reach those exposures.

Constraint Rule
ZeroInvestment Active/direct weights sum to zero
ZeroBeta Active/direct beta exposure equals zero
FullInvestment Total weights sum to one
LongOnly Total weights are nonnegative
UnitBeta Total beta exposure equals one
NoBuyingOnMargin Sum of positive total weights is at most one
GrossLeverage Sum of absolute total weights is at most max_leverage

In the dynamic optimizer, total-space constraints apply to h + b when benchmark weights b are provided and to h otherwise. Active-space constraints always apply to h. The standard optimizer applies these rules directly to its optimized weights without benchmark context.

An empty list, constraints: [], adds no mandate constraints. In a dynamic backtest, only the risk cap remains: dollar neutrality, beta neutrality, full investment, and long-only holdings are not implied. This can produce net exposure and short positions, and the risk cap need not bind. In a standard backtest, an empty list retains the mean-variance objective and its gamma risk penalty.

The same helper validates and constructs constraints before submission and on each worker. Malformed specifications, unknown names, unsupported parameters, and invalid limits fail before job preparation or submission. Configurations are validated again before job preparation so edits made after construction are also checked.

Worker command-line arguments

Generated SLURM scripts send the specifications as one shell-quoted JSON argument. Both worker modules accept --constraints-json or the legacy names-only --constraints, but not both:

# Parameterized specifications (append other required worker arguments)
--constraints-json '["ZeroInvestment", {"name": "GrossLeverage", "max_leverage": 1.6}]'

# Existing names-only form
--constraints ZeroInvestment ZeroBeta

# Explicitly empty constraints, using either form
--constraints-json '[]'
--constraints

JSON may not contain NaN or infinities. For manual shell commands, preserve the single quotes around the JSON payload so it reaches the worker as one argument.

Data format

Input parquet must have date, barrid (asset identifier), and alpha columns. Include predicted_beta when using ZeroBeta or UnitBeta.

For dynamic backtests, include benchmark_weight to define the benchmark on each asset/date. If the column is absent, the worker loads benchmark weights automatically, joins on date and barrid, and fills unmatched weights with zero. Loading a benchmark does not add any constraints. Constraints use weights aligned to the input universe, so ensure that universe and its benchmark weights match the intended mandate; for example, ZeroInvestment produces net total investment of one only when the aligned benchmark weights sum to one.

For standalone long-short research that applies gross or long-exposure limits directly to h, provide an explicit benchmark_weight column of zeros. Otherwise automatic benchmark loading will make total-space limits apply to h + b. With an empty constraint list the benchmark still loads when absent, but it does not anchor the solution; it only affects total-weight output.

Output is one parquet per year in output_dir/{year}.parquet containing portfolio weights.

Publishing

  1. Bump the version
uv version v*.*.*
  1. Add changes (it can be just the version change)
git add .
git commit -m "Bumped version."
  1. Tag the branch
git tag v*.*.*
  1. Push to origin
git push --tags

Metadata

Release files for sf-backtester 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sf-backtester 0.2.2
File Size Uploaded Provenance Actions
sf_backtester-0.2.2.tar.gz 13.5 kB Without provenance Details for sf_backtester-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sf-backtester 0.2.2
File Interpreter ABI Platform Actions
sf_backtester-0.2.2-py3-none-any.whl Python 3 none any Details for sf_backtester-0.2.2-py3-none-any.whl

Total release size: 28.1 kB

Release files / sf_backtester-0.2.2.tar.gz

Download URL sf_backtester-0.2.2.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a32cfcebc27c6a8ce35d84fd90f86cc3685cc42a19f7cee88dcaabf9d11dc328
BLAKE2b-256 checksum
How to use checksums
a65ab560996513f69e42e8bb4b7badc9eef67bdac99bf07d2d1eb8c69338c614
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / sf_backtester-0.2.2-py3-none-any.whl

Download URL sf_backtester-0.2.2-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e66ea9f3d4226267891c42a17825d770ef00f887e05f1dbc5e84d7daef04793b
BLAKE2b-256 checksum
How to use checksums
fe0e8567e497add799635b7136b1c6ff04da88003b5a4f6da539fbcb4ef2f6ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for sf-backtester 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page