https://github.com/enricoschumann/ibutils
R utility functions for the Interactive Brokers TWS API
https://github.com/enricoschumann/ibutils
ib-api interactive-brokers r twsapi
Last synced: 11 months ago
JSON representation
R utility functions for the Interactive Brokers TWS API
- Host: GitHub
- URL: https://github.com/enricoschumann/ibutils
- Owner: enricoschumann
- Created: 2016-10-07T08:36:46.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-12-06T21:41:56.000Z (over 1 year ago)
- Last Synced: 2025-03-29T17:51:09.516Z (about 1 year ago)
- Topics: ib-api, interactive-brokers, r, twsapi
- Language: R
- Homepage: https://enricoschumann.net/R/packages/IButils/index.htm
- Size: 296 KB
- Stars: 8
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- Changelog: ChangeLog
Awesome Lists containing this project
README
* IButils -- Utility Functions for the Interactive Brokers API
Utility functions for the Interactive Brokers API,
based on packages [[https://cran.r-project.org/package=IBrokers][IBrokers]] and [[https://github.com/lbilli/rib][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:
#+BEGIN_SRC R :eval never :export code
install.packages("IButils",
repos = c("https://enricoschumann.net/R", getOption("repos")))
#+END_SRC
** Example: The British Pound's Flash Crash
#+BEGIN_SRC R :exports code :session *R* :eval query
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)
#+END_SRC
#+RESULTS:
Plot it.
#+BEGIN_SRC R :exports both :session *R* :eval query :results graphics :file ./GBPUSD_crash.png :width 600 :height 450
plot(.POSIXct(x$timestamp), x$close, type = "s",
xlab = "", ylab = "USD per GBP")
#+END_SRC
#+RESULTS:
[[file:./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:
[[file:TWS_Contract_Detail.png]]
There you have it: Symbol (=C OESX DEC 17=\nbsp{}\nbsp{}
=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
[[https://quant.stackexchange.com/questions/37662/interactive-brokers-automating-collection-of-client-account-position-without-tw/37677#37677][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:
[[file: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.
#+begin_src R :eval never :export both
positions()
#+end_src
Get order status.
#+begin_src R :eval never :export both
order_status()
#+end_src
Get information on executions.
#+begin_src R :eval never :export both
executions()
#+end_src
** News, feedback and discussion
Please send bug reports or suggestions directly to the
package maintainer, for instance by using =bug.report=.
#+BEGIN_SRC R :eval never :export code
library("utils")
bug.report("[IButils] Unexpected behaviour in function XXX",
maintainer("IButils"), package = "IButils")
#+END_SRC