This repository provides two powerful backtesting methods for evaluating algorithmic trading strategies:
- Static Backtesting: Tracks performance with a fixed capital base.
- Compound Backtesting: Adjusts the trading capital dynamically based on account growth.
These tools allow traders and quants to assess the profitability and performance of trading strategies using historical data. The backtester handles trade entries, exits, and calculates key performance metrics like cumulative returns, profit and loss (PnL), and trade counts.
- Static Backtesting:
- Calculates profits and losses using a constant capital base.
- Tracks cumulative returns for a clear view of strategy performance.
- Compound Backtesting:
- Adjusts capital dynamically based on account balance.
- Incorporates leverage and trading costs for realistic simulations.
- Customizable Parameters:
- Leverage levels.
- Trading costs.
- Outputs results in a CSV file for further analysis.
To use the backtester, ensure you have the following dependencies installed:
- Python 3.7+
pandasnumpy
You can install the required packages using:
pip install pandas numpyClone the repository to your local machine:
git clone https://github.com/your-username/backtesting-strategies.git
cd backtesting-strategies-
perform_backtest_static- Runs a static backtest using the provided CSV file.
- Example:
perform_backtest_static('your_data.csv', leverage=2)
- Output: Creates a file named
final_file_your_data.csvwith results.
-
perform_backtest_compound- Runs a compound backtest using the provided CSV file.
- Example:
perform_backtest_compound('your_data.csv', leverage=2)
- Output: Creates a file named
final_file_compound_your_data.csvwith results.
The input CSV file should have the following columns:
datetime: The timestamp of the data (e.g.,YYYY-MM-DD HH:MM:SS).close: Closing price of the asset.net_position: Net position for the trade (1for long,-1for short,0for no position).volume: (Optional) Volume data for calculations.
Example:
| datetime | close | net_position | volume |
|---|---|---|---|
| 2023-01-01 10:00:00 | 100.50 | 1 | 2000 |
| 2023-01-01 10:05:00 | 101.00 | 0 | 1500 |
| 2023-01-01 10:10:00 | 99.75 | -1 | 2500 |
from backtester import perform_backtest_static
# Run the static backtest
perform_backtest_static('data.csv', leverage=1.5)from backtester import perform_backtest_compound
# Run the compound backtest
perform_backtest_compound('data.csv', leverage=1.5)Both functions produce a CSV file with the following columns:
datetime: Timestamp of the event.returns: Per-trade returns.cumulative_return: Cumulative PnL over the test period.- (For compound backtest)
amount_in_account: Tracks the account balance over time.
Contributions are welcome! If you'd like to add features, optimize performance, or fix bugs:
- Fork the repository.
- Create a new branch (
feature/my-feature). - Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Happy backtesting! 🚀