Skip to content

enricoschumann/IButils

Repository files navigation

IButils – Utility Functions for the Interactive Brokers API

Utility functions for the Interactive Brokers API, based on packages IBrokers and rib.

Installing the package

The latest build of the package is always available from https://enricoschumann.net/R/packages/IButils/index.htm.

To install the package from within an R session, type:

install.packages("IButils",
                 repos = c("https://enricoschumann.net/R", getOption("repos")))

Example: The British Pound’s Flash Crash

library("IButils")
x <- ib_hist_data("GBP.USD", "CASH", "IDEALPRO", "USD",
                  directory = tempdir(),
                  whatToShow = "MIDPOINT",
                  barSize = "1 secs",
                  start = as.POSIXct("2016-10-7 00:00:00"),
                  end   = as.POSIXct("2016-10-7 02:00:00"),
                  accumulate = TRUE)

Plot it.

plot(.POSIXct(x$timestamp), x$close, type = "s",
     xlab = "", ylab = "USD per GBP")

./GBPUSD_crash.png

Finding the contract specification

To retrieve data of an instrument, you need four things:

  • its local (native) symbol
  • its type
  • its currency
  • its exchange

The easiest way to get these data is to first find the contract in the TWS, and check the Contract Details. For instance, for the call on the EURO STOXX 50 that expires on 16 June 2017, the Contract Details look as follows:

TWS_Contract_Detail.png

There you have it: Symbol (C OESX DEC 17   3600), Security Type (OPT), its Currency (EUR) and its Exchange (DTB).

(It may even be a good idea to copy the symbol from there: there may be several spaces within a symbol string, which would be easy to miss otherwise.)

Reading flex reports

Function flex_web_service downloads flex queries; see Automating collection of client account position without TWS/IB Gateway.

Reports can be read with read_flex_report. That function assumes that section headers were used. See these example settings:

Delivery configuration: flex_delivery_config1.png

General configuration: flex_general_config1.png

TWS helper functions

The package provides several convenience functions. The functions follow the same pattern: connect to the TWS, send requests, receive and process results, and disconnect. Results are typically arranged as data frames. The functions try to connect at port 7496, and with clientId 1.

Fetch current positions.

positions()

Get order status.

order_status()

Get information on executions.

executions()

News, feedback and discussion

Please send bug reports or suggestions directly to the package maintainer, for instance by using bug.report.

library("utils")
bug.report("[IButils] Unexpected behaviour in function XXX",
           maintainer("IButils"), package = "IButils")

Packages

No packages published

Languages