Q & A


About IBridgePy installation

Q: How to run IBridgePy on Mac without Anaconda?

A: You can download IBridgePy for Mac from http://www.ibridgepy.com/download/ Then, you may use native Python 2.7 installed at Mac to run RUN_ME.py ( $ python RUN_ME.py ) . If the required python packages are not installed, for example, Pandas, Numpy, or Pytz, you will be prompted to install them.

Rent-a-Coder HELPS!

Q: Is PythonXY required to run IBridgePy?

A: IBridgePy does not support PythonXY any more for Nov 2017. If you are a windows user, please uninstall Python XY if you are using it and install Anaconda Python from here (Choose 64-bit if you want to use Python 3.6). If you are a Mac user, do NOT install Anaconda or Python XY. The default Python 2.7 on Mac will run IBridgePy well. IBridgePy does not support Python 3.6 on Mac yet but our team may specially build it upon request.

Rent-a-Coder HELPS!

Q: I set up the system following the instruction but python complained “ImportError: cannot import name IBCpp” when it ran. How can I solve the issue?

A: IBridgePy supports Python 2.7, Python 3.6  and Python 3.7 right now.

Windows: IBridgePy supports Anaconda Python 2.7 32-bit, Anaconda Python 2.7 64-bit, Anaconda Python 3.6 64-bit and Anaconda Python 3.7 64-bit. MUST install Anaconda Python!!!

Mac: Python 2.7 and Python 3.7

If you have installed Anaconda Python 2.7, be sure to download IBridgePy_Mac_Anaconda27_64.zip and use Anaconda Python to run RUN_ME.py

If you have not installed Anaconda Python 2.7, please download IBridgePy_Mac_Python27_64.zip and use Mac native Python to run RUN_ME.py. Also, please make sure that python framework comes from /System/Library/Frameworks/Python.framework/Versions/2.7/Python

Ubuntu: Python 2.7 and Python 3.7 Do NOT install Anaconda !

The error will show up if the downloaded IBridgePy version does not match the python in your system. The other reason of the error is that there are multiple python installed in your system. The solution is to uninstall unmatched Python from your system.

Solution steps:

  1. Check if the correct IBridgePy version has been downloaded. (It solves most of the issues)
  2. Make sure that Anaconda is used if you are using Windows.
  3. Check if multiple Python versions have been installed. If yes, uninstall the unused python version. Or, execute RUN_ME.py using the correct Python.

Q: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 0: invalid start byte

A: If you are using Windows, please go to “Windows Settings” –> “Region & Language”. Then change region to “United States” and change language to “English”.

Rent-a-Coder HELPS!

Q: I saw the following error codes: “from IBridgePy import IBCpp ImportError: DLL load failed: %1 is not a valid Win32 application”. How can I solve the issue?

A: Please check out this answer.

Q: NameError: name ‘runfile’ is not defined. Can you help?

A: Please check out this answer.

Q: ‘ascii’ codec can’t decode byte 0xb2 in position 20: ordinal not in range(128) What cause this error?

A: Please check out this answer.

Q: The error message is “from IBridgePy import IBCpp ImportError: DLL load failed: The specified module could not be found.” What can I do?

A: Please check out this answer.

Q: I am using IBridgePy on Ubuntu but see the error message of “undefined symbol: GENERAL_NAME_free”. Can you help?

A: Please check out this answer.

Q: [Errno 10038] An operation was attempted on something that is not a socket

A: Please check out this answer.

Q: [Errno 10093] Either the application has not called WSAStartup, or WSAStartup failed

A: Please check out this answer.

Q: IBridgePy on Ubuntu has errorCode = 509, error message: Exception caught while reading socket – Inappropriate ioctl for device

A: Please check out this answer.

Q: ImportError: dynamic module does not define module export function (PyInit_IBCpp)

A: Please check out this answer.

Q: Assertion failed: Successful WSASTARTUP not yet performed (bundled\zeromq\src\signaler.cpp:255)

A: This is a known issue related to Spyder, an integrated development environment (IDE) installed with Anaconda. The error is not caused by IBridgePy. The error happens when your computer is slow to create a kernel (because it lacks enough RAM or it’s undergoing high CPU usage). The solution is to restart kernel regularly. In Spyder, go to Consoles — > Restart kernel.

Rent-a-Coder HELPS!

Q: “Kernel died, restarting” It happened many time. Why? How to prevent it?

