Installing & Configuring TWS for the API – Instalación y configuración de TWS para la API

IB Gateway vs. TWS

  • IB Gateway is an alternative to TWS that is geared entirely towards API functionality without a GUI for account information.
  • Both programs are nearly identical from an API perspective.
  • Equivalent versions of TWS and IB Gateway are always released together.
  • IB Gateway requires fewer resources and is a smaller application.

Installing TWS

  1. Download and Install:
    • Run the installer from the Interactive Brokers website.
    • A new icon will be created on the desktop after installation.
  2. Login:
    • Double-click the TWS icon to bring up the login screen.
    • Enter your username and password.
    • Choose to log in to either your live or paper account.
    • If you don’t have an IB account, you can try the demo by clicking “Try the Demo” and using an email address.

Configuring TWS for API Use

  1. Initial Login:
    • Fully log into TWS and let it load completely.
  2. Enable API Connection:
    • Go to Global Configuration.
    • Navigate to API -> Settings.
    • Check the box labeled “Enable ActiveX and Socket Clients”.
  3. Socket Port Settings:
    • Default ports:
      • TWS: 7496 for live sessions, 7497 for paper sessions.
      • IB Gateway: 4001 for live sessions, 4002 for paper sessions.
    • Ensure the socket port in TWS matches the port used in the API client’s connect() function.
  4. Connection Settings:
    • Check “Allow Connections from Localhost Only” to:
      • Only accept incoming connections from API applications on the same computer.
      • Avoid the need to manually accept incoming API connections.

API Logging

  1. Enable API Message Log:
    • Create an API log file by enabling “Create API Message Log”.
    • Include market data in the log by checking “Include market data in API log file”.
    • Set the “Logging Level” to “Detail” for comprehensive logging of all API traffic.

Order Settings

  1. Global Configuration:
    • Read-Only: By default, it is enabled and will block all API orders. Uncheck it to allow orders from the API.
    • Precautions: Certain orders may not transmit by default if they exceed precautionary settings until manually confirmed in TWS.
    • Some order attributes not settable from the API can be configured in TWS, e.g., “Active order in pre-open session”.

Accede a tu cuenta