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
- 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')
- 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)
- 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)
- Equity Curve Analysis Visualize and analyze the equity curves from the backtest.
Perform equity curve analysis
bt.equity_curve_analysis(backtest_results)
- 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.
Metadata
Release files for backtest-equities 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| backtest_equities-0.3.5.tar.gz | 13.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| backtest_equities-0.3.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.1 kB
Release files / backtest_equities-0.3.5.tar.gz
| Download URL | backtest_equities-0.3.5.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ac77c0289f55c6c2e51d6b9ae39708d0911f74524e903980f9359a03f3c6889a
|
|
BLAKE2b-256 checksum How to use checksums |
6908d4d61b93d919c6eaf36605a4a0642c1a24cc74755ff71e95b785c364c6c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / backtest_equities-0.3.5-py3-none-any.whl
| Download URL | backtest_equities-0.3.5-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5beccca97a7e03633c5bfbaa89906268ccbb166b595776194f162c4b79977056
|
|
BLAKE2b-256 checksum How to use checksums |
1e3d5cd3db81ced3611184d62c4f9d1feb51eef395a6832288abbd8d85b4ef89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|