A: Please check out this answer.

Rent-a-Coder HELPS!

Q: OSError: Initializing from file failed

A: IBridgePy uses Pandas to read the csv file of IBridgePy/security_info.csv. This error jumps out when you have permission issues to read this file. Please change to the user (Window user) that have enough permissions.

Rent-a-Coder HELPS!


About trading

Q: Is it possible to request real time values of index, for example, SPX, NDX?

A: Yes. They can be received through IBridgePy as long as there is real time data subscription for the indexes in the account. Please check out the function of symbol( ) for specifying index.

Rent-a-Coder HELPS!

Q: How can I use IBridgePy to acquire delayed quotes?

A: Delayed quotes are available in TWS for many contracts but not through the API, so that you cannot do that by IBridgePy.

Rent-a-Coder HELPS!

Q: Can you give me an example for Futures Option symbology ? Say ES Dec 16 2200 call?

A: symbol(‘OPT, ES, USD, 20161216, 2200, C, 100). The format is Option, ES, base currency, expiry, strike price, Call/Put, multiplier.

Rent-a-Coder HELPS!

Q: Is it possible to view the current Implied Volatility on Options contracts with IBridgePy, or do I need to calculate those myself?

A: Yes, IB supplies Implied Volatility values and you don’t need to calculate them by yourselves. However, you need to do a little bit coding to request these values from the IB server.

Rent-a-Coder HELPS!

Q: When display_all is called the Positions ‘Latest_profit’ is always NA?

What is the reason for this?
##    POSITIONS    ##
Symbol Amount Cost_basis Latest_profit
CASH,EUR,USD 1000 1.05444285 NA
A: The last_price is not available at the moment when display_positions ( ) is called. The solution is to request real time prices from IB before display_positions ( ) is called.

Q: How can I get security string from position?

A: The positions are recorded in the context.portfolio.positions by an instance of SecurityClass. security.__str__ will return a security string.

Rent-a-Coder HELPS!

Q: I was using a DEMO account and kept getting “validateAccountCode issue”. How can I solve it?

A: When you are using a demo account, the accountCode changes every time after you get connected to IB server. For some unknown reasons, the behavior of the demo account is NOT as same as the real account or your paper account. If the error message says it is a read-only account, it is a read-only account. You cannot do a lot of things using that account. It is not something that IBridgePy can solve.

Rent-a-Coder HELPS!

Q: Is there any way for me to save history data using IBridgePy?

A: Yes. Please check out the tutorial. http://www.ibridgepy.com/2017/06/17/export-historical-data-and-save-them-to-csv-files/

Rent-a-Coder HELPS!

Q: Instead of localhost connection, can we connect IB gateway remotely?

A: Yes. In RUN_ME.py, add remoteHostIP=”xx.xx.xx.xx”  (xx is your real remote host IP). Also, you need to disable “localhost host connection only” option in your IB gateway first and then add trusted IP in the list before you do the remote connection.

Rent-a-Coder HELPS!

Q: I saw many errors warned by Spyder, for example, symbol and get_datetime is not defined. What am I missing?


A: It is normal if you are talking about the errors highlighted by Spyder. You can just click “run” to see results after it is connected to IB Gateway. If you are curious what functions are supported, you may open define_functions.txt to see the exhausted list of all functions defined.

Rent-a-Coder HELPS!

Q: error message: Requested market data is not subscribed.Error&BEST/STK/Top&BEST/STK/Top. What does it mean?

A: IBridgePy always requests real time data and historical data from IB server. If you see this error message, it means that you have not subscribed market data for the requested security. You need to contact with IB representative and subscribe the market data that you are interested in. Also, you need to share the subscription between your live accounts and your paper accounts, which can be done at IB’s account management website.  The market data cannot be shared by live account and paper account at same time. This is a common reason for error messages. IBridgePy does not hard-code any market data subscription requirements to run any codes. If you are using a paper account or demo account, you can try Forex contracts for testing purposes. For example, you can get prices by show_real_time_price(symbol('CASH, EUR, USD'), 'ask_price')

Also, you may check what data package you need to subscribe by visiting this website after you log into your IB account. IB market data assistant.

Rent-a-Coder HELPS!

Q: I got error: “Requested market data is not subscribed” or “No market data permissions for AMEX STK” when I use paper account but I do have data subscription in my live account. What may cause the problem?

A: Please see this answer.

