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.5.tar.gz (13.0 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.5-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: backtest_equities-0.3.5.tar.gz
  • Upload date:
  • Size: 13.0 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.5.tar.gz
Algorithm Hash digest
SHA256 ac77c0289f55c6c2e51d6b9ae39708d0911f74524e903980f9359a03f3c6889a
MD5 ad00f08e9e9721bb94219802ec4e3a6e
BLAKE2b-256 6908d4d61b93d919c6eaf36605a4a0642c1a24cc74755ff71e95b785c364c6c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for backtest_equities-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5beccca97a7e03633c5bfbaa89906268ccbb166b595776194f162c4b79977056
MD5 66f5cb3ece8f58bcf6c7eb64f7fcc110
BLAKE2b-256 1e3d5cd3db81ced3611184d62c4f9d1feb51eef395a6832288abbd8d85b4ef89

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