This post is a part of the series “Building Your Automated Strategy with IBKR: From Back Testing to Paper Trading to Live Execution”.
Back Testing Basics
Backtesting assesses the viability of a trading strategy by discovering how it performed using historical data.
Backtrader – An Open Source Backtesting Framework
Backtrader is a Python library that allows backtesting and strategy development before live trading. (see: https://www.backtrader.com/docu/) .
It is an open-source framework that enables strategy testing using historical data. Furthermore, it can be used to optimize strategies, create visual plots, and can also be used as a trading engine for actual trading in the market. It has an active community providing support and help for people using the platform (see: https://community.backtrader.com)
Backtrader and Interactive Brokers
Backtrader has a robust integration with Interactive Brokers for historical data, paper trading and live trading. But the current implementation has not been updated for many years.
An Updated Integration for Interactive Brokers
This article presents an updated integration, and describes how it can be seamlessly installed into Backtrader so that it can be used with the latest version of the IB Native API.
What is the IB TWS API?
The Interactive Brokers Python TWS API allows you to trade via a running TWS (or IB Gateway) using Python code.
Acting as a bridge, the API allows for sending of orders from custom software or scripts, receiving live or historical data. Note that all the orders are reflected in the TWS frontend.
See: /campus/ibkr-quant-news/python-and-the-ibkr-tws-api/ for more information
System Workflow
Getting Started
There are four basic steps to getting started:
- Open an account with IB and setup the trading frontend (TWS or IB Gateway)
- Download and install the IB TWS API
- Down and install BackTrader
- Update default IB integration built into Backtrader
Signup, Download and Install the IB TWS Frontend/TWS API
Open the account at Interactive Brokers (using https://www.interactivebrokers.com/en/pagemap/pagemap_newaccounts_v3.php) and download and install TWS.
You can download the TWS API by navigating to Technology – Trading APIs – Get API Software, or by following this link – http://interactivebrokers.github.io/
Installing the TWS API
Create a directory named back_testing, and copy the downloaded file twsapi_macunix.1016.10.zip into this directory, unzip and it will create the IBJts directory.
Create And Enable A Python Virtual Environment within the back_testing directory
python3 -m venv ./env
source ./env/bin/activate
Drill down to the directory back_testing/IBJts/source/pythonclient and run:
python setup.py install
To check if it is installed correctly, open a Python terminal and type:
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ibapi
>>>
If no errors appear, the install was successful!
Download and Install BackTrader
There is no special requirement for installing the BackTrader library. It also doesn’t have any dependencies. You can install this library by using the package manager “pip”.
pip install backtrader
Now you can use backtrader with its full functionality.
To check if it is installed correctly open a Python terminal and type:
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Import backtrader as bt
If no errors appear, the install was successful.
Download and Install the updated IB TWS integration
Either clone the github repo:
git clone https://github.com/atreyuxtrading/atreyu-backtrader-api.git
Or download a released version:
wget https://github.com/atreyuxtrading/atreyu-backtrader-api/archive/refs/tags/v1.0.zip
Unzip file, and install the Atreyu Backtrader API.
unzip v1.0.zip
cd atreyu-backtrader-api-1.0 ; python setup.py install
Check Settings of Locally Running TWS
Example: Download Realtime Bar Data from TWS
Stay tuned for the next installment in this series to learn how to set up Backtrader with the latest TWS API.
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: 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.
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.
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.