feat: implement Split-Account Mode for ISA/CFD hybrid execution
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Trading212 Python Scalping Bot - "Touch & Turn" (Opening Range Reversal)
|
||||
|
||||
This project implements the "Touch & Turn" scalping strategy for the Trading212 API, optimized for the UK ISA environment.
|
||||
This project implements the "Touch & Turn" scalping strategy for the Trading212 API, optimized for the UK ISA environment with optional CFD integration for shorting.
|
||||
|
||||
## Strategy Logic (The Workflow)
|
||||
|
||||
@@ -8,39 +8,41 @@ This project implements the "Touch & Turn" scalping strategy for the Trading212
|
||||
2. **Filter for Liquidity:** Opening range must be >= 25% of 14-day ATR.
|
||||
3. **Determine Direction:**
|
||||
- Bearish (Close < Open): Prepare **LONG** (Buy at Low).
|
||||
- Bullish (Close > Open): Prepare **SHORT** (Substitute with **3x Inverse ETP BUY** in ISA).
|
||||
- Bullish (Close > Open): Prepare **SHORT** (Sell at High).
|
||||
4. **Execution (09:45 EST):**
|
||||
- Entry via **Market Order** for immediate fill.
|
||||
- **Actual Fill Price** fetched from portfolio is used for all bracket calculations.
|
||||
5. **Hybrid Exit Strategy:**
|
||||
5. **Split-Account Routing (ISA / CFD):**
|
||||
- **ISA Account:** Used for all **LONG** trades and **SHORT** trades where an Inverse ETP is available.
|
||||
- **CFD Account:** Used for **SHORT** trades on stocks without Inverse ETPs (requires `SPLIT_ACCOUNT_MODE=True`).
|
||||
6. **Hybrid Exit Strategy:**
|
||||
- **Broker-Side:** Physical **Stop Loss** order placed immediately for protection.
|
||||
- **Bot-Side:** **Take Profit** monitored manually by polling current market price.
|
||||
- This bypasses ISA restrictions against multiple pending sell orders for the same shares.
|
||||
6. **Automatic Exit (11:00 EST):** Force close via Market Order and cleanup pending SL.
|
||||
7. **Automatic Exit (11:00 EST):** Force close via Market Order and cleanup pending SL.
|
||||
|
||||
## Risk & Capital Management
|
||||
|
||||
* **Virtual Balance Simulation:** In demo mode, subtracts £4,750 from total equity to simulate a realistic £250 starting point.
|
||||
* **5% Risk Rule:** Risks exactly 5% of the Virtual Balance per trade.
|
||||
* **Capital Partitioning:** Divides total available capital (£250) and risk budget equally among all active ticker threads for the day (max 3).
|
||||
* **Capital Partitioning:** Divides total available capital (£250) and risk budget equally among all active ticker threads for the day (max 3) per account.
|
||||
* **Precision & Minimums:** Automatically detects "precision-mismatch" or "min-quantity-exceeded" errors from T212 and retries with corrected values.
|
||||
|
||||
## Technical Architecture
|
||||
|
||||
* **`main.py`:** Daily orchestrator. Scan -> Backtest -> Select Top 3 -> Spawn Parallel Threads. Handles early API verification and unbuffered logging.
|
||||
* **`main.py`:** Daily orchestrator. Handles dual-account initialization, trade routing, and parallel thread management.
|
||||
* **`src/api/client.py`:** REST wrapper with Basic Auth.
|
||||
* **`src/strategy/touch_turn.py`:** Setup logic, Fibonacci calculation, and timezone conversion (UTC -> Eastern).
|
||||
* **`src/execution/manager.py`:** Handles ticker swapping (Inverse ETPs), market entries, hybrid brackets, and retry loops with jitter.
|
||||
* **`src/strategy/inverse_mapping.py`:** Map of US stocks to 3x Short Inverse ETPs (GraniteShares/Leverage Shares).
|
||||
* **`src/strategy/touch_turn.py`:** Setup logic, Fibonacci calculation, and ATR-based SL padding.
|
||||
* **`src/execution/manager.py`:** Handles ticker swapping (Inverse ETPs), market entries, hybrid brackets, and retry loops.
|
||||
* **`src/strategy/inverse_mapping.py`:** Map of US stocks to 3x Short Inverse ETPs for ISA shorting.
|
||||
|
||||
## Resilience Features
|
||||
|
||||
* **API Backoff:** Random jitter (1-10s) and exponential retry on 429 errors.
|
||||
* **Order Tracking:** Uses portfolio checks to infer status if order IDs disappear (404).
|
||||
* **Unbuffered Logging:** Force-flushes logs to `logs/bot_*.log` immediately for real-time monitoring.
|
||||
* **Aggressive Logging:** Custom `HardFlushHandler` uses `os.fsync()` to ensure real-time log writes to disk.
|
||||
|
||||
## Operation
|
||||
|
||||
1. **Timer:** Service managed by `systemd` timer firing at 09:30 America/New_York.
|
||||
2. **Tracking:** P&L recorded in `pnl_tracking.csv` (R-multiple based).
|
||||
2. **Tracking:** P&L recorded in `pnl_tracking.csv`.
|
||||
3. **Verification:** Always run `./venv/bin/python3 test_api_connection.py` before live days.
|
||||
|
||||
Reference in New Issue
Block a user