feat: implement Split-Account Mode for ISA/CFD hybrid execution
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Trading212 "Touch & Turn" Scalping Bot
|
||||
|
||||
This project implements the "Touch & Turn" scalping strategy (Opening Range Liquidity Reversal) in Python for the Trading212 API. It is specifically designed to trade US Equities at the 09:30 EST market open.
|
||||
This project implements the "Touch & Turn" scalping strategy (Opening Range Liquidity Reversal) in Python for the Trading212 API. It is optimized for UK traders using ISA and optional CFD accounts.
|
||||
|
||||
## ⚠️ Disclaimer
|
||||
**This software is for educational purposes only.** Trading in financial markets involves a high degree of risk. Always use the practice/demo environment (`demo.trading212.com`) to test strategies before using real money.
|
||||
@@ -9,72 +9,78 @@ This project implements the "Touch & Turn" scalping strategy (Opening Range Liqu
|
||||
|
||||
## Strategy Overview
|
||||
|
||||
The strategy capitalizes on the initial liquidity and volatility of the US market open.
|
||||
The strategy capitalizes on the initial liquidity and volatility of the US market open (09:30 EST).
|
||||
|
||||
1. **The Setup:** Captures the high and low of the first 15-minute candle (09:30 - 09:45 EST).
|
||||
2. **The Filter:** The range of this opening candle must be at least **25%** of the stock's 14-day Average True Range (ATR).
|
||||
3. **The Trigger (ISA Optimized):**
|
||||
- **LONG (Bearish candle):** Bot places an immediate **Market BUY** order for the stock.
|
||||
- **SHORT (Bullish candle):** Since standard shorting is restricted in UK ISAs, the bot automatically substitutes this with a **Market BUY** order for a **3x Inverse ETP** (e.g., buying `3SLA` if `TSLA` gives a short signal).
|
||||
1. **The Setup:** Captures the 15-minute opening candle.
|
||||
2. **The Filter:** Minimum range of 25% of 14-day ATR.
|
||||
3. **The Trigger (Split-Account Optimized):**
|
||||
- **LONG (Bearish candle):** Bot executes a **Market BUY** in the ISA account.
|
||||
- **SHORT (Bullish candle):**
|
||||
- **ISA Option:** Buys a **3x Inverse ETP** (if available).
|
||||
- **CFD Option:** Performs a **Direct SELL** in the CFD account (if `SPLIT_ACCOUNT_MODE=True`).
|
||||
4. **The Targets:**
|
||||
- Brackets are placed **immediately** after the market order is filled, using the **Actual Fill Price** from your portfolio.
|
||||
- **Take Profit (TP):** The 38.2% Fibonacci retracement level.
|
||||
- **Stop Loss (SL):** Placed to ensure a Risk:Reward ratio of 1:2.
|
||||
5. **Time Exit:** All open positions are forcefully closed via Market Order at **11:00 EST**.
|
||||
- **Stop Loss (SL):** Physical broker-side order with ATR-based padding.
|
||||
- **Take Profit (TP):** Manually monitored by the bot to hit 38.2% Fibonacci retracement.
|
||||
5. **Time Exit:** All positions forcefully closed at **11:00 EST**.
|
||||
|
||||
---
|
||||
|
||||
## Installation & Setup
|
||||
|
||||
1. **Clone the repository and set up a virtual environment:**
|
||||
1. **Setup environment:**
|
||||
```bash
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
2. **Configure Environment Variables:**
|
||||
Create a `.env` file in the root directory:
|
||||
2. **Configure `.env`:**
|
||||
```ini
|
||||
TRADING212_API_KEY_ID=your_key_id_here
|
||||
TRADING212_API_KEY=your_api_key_here
|
||||
# Primary Account (ISA)
|
||||
TRADING212_API_KEY_ID=...
|
||||
TRADING212_API_KEY=...
|
||||
TRADING212_BASE_URL=https://demo.trading212.com/api/v0/
|
||||
|
||||
# Secondary Account (CFD - Optional for Shorting)
|
||||
CFD_API_KEY_ID=...
|
||||
CFD_API_KEY=...
|
||||
CFD_BASE_URL=...
|
||||
|
||||
SPLIT_ACCOUNT_MODE=True
|
||||
VIRTUAL_STARTING_BALANCE=250
|
||||
ISA_MODE=True
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Risk Management & Position Sizing
|
||||
## Split-Account Mode
|
||||
|
||||
The bot uses dynamic **Risk-Based Position Sizing** to ensure consistent exposure.
|
||||
To overcome the lack of Inverse ETPs for certain stocks, the bot can use a Trading212 CFD account for shorting.
|
||||
|
||||
- **5% Risk Rule:** By default, the bot risks **5% of your account balance** per trade.
|
||||
- **Virtual Balance simulation:** If you are testing on a demo account with a large balance (e.g., £5,000) but plan to trade live with £250, the bot can maintain perspective. It automatically calculates a "Virtual Balance" by subtracting £4,750 from your actual total, ensuring your risk amount is exactly what it will be in the real world. (e.g. £12.50 risk on a £250 virtual balance).
|
||||
- **Leverage Adjusted:** For Inverse ETPs (3x leverage), the bot adjusts the quantity and bracket percentages to ensure the monetary risk remains identical to a standard 1x stock trade.
|
||||
- **How it works:** When a Short signal is found, the bot checks if an Inverse ETP exists. If not (or if Split-Account mode is preferred), it routes the trade to the CFD account as a direct `SELL` order.
|
||||
- **Benefit:** 100% coverage of all market opportunities.
|
||||
|
||||
---
|
||||
|
||||
## Automation Workflow
|
||||
## Risk Management
|
||||
|
||||
The bot is designed to be triggered once per day (e.g., via a **systemd timer** or cron) at exactly **09:30 EST**.
|
||||
|
||||
1. **Scan:** Runs the ISA candidate filter to find the most volatile US stocks.
|
||||
2. **Backtest:** Runs a 60-day historical backtest on the top 10 candidates.
|
||||
3. **Select:** Picks the **Top 3** tickers that showed a positive historical return (Net PnL > 0 R).
|
||||
4. **Execute:** Spawns parallel threads to monitor and trade the selected assets.
|
||||
5. **Clean:** Shuts down automatically after the 11:00 EST exit and cleanup.
|
||||
- **5% Risk Rule:** Risks 5% of the Virtual Balance (£250 starting point) per trade.
|
||||
- **Capital Partitioning:** Automatically divides capital among active trades to prevent over-exposure.
|
||||
- **ATR Padding:** Stop losses are automatically widened to at least 10% of daily ATR to avoid premature stop-outs from noise.
|
||||
|
||||
---
|
||||
|
||||
## Monitoring
|
||||
|
||||
- **Logs:** All activity is recorded in `logs/bot_YYYY-MM-DD.log`.
|
||||
- **PnL Tracking:** A permanent ledger of every trade (including ETP substitutions) is kept in `pnl_tracking.csv` for graphing and analysis.
|
||||
- **Journal:** Monitor via `journalctl -u touchturn.service`.
|
||||
- **Logs:** Real-time mirrored logs in `logs/bot_YYYY-MM-DD.log`.
|
||||
- **PnL:** Performance ledger in `pnl_tracking.csv`.
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
* **`src/api/client.py`:** REST API wrapper with Basic Auth.
|
||||
* **`src/strategy/touch_turn.py`:** Logic engine and Fibonacci calculator.
|
||||
* **`src/strategy/inverse_mapping.py`:** Map of US stocks to 3x Short Inverse ETPs.
|
||||
* **`src/execution/manager.py`:** Handles market entries, actual fill-based bracketing, and ISA substitutions.
|
||||
* **`main.py`:** The morning orchestrator.
|
||||
* **`main.py`:** Daily orchestrator with dual-account routing.
|
||||
* **`src/execution/manager.py`:** Hybrid exit management (Broker SL / Bot TP).
|
||||
* **`src/strategy/touch_turn.py`:** Logic engine with ATR padding.
|
||||
* **`src/strategy/inverse_mapping.py`:** ISA-specific shorting map.
|
||||
|
||||
Reference in New Issue
Block a user