Skip to main content

5. Configure IB TWS for API Access

Before XeroLite can connect to Interactive Brokers, you need to configure TWS (Trader Workstation) or IB Gateway to allow API access. This section guides you through the configuration process.

Prerequisites

  • XeroLite CE instance accessed – Logged into your XeroLite CE web interface (see Getting Started)
  • TWS or IB Gateway pre-installed – Already installed in your XeroLite CE instance (see System Requirements)
  • Interactive Brokers account – Paper or Live account credentials ready

TWS vs IB Gateway

  • TWS: Full trading platform with charts and tools. Use if you need visual interface.
  • IB Gateway: Lightweight API-only connection. Recommended for automated trading.

Both applications support the same API configuration steps.

5.1: Access TWS Settings in XeroLite CE

In XeroLite CE, TWS/Gateway is pre-installed and accessible through the web interface:

  1. Log in to your XeroLite CE instance via web browser
  2. Access TWS/Gateway: Navigate to the TWS/Gateway interface through the XeroLite CE web interface
  3. Log in to TWS/Gateway: Enter your Interactive Brokers username and password
  4. Access Global Configuration:
    • Click on Edit in the TWS/Gateway interface
    • Select Global Configuration from the dropdown menu
  5. Navigate to API Section: The Global Configuration window will open. Look for the API section in the left sidebar

5.2: Enable API Access

  1. Select API → Settings: Click on API in the left sidebar, then select Settings
  2. Enable ActiveX and Socket Clients:
    • Check the box for Enable ActiveX and Socket Clients
    • This allows external applications (like XeroLite) to connect via the API
  3. Configure Socket Port:
    • Set Socket port to 7497 (default for paper trading)
    • For live accounts, you may use port 7496 or 7497 depending on your configuration
  4. Disable Read-Only Mode:
    • Ensure Read-Only API is unchecked
    • Read-only mode prevents order execution, which is required for XeroLite

TWS/IB Gateway API Settings Configuration 1

TWS/IB Gateway API Settings Configuration 2

5.3: Allow Local Connections

  1. Configure Trusted IPs (if applicable):

    • Some TWS versions have a "Trusted IPs" section
    • Add 127.0.0.1 (localhost) to allow local connections
    • This ensures XeroLite running on the same machine can connect
  2. Verify Connection Settings:

    • Ensure the API is configured to accept connections from localhost
    • Default settings typically allow local connections automatically

5.4: Save Settings

  1. Configure API Precautions:

    • In the same Global Configuration window, go to API → Precautions
    • Enable all "Bypass Order Precautions for API Orders" options to ensure smooth order execution

    Which Bypass Options to Enable?

    • Basic Trading: Enable the main "Bypass Order Precautions for API Orders"
    • Full Automation: Enable all options for complete automated trading functionality

    TWS/IB Gateway API Precautions Configuration

  2. Apply Changes: Click Apply or OK to save your API settings

  3. Restart TWS/IB Gateway:

    • Important: You can restart TWS or IB Gateway for API settings to take effect, if it is not taking effect
    • Close the application completely and relaunch it
    • Log back in with your credentials
  4. Verify Settings Persisted:

    • After restart, go back to Edit → Global Configuration → API → Settings
    • Confirm your settings are still configured correctly
    • Verify API Precautions settings are still enabled

Important Notes

  • API settings must be saved and TWS/IB Gateway must be restarted for changes to take effect
  • Keep TWS/IB Gateway running while using XeroLite
  • The API port (7497) must not be blocked by your firewall
  • API Precautions must be enabled for orders to execute properly

Troubleshooting

API Settings Not Saving

  • Restart TWS: Restart TWS/IB Gateway after changing API settings
  • Check Permissions: Ensure you have write permissions for TWS configuration
  • Verify Settings: Go back to API settings and confirm they persisted

Orders Not Executing

  • Check API Precautions: Ensure "Bypass Order Precautions for API Orders" is enabled
  • Verify Read-Only Mode: Ensure Read-Only API is unchecked
  • Check Port: Verify port 7497 is correct and not blocked

Next Steps

After configuring API access, proceed to: