Close Navigation
Learn more about IBKR accounts

Introduction

The Dynamic Data Exchange protocol is a method of inter-process communication developed by Microsoft to establish communication between Windows applications running on the same computer. The DDE API is available for Windows computers to create a means of communication between Microsoft Excel and TWS or IB Gateway. This allows users to make calls to the standard API library using Visual Basic.

The Excel DDE API has the full functionality offered by the Trader Workstation API implemented via a DDE socket bridge that uses the open source Java – DDE interoperability library JDDE. This means the DDE API offering is built utilizing both Java and VB together in order to create a complete Excel offering to the standard put in place by Microsoft.

Notes & Limitations

  • Sample spreadsheet applications are distributed with the API download for each of the API technologies (RTD Server, ActiveX, DDE). It is important to keep in mind that the sample applications are intended as simple demonstrations of API functionality for third party programmers. They do not have robust error handling functionality and are not intended to be used as production level trading tools.
  • Interactive Brokers does not offer any programming assistance and therefore it is strongly advised to anyone willing to use any of the TWS DDE API to become familiar with the technologies involved such as the DDE protocol and VBA.
  • By design, Microsoft Excel gives precedence to the user interface over the data connection to other applications. For that reason, Excel only receives updates when it is in a ‘ready’ state, and may ignore data sent for instance when a modal dialogue box is displayed to the user, a cell is being edited, or Excel is busy doing other things. A new Excel Real Time Data server (RTD) API has been introduced to help address some of these limitations, but they are inherent to Excel as a trading application and not specific to an API technology.
  • Excel must be set to the US convention for commas and periods. That is, commas denote thousands and periods denote decimals.
  • Other programs running on the same computer which use DDE can interfere with the communication between Excel and TWS and cause Excel to ‘hang’ or ‘freeze’ after making the initial request. The only solution to this problem is to close other programs using DDE one-by-one to find the culprit. Programs which are known to cause this issue include Google Chrome, Microsoft OneNote, Skype and Adobe Creative Cloud.
  • Because the DDE Bridge is built using Java technologies, please install the latest version on your operating system from https://www.java.com/en/download/ .

Unset Value Responses

In many cases, customers might see a response for a given market data column which displays “N/A”, “0”, “-1” in some of their cells. This can be occur for a few different reasons.

  1. While you may have part of the required market data subscription on the instrument, you may not have all of it. This is particularly common for derivatives traders who may not have a market data subscription for the given underlying.
  2. You might also encounter this by requesting too many symbols simultaneously. This is discussed further in the Outgoing Message Rate Limitations section
  3. Similar to (2) above, every client has a maximum number of market data lines shared between TWS and their API. By default, all customer’s have a maximum of 100 market data lines, allowing for 100 unique contracts to be requested at any one time. If you surpass this limit, you may find that some fields can not be returned.
  4. This may also happen in the event your machine, in tandem with Excel, simply can not handle the volume of data requested. While the machine limitations are unique to each user, you may explore and modify the refresh rate as described in our documentation

Download TWS or IB Gateway

In order to use the TWS API, all customers must install either Trader Workstation or IB Gateway to connect the API to. Both downloads maintain the same level of usage and support; however, they both have equal benefits. For example, IB Gateway will be less resource intensive as there is no UI; however, the Trader Workstation has access all of the same information as the API, if users would like an interface to confirm data.

Download Trader Workstation Download IB Gateway

Download the TWS API

It is important to know that the TWS API is only available through the interactivebrokers.github.io MSI or ZIP file. Any other resource, including pip, NuGet, or any other online repository is not hosted, endorsed, supported, or connected to Interactive Brokers. As such, updates to the installation should always be downloaded from the github directly.

TWS API Download Page

Configure Trader Workstation for DDE

  1. Log in to the Trader Workstation or IB Gateway
  2. Open the Global Configuration by selecting the Cog Wheel icon in the top right corner

  3. On the left, select API and then Settings
  4. At a minimum, you will need to maintain these settings:
    • “Enable ActiveX and Socket Clients” must be checked
    • “Read-only API” should be unchecked
    • Unless you are interested in modifying each request or the base code, we would advise setting your Socket port to 7496
  5. This will let you get started; however, there are some option settings we would encourage you to enable:
    • “Create API message log file” should be checked
    • “Include market data in API log file” should be checked
    • “Logging Level” should be changed from “Error” to “Detail”

DDE Socket Bridge

