Skip to main content

Python framework optimized for running backtests on multiple assetss

Project description

btester - Trading Strategy Backtesting Framework

Test btester PyPi Downloads Codecov

btester is a Python framework optimized for running backtests on multiple assets and supports full portfolio backtesting. 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

  1. Define your custom trading strategy by creating a class that inherits from the Strategy abstract class.

  2. Implement the required methods in your custom strategy: init for initialization and next for the core strategy logic.

  3. Instantiate the Backtest class with your custom strategy, historical market data, and other parameters.

  4. Run the backtest using the run method, which returns a Result object 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, 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.

Feel free to explore and adapt these examples to suit your specific needs and trading strategies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

btester-0.1.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

btester-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file btester-0.1.1.tar.gz.

File metadata

  • Download URL: btester-0.1.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for btester-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9648e38749f3fb3810b6d428a6be24a649800130f5f7e24fef7b9e74a065c96b
MD5 e869ec5c3c9cac956293a434d2f56a4b
BLAKE2b-256 079f7f1e3c7724998d375d8451c79bef7d8f4dd19c8643da2be43f4b0e53759b

See more details on using hashes here.

File details

Details for the file btester-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: btester-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for btester-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3221582cc31a4476fdb9ec1d25d831e435ee87d64e39dd0fdecf7cf297568bb
MD5 35bed594e6d6f7706d157c54d1be76e0
BLAKE2b-256 cd806de4a391b5571f56b946c561d7bdb80a75e76ecf5107b3445d865f2d29a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page