- 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.
2.4 KiB
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:
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_datadatabase.
How to Run:
# 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:
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 21hurst_multiplier: 1.2 to 2.5partial_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
- Run the backtester to see baseline performance.
- Adjust parameters in
config/ping_pong_config.yaml. - Rerun the backtest to see the impact of your changes.
- (Optional) Ask me to implement the
optimize_strategy.pyscript once you have Optuna installed.