The DDE Socket Bridge is an essential component of the Excel DDE API library. This governs all communication between the Trader Workstation and Excel using the JDDE implementation.

Steps to launch DDE Socket Bridge

  1. Launch Trader Workstation or IB Gateway
  2. Open the Windows File Explorer
  3. Navigate to C:\TWS API\samples\DdeSocketBridge
  4. Double click the file, runDdeSocketBridge.bat. It may just be labeled as runDdeSocketBridge on your computer.
  5. There should be two popup windows that appear.
    • The first will be a black command prompt window. You may see a few lines stating you are connected, and a few stating “Error… data farm connection is OK.” These “errors” are only notifications, and should not cause concern.
    • You will also see a small java window. This will list the service name, host, port, clientId, and you should see “Connected!”. If you see “Disconnected” instead, you may need to Configure Your Socket Bridge.
  6. Assuming your socket bridge is showing “Connected!” then you can navigate to C:\TWS API\samples\Excel and open the  TwsDde.xls file sample.

Configuring the Socket Bridge

For various reasons, you may find the need to modify the connection values used for the DDE Socket Bridge. You are able to modify the ClientID, Host IP, Socket Port, or even the Service Name.

Configure the Connecting ClientId

  1. Launch Trader Workstation or IB Gateway
  2. Open the Windows File Explorer
  3. Navigate to C:\TWS API\samples\DdeSocketBridge
  4. Right click the “runDdeSocketBridge.bat” file.
  5. Click Edit from the list. This will open a new Notepad window.
    • NOTE: Windows 11 users may need to choose “Show more options” first.
  6. On the third line after “DdeSocketBridge.jar” add -c followed by whichever ClientID you wish to connect with.
echo off
if not exist "DdeSocketBridge.jar" goto :error
java -Djava.library.path=.\src\main\resources -jar DdeSocketBridge.jar -c12345
goto :end
:error
echo DdeSocketBridge.jar is not found
:end

 

Configure the Connecting Host IP Address

  1. Launch Trader Workstation or IB Gateway
  2. Open the Windows File Explorer
  3. Navigate to C:\TWS API\samples\DdeSocketBridge
  4. Right click the “runDdeSocketBridge.bat” file.
  5. Click Edit from the list. This will open a new Notepad window.
    • NOTE: Windows 11 users may need to choose “Show more options” first.
  6. On the third line after “DdeSocketBridge.jar” add -h followed by whichever IP Address you need to connect to.

NOTE: Be sure this IP address matches the location of your Trader Workstation. Also be sure that the Socket Bridge’s IP matches where Excel is being hosted.

echo off
if not exist "DdeSocketBridge.jar" goto :error
java -Djava.library.path=.\src\main\resources -jar DdeSocketBridge.jar -h123.123.123.123
goto :end
:error
echo DdeSocketBridge.jar is not found
:end

 

Configure the Connecting Service Name

  1. Launch Trader Workstation or IB Gateway
  2. Open the Windows File Explorer
  3. Navigate to C:\TWS API\samples\DdeSocketBridge
  4. Right click the “runDdeSocketBridge.bat” file.
  5. Click Edit from the list. This will open a new Notepad window.
    • NOTE: Windows 11 users may need to choose “Show more options” first.
  6. On the third line after “DdeSocketBridge.jar” add -s followed by whichever Service name you wish to use in Excel.

NOTE: Be sure on each page of your connected DDE that you match the User Name cell with what is listed here.

echo off
if not exist "DdeSocketBridge.jar" goto :error
java -Djava.library.path=.\src\main\resources -jar DdeSocketBridge.jar -sExcelsior
goto :end
:error
echo DdeSocketBridge.jar is not found
:end

 

Configure the Connecting Socket Port

  1. Launch Trader Workstation or IB Gateway
  2. Open the Windows File Explorer
  3. Navigate to C:\TWS API\samples\DdeSocketBridge
  4. Right click the “runDdeSocketBridge.bat” file.
  5. Click Edit from the list. This will open a new Notepad window.
    • NOTE: Windows 11 users may need to choose “Show more options” first.
  6. On the third line after “DdeSocketBridge.jar” add -p followed by whichever socket port you have specified in TWS or IB Gateway.

The default ports supplied by Interactive Brokers are as follows:

  • TWS Live Account: 7496 (Socket Bridge’s default value)
  • TWS Paper Account: 7497
  • IB Gateway Live Account: 4001
  • IB Gateway Paper Account: 4002
