btester - Multi-Assets Backtesting Framework
btester is a Python framework optimized for running backtests on multiple asset portfolios.
It provides tools for backtesting trading strategies based on historical market data. The framework includes classes for managing financial positions, completed trades, and a flexible abstract base class for implementing custom trading strategies.
Installation
You can install btester using pip. Simply run the following command:
pip install btester
Usage
-
Define your custom trading strategy by creating a class that inherits from the
Strategyabstract class. -
Implement the required methods in your custom strategy:
initfor initialization andnextfor the core strategy logic. -
Instantiate the
Backtestclass with your custom strategy, historical market data, and other parameters. -
Run the backtest using the
runmethod, which returns aResultobject containing backtest results.
Example Usage
# Example usage of the btester
from btester import Strategy, Backtest
import pandas as pd
# Define a custom strategy by inheriting from the abstract Strategy class
class MyStrategy(Strategy):
def init(self):
# Custom initialization logic for the strategy
pass
def next(self, i: int, record: Dict[Hashable, Any]):
# Custom strategy logic for each time step
pass
# Load historical market data
data = pd.read_csv('historical_data.csv', parse_dates=['Date'])
data.set_index('Date', inplace=True)
# Initialize and run the backtest
backtest = Backtest(strategy=MyStrategy, data=data, cash=10000, leverage=1.0, commission=0.01)
result = backtest.run()
# Access backtest results
returns_series = result.returns
completed_trades = result.trades
remaining_positions = result.open_positions
Examples
Check out the examples in the examples directory for additional use cases and demonstrations. The examples cover various scenarios and strategies to help you understand the versatility of the btester.
- Example 1: Multi-Assets Moving Average Crossover Strategy
- Example 2: Multi-Assets Breakout Strategy
- Example 3: Single Asset Moving Average Crossover Strategy
- Example 4: Single Asset Breakout Strategy
- Example 5: Index Short-Long Day Trading Strategy
Feel free to explore and adapt these examples to suit your specific needs and trading strategies.
Metadata
Release files for btester 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 | |
|---|---|---|---|
| btester-0.2.0.tar.gz | 10.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| btester-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.9 kB
Release files / btester-0.2.0.tar.gz
| Download URL | btester-0.2.0.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a51cf23b24f44ac9bf63f4cb3dc3d8a75c3346d780f6f2ac3f747f35cc93601b
|
|
BLAKE2b-256 checksum How to use checksums |
0f4c3e5c8640114fb1c5a90cf2a876be4c2a60cf98d959669aaee3ffa7024e83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / btester-0.2.0-py3-none-any.whl
| Download URL | btester-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5516e946dd8de26d974bc1396a2362a95504221a135024494b19bff94ec2b5c3
|
|
BLAKE2b-256 checksum How to use checksums |
4a437717854cd6d56561f24ee8cd9f147928ee5153a886cd9855f190e124645d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|