This is a follow up from PART 1: Back Testing with IBKR with Backtrader, where we setup the Backtrader and the IBKR TWS API.
The IBKR TWS API
Although the Interactive Brokers TWS API is mainly used for trading, it can also be used to retrieve historical data from Interactive Brokers. This data can either be written into files and used in subsequent analysis or used without saving to back test a strategy.
Note, that data will only be available for the markets and securities that have been enabled for the account.
Historical Data Types
The IB data feed can also supply market data at different resolutions (if it is available). In these examples we will retrieve historical data in the form of candlesticks (OHLCV). Note that different data types can also be accessed by using the “what=” parameter, see that table below for further details. (Reference: https://interactivebrokers.github.io/tws-api/historical_bars.html)
Details:
TRADES data is adjusted for splits, but not dividends.
ADJUSTED_LAST data is adjusted for splits and dividends.
SCHEDULE returns historical trading schedule only with no information about OHLCV
Using Backtrader with Historical Data
Backtrader has extensive support for retrieving and analysing data. To display the data we must first create a TestPrinter and register it with the framework so that it prints the market data as it arrives.
Create A Test Printer
Retrieving Simple BID_ASK Historical Data
Note the use of the historical=True and what=BID_ASK.
Output
Fetch TRADES for a Time Range (2016/01/01 – 2018/01/01)
Output
How is the Data Presented in the Strategy?
The data retrieved from IB is presented in the strategy as the variable self.datas[0].
The latest close price is available at index 0, and progressively earlier prices are stored using a negative index. (See diagram below)
Using IB Historical Data to Drive a Strategy with what=MIDPOINT
The data retrieved from IB is presented in the strategy as self.datas[0].
The latest close price is available at index 0, and progressively earlier prices are stored using a negative index. (See diagram below)
Naming Datasources and using them in a Strategy
Datasources can be given logical data names (i.e. based on the “what” parameter), the logical name can then be accessed using the _name variable. In the strategy below the logical name is stored in the self.name0 and self.name1 variables, and can be used to identify the buy for each symbol.
See the name parameter being used to tag each data source in the example below:
Output
Stay tuned for the next installment in this series to learn about various types of real-time data that can be used to run trading strategies.
Visit Atreyu Trading Services https://www.atreyutrading.com/ for additional insight on Backtrader.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation 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 Atreyu Trading Services and is being posted with its permission. The views expressed in this material are solely those of the author and/or Atreyu Trading Services and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.
Disclosure: API Examples Discussed
Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.
Disclosure: Order Types / TWS
The order types available through Interactive Brokers LLC's Trader Workstation are designed to help you limit your loss and/or lock in a profit. Market conditions and other factors may affect execution. In general, orders guarantee a fill or guarantee a price, but not both. In extreme market conditions, an order may either be executed at a different price than anticipated or may not be filled in the marketplace.
Join The Conversation
If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.