echo off
if not exist "DdeSocketBridge.jar" goto :error
java -Djava.library.path=.\src\main\resources -jar DdeSocketBridge.jar -p7497
goto :end
:error
echo DdeSocketBridge.jar is not found
:end

 

Frequently Asked Questions

Architecture

To elaborate further on the Excel DDE API, it is worth elaborating upon the underlying architecture of the system given its complexity. As stated prior, the Excel DDE is implemented using the open source Java – DDE interoperability library, JDDE because DDE is built around 32-bit systems natively. This is implemented utilizing two key parts.

First and foremost, there is the creation of the socket bridge itself. If you explore further in the DdeSocketBridge directory, you will find the full path towards  C:\TWS API\samples\DdeSocketBridge\src\main where the source code of the socket bridge is housed. The socket bridge is used as a translation tool between the DDE implemented by Visual Basic and the Trader Workstation. Listed within  C:\TWS API\samples\DdeSocketBridge\src\main\java\com\ib\api\impl\EWrapperImpl.java users will be able to find the full Java implementation structure of the TWS API. This particular file is used to receive and transmit data from Trader Workstation to Excel.

Meanwhile, the C:\TWS API\samples\DdeSocketBridge\src\main\java\com\ib\api\dde\dde2socket\requests directory encapsulates all of the various outbound requests sent from Excel to Trader Workstation. These files could be modified such that all outbound requests will then better modify values such as defaults or other passed information so as to not require they be implemented in each Excel request directly.

It is important to note that whenever modifications are made under  C:\TWS API\samples\DdeSocketBridge\src\main then the socket bridge will need to be recompiled

The socket bridge is only half of the greater DDE API. As the socket bridge handles the transmission of data from Excel to the socket bridge, then the socket bridge to TWS and back again. However, once the data has reached Excel from the socket bridge, Visual Basic must now carry the translations into the designated cells for which the data will be displayed. The content of the visual basic which impacts the default sample sheet is hosted in C:\TWS API\samples\Excel\TwsDde. Users who wish to modify how data behaves within Excel itself, such as how or where data is set, would need to make modifications here. Files such as BasicOrders.cls will handle how orders are placed or from which sheets and cells the data can be handled. These functions and can be copied to new files or resaved as needed by developers so long as they are appropriately modified to match their new environment.

Excel Formula Structure

The Excel DDE formula is comprised of several separate components. As a baseline there is the Server, also known as the Service Name from your DDE Socket Bridge. This is followed by your Request Type, and finally the Request String.

We will use this formula to elaborate on this structure: =Stwsserver|reqMktData!'id1?EUR_CASH_IDEALPRO_USD_~/'

  • ‘=Stwsserver’: This first part indicates the Service Name set for our socket bridge connection. The ‘=S’ indicates the start of a formula, and that the next value will be our Service Name.
  • ‘|’: The pipe, the character typically above the Enter/Return key, is used to divide our service name from the rest of the request.
  • ‘reqMktData’ is used to state the Request Type we would like to make. These request types are based on the TWS API EClient request types documented in the TWS API Documentation.
  • “!'”: The exclamation, (!) indicates the start of the Request Type.
  • After the exclamation, we will see a pair of single quotes (‘). These will encapsulate our Request String. This may include the Id, Contract Object, or otherwise.
  • All of the arguments included within the Request String should be delimited with a question mark (?) between them. In our example above, we can see this separates the id from the Contract.

Single value responses

It is possible to view live quotes for multiple products updating real time within Excel. Requests via the TWS DDE API are nothing but Excel formulas (DDE data links) each of them serving a very specific purpose. Market Data retrieval requires at least two different DDE links: one to start the market data subscription and another one which will be receiving the specific tick type.

In many instances, customers are able to designate a single cell and print out values as they are retrieved from the DDE Socket Bridge as they are produced. These are single variable responses, such as data ticks to retrieve specifically a “LAST” or “BID” value.

Array Value Responses

In many cases, you will find that data may be returned to visual basic in an array of values rather than as a single value. In these instances, we must implement Visual Basic to receive and distribute this data throughout the sheet. This can not be done using a formula alone.

Building A Contract

For nearly all instrument-based requests, you will need to create a contract. The standard format of a contract is built for the request, and will utilize an underscore (_) delimited string of all of the contracts attributes. The contract is then concluded with a final “~/'”

Below, we have included an array of different samples that can be used as samples.

Combo / Spread (BAG)

Formula

