IBKR Quant Blog



Quant

Making Algorithmic Trading Easy With R and IBrokers API


The book, “An Introduction to Algorithmic Trading” by Edward Leshik and Jane Cralle contains a very interesting remark on markets. It says, “In the order of complexity the markets rank a good fourth after the Cosmos, Human Brain, and Human Immune System.”

It is indeed true that markets can be complex, but I feel that this complexity should not deter anyone from not participating in the markets and not trading them. Markets are very exciting and many a passionate traders have made a fortune for themselves by trading in the markets.

Thanks to the ever-evolving technological advances in trading, traders of the 21st century have access to feature-rich trading platforms, high-speed connectivity, algorithmic trading, and have multiple resources available to learn the nitty-gritties of trading. All these vital factors, I feel, have reduced the order of complexity for the trading community at large and especially for the newbie traders.

You must have noticed the word, “algorithmic trading” in the previous paragraph. Since its advent, thousands of traders have taken to algorithmic trading and thousands more are looking to switch from discretionary trading to algorithmic trading. Towards this end, Interactive Brokers offers and supports multiple API solutions in different programming languages along with its feature-rich trading platform which enables algorithmic trading for its clients.

Algorithmic Trading using R programming

Traders can use R programming for algorithmic trading on the Interactive Brokers platform. Here’s how R makes it easy to trade markets algorithmically:

  • Free and open-source software – R is a programming language used for scientific computing and graphics, which has gained wide popularity in recent years and ranks among the top programming languages for data science. R also refers to the software environment used to run programs written in R. The R environment is free and open-source software and one can use an integrated development environment (IDE) like RStudio to develop trading strategies in R.

  • Thousands of contributed packages on CRAN – The Comprehensive R Archive Network (CRAN) offers a wide variety of packages for R users on many different topics including statistical modeling, time series analysis, classification, clustering, and visualization (see the complete list of available packages here). R also offers the ability to create your own packages.  

  • Strong developer community – The popularity of R can be gauged by its strong developer community. There are many forums and sites like Stackoverflow, R-bloggers, Revolutions that serve as a great help to R programmers.

  • Packages for Quantitative Trading – Traders can make use of packages like Quantstrat, QuantTools and PerformanceAnalytics for backtesting and analyzing their trading strategies. You can also find numerous R packages on Machine Learning and Sentiment Analysis to create trading strategies.

  • Quick prototyping of back-tested strategies – Since R-based strategies can be used for both backtesting and in live markets, algorithmic traders can quickly prototype their backtested strategies and implement it live by making minimal code changes. See the example of the pairs trading strategy in R.

Given all these factors, traders who want to learn R but are little skeptical can easily overcome their fear of programming and trade with trading strategies based in R.

Algorithmic Trading using IBrokers R API

Given the simplicity and the benefits of R, it’s the ideal environment to develop trading strategies. The IBrokers API authored by Jeffrey Ryan and maintained by Joshua Ulrich offers traders the necessary support to implement their R-based strategies in live markets.

The IBrokers API connects the user’s R application to Interactive Brokers Trader Workstation (TWS) and helps execute the R-based trading strategies. Some of the key features of the IBrokers package include:

  • Access to account information – The user connects his R application to TWS using the twsConnect function and can retrieve the real-time account information using the reqAccountUpdates function. 

  • Retrieval of historical and real-time data from TWS – The IBrokers package offers functions like the reqHistoricalData, reqMktData, reqMktDepth, and reqRealTimeBars to retrieve data from TWS. The duration of the data can be in seconds, days, weeks, months, and years. Similarly, the user can set the bar size with these functions. The retrieved data can also be stored in files of the desired format for later use. 

  • Customization of the data functions – The market data functions can also be customized via their eventWrapper and CALLBACK arguments to obtained data in the desired format and desired fields.

  • Construct contracts for different instruments – The IBrokers package offers functions like the twsEquity, twsOption, twsFuture, twsCurrency, and the twsIndex to construct contracts for different instruments. 

  • Execute orders programmatically – Once the contracts are constructed and the signals generated from the trading strategy, users can submit orders to the TWS. The orders can be of different types (market, limit, stoploss, trailing etc.) and these can also be modified or cancelled per the strategy requirements.

These are some of the key features of the IBrokers API. Traders can build a variety of trading strategies using the IBrokers API and also test them with the paper trading simulation offered by Interactive Brokers before implementing them live in the markets.

To lean more about automated trading using the IBrokers API, you can check our latest course, “Trading Using R” offered on IB Trader’s Academy. It is an exciting course that covers the key functions from the IBrokers package with illustration of the code execution in RStudio. It also includes relevant examples and an R sample trading strategy at the end. We are sure that you will enjoy the course and influence you to explore trading with Interactive Brokers using R.

Start the course from here: https://gdcdyn.interactivebrokers.com/en/index.php?f=25243#course5

R-Course

 

Milind Paradkar holds an MBA in Finance from the University of Mumbai and a Bachelor’s degree in Physics from St. Xavier’s College, Mumbai. At QuantInsti®, Milind is involved in creating technical content on Algorithmic & Quantitative trading. Prior to QuantInsti®, Milind had worked at Deutsche Bank as a Senior Analyst where he was involved in the cash flow modeling of structured finance deals covering Asset-backed Securities (ABS) and Collateralized Debt Obligations (CDOs).

Learn more QuantInsti here https://www.quantinsti.com

This article is from QuantInsti and is being posted with QuantInsti’s permission. The views expressed in this article are solely those of the author and/or QuantInsti and IB is not endorsing or recommending any investment or trading discussed in the article. This material is 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 IB 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.


16197




Disclosures

We appreciate your feedback. If you have any questions or comments about IBKR Quant Blog please contact ibkrquant@ibkr.com.

The material (including articles and commentary) provided on IBKR Quant Blog is offered for informational purposes only. The posted material is NOT a recommendation by Interactive Brokers (IB) that you or your clients should contract for the services of or invest with any of the independent advisors or hedge funds or others who may post on IBKR Quant Blog or invest with any advisors or hedge funds. The advisors, hedge funds and other analysts who may post on IBKR Quant Blog are independent of IB and IB does not make any representations or warranties concerning the past or future performance of these advisors, hedge funds and others or the accuracy of the information they provide. Interactive Brokers does not conduct a "suitability review" to make sure the trading of any advisor or hedge fund or other party is suitable for you.

Securities or other financial instruments mentioned in the material posted are not suitable for all investors. The material posted does not take into account your particular investment objectives, financial situations or needs and is not intended as a recommendation to you of any particular securities, financial instruments or strategies. Before making any investment or trade, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice. Past performance is no guarantee of future results.

Any information provided by third parties has been obtained from sources believed to be reliable and accurate; however, IB does not warrant its accuracy and assumes no responsibility for any errors or omissions.

Any information posted by employees of IB or an affiliated company is based upon information that is believed to be reliable. However, neither IB nor its affiliates warrant its completeness, accuracy or adequacy. IB does not make any representations or warranties concerning the past or future performance of any financial instrument. By posting material on IB Quant Blog, IB is not representing that any particular financial instrument or trading strategy is appropriate for you.