Skip to main content

Backtest Framework

This Python backtesting framework allows you to evaluate trading strategies on historical market data. It supports stop-loss (SL), take-profit (TP), trailing stop (TS), and various risk management features. You can backtest both long and short strategies with configurable settings for transaction costs and balance management.

Features Backtest long and short trading strategies Configurable stop-loss (SL) types: ATR-based, standard deviation (SD)-based, and percentage-based Take-profit and trailing stop functionality Risk management through percentage-based position sizing Transaction costs handling Equity curve and drawdown tracking Detailed trade history, including entry and exit points, stop-loss updates, and trade returns

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org

Installation

Install cerebro_backtester with pip

  pip install cerebro-backtester

Usage/Examples

Example 1: Simple Long Strategy with ATR-based Stop Loss

import pandas as pd
from backtest import Backtest

# Example historical data
data = pd.DataFrame({
    'Close': [100, 102, 101, 104, 106],
    'High': [101, 103, 102, 105, 107],
    'Low': [99, 100, 100, 103, 105],
    'buy_signal': [1, 0, 0, 1, 0],
    'sell_signal': [0, 0, 0, 0, 1]
})

# Configure and run the backtest
backtest = Backtest(
    info=data,
    side='long',
    SL_type='atr',
    SL=2,  # 2x ATR for stop-loss
    SL_spike_out=True,
    TP=3,  # 3x ATR for take-profit
    TS=True  # Enable trailing stop
)
backtest.run()

# Analyze results
print(backtest.info[['equity_curve', 'drawdown', 'trade_return_hist']])

Example 2: Short Strategy with Standard Deviation-based Stop Loss

import pandas as pd
from backtest import Backtest

# Example historical data
data = pd.DataFrame({
    'Close': [100, 102, 101, 98, 96],
    'High': [101, 103, 102, 100, 97],
    'Low': [99, 100, 100, 95, 94],
    'buy_signal': [0, 0, 0, 1, 0],
    'sell_signal': [1, 0, 1, 0, 0]
})

# Configure and run the backtest
backtest = Backtest(
    info=data,
    side='short',
    SL_type='sd',
    SL=1,  # 1x standard deviation for stop-loss
    SL_spike_out=False,
    TP=2,  # 2x standard deviation for take-profit
    TS=False  # Disable trailing stop
)
backtest.run()

# Analyze results
print(backtest.info[['equity_curve', 'drawdown', 'trade_return_hist']])

Authors

Nathan Schmidt is a programmer with years of experience in software development. Specializing in innovative programming solutions, Nathan has a commitment to open-source development and community collaboration. Known for his passion for clean code and efficiency, Nathan continues to contribute to the field of software engineering with a focus on impactful, real-world applications.

Support

For support, email nathan.schmidt.ns89@gmail.com or raise a Github issue.

Release files for cerebro-backtester 10.0.4

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

Source distribution (sdist)

Source distribution for cerebro-backtester 10.0.4
File Size Uploaded
cerebro_backtester-10.0.4.tar.gz 15.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cerebro-backtester 10.0.4
File Interpreter ABI Platform
cerebro_backtester-10.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 29.6 kB

Release files / cerebro_backtester-10.0.4.tar.gz

Download URL cerebro_backtester-10.0.4.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
273f461d1e396b00548599c2b78e8648304cd492aac09c1104674c4f50c5bbef
BLAKE2b-256 checksum
How to use checksums
0772a6512b5d8093186ec57e110f760cf12979ba9bceb9e01732f542539f5786
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / cerebro_backtester-10.0.4-py3-none-any.whl

Download URL cerebro_backtester-10.0.4-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3612336f498fdf7b3129d3887d0a7584d17f1f2bac4acd9b2648a6864791a47
BLAKE2b-256 checksum
How to use checksums
d673aa7772fd4b1e0a837827b4fb176f9c98be26e5d991f1db670238ba95feaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

10.0.4 This release

2 release files

10.0.3

2 release files

10.0.2

2 release files

10.0.1

2 release files

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