• C#
  • Java
  • VB
  • C++
  • Python
Contact us
TWS RTD Server Samples

This page is provided as a demonstration of RTD formulas categorized by security type as well as syntax type. Make sure to get yourself familiar with the various available Syntaxs for RTD formula before looking into the samples.

Forex Pairs

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"EUR.USD/CASH", "Bid")

Comment: Forex Ticker is defined in format "CURRENCY1.CURRENCY2/CASH".

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=EUR","cur=USD", "exch=IDEALPRO", "sec=CASH", "qt=Bid")

Comment: For Complex Syntax, Forex Symbol is defined as the foreign currency, and the Currency is defined as the base currency.

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"EUR.USD/CASH", "Bid", "port=1234", "clientId=1")


Stocks

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"SPY")

Comment: Default values are used: Exchange = "SMART", Currency = "USD", Security Type = "STK", Topic = "Last".

=RTD("Tws.TwsRtdServerCtrl",,"SPY@ARCA", "Bid")

Comment: Specifying the Exchange directly means requesting data from that exchange specifically.

=RTD("Tws.TwsRtdServerCtrl",,"BMO@SMART//////CAD", "Bid")

Comment: Currency = "CAD" is needed for BMO listed on TSE, but the rest of the fields can be left out as blank.

=RTD("Tws.TwsRtdServerCtrl",,"ABG.P@SMART//////EUR", "Close")

Comment: Stock symbols that contain a '.' are supported in Simple Syntax as well.

=RTD("Tws.TwsRtdServerCtrl",,"SPY@SMART/ARCA", "Ask")

Comment: For certain smart-routed stock contracts that have the same Symbol, Currency and Exchange, you would also need to specify the PrimaryExchange attribute to uniquely define the contract. This should be defined as the native exchange of a contract, and is good practice for all stocks.

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=SPY", "sec=STK", "exch=SMART", "cur=USD", "qt=Volume")

Comment: Generally speaking, using the Symbol, SecurityType="STK", Currency and Exchange is sufficient to define a stock.

=RTD("Tws.TwsRtdServerCtrl",,"sym=SPY", "sec=STK", "exch=SMART", "cur=USD", "prim=ARCA", "qt=Open")

Comment: Specifying the PrimaryExchange as a seprate string to resolve contract ambiguity.

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"SPY@SMART", "prim=ARCA", "qt=High", "paper")

Comment: Use the pre-defined string "paper" to connect to the default port 7497 for paper TWS sessions.


Indexes

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"SPX@CBOE//IND", "Last")

Comment: Default Currency = "USD" is used.

=RTD("Tws.TwsRtdServerCtrl",,"DAX@EUREX//IND////EUR", "Last")

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=INDU","cur=USD", "exch=NYSE", "sec=IND", "qt=Close")

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"DAX@EUREX//IND", "cur=EUR", "qt=Last", "host=1.2.3.4")


CFDs

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"IBDE30@SMART//CFD////EUR", "Bid")

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=IBDE30","cur=EUR", "exch=SMART", "sec=CFD", "qt=ASK")

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"IBDE30@SMART//CFD", "cur=EUR", "Bid", "gw")

Comment: Use the pre-defined string "gw" to connect to the default port 4001 for live IB Gateway sessions.

Note: Only Index CFD data can be directly queried via the API, but not equity CFD. Please directly request data for the underlying equity if you need data for an equity CFD contract.


Futures

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"ES@CME//FUT/201712///USD", "Bid")

Comment: Use underlying Symbol and LastTradeDateOrContractMonth to define futures contract.

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"loc=ESZ7","cur=USD", "exch=CME", "sec=FUT", "qt=Ask")

Comment: The LastTradeDateOrContractMonth and underlying Symbol can be replaced with the contract's own symbol, also known as LocalSymbol (named as Symbol within the TWS' Contract Description dialog). Local Symbol is not available in Simple Syntax.

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"DAX@EUREX//FUT/201706///EUR", "mult=5", "Low")

Comment: For futures that have multipler Multipliers (e.g. DAX has 5 and 25), Simple Syntax is not adequate to define the contract uniquely. Mixed Syntax can help to add addition specification for the Multiplier.


Options

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"GOOG@SMART//OPT/20170421/C/835/USD", "Bid")

Comment: Use Symbol, LastTradeDateOrContractMonth, Right and Strike to define options contract.

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"loc=C DBK DEC 20 1600", "cur=EUR", "exch=EUREX", "sec=OPT", "qt=Close")

Comment: Use LocalSymbol to define options contract.

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"SANT@MEFFRV//OPT/20190621/C/7.5/EUR", "mult=100", "tc=SANEU", "Close")

Comment: For options that have multiple Multipliers or TradingClasses, Simple Syntax is not adequate to define the contract uniquely. Mixed Syntax can help to add addition specifications for Multipler and TradingClass properly.


Futures Options

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"ES@CME//FOP/20180316/C/1000/USD", "Close")

Comment: Futures Options follow the same rule as conventional option contracts.

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"loc=ESH8 C1000", "cur=USD", "exch=CME", "sec=FOP", "qt=Close")

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"ES@CME//FOP/20180316/C/1000/USD", "mult=50", "tc=ES", "Close")


Standard Warrants

- Simple Syntax

=RTD("tws.twsrtdserverctrl",,"MSFT@FWB//WAR/202401/C/200/EUR", "Bid")

Comment: Use Symbol, LastTradeDateOrContractMonth, Right and Strike to define warrants contract.

- Complex Syntax

=RTD("tws.twsrtdserverctrl",,"loc=TT45K3","cur=EUR", "exch=FWB", "sec=WAR", "qt=Bid")

