Backtest strategies on IBridgePy

We are very pleased to announce that IBridgePy Backtester is released from V 5.0.1. The main feature of IBridgePy Backtester is to backtest strategies using historical data that are retrieved from Interactive Brokers or other data providers. The backtester supports most of IBridgePy functions and Quantopian functions. In this blog, we are going to introduce how to backtest strategies.

The webinar of backtest and live trading with Interactive Brokers is on youtube

Step 0: Open and configure IB TWS or IB Gateway, following this tutorial.

Step 1: Open TEST_ME_IB.py in the folder of unzipped IBridgePy.

Step2: Change accountCode to your accountCode so that you can download historical data from Interactive Brokers. Choose which strategy you want to backtest by changing fileName.

Step 3: Configure to download historical data from Interactive Brokers.

step 3.1 Create an input parameter of histIngestionPlan which is an object of HistIngestionPlan. histIngestionPlan is a reserved keyword for backtesting.

histIngestionPlan = HistIngestionPlan()

Step 3.2 Specify what historical data you want to download from Interactive Brokers using objects of Plan.

If you want to test your strategy using one-minute bar, you must download minute-bar historical data.

histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 min', goBack='10 D'))

If you need to use historical data in your strategy, you must download the corresponding historical data. For example, you want to calculate daily bar moving average of close prices. Then, you need to prepare the daily bar historical data.

histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 day', goBack='50 D'))

If you want to backtest multiple contracts in one strategy, you need to retrieve historical data of all of these contacts by add these Plans to histIngestionPlan one by one.

histIngestionPlan = HistIngestionPlan()

histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 min', goBack='10 D'))

histIngestionPlan.add(Plan(security=symbol('AAPL'), barSize='1 min', goBack='10 D'))

Step 4: Specify startTimeendTimeand freq. startTime, endTime and freq  are reserved keywords for backtesting.

startTime = dt.datetime(2019, 4, 24, 0, 0) # default timezone = 'US/Eastern'
endTime = dt.datetime(2019, 4, 26, 15, 40) # default timezone = 'US/Eastern'
freq = '1H' # 1S = 1 second; 1T = 1 minute; 1H = 1 hour

Step 5: Run TEST_ME_IB.py

What will happen next:

  1. IBridgePy will retrieve historical data from Interactive Brokers following user’s instruction specified in histIngestionPlan It may take a while for IBridgePy to download data from IB server if many historical data are needed.
  2. Backtester will supply historical data to your strategy and simulate orders, positions and portfolio values accordingly.
  3. If historical data of a simulated spot time, handle_data and scheduled_function will not be triggered for that spot time during backtesting. And warning messages will show up.
  4. It is user’s responsibility to track account performances after each transactions. record function may be used to track performances. We are developing default account performance analytics and please let us know your suggestions.
  5. User can designate spot times to backtest quickly instead of simulating every minute from startTime to endTime. Or change freq to simulate other frequencies, such as every hour, every day.

We are still developing backtest features and plan to provide an online backtest platform. We love to hear your comments and suggestions. Our email is IBridgePy@gmail.com

 

 

6 thoughts on “Backtest strategies on IBridgePy”

  1. Pingback: How Do You Backtest On Interactive Brokers? | Every Answer

  2. Pingback: How Do You Backtest In Interactive Brokers? | Every Answer

  3. Pingback: Unlocking Profit Potential: A Comprehensive Guide to Backtesting with Interactive Brokers - IBridgePy

  4. Pingback: Unveiling the Power of Backtesting with Interactive Brokers: A Comprehensive Guide - IBridgePy

Leave a Comment