Introduction to IB API

Table Of Contents

  1. Introduction
    1. Why Automate Finance?
    2. Why Interactive Brokers?
    3. Why Python?
    4. Why Use this Guide?
  2. Environment Setup
    1. Account Setup
    2. The Trader Workstation
    3. API Files
  3. Connection
    1. Program Overview
    2. Import Statements
    3. Test Wrapper (In book, preview coming soon)
    4. Test Client (In book, preview coming soon)
    5. Test App (In book, preview coming soon)
    6. Program Execution (In book, preview coming soon)
  4. Chapters 4-10 In book

    1. Why Automate Finance?

    In 1995, a firm called Renaissance Capital began changing the way the world used mathematics and data to interact with the finance world. Founded by a famous and respected mathematician, James Simons, Renaissance Capital began breaking, then shattering Wall Street records. Between the years of 1994 to 2014, Renaissance returned an unprecedented 71.8% annual average return.

    Despite the firm's internal secrecy, the word of their success spread throughout Wall Street and the world. Today, the words "quantitative finance" buzz around the internet as anyone from the solo hobbyists to high profile investors, such as Marc Andreessen and Steven Cohen, try to duplicate Renaissance's success.

    Despite the widespread discussion, successful quantitative finance and systemic methods of investing introduce little shortcuts. There's no publicly available secret or promise to get rich overnight; however, by gaining proficiency in algorithms and developing strategies of your own, it's more than possible to produce systems that can deliver an upper hand over traditional investors.

    Before the 1960s, the traditional pattern of trading was to wait for a notification of a news article or signal, open and read the content, make a decision, then execute the trade. Usually, this process was done by representatives on the NYSE trading floor. Purchasing a security could take up to an hour, with no guarantee that you're receiving the price you expected. The process improved through a wave of technology that enabled individual brokers to trade through a digital terminal; however until the 2000s, a substantial human aspect remained, and the process took minutes.

    Now, we are on the dawn of a new age of digital intervention in the stock market. Computers are able to process an incomprehensible amount of data with unparalleled execution times. Instead of hours or minutes, algorithms can execute the same trading process within seconds or even milliseconds. Trades are delivered magnitudes quicker and leave traditional investors scrambling to find a new advantage.

    Algorithms deliver convictions at the best point calculated. They rarely fall prey to the human pitfalls that hold many traders back. There is no crippling hesitation, just probability and decisions. By learning quantitative finance methods, you will be able to drastically set yourself apart from others in the field of finance. Algorithms run as an extension of yourself and your strategies. By bringing your vision to life, you can learn quicker and profit more.  

    2. Why Interactive Brokers?

    Interactive Brokers is commonly ranked among the top online trading platform for many reasons, here are a few: 

    Low commission and free trades: Interactive Brokers offers one of the most competitive pricing platforms. With IBKR Lite, you receive free trading. With Pro, stock commissions are down to $0.002 per share for bulk orders with competitive options pricing (65 cents per contract).

    Powerful trading platform: The TWS considered one of the best interfaces for advanced users. It offers quick and real-time data, tools like volatility lab and heat mapping, paper trading, and custom notifications.  

    Investment Selection: The selection of investment assets include stocks, options, bonds, ETFs, forex, warrants, and futures. These assets can be traded in 31 countries on over 125 markets. 

    Robust API: Finally, the feature most relevant to this guide is their extensive API. The Interactive Brokers API (IB API) gives access to all the features included in the Trader Workstation. This allows unrestricted access to trades from inside a program.

    3. Why Python?

    Python is quickly becoming one of the most powerful programming languages that exist. Large companies, universities, and disciplines have embraced the combination of simplicity and power of Python, which has led to widespread adoption.

    Python is an object-oriented scripting language designed to make development easier and code more readable. The syntax is clean and straightforward, lending large amounts of content to be visually digestible. This guide implements fairly simplistic symbolism, such as colons, parentheses, and basic math operators, with the intent that only a beginner's level knowledge is necessary to complete this guide.

    Python is known for reading similarly to the English language. Operations can be performed with keywords that contribute to a smooth learning curve. Users of this guide come from all backgrounds; hence, little programming experience is required. Any user of this guide should be able to digest any code snippet without extensive references to outside documentation (although it can't hurt to brush up on the fundamental components).

    Widespread adoption has led to comprehensive documentation and forum support. So, many common questions can be answered with a Google search. There is a community of people on the internet capable of answering many of the problems users of this guide may encounter (especially on StackOverflow).

    Despite its simplicity, Python is known to be one of the most powerful languages available. The applications of Python range from computation and data manipulation to natural language processing and artificial intelligence. Hence, the skills learned in this guide can carry over to several other disciplines and projects in the future.

    Critics of the role of Python in quantitative finance mention the fact that Python is an interpreted language, as opposed to alternatives such as Java, which are pre-compiled; however, for the majority of applications, the difference is an unnoticeable influence in performance (obvious exceptions include strategies that employ HFT or instantaneous market reactions). For those determined to use Java, C, or other, the concepts presented in this guide are similar and the explanations will still prove to be valuable (The official documentation can assist in the syntax conversion).

    In short, the brevity, readability, and power of Python make this language an excellent pair for algorithmic trading.  

    4. Why This Guide?

    The world of systematic trading can be incredibly confusing and competitive. Creating a great algorithm requires complexity and creativity. Every strategy is different, and there is no holy grail to being successful. This book exists as the quickest path to learn about the field of quantitative finance and generate a functioning algorithm.

    Many of the producers of current algorithms give little back to the community to which they belong. Those with the most knowledge rarely fill the field with educational content. When I was just starting, I found the community severely lacked quality and depth.

    Instead of needlessly spending dozens of hours troubleshooting, experimenting, and testing on your own, this book will help you accomplish the same algorithm in one-tenth of the time. Development teams can potentially save thousands of dollars, and new strategists can gain exposure to a new range of ideas.

    Other platforms exist for absolute beginners to experiment with; however, these are nothing more than a restricted playground. After developing an algorithm on a 3rd party text editor, testing on a limited set of data, and deploying on a delayed engine, your algorithm would suffer in performance and creativity. In an effort to oversimplify quantitative trading for absolute beginners, other platforms have limited their user's algorithms from ever outgrowing their box. This guide is the opposite. It intends to expose readers to the power of Interactive Brokers and enables users to run and test programs on their own, more powerful systems.

    This guide not only delivers a practical algorithm by the end, but it also sets the reader up on a platform to fulfill their expectations and execute their vision. The knowledge gained in this guide will empower you to create amazing, high-performance algorithms.