In the previous lesson, we explained how to configure the Trader Workstation (TWS). This lesson introduces the IBrokers package and some of the basic API methods like establishing connection with TWS, getting account details, and creating contracts for use in API calls.
In this lesson, you would:
- Get introduced to the IBrokers Package
- Learn the basic API methods
What is IBrokers Package?
The IBrokers package has been created by Jeffery Ryan and is currently maintained by Joshua Ulrich. It is a pure R implementation of the Interactive Brokers Trader Workstation API. It allows an account holder to trade on Interactive Brokers using the R programming language.
Some of the key uses of the IBrokers API include retrieval of real-time data from Trader Workstation; access to contract details, account information and programmatic execution of the orders. Some of the important API methods supported by the IBrokers package include:
Basic API Methods
Before we understand some of the basic API methods listed in the above table, we first need to install the IBrokers package. To install the package, use the standard installation function called install.packages in R.
Connection Functions
twsConnect Function
This function is used to establish a connection with TWS. The function returns a twsConnection object which is used in the subsequent TWS API calls.
Example:
- port – the port number that the TWS is listening on
isConnected Function
The twsConnection object created in the above step can be used to check whether we have successfully established a connection with the Trader Workstation (TWS) using the isConnected function. The function returns a “TRUE” Boolean upon a successful connection.
twsConnectionTime function
The twsConnectionTime function gives us the time when the connection to the TWS was made.
Example:
twsDisconnect Function
To disconnect from TWS, use the twsDisconnect. Simply, provide the twsConnection object created earlier and run the code to disconnect.
reqAccountUpdates Function
This function is used to request and view account details from TWS. A valid twsConnection object is required as the argument to the function in order to fetch the account details.
Example:
Contract Functions
twsContract Function
The twsContract function is used to create and test a twsContract for use in API calls. You can get the particular contract details from the Trader Workstation (TWS) in the Financial Instrument Information section.
Example:
Wrapper functions
In addition to the twsContract function, there are other helper functions which enable a user to create a contract instead of using the twsContract function. These are a wrapper to the twsContract function which makes the contract easier to specify. These functions include:
twsEquity: wrapper to create equity Contract objects.
twsFuture: wrapper to create futures Contract objects.
twsOption: wrapper to create option Contract objects.
twsCurrency: wrapper to create currency Contract objects.
Summary
This lesson covered the workings of some of the basic API methods available in the IBrokers package. In the next lesson, we will explain the other advanced API methods which are used to fetch historical data, real-time data, and to place orders.
Additional Resources
Reference Documentation for IBrokers Package
Note – The IBrokers package provides native R access to Interactive Brokers Trader Workstation API
Join The Conversation
If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.