Q: I got an error: No market data permissions for  AMEX STK / ISLAND STK. What should I do?

A:Please see this answer.

Rent-a-Coder HELPS!

Q: How can I get the final closing price after a position is cleared off?

A: Status of all orders, executed or not, are saved at context.portfolio.orderStatusBook, which is a python dictionary keyed by orderId. To get the final closing price, the code looks like this:


Rent-a-Coder HELPS!

Q: I saw error message of “EXIT, waiting time is too long” after I placed a market order to buy a security. What caused the issue and how can I fix it?

A: It typically takes less than 1 second for IB server to execute a market order for a security with high liquidity when the market is open. This error will show up if IBridgePy has not received an order execution response after 30 seconds and IBridgePy will stop everything for users to check what might go wrong as a critical warning sign.

The following step may help to solve the issue:

  1. Try to place an order when the market is open.
  2. Place an order for a security with high liquidity.
  3. Check IB Gateway status and make sure the connection to IB server is good.
  4. If you are using a demo account, you may change to your paper account.
  5. If you know it will take more than 30 seconds for IB server to process the market order, you may increase the waiting time to 1 minute.

Rent-a-Coder HELPS!



Q: Do you support live trading for institutional account holders?

A: Yes, we support both individual account holders and institutional account holders.

Rent-a-Coder HELPS!

Q: Can I change a port number other than 7496?

A: Yes. Step 1: change the port number in IB Gateway or TWS, under section “API” -> “Settings” -> Socket port. Step 2: Go to IBridgePy/Config/BrokerClientConfig/BrokerClientConfig.py and change line 15.

Rent-a-Coder HELPS!

Q: Will IBridgePy collect my trading secrets or other information without my permission?

A: IBridgePy does NOT collect any information without user’s permission.

Rent-a-Coder HELPS!

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.

Q: It seems the  stock of “XYZ” is not in the file of ‘IBridgePy/security_info.csv’. I wonder for what purpose you use this csv file? What should I do?

A: This file works as a database of stock information. IBridgePy will look for the symbol in this file and obtain the related information for further processing. The stock symbols in this file are not a complete list so that users may not find the securities that they want to trade in this file. In that case, users need to manually add additional rows to this file.

For example, there is a position of “STK, ZZZ, USD” in your account. In the IBridgePy initialization stage, it will receive all of the positions from IB server and IBridgePy will search these positions in security_info.csv to look for exchange and primaryExchange. However, “STK, ZZZ, USD” is not in the security_info.csv. Therefore, IBridgePy does not know how to handle this position and it stops. The solution is to add this security into security_info.csv and re-run the code.

When you are editing the security_info.csv, it is recommended to open the file using Microsoft Excel because you can easily see the names of every columns. Two critical columns are “primaryExchange” and “exchange”. You need to put correct information there after you check it at https://www.interactivebrokers.com/en/index.php?f=463 Occasionally, you still see the same error messages after you make the changes. Then, you need to talk to an IB representative and ask them what the correct primaryExchange and exchange are.


Q: May I set the frequency of handle_data function call more than 60s? Default is 1s, quantopian is 60s. But I don’t need that frequent.

A: Yes. In the RUN_ME.py, add a line. “repBarFreq = 120”. Then, handle_data ( ) will run every two minutes. However, you need to pay attention to the performance of schedule_function ( ) because it may not run as you scheduled if it is not set up correctly.

Rent-a-Coder HELPS!

Q: Deprecation Warning: Panel is deprecated and will be removed in a future version. Why does it happen? How can I solve it?

A: This warning message shows up because you request historical data of multiple securities with multiple fields and the returned results are saved in a Pandas Panel, following Quantopian’s data.history ( ) implementation. However, Pandas Panel is deprecated and Pandas package gives this warning. The solution is to avoid using data.history ( ) in this way.

Rent-a-Coder HELPS!

Q: What is “pacing violation”?

A: https://interactivebrokers.github.io/tws-api/historical_limitations.html#gsc.tab=0

Rent-a-Coder HELPS!

Q: The error is KeyError: ‘currency’ . How to solve it?

A: Open “security_info.csv” in the folder of IBridgePy using Excel. It should have a column called “currency”. Please double check that the name of column “currency” should be all lower case.

Rent-a-Coder HELPS!

Q: What will IBridgePy do when IB gateway lost its connection to IB server at 1:00 AM U.S. Eastern time every day?

