Fetch a wealth of information about contracts at Interactive Brokers that are found to match parameters supplied in the contract argument.

req_contract_details(contract = NULL, channel = NULL)

Arguments

contract

Named character vector of contract parameter(s). The name of each element identifies the parameter -- for example, secType, symbol, exchange... -- and each element itself gives the set value (e.g., "STK", "IBM", "SMART").

The `contract` parameters that may be used with req_contract_details() are:
secId, tradingClass, primaryExchange, conId, symbol, secType, lastTradeDateOrContractMonth, strike, right, multiplier, exchange, currency, localSymbol, includeExpired, secIdType

See contract for detailed information on all contract parameters.

channel

One of the following:

  • Not Specified (Default): Opens a new connection to IB, uses it to issue the request and retrieve the response, and closes connection behind itself upon completion.

  • The Name of a Sock: Character vector, length 1. The name of an open, connected socket in the sock_drawer; e.g., "master", "tws", or "sock_123"

  • Numeric Client ID: Numeric, length 1. The client ID for which open orders are to be retrieved; e.g., 0, 874, 123. If a client ID is passed, and no socket in the sock_drawer is connected on that ID, then a new socket will be opened on that ID, and closed upon function exit.

  • A sockconn Connection: An open connection object of class "sockconn", connected to the IB API; e.g., sock_drawer$tws

Details

IB's documentation describes each column variable that can appear in the output of InteractiveTradeR's implementation of req_contract_details() in the ContractDetails Members section. A list of the contract parameters that may be passed in via the contract object can be found in IB's Contract Class Reference.

Pacing and Large Queries: req_contract_details() is able to send queries that involve the transfer of high amounts of data in cases where many contracts are found to match the selection criteria. As a consequence, Interactive Brokers may pace requests made by this function by placing similar or identical requests on hold for one minute, with the amount of time increasing each time subsequent similar/identical requests are made.

The exact criteria regarding what constitutes a "similar" request, and the rules governing pacing behavior, are not published. However, by following three general rules of thumb, pacing should not be a problem when using req_contract_details() in InteractiveTradeR:

  • In SYNC mode ( channel = NULL) , bump up the timeout parameter for large queries with sync_timeout(). If a large number of contracts are found to match the parameters in contract, then the function might return an error if called with the default timeout because it needs bit more time than the default 5 seconds in order to complete. Try using sync_timeout(10) or sync_timeout(15).

  • Using conId Only: If the object passed in as contract has length = 1, then req_contract_details() will assume that contract contains a conId, which is sufficient to specify a unique contract. This shortcut can help speed up usage.

See also

Examples

