Skip to main content

5. Configure IB TWS for API Access

What this does: You're opening a "phone line" between XeroLite and Interactive Brokers so they can talk to each other. Without this, XeroLite can't send orders to IB.

Why it matters: Without API access enabled in TWS (or IB Gateway), XeroLite cannot connect and you cannot trade. This is a required step.

Simple explanation: Think of the API like a phone line. Right now, XeroLite and IB can't talk. You're enabling this "phone line" so XeroLite can call IB and say "place this order."

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

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: Open TWS Settings

  1. Launch TWS or IB Gateway: Ensure the application is running and you're logged in
  2. Access Global Configuration:
    • Click on Edit in the top menu bar
    • Select Global Configuration from the dropdown menu
  3. 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:

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

You'll know it worked when: No error messages appear, and the settings stay saved when you close and reopen TWS. After restarting TWS, XeroLite can connect (see Connect XeroLite to IB).

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 must restart TWS or IB Gateway for API settings to take 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:


Need more help? Contact support at [email protected]

See also: