CURRENT_ISSUE_TWS_API_BLOCKED.mdโข3.58 kB
# ๐ด CRITICAL ISSUE: TWS API Handshake Timeout
## Current Status: Connection Blocked
### Diagnosis Complete โ
Your diagnostic output shows:
```
INFO:ib_async.client:Connecting to 127.0.0.1:7496 with clientId 0...
INFO:ib_async.client:Connected โ Socket works โ
ERROR:ib_async.client:API connection failed: TimeoutError() โ API fails โ
```
**Translation:**
- โ
TWS is running on port 7496
- โ
Network/socket connection works (telnet succeeds)
- โ
ib_async can connect to the socket
- โ **TWS is NOT completing the IB API handshake**
## The Problem
The IB API requires a specific handshake protocol after the socket connects. TWS is not responding to this handshake, which means:
**TWS API is either:**
1. Not enabled properly in settings
2. Blocking the connection for security reasons
3. In a bad state and needs restart
## The Solution
### ๐ฅ MOST LIKELY FIX: TWS API Settings
**Go to TWS:**
```
File โ Global Configuration โ API โ Settings
```
**Make sure these are set:**
- โ
**"Enable ActiveX and Socket Clients"** = CHECKED
- โ
**Socket port** = 7496
- โ
**"Read-Only API"** = UNCHECKED (unless you only want read-only)
- โ
If "Allow connections from localhost only" = CHECKED
- Then **"Trusted IP Addresses"** MUST include `127.0.0.1`
**CRITICAL:** After changing ANY settings:
1. Click "OK"
2. **Completely QUIT TWS** (File โ Exit)
3. Wait 10 seconds
4. **Restart TWS**
5. Log in and wait for it to fully load
6. Try the connection again
### ๐งช Test After Restart
```bash
# Run the minimal test
uv run python test_minimal_tws.py
```
**Expected when working:**
```
โ
SUCCESS! API connection established.
๐ Connected accounts: ['DU123456']
โ
TWS API IS WORKING CORRECTLY
```
## Files Created for You
1. **`test_minimal_tws.py`** - Simplest possible connection test
```bash
uv run python test_minimal_tws.py
```
2. **`test_tws_connection.py`** - Full diagnostic with verbose logging
```bash
uv run python test_tws_connection.py --client-id 0
```
3. **`docs/TWS_API_HANDSHAKE_TIMEOUT.md`** - Complete troubleshooting guide
## Quick Actions
### Action 1: Verify TWS Settings โก
```
TWS โ File โ Global Configuration โ API โ Settings
โ Enable ActiveX and Socket Clients
โ Socket port = 7496
โ Trusted IPs includes 127.0.0.1
โ Click OK
โ RESTART TWS (completely quit and restart)
```
### Action 2: Test Again
```bash
uv run python test_minimal_tws.py
```
### Action 3: If Still Failing
Check TWS logs for error messages:
- Windows: `C:\Users\<you>\Jts\<you>\log\api.*.log`
- Mac/Linux: `~/Jts/<you>/log/api.*.log`
Look for messages about rejected connections or authentication errors.
## Alternative: Try IB Gateway
If TWS continues to fail, IB Gateway is more reliable for API access:
1. Download IB Gateway (lighter than TWS)
2. Configure for API connections
3. Use port 4001 (live) or 4002 (paper)
4. Update .env: `TWS_PORT=4001`
## What We've Ruled Out
- โ Event loop issues (fixed by migrating to ib_async)
- โ Network/firewall blocking (telnet works)
- โ Wrong port (7496 is confirmed listening)
- โ ib_async library issues (connects to socket fine)
- โ Client ID conflicts (tried multiple IDs)
## What It Is
- โ
**TWS API configuration or state issue**
The handshake requires TWS to actively respond. It's listening but not responding = configuration or TWS state problem.
## Status
๐ด **BLOCKED** - Waiting for TWS API to be properly configured
**Next Step:** Fix TWS API settings, restart TWS, run `test_minimal_tws.py`