Comment: LocalSymbol can be used to define warrants contract.

- Mixed Syntax

=RTD("tws.twsrtdserverctrl",,"GOOG@SWB//WAR/202401/P/1500/EUR","mult=0.01", "Last")

Comment: For warrants that have multiple Multipliers or TradingClasses, Simple Syntax is not adequate to define the contract uniquely. Mixed Syntax can help to add addition specifications for Multipler and TradingClass properly.


Bonds

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"912828C57@SMART//BOND", "Bid")

Comment: Bonds can be specified by defining the Symbol as the CUSIP. Currency = "USD" is used as default here.

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=912828C57","cur=USD", "exch=SMART", "sec=BOND", "qt=Bid")

=RTD("Tws.TwsRtdServerCtrl",,"conid=147554578", "exch=SMART", "qt=Ask")

Comment: Bonds can also be defined with the ConId and Exchange as with any security type.

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"912828C57@SMART, "sec=BOND", "Bid", "gwpaper")

Comment: Use the pre-defined string "gwpaper" to connect to the default port 4002 for paper IB Gateway sessions.


Mutual Funds

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"VINIX@FUNDSERV//FUND", "Close")

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=VINIX","cur=USD", "exch=FUNDSERV", "sec=FUND", "qt=Close")

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"VINIX@FUNDSERV//FUND", "Bid", "host=1.2.3.4", "port=1234", "clientId=1")


Commodities

- Simple Syntax

=RTD("Tws.TwsRtdServerCtrl",,"XAUUSD@SMART//CMDTY", "Bid")

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=XAUUSD","cur=USD", "exch=SMART", "sec=CMDTY", "qt=Ask")

- Mixed Syntax

=RTD("Tws.TwsRtdServerCtrl",,"XAUUSD@SMART//CMDTY", "Last", "port=1234", "clientId=1")


Spreads

Spread contracts, also known as combos or combinations, combine two or more instruments. To define a combination contract it is required to know the Contract ID of the combo legs. The ConId can be easily found in the Contract Description page in TWS. The spread contract's symbol can be either the symbol of one of the contract legs or, for two-legged combinations the symbols of both legs separated by a comma as shown in the examples below.

Simple Syntax is not sufficient to define spread contracts. You need to use either Complex Syntax or Mixed Syntax . As a reminder, here is the string formula for defining the Combo Legs:

"cmb=<conid1>#<ratio1>#<action1>#<exchange1>;<conid2>#<ratio2>#<action2>#<exchange2>;"

Stock Spread

Buy 1 IBKR@SMART + Sell 1 MCD@SMART:

- Complex Syntax

=RTD("Tws.TwsRtdServerCtrl",,"sym=IBKR,MCD", "exch=SMART", "cur=USD", "sec=BAG", "cmb=43645865#1#BUY#SMART;9408#1#SELL#SMART;", "Bid")

- Mixed Syntax

=RTD("tws.twsrtdserverctrl",,"IBKR,MCD@SMART//BAG////USD", "cmb=43645865#1#BUY#SMART;9408#1#SELL#SMART;", "Bid")

Note: EFPs are simply defined as a bag contract of stock and corresponding SSF with a ratio of 100:1.


Futures Spread

Buy 1 VXJ7@CFE + Sell 1 VXK7@CFE:

- Complex Syntax

=RTD("tws.twsrtdserverctrl",,"sym=VIX", "exch=CFE", "cur=USD", "sec=BAG", "cmb=249139906#1#BUY#CFE;252623425#1#SELL#CFE;", "Bid")

- Mixed Syntax

=RTD("tws.twsrtdserverctrl",,"VIX@CFE//BAG////USD", "cmb=249139906#1#BUY#CFE;252623425#1#SELL#CFE;", "Bid")


Options Spread

Buy 1 DBK May19'17 15 CALL @EUREX+ Sell 1 DBK May19'17 16 CALL @EUREX:

- Complex Syntax

=RTD("tws.twsrtdserverctrl",,"sym=DBK", "exch=EUREX", "cur=EUR", "sec=BAG", "cmb=270579950#1#BUY#EUREX;270579957#1#SELL#EUREX;", "Bid")

- Mixed Syntax

=RTD("tws.twsrtdserverctrl",,"DBK@EUREX//BAG////EUR", "cmb=270579950#1#BUY#EUREX;270579957#1#SELL#EUREX;", "Bid")


Inter-Commodity Futures Spread

For Inter-Commodity futures, the 'Local Symbol' field in TWS is used for the 'Symbol' field in the TWS Contract Description.

Buy 1 CL May'17 @COMEX+ Sell 1 BZ Jun'17 @COMEX:

- Complex Syntax

=RTD("tws.twsrtdserverctrl",,"sym=CL.BZ", "exch=COMEX", "cur=USD", "sec=BAG", "cmb=55977404#1#BUY#COMEX;55807026#1#SELL#COMEX;", "Bid")

- Mixed Syntax

=RTD("tws.twsrtdserverctrl",,"CL.BZ@COMEX//BAG////USD", "cmb=55977404#1#BUY#COMEX;55807026#1#SELL#COMEX;", "Bid")

Note: Please be mindful of the fact that Inter-commodity Futures contracts are spread contracts offered by the exchange directly, and the contract definition is different from regular combo contracts. Please make sure all the contract attributes are specified in accordance with TWS Contract Description page.


Sample Spreadsheet

A sample RTD spreadsheet is provided within the API installation directory. By default, you will be able to find it under C:\TWS API\samples\ExcelTwsRtdServer.xls.