A: You don’t need to do anything on IBridgePy because the connection/re-connection between your computer and IB server is maintained by IB gateway and IBridgePy is always connected to IB gateway when they reside at the same server. However, the user needs to handle the scenario that no any new callbacks come in during the disrupted connection time. It is very likely that the user decides to do nothing because of no new information.

Rent-a-Coder HELPS!

Q: IBridgePy and IB Gateway do not reside in the same server so that the connection may be lost. Is it possible to get automatically re-connected to a remote IB Gateway?

A: Yes, IBridgePy is able to support re-connection to a remote IB Gateway.

Rent-a-Coder HELPS!

Q: My strategy (handle_data) did not get executed and saw a message of “Market is closed but IBridgePy is still running”. Why did it happen?

A: In runMode = ‘run_like_quantopian’, the function of handle_data is executed every minute from 9:30 AM to 3:59 PM US Eastern time. Outside that time period, you will see the message of “Market is closed but IBridgePy is still running”. You may change to default runMode, which execute handle_data every second regardless market status, if you want to execute your strategy immediately.

Rent-a-Coder HELPS!

Q: I did not use any premium features in my code. Why did I see an error code of Access to IBridgePy premium features is prohibited?

A: One possible reason is that IBridgePy is executing another file that the user did not aware of.

For example, the fileName that IBridgePy is really executing is “example_security_screener.py”, instead of “example_get_historical_data.py”

The best way to check which fileName IBridgePy is executing is to check IBridgePy print-out, like this:

In the above case, IBridgePy is executing “example_get_historical_data.py”

Rent-a-Coder HELPS!

Q: How can I set IBridgePy to wait for a longer time to retrieve more historical data from IB server?

A: http://www.ibridgepy.com/ibridgepy-documentation/#request_historical_data

Rent-a-Coder HELPS!

Q: How do I know what version of IBridgePy I am using?

A: Use editor to open the file of /IBridgePy/broker_client_factory/BrokerClientBase.py and go to line 46. You will see versionNumber = ‘x.x.x’ which is the current IBridgePy version number.

Q: No security definition has been found for the request. How to solve it?

A: The root cause of the issue is that the contract was not correctly described by symbol or superSymbol. The most common error is incorrect exchange or primaryExchange but other field could be wrong. The best way to solve the issue is to check IB’s official website IB Contract Search and find out the correct contract information.

Q: errorCode = 10197 errorMessage = No market data during competing live session. What caused this error?

A: As a simple and short answer, IB only supports one active login. If you login more, for example, one from your trading computer and one from cell phone, this error will show up. This will happen in both paper and live trading.

The following is quoted from IB’s forum.

As a licensed distributor of exchange market data, IB is obligated to respect constraints imposed by the exchanges which serve to govern the dissemination and/or retransmission of this data. As a general rule, a given subscription can be associated with only one account or user ID, although the sharing of that user ID between different devices (e.g., stand-alone PC, smart phone) is permitted as long as the user ID does not access multiple devices simultaneously.

Accordingly, clients seeking to simultaneously operate both live and paper trading account TWS sessions must do so on the same device in order to share a given market data subscription. If concurrent logins on different devices are detected, the live account session will be afforded the real-time market data subscriptions and the paper trading account session delayed data, as available.

Clients who wish to run multiple sessions on different devices may do so by opening a second user for their existing account. While this approach will result in additional fees for any subscriptions elected, one would be able to maintain a different subscription set from that of the main user and will then be capable of running simultaneous live and paper trading account sessions on different devices. Moving forward, an option will be provided whereby the same fee-based subscriptions will be offered for the paper trading account independent of those elected in the live account.

Q: How can I share Market Data Subscriptions in my IB account?

A:  From IB API documentation

Market data subscriptions are done at a TWS user name level, not per account. This implies that live market data subscriptions need to be purchased per every live TWS user. The only exception to this rule are paper trading users. To share the market data subscriptions simply access your account management and navigate to Manage Account -> Settings -> Paper Trading where you will be presented to the screen below. It will take up to 24 hours until the market data sharing takes effect.

Q: errorCode=509 errorMessage=Exception caught while reading socket – Numerical result out of range

A: This error message shows up ONLY when IBridgePy tries to get real time prices. It is fully understood yet but it is highly related to coexistence of multiple Python on one operating system. The best solution is to remove extra python installations.