Tutorial: Migrating from Quantopian to IBridgePy

Migrating from Quantopian to IBridgePy can be very easy in some cases even without making any code changes. In this thread, I am going to talk about how to make the migration as smooth as possible. In the following, I will explain the detailed steps assuming you are using a Microsoft Windows system.

As a short description, in the runMode of run_like_quantopian, the initialize function will run once at the beginning of the algorithm and  handle_data function will run every minute, just like how they perform at Quantopian.

Step 1:  Download IBridgePy to your local folder

You need to check the version of your Python, either Python 2.7 or Python 3.x and check if it is 32-bit Python or 64-bit Python. Then, download IBridgePy according to your Python version from www.IBridgePy.com/download
Assume the path of the folder where you save IBridgePy is C:\ABC\DEF\IBridgePy




Step 2: Complete preparation steps

step 2. 1 Install Anaconda

For Windows users, Anaconda is required.

For Ubuntu users, do NOT install Anaconda. You need to use native Python 2.7 64-bit

For Mac users, you can choose to install Anaconda or not. You may refer to this tutorial to debug on Mac if any issues.

step 2.2 Configure TWS or IB Gateway

The detailed steps are described in this webpage.

Configure TWS or IB Gateway

step 2.3 Log into IB TWS or IB Gateway

IBridgePy can trade in IB live accounts and paper accounts. It is highly recommended to test your strategies in a paper account. If you don’t have an IB account yet, you are welcome to use IB’s public demo account. However, IB’s public demo account does not behave as same as live accounts or paper accounts do.
User name is “fdemo”   —- without quotation marks
Password is “demouser” —- without quotation marks

After you finish this step, either IB Gateway or TWS should be open and active.

Step 3: Copy and Save your Quantopian algorithms

strategies_folder

Save your Quantopian algorithms in a folder called “Strategies” that is a sub-folder of IBridgePy (C:\ABC\DEF\IBridgePy\Strategies). The file extension should be “.py” because they are Python scripts. In the following, assume the file name of your algorithm is MyAlgo.py , and it is saved at C:\ABC\DEF\IBridgePy\Strategies\MyAlgo.py




Step 4: Edit RUN_ME.py in Spyder

Open RUN_ME.py  (C:\ABC\DEF\IBridgePy\RUN_ME.py) in Spyder.

 

Put your IB account code into RUN_ME.py

put_in_account_code

Add one line in RUN_ME.py  ->   fileName = ‘MyAlgo.py’

add_myalgo

Set runMode = ‘run_like_quantopian’

runmode

Step 5: Check what Quantopian functions you are using in your algorithm

The Quantopian functions that are supported by IBridgePy are listed here. These functions are supposed to behave as similar as they do in Quantopian.  You are welcome to send an Email to IBridgePy@gmail.com if you find out that any functions perform very differently. We will try our best to fix them.

* You need to delete any Quantopian functions that are not supported by IBridgePy in your algorithms. As of Aug 27th 2017, IBridgePy does not support Quantopian’s pipeline yet.

* You need to install any 3rd part Python packages that you are using in your algorithms, for example, TaLib, if these 3rd part Python packages are not automatically installed with Python XY or Anaconda. As an example, pytz is a Python package to handle time zones and it has been automatically installed with Python XY or Anaconda so that you don’t need to install it again.

 Step 6: Run your algorithm in Spyder.

Actually, you need to run RUN_ME.py to run your algorithm in Spyder so that you can think RUN_ME.py as a main entrance to any customer’s algorithms. In Spyder, you can click “F5” or the green triangle to run a Python script.

run_scripts




Step 7: Watch out any error messages

After IBridgePy completes its initialization procedures, it will print out a summary of your accounts. If you see a similar summary, it means IBridgePy gets connected with IB Gateway / TWS successfully.

acc_summary

If you see any error messages, you may follow the error messages to debug.

