introduce to tests

This commit is contained in:
2026-01-02 09:05:01 +01:00
parent 9eb1abcfc8
commit e37369608b
12 changed files with 2126 additions and 705 deletions

View File

@ -43,76 +43,29 @@ The system consists of three independent Python processes that coordinate via sh
| `telegram_monitor.py` | Telegram bot for notifications. |
| `{TARGET_DEX}_status.json` | **Critical:** Shared state file acting as the database between Manager and Hedger. |
| `.env` | Stores secrets (Private Keys, RPCs). **Do not commit.** |
| `tests/backtest/` | **New:** Professional Backtesting & Optimization Framework. |
| `tools/` | Utility scripts, including the Git Agent for auto-backups. |
| `logs/` | Detailed logs for all processes. |
## Configuration
## Backtesting Framework (Jan 2026 Update)
A robust simulation engine has been implemented to validate strategies before capital commitment.
### Environment Variables (`.env`)
Required variables for operation:
```env
# Blockchain
MAINNET_RPC_URL=... # Arbitrum
BNB_RPC_URL=... # BNB Chain
BASE_RPC_URL=... # Base
MAIN_WALLET_PRIVATE_KEY=...
MAIN_WALLET_ADDRESS=...
### Components
* **`tests/backtest/backtester.py`**: Event-driven engine mocking Web3/Hyperliquid interactions.
* **`tests/backtest/mocks.py`**: Stateful simulator handling balance tracking, V3 tick math, and fee accrual.
* **`tests/backtest/grid_search.py`**: Optimization runner to test parameter combinations (Range Width, Hedging Threshold).
* **`tests/backtest/analyze_results.py`**: Helper to interpret simulation CSV results.
# Hyperliquid
HEDGER_PRIVATE_KEY=... # Usually same as Main Wallet or specialized sub-account
# Telegram
TELEGRAM_BOT_TOKEN=...
TELEGRAM_CHAT_ID=...
TELEGRAM_MONITOR_ENABLED=True
```
### Strategy Config (`clp_config.py`)
Key parameters controlling the bot's behavior:
* `TARGET_DEX`: Selects the active chain/profile (e.g., "UNISWAP_V3", "PANCAKESWAP_BNB").
* `RANGE_WIDTH_PCT`: Width of the LP position (e.g., `0.05` for +/- 5%).
* `TARGET_INVESTMENT_AMOUNT`: Notional size of the position in USD.
* `SLIPPAGE_TOLERANCE`: Max slippage for minting/swapping.
## Usage
The system is designed to run continuously. It is recommended to use a process manager like `pm2` or `systemd`, or simply run in separate terminal tabs.
1. **Start the Manager:**
```bash
python clp_manager.py
```
* *Action:* Will check for existing positions. If none, it prepares to open one based on config.
2. **Start the Hedger:**
```bash
python clp_hedger.py
```
* *Action:* Will read the position created by the Manager and open a corresponding short on Hyperliquid.
3. **Start Monitoring (Optional):**
```bash
python telegram_monitor.py
```
## Development & Git Agent
This project uses a custom **Git Agent** (`tools/git_agent.py`) for automated version control and backups.
* **Auto-Backup:** Runs hourly (if configured) to create backup branches (e.g., `backup-2025-01-01-12`).
* **Manual Commit:**
```bash
python tools/git_agent.py --backup
```
* **Status:**
```bash
python tools/git_agent.py --status
```
* **Restoration:**
To restore a file from a backup branch:
```bash
git checkout backup-BRANCH-NAME -- path/to/file.py
```
### Progress Status (Jan 1, 2026)
* **Completed:**
* Simulation loop runs end-to-end (Mint -> Accrue Fees -> Close).
* Fixed Mock Pricing logic (handling inverted T0/T1 pairs like USDT/WBNB).
* Implemented realistic Fee Accrual based on Trade Volume + Market Share.
* Verified "In Range" detection and position lifecycle.
* **Pending / Next Steps:**
* **Hedger PnL Verification:** Simulation showed 0.0 Hedging Fees because the price volatility in the 1-day sample was too low to trigger the 10% rebalance threshold. We are lowering thresholds to 1% to force activity and verify costs.
* **NAV Calculation:** Refine "Total PnL" to include Unrealized PnL from both LP and Hedge to handle Impermanent Loss correctly.
* **Final Optimization:** Run the `grid_search.py` with the corrected Market Share (0.02%) and lower thresholds to find the profitable "Sweet Spot".
## Logic Details
@ -134,4 +87,4 @@ Rebalancing is triggered when:
* **Logs:** Check `logs/clp_manager.log` and `logs/clp_hedger.log` first.
* **Stuck Position:** If a position is closed on-chain but `{TARGET_DEX}_status.json` says `OPEN`, manually edit the JSON status to `CLOSED` or delete the entry (with caution).
* **RPC Errors:** Ensure your RPC URLs in `.env` are active and have sufficient rate limits.
* **RPC Errors:** Ensure your RPC URLs in `.env` are active and have sufficient rate limits.