In the previous article on IBPy Tutorial to implement Python in Interactive Brokers API, I talked about Interactive Brokers, its API and implementing Python codes using IBPy. In this article, I will be talking about implementing python in IB's C++ API using a wrapper, written by Dr. Hui Liu.
About Dr. Hui Liu
Dr. Hui Liu is the founder of Running River Investment LLC, which is a private hedge fund specialized in development of automated trading strategies using Python. Hui is the author of IBridgePy, a famous Python trading platform that allows traders to implement their trading strategies quickly.
Dr. Hui Liu obtained his bachelor degree and master degree in materials science and engineering from Tsinghua University, China and Ph.D from University of Virginia, U.S.A. His MBA was from Indiana University, U.S.A and his study interest at Indiana was quantitative analysis.
Dr. Liu’s IBridgePy is the wrapper that will help you trade in Interactive Brokers API using Python, instead of IBPy or Quantopian. QuantInsti hosted a highly successful webinar on this subject and we had a record number of registrants (1000+) attending the webinar which was conducted by Dr. Liu himself. I will be sharing the link to the webinar at the end of this article.
IBridgePyIBridgePy is one of the most flexible and friendliest Python packages which can be used to implement Python on Interactive Brokers API. It is quite different from IBPy.
How?Well, is a third-party implementation of the Application Programming Interface that is used to access IB’s Trader Workstation. While IBridgePy works differently; it does not re-implement Interactive brokers’ API. Instead it helps Python to call IB’s C++ API directly as it acts as a wrapper. Since IBridgePy calls on IB’s C++ API directly, therefore, we can expect less number of errors and exceptions in the program.
Why?For a simple reason: Interactive Brokers C++ API is officially maintained by Interactive Brokers. In fact, when Interactive Brokers will have a new release of its platform, then the API will also be updated.
What are the benefits of using IBridgePy?IbridgePy has some key features which make it more beneficial for users and I am going to list these as:
- Ease of Use
FlexibilityThe best thing about IBridgePy is the fact that you can use it to trade any kind of securities. You can trade stocks, futures, options, forex etc. One of Dr. Liu’s motivations to develop IBridgePy was to make the application flexible. Quantopian lets you trade only in stocks and the time frequency for trading is only two minutes. On IBridgePy, you can use any python package and access data source from anywhere.
Easy-to-useThe complexities of Interactive Brokers API have been completely eclipsed by the user friendly interface of IBridgePy. You will not have to put any extra efforts to manage your orders that are pending or writing codes to get historical data or quotes from the server, as the wrapper takes care of it.
PrivacyPrivacy is very important and one of the overlooked factors at times when you give out your credentials to a third party program that is running on a different server. However, IBridgePy is running on your computer, so your privacy is totally under your control.
Who should use IBridgePy?
- People who are into Automated trading
- Those who want to use it as a market or stock screener
How to install IBridgePy?First of all, you will have to send an email to email@example.com requesting for the standalone application. You will receive the download link by email.
Download the compressed file (.zip) and extract it in your local folder.
PrerequisitesYou should have the following programs installed on your system:
- Interactive Brokers TWS. We have discussed in detail how to install Interactive Brokers TWS in the previous article.
- Alternately, you can also use IB Gateway (which comes along with TWS)
- You should also have a python package installed on your system. I am using Anaconda, however, Dr. Liu has recommended using Python(XY) which is a scientific IDE for Python which only runs on windows.
- You should have IbridgePy (and the set of sample programs) which you can get by writing a request to IbridgePy's email mentioned above.
TWS or IB Gateway?Both TWS and Gateway can be used to trade. If you are relatively new to the trade, then I would recommend TWS as it is more user friendly. However, if you want to save time and avoid IB system restarts then go for the IB Gateway.
We will follow the same steps as in the previous article. When the login screen appears, check on IB Gateway and proceed. When the IB Gateway opens, we go to its configuration, just like we did with TWS.
This will be followed by the manual configuration of the IB Gateway. Next, click on Settings. Though there is nothing to change here, we can set the port as per your need, although it is not recommended.
We will also set TWS, like we did in the previous article. Restart TWS after configuring it.
Preparing IDEThe next important step in this tutorial is to prepare the Python’s IDE. If you are using Python(XY), then you must start the executable and run spyder from there:
Once Spyder starts, and follow these steps:
- Go to View then Panes and check Editor and Console
- Add the folder to the Python Path by clicking on Tools then selecting PYTHONPATH manager. Here you will click on Add path and choose the folder where you had unzipped IBridgePy.
- Click on close to accept it.
- Restart Spyder.
Running Sample ProgramClick on RUN_ME.py in Spyder IDE by clicking on File and then on Open.
*RUN_ME.py is the program you may run this as the entrance but example_moving_average_cross.py is the file where the trading strategy is kept.
After this, ensure that you log in to IB TWS or IB gateway. Hit on the green triangle or press F5.
Decoding the Code StructureJust like I had discussed about the structure of the program in my previous article, I am going to talk about the code structure here as well.
We define initialize() which is an built-in method to claim variables which will only be run once.
Next we define handle_data(), which is also a built-in method where trading decision is made. Two inputs are given here (context, data). Context contains the variables claimed in initialize(). While data is the live feed received either daily or minutely.
Line 16~ line 19: You may choose one algorithm that you want to execute by commenting out others
Line 20: Your Interactive Brokers account code
Line 21: The frequency or how often the function of handle_data(context, data), 60 means to run it every minute and 1 means to run it every second.
Line 22: There are 4 levels to show results
- ERROR: only show error messages
- INFO: typical users will use it to know the results of your algo
- DEBUG: you may know more info when you debug your algo
- NOTSET: You will see tremendous info if you really want to know what is going on
The 3 Corner Stones of IBridgePyHere are three of the most striking in-built functions which form the cornerstones of IbridgePy:
- Placing Order
Real-time quotesWhenever you need real-time quotes, then you can use the built-in function show_real_time_price
In the above line, you will see the parameters used are CASH, EUR, USD which will return with the forex rates in real-time for the currencies specified.
Similarly, you can use STK, AAPL, USD instead and the value of APPLE’s stock in US dollars. STK is the security type, which we have discussed in detail in the previous article on Implementing Python in Interactive Brokers API using IBPy.
Fetching Historical Datarequest_data is the function that is used to get historical data from Interactive Brokers. You will also have to specify a parameter historyData.
- Using the parameters we select the instrument for which the historical data needs to be obtained, in the above example it is SPY which is an ETF tracking on the S&P500 index
- Next we specify the time gap, in our example it is ‘1 day’
- Go-back period is the next parameter where we specify the period of historical data that is required. In our example it is ’50 D’ means go back 50 days from today.
- The retrieved historical data are saved in a pandas dataframe that are saved at hist, an attribute of the DataClass, saved in a dictionary called data.
Requesting Multiple Historical DataJust like you requested historical data from Interactive Brokers for a specific period of time, you can also fetch multiple historical data at once. Look at the code below:
I have specified SPY and AAPL and the time gap specified is 1 min (1 minute) and 1 day respectively. The period specified is 1 D & 10 D (D = days) respectively. Similarly, we have specified the requirements in the output function as well.
Placing OrderOrder placing is the important step in our entire process and here is how we place order on Interactive Brokers using IBridgePy:
- Place market orders: order(symbol(‘security’), x)
Eg, order(symbol(‘SPY’), 100)
- The target security is SPY
- The action is to BUY 100 shares when n > 0
- If it was -100, then the negative number means SELL, -100 = SELL 100 shares
- order_target(symbol(‘SPY’), 100) will adjust positions based on your holding positions by either BUY or SELL until you hold 100 shares
- Place Limit/Stop orders
- order(symbol(‘SPY’), 100, LimitOrder(213.42)) place a limit order to BUY 100 shares of SPY at price = $213.42 per share
- order(symbol(‘SPY’), -100, StopOrder(213.42)) place a stop order to SELL 100 shares of SPY at price = $213.42 per share
- It is highly recommended to follow up on the status of the order you placed by using order_status_monitor()
Just like in the previous tutorial, we had specified an orderId, here also we specify a function orderId which is the unique identity of your order requests. For market orders, you should expect ‘Filled’ as the ending point of your order request, which means the orders have been executed at Interactive Brokers.
For limit and stop orders, the expected status is ‘Submitted’, which means the orders have been accepted by IB and waiting for executions. For high liquidity securities, it won’t take too long (a few seconds) to complete the transactions.
The IbridgePy.Trader_single_account is the name of the module and it is followed by a REQUEST to buy 100 shares. This is followed by the Account Balance.
You can watch recording of Dr. Hui Liu's webinar here.
Next StepThis is a relatively simple tutorial and can be easily understood if you already gone through our previous article. We would like you to try using IBridgePy to trade on Interactive Brokers and see which one you find is easier- IBPy or IBridgePy. We will wait for your feedback. In the meantime, we would also like to tell you that we will be discussing about another API called KiteConnect, which will also be used to trade using Python without being bound to some trading platform’s User interface.
You can read more about using Python for algorithmic trading here and it is preferred by programmers all over the world. If you want to know more about algorithmic trading and quantitative finance, then you should register for a free call from our EPAT specialist by signing up here.