Close Navigation
Learn more about IBKR accounts

ActiveX in Excel, with a TWS Sample spreadsheet

Lesson 4 of 6

Duration 4:07
Level Beginner

To watch this video you must accept functional cookies.

What is ActiveX?

ActiveX is a legacy technology developed by Microsoft, it essentially allows applications to share information with each other. This lesson will cover the basics of ActiveX.

The current ActiveX API for Excel wraps the C#/.NET API and is provided as an open source project TWSLib.

Initial Connection

The initial connection procedure is like other socket-based technologies such as the DDE socket bridge, the python API or Java.
In TWS, navigate to Global Configuration -> API -> Settings and ensure that “Enable ActiveX and Socket Clients” is selected.

  • By default, the ActiveX sample connects on port 7496, so let’s make sure this option is selected as well.
  • Then, navigate to C:TWS APIsamplesExcel and open the ActiveX Sample spreadsheet.

  • In the ActiveX sample, the connectivity to TWS via the API is handled in the “General” tab.
  • As with other socket-based technologies, the host, port, and clientId are required when establishing a connection to TWS
    When using Excel and TWS on the same machine, leave the Host column blank, as this represents the “localhost”
  • The port will be the same port TWS listens on, 7496, by default.
  • The client ID can be any positive integer and will be used to this specific API connection. See our Connectivity documentation for further details.
  • Once you review these parameters, click “Connect to TWS”. If successful, there will be a green “Connected” button displayed.
  • When you are done using the spreadsheet, to terminate the socket connection by clicking on “Disconnect from TWS”.

Requesting Market Data

Similar to the new DDE spreadsheet, the ActiveX sample consists of a separate tab for each of the different functionalities offered with the API.
The tickers tab demonstrates how to query watchlist data from TWS. For a more in-depth overview of what is required to request market data in the sample, such as the contract description, we encourage you to review Lesson 3 of the course, as the spreadsheets are similar.

  • After entering the parameters of the contract click the symbol, for example IBM in row 12.
  • Then click on “Request Market Data”.
  • The quotes for available tick types will begin to populate.
  • When you want data to stop streaming click the button “Cancel Market Data”.

Details for programmers

When creating your own custom spreadsheet using the ActiveX API, we would encourage you to review the underlying VBA code of the sample spreadsheet.

Please note, IBKR is not able to provide any programming assistance.

To view the underlying VBA code, press Alt-F11 to open the VBA Editor.  Here the underlying code for the Tickers tab is shown, showing the different subroutines this tab uses to perform requests and return data.  See other forms and modules in the VBA project explorer on the left.

Join The Conversation

For specific platform feedback and suggestions, please submit it directly to our team using these instructions.

If you have an account-specific question or concern, please reach out to Client Services.

We encourage you to look through our FAQs before posting. Your question may already be covered!

5 thoughts on “ActiveX in Excel, with a TWS Sample spreadsheet”

  • Tarhib

    Compare ActiveX with other TWS API methods: We can discuss the pros and cons of ActiveX vs. RealTimeData or DDE approaches.

  • Mark Switzer

    The advantage of using Excel is the cell references. I googled how to replace the AMZN with for instance B2, and the advice I received didn’t help me. I know there is a simple answer to this, but I don’t know what it is. How can I grab the Symbol, Currency, instrument type and exchange from somewhere else on the worksheet.

    • Interactive Brokers

      Hello Mark. Please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://spr.ly/IBKR_TicketCampus

      We also have a user guide for the APIs (and other trading platforms) on our website: https://spr.ly/IBKR_UserGuideCampus

  • Mark H Switzer

    Thanks for that. I did. I’m wondering how the ActiveX Sample Spreadsheet deals with User Accounts with multiple trading accounts. I set up a paper trading account, and it has a single account, and that works fine. I need to get this to work in an account with multiple sub accounts. Searching, it indicated that the account was handled on the general page in the spreadsheet. Any advice?

    • Interactive Brokers

      Hello, thank you for reaching out. The Excel ActiveX sheet supports multiple Accounts in each connection; however, the General tab would only have the user provide a “Client ID” which is just part of the connection process. The account would need to be supplied in any given sheet to supply which account the request should be directed towards. For example, Column “AO” of the “Basic Orders” tab can be used to provide the account ID your order should route to for any given order. Meanwhile, cell A6 on the Account tab can be supplied to specify which account you should receive account data for. This same behavior is prevalent throughout the other tabs as well. We hope this helps!

Leave a Reply

Disclosure: Interactive Brokers

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

The views and opinions expressed herein are those of the author and do not necessarily reflect the views of Interactive Brokers, its affiliates, or its employees.

Disclosure: API Examples Discussed

Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.