[symbol]_[SecType]_[exchange]_[currency]_CMBLGS_[num_of_legs]_[legId]_[legQuantity]_[legAction]_[legExchange]_[legPrice]…CMBLGS_~/

Example

SPY_BAG_SMART_USD_CMBLGS_2_141149249_1_BUY_SMART_0_141149252_1_SELL_SMART_0_CMBLGS_~/

SPY_BAG_SMART_USD_CMBLGS_2_141149249_1_BUY_SMART_0_141149252_1_SELL_SMART_0_CMBLGS_~/

 

Forex Pairs

Formula

[symbol]_[SecType]_[exchange]_[currency]_~/

Example

EUR_CASH_IDEALPRO_USD_~/

Futures contract w/ local symbol

Formula

[symbol]_[sec type]_[exchange]_[currency]_~/

Example

ESZ6_FUT_CME_USD_~/

Futures contract w/ underlying

Formula

[underlying symbol]_[sec Type]_[expiry]_[multiplier]_[exchange]_[currency]_~_~/

Example

ES_FUT_201612_50_CME_USD_~_~/

Futures Option w/ local symbol

Formula

[symbol]_[SecType]_[exchange]_[currency]_~/

Example

XTZ6 C1100_FOP_CME_USD_~/

Futures Option w/ underlying contract

Formula

[underlying symbol]_[sec type]_[expiry]_[strike]_[P/C]_[multiplier]_[exchange]_[currency]_~_[trading class]/

Example

EUR_FOP_20161209_1.1_C_125000_CME_USD_~_XT/

Indices

Formula

[symbol]_[sec type]_[exchange]_[currency]_~/

Example

ES_IND_CME_USD_~/

Options contract w/ local symbol

Note: The format of the option local symbol conforms to the Option Symbology Initiative (OSI).

Formula

[symbol]_[SecType]_[exchange]_[currency]_~/’

Example

C DBK DEC 22 1300_OPT_EUREX_EUR_~/’

Options contract w/ underlying

Formula

[underlying symbol]_[sec type]_[expiry]_[strike]_[P/C]_[multiplier]_[exchange]_[currency]_~_~/’

Example

DBK_OPT_20221007_13_C_100_EUREX_EUR_~_~/’

Stocks & ETFs

Formula

[symbol]_[sec Type]_[exchange]_[currency]_~/’

Example

IBKR_STK_SMART_USD_~/’

Sample Spreadsheet Guide

This guide is intended to introduce new customers to the DDE Excel Sample Sheet and introduce the systems available of our underlying API.

Sample spreadsheet applications are distributed with the API download for each of the API technologies (RTD Server, ActiveX, DDE). It is important to keep in mind that the sample applications are intended as simple demonstrations of API functionality for third party programmers. They do not have robust error handling functionality and are not intended to be used as production level trading tools.

Initial Connection

To build a connection between Excel and the TWS for DDE, users must first launch the DdeSocketBridge. This can be found in the C: drive by default, inside {TWS API}\samples\DdeSocketBridge. Double clicking the file, runDdeSocketBridge.bat, will launch the socket bridge and allow communication between the Trader Workstation and Excel.

If you have any issues connecting with the socket bridge, please see the section, DDE Socket Bridge

After launching the socket bridge and all the necessary API settings are set on TWS, as show in Configure Trader Workstation for DDE and having TWS running, we can then locate our sample sheet. Navigate to C:/TWS API/samples/Excel and select “TwsDde.xls”.

Trader Workstation DDE Socket Bridge

Locating the Sample Spreadsheet

After launching the socket bridge and while having TWS open and all the necessary API settings are set on it, as show in Configure Trader Workstation for DDE, we can then locate and open our sample sheet. Navigate to C:\TWS API\samples\Excel and select “TwsDde.xls”.

Navigating Through The Sample

The Excel DDE Sample sheet should launch, initially displaying a yellow and grey sheet with several symbols listed. By looking in the bottom let, you should see a white banner labeled “Tickers”. You will then see several additional tabs on the bottom such as Errors, BasicOrders, ExtendedOrderAttributes, and more. All of these tabs on the bottom correspond to different functionality of the API. You may need to click the arrow in the bottom left to scroll to the right, showing even more tabs.

Requesting Data

Requesting data through the Excel DDE API is quite simple in our sample structure. All that must be done for most pages is to select the row or rows of data, and select the “Request ___ Data” button at the top. This will request data for whatever respective row is selected.

Receiving Data