There are many reasons for errors. You may debug from the following areas:

  1. The bugs come from IBridgePy. We will try our best to fix the bugs coming from IBridgePy.
  2. IB servers may not response as you expect. For example, you may not receive historical data around 6PM eastern time, 1:30 AM eastern time because of IB maintenance or data server updates.
  3. Demo account, paper account and live account behave differently.
  4. Bugs in customer’s algorithms.

 

Q & A

Q: With all due respect, how can we trust you if you shut down like Quantopian?

A: You will set up your own computing environment using IBridgePy so that you don’t need to give password to Quantopian any more. That is the main motivation of IBridgePy. You can still run your codes on your computers using IBridgePy even if our team discontinues. That is totally different than running codes on Quantopian. You have 100% control on everything.

37 thoughts on “Tutorial: Migrating from Quantopian to IBridgePy”

          1. they have an unofficial api. I don’t believe partner status is really needed, they will be rolling out an official api here soon and it would be really nice to not have to migrate code.

  1. Following mail sent yesterday (traceback,last line:
    File “C:\Python27\lib\site-packages\IPython\core\ultratb.py”, line 617, in _format_exception_only
    Colors.Normal, s))

    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xf3 in position 38: ordinal not in range(128)
    Any sugestion?
    I am installing and trying to connect thru run_me.py and getting historical data

  2. Anyone here have any success experience running the IBridgePy in Ubuntu with https://ibridgepy.com//wp-content/uploads/2017/09/IBridgePy_ubuntu.zip

    I’m hitting the following roadblock:
    Traceback (most recent call last):
    File “RUN_ME.py”, line 35, in
    exec(script)
    File “”, line 8, in
    File “/root/ib-api/IBridgePy/IBridgePy/quantopian.py”, line 1, in
    from IBridgePy import IBCpp
    ImportError: /root/ib-api/IBridgePy/IBridgePy/IBCpp.so: undefined symbol: GENERAL_NAME_free

    Any idea what went wrong? The following is my python environment:
    Python 2.7.12 (default, Nov 19 2016, 06:48:10)
    [GCC 5.4.0 20160609] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import struct
    >>> print(8 * struct.calcsize(“P”))
    64

    Thanks in advance.

  3. Has order_target_percent been implemented? Don’t see any references to it in the release notes.

    I am running an algo but it hasn’t placed any orders yet so I am worried that it will fail when it comes onto that command.

  4. Hi,

    I read the Tutorial again and have the following questions:

    1. before_trading_start
    >>The time when before_trading_start is called can be configured by users.

    In the ‘configuration.txt’? But how?

    2. sid()
    Do you support the sid() as in Qauntopian?

    3. Order types
    It seems you have only three order functions: oder(), order_target() and order_percent(). How about the others as by Quantoipian such like: order_value(), order_target_value() and order_target_percent()

    4. ocaGroup in create_order()
    Seems the oca = oco (one cancle the other), right?

    5. Relation betwwen create_order() and place_combination_orders()
    I understand one has to use the create_order() to create order legs and then use the place_combination_orders() to place these legs. Correct?

  5. Hi,

    Looking forward to trying this out. Just installed and setup everything, will try porting over my algo today and running for the next few days.

    Qustion: How do we run multiple algos? Do we clone RUN_ME.py for each algo and run them simultaneously? Example: RUN_ME_algo1.py, RUN_ME_algo2.py, etc.

    Thank you.

      1. This means you run multiple algos on a single account. I am not sure if this is a good idea.

        The best way is to run single algo on single account, the so-called multiple account. But you would chage for that. :-/

  6. I can’t get historical data for some etfs. Is there a exhaustive list of security_info.csv?
    For e.g. I can’t get prices for EWG
    Also, if I want the index price for Shanghai index , or SENSEX what should I do?

  7. I simply cloned an algo from quantopian to see if I could figure this all out. I have IB, everything nearly set. Forgive my ignorance as im no expert at this but I get an error “no module: quantopian.pipeline.data.builtin” What do I do?

Leave a Comment