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 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.
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.
- port – the port number that the TWS is listening on
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.
The twsConnectionTime function gives us the time when the connection to the TWS was made.
To disconnect from TWS, use the twsDisconnect. Simply, provide the twsConnection object created earlier and run the code to disconnect.
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.
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.
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.
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.
Note – The IBrokers package provides native R access to Interactive Brokers Trader Workstation API
Disclosure: Interactive Brokers
The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Supporting documentation for any claims and statistical information will be provided upon request.
Any stock, options or futures symbols displayed are for illustrative purposes only and are not intended to portray recommendations.
Disclosure: R API Disclosure
This software is in no way affiliated, endorsed, or approved by Interactive Brokers or any of its affiliates. It comes with absolutely no warranty and should not be used in actual trading unless the user can read and understand the source. IBrokers is a pure R implementation of the TWS API.