feat: implement backtesting environment and refine strategy logic (v1.7.5)
- Extract strategy logic into a reusable PingPongStrategy class for bot and backtester. - Create src/strategies/backtest_engine.py for local historical testing using DB data. - Add BACKTESTING_GUIDE.md with instructions on how to use the new environment. - Update dashboard to show Net Realized PnL (including fees). - Verified bot and backtester compatibility with existing Docker setup.
This commit is contained in:
56
BACKTESTING_GUIDE.md
Normal file
56
BACKTESTING_GUIDE.md
Normal file
@ -0,0 +1,56 @@
|
||||
# Ping-Pong Bot: Backtesting & Optimization Guide
|
||||
|
||||
This guide explains how to use the local backtesting environment to test and optimize your BTC trading strategy using historical data from your PostgreSQL database.
|
||||
|
||||
## 1. Prerequisites
|
||||
The backtesting engine requires `pandas`, `numpy`, and `asyncpg`. These are already installed in your `btc_ping_pong_bot` Docker container.
|
||||
|
||||
To run the backtester, use the following command:
|
||||
```bash
|
||||
docker exec -it btc_ping_pong_bot python src/strategies/backtest_engine.py
|
||||
```
|
||||
|
||||
## 2. Backtest Engine (`backtest_engine.py`)
|
||||
The backtest engine reuses the core logic from `ping_pong_bot.py` via the `PingPongStrategy` class.
|
||||
|
||||
### Key Features:
|
||||
* **Virtual Exchange:** Simulates a $1,000 account with customizable leverage and fees.
|
||||
* **Fee Simulation:** Applies a 0.05% taker fee (configurable) to every entry and exit.
|
||||
* **Mark-to-Market:** Calculates real-time equity based on current price and position size.
|
||||
* **Data Sourcing:** Automatically pulls the last 10,000 candles from your `btc_data` database.
|
||||
|
||||
### How to Run:
|
||||
```bash
|
||||
# From the project root
|
||||
docker exec -it btc_ping_pong_bot python src/strategies/backtest_engine.py
|
||||
```
|
||||
|
||||
## 3. Strategy Optimization (Optional)
|
||||
To find the absolute best parameters for RSI and Hurst, you can use **Optuna**.
|
||||
|
||||
### Installation:
|
||||
Inside the Docker container:
|
||||
```bash
|
||||
docker exec -it btc_ping_pong_bot pip install optuna
|
||||
```
|
||||
|
||||
### Planned Optimizer (`optimize_strategy.py`):
|
||||
Once installed, we can implement an optimization script that searches for:
|
||||
* `rsi_period`: 7 to 21
|
||||
* `hurst_multiplier`: 1.2 to 2.5
|
||||
* `partial_exit_pct`: 0.05 to 0.30
|
||||
|
||||
## 4. Local DB Data
|
||||
The engine connects to your local PostgreSQL DB using the credentials in your `.env` file. It specifically queries the `candles` table for the symbol and interval defined in `config/ping_pong_config.yaml`.
|
||||
|
||||
## 5. Interpreting Results
|
||||
* **Final Equity:** Your simulated account balance after all trades.
|
||||
* **ROI:** Return on Investment (Percentage).
|
||||
* **Total Fees:** Total cost paid to the "Virtual Exchange". High fees indicate over-trading.
|
||||
* **Trade Count:** Total number of Enter/Exit signals triggered.
|
||||
|
||||
## 6. Next Steps
|
||||
1. Run the backtester to see baseline performance.
|
||||
2. Adjust parameters in `config/ping_pong_config.yaml`.
|
||||
3. Rerun the backtest to see the impact of your changes.
|
||||
4. (Optional) Ask me to implement the `optimize_strategy.py` script once you have Optuna installed.
|
||||
Reference in New Issue
Block a user