Configure TWS API
For OptimizeOptions to load live data from Interactive Brokers, the API must be enabled and configured in TWS (Trader Workstation) or IB Gateway. The settings below refer to TWS/IB Gateway; you also enter Host, Port and Client ID in OptimizeOptions (menu Settings → TWS Connection).
1. Enable API in TWS or IB Gateway
TWS: Menu Edit → Global Configuration (or File → Global Configuration). In the left panel select API → Settings.
IB Gateway: After logging in via the web interface, go to Configuration → API → Settings (or the equivalent API section).
Set the following:
- Enable ActiveX and Socket Clients – enable.
- If available: Read-Only API – optionally enable if the application only reads data (OptimizeOptions does not place orders).
2. Socket port
The port on which TWS/IB Gateway accepts API connections must match what OptimizeOptions uses. Defaults:
| Application | Port (Paper) | Port (Live) |
|---|---|---|
| TWS | 7496 | 7497 |
| IB Gateway | 4001 | 4002 |
Enter the desired port in TWS/IB Gateway (e.g. 7496 for TWS Paper). In OptimizeOptions under Settings → TWS Connection, enter the same port.
3. Allow connections from localhost
OptimizeOptions connects by default to 127.0.0.1 (localhost), i.e. the same machine running TWS/IB Gateway. In the API settings you should:
- Trusted IPs: enter 127.0.0.1 (or leave the field empty if only local connections are allowed).
- If you run OptimizeOptions on a different machine than TWS: enter the IP of the machine running OptimizeOptions.
4. Client ID
Each API connection needs a unique Client ID (number between 1 and 9999). This is not set in TWS but in OptimizeOptions under Settings → TWS Connection. You can set a fixed value (e.g. 6) or 0 – then OptimizeOptions assigns a random ID. Important: If TWS is already used by another application with the same Client ID, disconnect that app or use a different Client ID in OptimizeOptions.
5. Startup order
Start TWS or IB Gateway first and log in. When it is running and the API is enabled as above, start the IB scan in OptimizeOptions (or check Host/Port/Client ID under Settings → TWS Connection first). OptimizeOptions will then establish the connection.
Summary for OptimizeOptions
- TWS/IB Gateway: Enable API, set socket port (e.g. 7496 for TWS Paper), add 127.0.0.1 as trusted IP.
- OptimizeOptions: Settings → TWS Connection – Host 127.0.0.1, Port as in TWS (e.g. 7496), Client ID 0 (auto) or a fixed number.