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.
Reference Documentation for IBrokers Package
Note – The IBrokers package provides native R access to Interactive Brokers Trader Workstation API
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material 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 to buy, sell or hold such security. 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.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading 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.