Endpoints can be received in two standard ways. The first most common method is to retrieve all of the data in-line within the same page. This is prevalent in the Tickers and BasicOrders and several others. This is common for situations where single cell values are released for a given value.

Other times, you will see a new page created to house returned data. This is prevalent for Historical Market Data, Market Scanners, and more. This is where multiple values will be retrieved that correspond to the same identifier.

Receiving Data: In-row data

For in-line data, you will see the value appear directly to the right. The code has been designed to reference the column headers. As such, if you find that certain data in in-line responses are not needed, you may simply remove the column form the sheet.

Receive data for DDE inline

Receiving Data: New Page

This will generate a brand new page at the end of the tab list as defined in the “Page Name” column of the original request. In the case of Historical data and the IBKR Stock request, you will see column V list “IBKR_STK_SMART_USD”. In Column W, we will see the Activate Page column.

Receiving Historical data in DDE.

Underlying Requests

As stated within the Architecture section, the Excel DDE API is built on the underlying Java API with standard Visual Basic Translations to allocate the data onto the Excel sheets. For additional insight into the underlying methods, see the TWS API Documentation.

Use Case

This guide is intended to introduce new customers to the DDE Excel example use cases.

Option Chain

You can use “SecDefOptParams” tab to request Option Chain live market data.

Getting Option Chain live market data requires entering:

  • Underlying Symbol
  • Underlying SecType
  • Underlying ConId
  • Page Name
  • Activate Page (TRUE / FALSE)

 

The images shown in the right-side show how to request IBM STK Option data and its output.

DDE Option Chain

DDE Option Chain Output

Placing Conditional Order

In TWS API 10.28+, you can use Excel DDE to place conditional order again.

In “Basic Order Tab”, placing conditional order requires entering:

  • Conditions (Column DY)
  • Ignore Rth (0 / 1) (Column DZ)
  • Cancel Order (0 / 1) (Column EA)

 

The images shown in the right-side show how to place a Price Conditional Order for IBM (ConId: 8314).

 

Here are the example formulas for Conditions (Column DY):

 

  • Price

Default:

default Price of 8314(SMART) is <= 55 and

default Price of 8314(SMART) is <= 55 or

 

Double Bid/Ask:

double bid/ask Price of 8314(SMART) is <= 55 or

 

Last:

last Price of 8314(SMART) is <= 55 or

 

Double Last:

double last Price of 8314(SMART) is <= 55 or

 

Bid/Ask:

bid/ask Price of 8314(SMART) is <= 55 or

 

Last or Bid/Ask:

last of bid/ask Price of 8314(SMART) is >= 55 or

 

Mid-point:

mid-point Price of 8314(SMART) is <= 55 or

 

  • Time

time is <= 20240415 12:00:00 Europe/Tallinn and

time is >= 20240415 12:00:00 Europe/Tallinn and

 

  • Margin Cushion

the margin cushion percent is <= 4 and

the margin cushion percent is >= 5 and

 

  • Daily P&L

 

  • Volume

Volume of 8314(SMART) is <= 1200 and

Volume of 8314(SMART) is >= 1200 or

 

  • Trade

trade occurs for IBM symbol on ARCA exchange for STK security type and

 

  • Percentage Change

PercentChange of 8314(SMART) is <= 1 and

PercentChange of 8314(SMART) is >= 2 and

 

  • Shortable Shares

 

  • Fee Rate in %

 

Here is an example of placing an order with multiple conditions:

  • OR

bid/ask Price of 8314(SMART) is <= 55 or default Price of 8314(SMART) is <= 55 or

  • AND

bid/ask Price of 8314(SMART) is <= 55 and default Price of 8314(SMART) is <= 55 and

DDE Conditional Order

DDE Conditional Order Output

Contact Support

Interactive Brokers always welcomes users to contact customer support in the event of API behavior issues. However, to help expedite the troubleshooting process for customers, it is encouraged to first consider the following:

  • Does this issue persist in other platforms? Am I receiving errors with an order even when performing the same action in Trader Workstation rather than through the API?
  • Did the request match the Required parameters from the documentation?
  • Were documentation or guides consulted on the matter prior?
  • How long has this issue been going on? Is the error on the first attempt, or has this request been working for days prior?
  • Do other, similar requests work? Perhaps portfolio data is returned, but not trading information.

Interactive Brokers offers an array of contact methods based on your needs. The buttons listed here will direct users to the affiliated contact page so they may get started with the support team.

Create a ticket Chat in with Customer Support Phone Support

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.