OpenBacktesting
Historical backtesting in Python, from strategy rules to inspectable results.
English · 简体中文 · Español · Português (Brasil) · Français · Deutsch · Italiano · हिन्दी
Write bar-by-bar strategies, evaluate them on historical market data, and inspect orders, trades, equity, and risk statistics. OpenBacktesting is a standalone Python package with indicators, parameter optimization, Portfolio reports, and plotting-data export.
Python 3.12–3.14 · Version 0.2.0 · Distribution and import: openbacktesting
Quick start · Manual · Examples · Report an issue
Top sponsor — Pineify
Pineify is OpenBacktesting's top paid sponsor. Its financial support helps fund this project.
Pineify is an AI trading toolkit for building indicators and strategies across multiple platforms. It brings together Pine Script AI, a Finance Agent, AI stock picking, market screening, chart analysis, and MCP tools. Explore Pineify →
What you can do
- Build strategies: use a bar-by-bar lifecycle, typed inputs, built-in indicators, and composable helpers.
- Inspect execution: examine orders, fills, position sizing, fees, closed trades, and open lots.
- Analyze results: read pandas statistics, equity curves, and immutable Portfolio reports for each run.
- Compare parameters: run grid searches or optional SAMBO optimization, save experiment results, and export plotting data for your own renderer.
Quick start
Install uv and use Python >=3.12,<3.15. From a terminal:
git clone https://github.com/lucidcog/openbacktesting.git
cd openbacktesting
uv sync --locked
uv run --locked python examples/advanced/native_run.py
The example uses bundled GOOG historical data; no market-data download is required. It prints return, trade count, maximum drawdown, and result tables. See your first backtest for a walkthrough.
To install the checked-out source into an existing Python environment instead:
python -m pip install .
The base dependencies are NumPy, pandas, and pandas-ta-classic==0.8.32. For SAMBO optimization, use python -m pip install '.[optimize]' or uv sync --locked --extra optimize. These are source-installation instructions; they do not assume a PyPI release. Build and distribution details.
Minimal backtest
Save this as quickstart.py in the repository root and run uv run --locked python quickstart.py:
from openbacktesting import Backtest, Strategy
from openbacktesting.test import GOOG
class BuyAndHold(Strategy):
def init(self):
pass
def next(self):
if self.bar_index == 0:
self.buy(qty=1)
result = Backtest(GOOG, BuyAndHold, end_policy="close_at_last_close").run()
print(result[["Return [%]", "# Trades", "Max. Drawdown [%]"]])
This strategy requests one unit on the first bar. Default execution occurs at the next bar's open; close_at_last_close closes the remaining position at the final close.
run() returns a pandas Series. Use result["_trades"] for trades and result["_equity_curve"] for equity history. See the results guide for the full field list and Portfolio export; underscore-prefixed fields are native diagnostics, not a stable cross-language contract.
Documentation
The detailed manual and examples are currently in English.
The online manual includes sidebar navigation, page outlines, and full-text search. To preview documentation changes locally:
uv run --locked --group docs mkdocs serve
Open http://127.0.0.1:8000. The server reloads when documentation changes. Build a static site with bash scripts/build_docs.sh; the output is in build/docs/. See Cloudflare Pages deployment for the build settings.
| Topic | Reference |
|---|---|
| Getting started | First backtest · Example index |
| Strategies and data | Strategy lifecycle · Market data · Strategy API · Backtest API |
| Orders and positions | Order, Position, and Trade |
| Indicators | Indicator catalog |
| Execution and optimization | Running strategies · Parameter experiments |
| Results and plotting | Results and Portfolio · Plotting-data export |
| Development | Verification and builds · Current limitations |
Scope and compatibility
- The trading model is single asset, single settlement currency. Portfolio reports describe one run; they are not multi-asset portfolio accounts.
- The package provides computation and plotting data. It includes no chart renderer, live-trading engine, code sandbox, or Pine Script converter, and does not promise Pine-equivalent semantics. Run only trusted strategy code.
- Windows support and full Pine comparisons have not been validated. See current limitations for execution and market-model boundaries.
The distribution and import name are both openbacktesting; there are no legacy-name entry points. Old module-path pickles are not migrated automatically, earlier branded archives are not guaranteed to load, and experiments created under an old execution identity must be rerun. Migration and provenance details.
Support and contributing
Use GitHub Issues for bugs, questions, and feature requests. Include your Python/package versions, a minimal reproducer, and expected versus actual behavior. Contributions are welcome through pull requests.
Before submitting a code change, run:
uv sync --locked --extra test
uv run --locked python -m pytest
uv run --locked python -m openbacktesting.test
uv run --locked python scripts/check_package.py
Follow the development guide for the complete checks. Public behavior changes need corresponding documentation, runnable examples, and execution validation. Code, comments, and technical documentation default to English; localized README content is intentional.
Translations
This English README is the source of truth. Translations cover Simplified Chinese, Spanish, Brazilian Portuguese, French, German, Italian, and Hindi. Use the language links above, and follow the translation guide when updating them. Keep commands, API identifiers, capability boundaries, and sponsor attribution consistent across languages.
License and provenance
The project license declaration is Apache-2.0. Third-party materials retain their original licensing terms, including the indicator reference data.
Metadata
Release files for openbacktesting 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| openbacktesting-0.2.0.tar.gz | 1.1 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openbacktesting-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.5 MB
Release files / openbacktesting-0.2.0.tar.gz
| Download URL | openbacktesting-0.2.0.tar.gz |
|---|---|
| Size | 1.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
917ed8c33590fd5819e28c6537577a499c14790212fa2ecbee274ce296467fc6
|
|
BLAKE2b-256 checksum How to use checksums |
4480a5ffa0cf83f7f8ba9c51e34fee375a3b91ca2cc933a523987ae783006f5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / openbacktesting-0.2.0-py3-none-any.whl
| Download URL | openbacktesting-0.2.0-py3-none-any.whl |
|---|---|
| Size | 441.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7131281ebb8000bde99d3de1ff328360e483a242d56f106b746b822fd375dfbe
|
|
BLAKE2b-256 checksum How to use checksums |
3edd2a12d3ceb70322c2734f91c9d62cf1c915e36803c7dde10c12f670eab3ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|