Skip to main content

A tool for simulating trading strategies against historical market data to evaluate performance and refine approaches before live trading.

Project description

Backtesting Package

A Python package for backtesting and analyzing trading strategies over various time periods. This package allows users to easily split data, run backtests, optimize strategies, and perform equity curve analysis.

Features

  • Split Data Periods: Split time series data into yearly, monthly, or custom periods.
  • Backtest: Run backtests on trading strategies across different periods.
  • Equity Curve Analysis: Analyze and visualize equity curves for deeper insight into performance.
  • Monte Carlo Simulation: Perform Monte Carlo simulations to evaluate strategy robustness.
  • Parameter Optimization: Optimize strategy parameters for better performance.

Installation

You can install the package using pip: pip install backtest_equities

Usage

Import the Package

import backtest_equities as bt

  1. Split Data into Periods You can split your time series data (such as stock price data) into periods for different types of analysis.

import pandas as pd

Assuming df is your time series data with a DateTime index

df = pd.read_csv('your-data.csv', index_col='Date', parse_dates=True)

Split the data into yearly periods

split_data = bt.split_periods(df, periods_for_split='yearly')

  1. Run a Backtest Over Periods Run a backtest on the split data for different periods.

Define your strategy parameters

side = 'long' SL_type = 'fixed' SL = 0.02 SL_spike_out = False TP = 0.05 TS = 0.03

Run backtest over the split periods

backtest_results = bt.backtest_over_periods(split_data, side, SL_type, SL, SL_spike_out, TP, TS)

Analyze the backtest results

for result in backtest_results: print(result)

  1. Optimize Strategy Parameters You can optimize your strategy by testing different parameters.

Define parameter ranges

param_ranges = { 'SL': [0.01, 0.02, 0.03], 'TP': [0.03, 0.05, 0.07] }

Optimize parameters

optimal_params = bt.optimize_parameters(df, param_ranges) print("Optimal Parameters:", optimal_params)

  1. Equity Curve Analysis Visualize and analyze the equity curves from the backtest.

Perform equity curve analysis

bt.equity_curve_analysis(backtest_results)

  1. Monte Carlo Simulation Run a Monte Carlo simulation to assess the strategy’s robustness.

Run Monte Carlo simulation with 1000 iterations

bt.monte_carlo_simulation(backtest_results, iterations=1000)

Example Workflow

Here’s a full workflow example of using the package to split data, backtest, and analyze results.

import pandas as pd import your_package_name as bt

Load data

df = pd.read_csv('your-data.csv', index_col='Date', parse_dates=True)

Split into yearly periods

split_data = bt.split_periods(df, periods_for_split='yearly')

Run backtest on each period

backtest_results = bt.backtest_over_periods(split_data, side='long', SL_type='fixed', SL=0.02, SL_spike_out=False, TP=0.05, TS=0.03)

Perform equity curve analysis

bt.equity_curve_analysis(backtest_results)

Run Monte Carlo simulation for further analysis

bt.monte_carlo_simulation(backtest_results, iterations=1000)

Contributing

Contributions are welcome! Please fork the repository, create a branch for your changes, and submit a pull request.

License

This project is dedicated to the public domain under the Unlicense. You can freely use, modify, and distribute this software without any restrictions.

Contact

For any issues or support, please reach out via the GitHub repository or email at jack.martin.codes@gmail.com.

Sections Explained:

  • Overview: General introduction to the package features.
  • Installation: Instructions on how to install the package using pip.
  • Usage: Step-by-step examples showing how to split periods, backtest, optimize parameters, and perform equity analysis.
  • Example Workflow: A full example that combines everything.
  • Contributing and License sections provide information for contributors and license details.

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

backtest_equities-0.3.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

backtest_equities-0.3.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file backtest_equities-0.3.1.tar.gz.

File metadata

  • Download URL: backtest_equities-0.3.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for backtest_equities-0.3.1.tar.gz
Algorithm Hash digest
SHA256 28aada8ec934902723477de42a5cae688a3d59c275e3875b86613084bbb2e183
MD5 d007775e866edf61337b199fca162cc5
BLAKE2b-256 d37ebf595df9f8fe5b2aa037c376a8fc681d0ff5a85f7199a65ae89100cc8ee3

See more details on using hashes here.

File details

Details for the file backtest_equities-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for backtest_equities-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f0b5c28d31842c7844bfec1ed68883b85d531d8b951d64055b8b42bdc23ec1b
MD5 8e3b681f037ba9659d7fdd400298c123
BLAKE2b-256 c71e624b8b6120a0f2b4358bf16016a3c1d4365818f8452305a682faff052725

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