#### Example: supplying more parameters gives more specific results. #### Goal: We would like to retrieve the contract details of IBM common stock, #### which, at IB, happens to have conId = 8314. # The fastest and simplest way to get IBM's contract details is by conId only: contract_details <- req_contract_details(8314) contract_details
#> # A tibble: 1 x 23 #> symbol secType exchange currency localSymbol marketName tradingClass conId #> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> #> 1 IBM STK SMART USD IBM IBM IBM 8314 #> # … with 15 more variables: minTick <dbl>, mdSizeMultiplier <dbl>, #> # orderTypes <chr>, priceMagnifier <dbl>, longName <chr>, #> # primaryExchange <chr>, industry <chr>, category <chr>, subcategory <chr>, #> # timeZoneId <chr>, tradingHours <chr>, liquidHours <chr>, aggGroup <dbl>, #> # stockType <chr>, exchange_info <list>
# You can select, manipulate, and view the fetched details: contract_details$exchange_info # View the exchange info
#> [[1]] #> # A tibble: 21 x 2 #> validExchanges marketRuleIds #> <chr> <chr> #> 1 SMART 26 #> 2 AMEX 26 #> 3 NYSE 26 #> 4 CBOE 26 #> 5 PHLX 26 #> 6 ISE 26 #> 7 CHX 26 #> 8 ARCA 26 #> 9 ISLAND 26 #> 10 DRCTEDGE 26 #> # … with 11 more rows #>
contract_details$orderTypes # See what order types are available
#> [1] "ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DARKONLY,DARKPOLL,DAY,DEACT,DEACTDIS,DEACTEOD,DIS,GAT,GTC,GTD,GTT,HID,IBKRATS,ICE,IMB,IOC,LIT,LMT,LOC,MIDPX,MIT,MKT,MOC,MTL,NGCOMB,NODARK,NONALGO,OCA,OPG,OPGREROUT,PEGBENCH,POSTONLY,PREOPGRTH,REL,RPI,RTH,RTHIGNOPG,SCALE,SCALEODD,SCALERST,SMARTSTG,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF"
contract_details$conId # Check the conId
#> [1] 8314
# ... and so on. # You can also use glimpse() to print the information in what may be an # easier-to-read format: dplyr::glimpse(contract_details)
#> Observations: 1 #> Variables: 23 #> $ symbol <chr> "IBM" #> $ secType <chr> "STK" #> $ exchange <chr> "SMART" #> $ currency <chr> "USD" #> $ localSymbol <chr> "IBM" #> $ marketName <chr> "IBM" #> $ tradingClass <chr> "IBM" #> $ conId <dbl> 8314 #> $ minTick <dbl> 0.01 #> $ mdSizeMultiplier <dbl> 100 #> $ orderTypes <chr> "ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET… #> $ priceMagnifier <dbl> 1 #> $ longName <chr> "INTL BUSINESS MACHINES CORP" #> $ primaryExchange <chr> "NYSE" #> $ industry <chr> "Technology" #> $ category <chr> "Computers" #> $ subcategory <chr> "Computer Services" #> $ timeZoneId <chr> "EST (Eastern Standard Time)" #> $ tradingHours <chr> "20200317:0400-20200317:2000;20200318:0400-20200318:… #> $ liquidHours <chr> "20200317:0930-20200317:1600;20200318:0930-20200318:… #> $ aggGroup <dbl> 1 #> $ stockType <chr> "COMMON" #> $ exchange_info <list> [<tbl_df[21 x 2]>]
# But what if you didn't already know IBM's conId? # 2) You could try to get IBM's contract details by symbol only, but because # there are many securities across many exchanges that have the symbol "IBM" # this query won't work -- IB responds by asking for more info. contract_details_1 <- req_contract_details(contract = c(symbol = "IBM")) # 3) So, try providing a valid security type: contract_details_2 <- req_contract_details( contract = c(symbol = "IBM", secType = "STK") ) contract_details_2
#> # A tibble: 28 x 24 #> symbol secType exchange currency localSymbol marketName tradingClass conId #> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> #> 1 IBM STK SMART USD IBM IBM IBM 8.31e3 #> 2 IBM STK AMEX USD IBM IBM IBM 8.31e3 #> 3 IBM STK NYSE USD IBM IBM IBM 8.31e3 #> 4 IBM STK SMART EUR IBM XETRA XETRA 1.41e6 #> 5 IBM STK CBOE USD IBM IBM IBM 8.31e3 #> 6 IBM STK PHLX USD IBM IBM IBM 8.31e3 #> 7 IBM STK ISE USD IBM IBM IBM 8.31e3 #> 8 IBM STK CHX USD IBM IBM IBM 8.31e3 #> 9 IBM STK ARCA USD IBM IBM IBM 8.31e3 #> 10 IBM STK ISLAND USD IBM IBM IBM 8.31e3 #> # … with 18 more rows, and 16 more variables: minTick <dbl>, #> # mdSizeMultiplier <dbl>, orderTypes <chr>, priceMagnifier <dbl>, #> # longName <chr>, primaryExchange <chr>, industry <chr>, category <chr>, #> # subcategory <chr>, timeZoneId <chr>, tradingHours <chr>, liquidHours <chr>, #> # secIdList <list>, aggGroup <dbl>, stockType <chr>, exchange_info <list>
# This call will work, but returns ALL of the contracts whose symbol is "IBM". # Only one of these is the contract of interest, so this doesn't help much. # 4) Narrow things down by specifying a currency: contract_details_3 <- req_contract_details( contract = c( symbol = "IBM", secType = "STK", currency = "USD" ) ) contract_details_3
#> # A tibble: 21 x 23 #> symbol secType exchange currency localSymbol marketName tradingClass conId #> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> #> 1 IBM STK SMART USD IBM IBM IBM 8314 #> 2 IBM STK AMEX USD IBM IBM IBM 8314 #> 3 IBM STK NYSE USD IBM IBM IBM 8314 #> 4 IBM STK CBOE USD IBM IBM IBM 8314 #> 5 IBM STK PHLX USD IBM IBM IBM 8314 #> 6 IBM STK ISE USD IBM IBM IBM 8314 #> 7 IBM STK CHX USD IBM IBM IBM 8314 #> 8 IBM STK ARCA USD IBM IBM IBM 8314 #> 9 IBM STK ISLAND USD IBM IBM IBM 8314 #> 10 IBM STK DRCTEDGE USD IBM IBM IBM 8314 #> # … with 11 more rows, and 15 more variables: minTick <dbl>, #> # mdSizeMultiplier <dbl>, orderTypes <chr>, priceMagnifier <dbl>, #> # longName <chr>, primaryExchange <chr>, industry <chr>, category <chr>, #> # subcategory <chr>, timeZoneId <chr>, tradingHours <chr>, liquidHours <chr>, #> # aggGroup <dbl>, stockType <chr>, exchange_info <list>
# This helped somewhat, but still have a large number of matching contracts. # 5) Specify an exchange: contract_details_4 <- req_contract_details( contract = c( symbol = "IBM", secType = "STK", currency = "USD", exchange = "SMART" ) ) contract_details_4
#> # A tibble: 1 x 23 #> symbol secType exchange currency localSymbol marketName tradingClass conId #> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> #> 1 IBM STK SMART USD IBM IBM IBM 8314 #> # … with 15 more variables: minTick <dbl>, mdSizeMultiplier <dbl>, #> # orderTypes <chr>, priceMagnifier <dbl>, longName <chr>, #> # primaryExchange <chr>, industry <chr>, category <chr>, subcategory <chr>, #> # timeZoneId <chr>, tradingHours <chr>, liquidHours <chr>, aggGroup <dbl>, #> # stockType <chr>, exchange_info <list>
# Success! For IBM, these four exchanges are enough to specify the contract. # Bond details return slightly different parameters. See the "ContractDetails" # documentation on IB's website at the link provided in the "Value" section. broadcom_bond <- req_contract_details(359401413) broadcom_bond
#> # A tibble: 1 x 10 #> secType descAppend exchange tradingClass conId minTick mdSizeMultiplier #> <chr> <chr> <chr> <chr> <int> <dbl> <int> #> 1 BOND AVGO 4 1/… SMART AVGO 3.59e8 0.0001 1 #> # … with 3 more variables: orderTypes <chr>, aggGroup <int>, #> # exchange_info <list>