Data in. Trades out. Nothing hidden.

Simple Backtest is a Python library for testing a trading idea on historical prices before risking real money. A backtest is a simulation: it applies your buy and sell rules to past data, then shows what would have happened.

Bring a pandas table with each period's open, high, low, and close prices (volume is optional) and a Strategy—a small Python class that decides when to buy or sell. You get every simulated transaction, every cash change, the account value over time, and 20 summary measurements.

pip install simple-backtest

v0.4.0Python 3.10+MITsimulation only

Example: account value over timeillustrative

Illustrative account value over time A made-up account value that falls early, then rises over four years. Ringed points mark simulated purchases or sales. The values only demonstrate the chart format and make no performance claim.
Made-up values to show the output format. Not a real result or return claim.
From past prices to a result you can inspect.

You provide

  • past prices in pandas
  • buy and sell rules in Python
  • starting cash and costs

The library simulates

  • your rules, one period at a time
  • purchases, sales, and fees
  • cash and what you own

You can inspect

  • every simulated purchase and sale
  • account value over time
  • 20 summary measurements
  • interactive charts

Not simulated

  • margin or leverage
  • short selling
  • contract multipliers
  • funding costs
  • currency conversion
  • live broker orders

What the simulation assumes.

These eight rules explain how the library handles money, purchases, sales, and costs. Each one is visible in the source, so you can understand exactly how a result was made.

Simple Backtest accounting rules and their configurable options
Asset One asset at a time, bought with available cash sales use the oldest purchases first (FIFO)
Amount Fractional units allowed shares is a generic quantity
Price used Bar open open · close · typical · custom
Trading fee 0.1% of each purchase or sale by default percentage · flat fee · tiered · custom
Cash A purchase that costs more than the available cash is rejected the library reports the problem instead of changing the order
Yearly results The dates are used to convert results to a yearly rate override with periods_per_year
Summary 20 measurements per run returned as a Python dictionary with every result
Problems The simulation stops and reports them by default error_policy="continue" records them and keeps going

Try it with a pandas price table.

The package runs on your computer with data from a CSV, an API, or any other source. There is no account to create and no required data provider. The result is an ordinary Python object you can inspect immediately.

pip install simple-backtest

quick_start.pyPython

from simple_backtest import (
    Backtest,
    BacktestConfig,
    MovingAverageStrategy,
)

strategy = MovingAverageStrategy(
    short_window=10,
    long_window=30,
    shares=10,
)

config = BacktestConfig.default(
    initial_capital=10_000
)

backtest = Backtest(data, config)
results = backtest.run([strategy])

print(results.compare())

Learn with six examples.

Start with loading prices and running your first set of buy and sell rules. Each notebook adds one idea, ending with tools for comparing many variations over time.

  1. 01 Basic usage load data, run one strategy, read the result
  2. 02 Candle strategies buy and sell rules based on price-bar shapes
  3. 03 Technical analysis indicator-driven entries and exits
  4. 04 Machine learning use a fitted model to decide when to buy or sell
  5. 05 Commissions percentage, flat, tiered, and custom costs
  6. 06 Advanced optimization compare settings across rolling time windows