Q & A
- 1.1 Q: How to run IBridgePy on Mac without Anaconda?
- 1.2 Q: Is PythonXY required to run IBridgePy?
- 1.3 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?
- 1.4 Q: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 0: invalid start byte
- 1.5 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?
- 1.6 Q: NameError: name ‘runfile’ is not defined. Can you help?
- 1.7 Q: ‘ascii’ codec can’t decode byte 0xb2 in position 20: ordinal not in range(128) What cause this error?
- 1.8 Q: The error message is “from IBridgePy import IBCpp ImportError: DLL load failed: The specified module could not be found.” What can I do?
- 1.9 Q: I am using IBridgePy on Ubuntu but see the error message of “undefined symbol: GENERAL_NAME_free”. Can you help?
- 1.10 Q: [Errno 10038] An operation was attempted on something that is not a socket
- 1.11 Q: [Errno 10093] Either the application has not called WSAStartup, or WSAStartup failed
- 1.12 Q: IBridgePy on Ubuntu has errorCode = 509, error message: Exception caught while reading socket – Inappropriate ioctl for device
- 1.13 Q: ImportError: dynamic module does not define module export function (PyInit_IBCpp)
- 1.14 Q: Assertion failed: Successful WSASTARTUP not yet performed (bundled\zeromq\src\signaler.cpp:255)
- 1.15 Q: “Kernel died, restarting” It happened many time. Why? How to prevent it?
- 1.16 Q: OSError: Initializing from file failed
- 2.1 Q: Is it possible to request real time values of index, for example, SPX, NDX?
- 2.2 Q: How can I use IBridgePy to acquire delayed quotes?
- 2.3 Q: Can you give me an example for Futures Option symbology ? Say ES Dec 16 2200 call?
- 2.4 Q: Is it possible to view the current Implied Volatility on Options contracts with IBridgePy, or do I need to calculate those myself?
- 2.5 Q: When display_all is called the Positions ‘Latest_profit’ is always NA?
- 2.6 Q: How can I get security string from position?
- 2.7 Q: I was using a DEMO account and kept getting “validateAccountCode issue”. How can I solve it?
- 2.8 Q: Is there any way for me to save history data using IBridgePy?
- 2.9 Q: Instead of localhost connection, can we connect IB gateway remotely?
- 2.10 Q: I saw many errors warned by Spyder, for example, symbol and get_datetime is not defined. What am I missing?
- 2.11 Q: error message: Requested market data is not subscribed.Error&BEST/STK/Top&BEST/STK/Top. What does it mean?
- 2.12 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?
- 2.13 Q: I got an error: No market data permissions for AMEX STK / ISLAND STK. What should I do?
- 2.14 Q: How can I get the final closing price after a position is cleared off?
- 2.15 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?
- 3.1 Q: Do you support live trading for institutional account holders?
- 3.2 Q: Can I change a port number other than 7496?
- 3.3 Q: Will IBridgePy collect my trading secrets or other information without my permission?
- 3.4 Q: With all due respect, how can we trust you if you shut down like Quantopian?
- 3.5 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?
- 3.6 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.
- 3.7 Q: Deprecation Warning: Panel is deprecated and will be removed in a future version. Why does it happen? How can I solve it?
- 3.8 Q: What is “pacing violation”?
- 3.9 Q: The error is KeyError: ‘currency’ . How to solve it?
- 3.10 Q: What will IBridgePy do when IB gateway lost its connection to IB server at 1:00 AM U.S. Eastern time every day?
- 3.11 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?
- 3.12 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?
- 3.13 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?
- 3.14 Q: How can I set IBridgePy to wait for a longer time to retrieve more historical data from IB server?
- 3.15 Q: How do I know what version of IBridgePy I am using?
- 3.16 Q: No security definition has been found for the request. How to solve it?
- 3.17 Q: errorCode = 10197 errorMessage = No market data during competing live session. What caused this error?
- 3.18 Q: How can I share Market Data Subscriptions in my IB account?
- 3.19 Q: errorCode=509 errorMessage=Exception caught while reading socket – Numerical result out of range
Q: How to run IBridgePy on Mac without Anaconda?
A:
- Windows Users must use Anaconda Python. We support Anaconda Python 3.7, 3.8, 3.9, 3.10 and 3.11. Please refer to the following tutorials for Windows users. https://youtu.be/7UheMvFD5uk
- Mac Users: Please use plain Python 3.7, 3.8, 3.9, 3.10 and 3.11on Mac. Please refer to these tutorial:
Choose the correct Python and install dependencies on Mac
Installation instructions for Mac users
- Ubuntu Users: Please use plain Python 3.8 and 3.9 on Ubuntu.
- IBridgePyInDocker: A docker image based on IBridgePy on Ubuntu Python 3.8
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.
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:
- Windows Users must use Anaconda Python. We support Anaconda Python 3.7, 3.8, 3.9, 3.10 and 3.11. Please refer to the following tutorials for Windows users. https://youtu.be/7UheMvFD5uk
- Mac Users: Please use plain Python 3.7, 3.8, 3.9, 3.10 and 3.11on Mac. Please refer to these tutorial:
Choose the correct Python and install dependencies on Mac
Installation instructions for Mac users
- Ubuntu Users: Please use plain Python 3.8 and 3.9 on Ubuntu.
- IBridgePyInDocker: A docker image based on IBridgePy on Ubuntu Python 3.8
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:
- Windows Users must use Anaconda Python. We support Anaconda Python 3.7, 3.8, 3.9, 3.10 and 3.11. Please refer to the following tutorials for Windows users. https://youtu.be/7UheMvFD5uk
- Mac Users: Please use plain Python 3.7, 3.8, 3.9, 3.10 and 3.11on Mac. Please refer to these tutorial:
Choose the correct Python and install dependencies on Mac
Installation instructions for Mac users
- Ubuntu Users: Please use plain Python 3.8 and 3.9 on Ubuntu.
- IBridgePyInDocker: A docker image based on IBridgePy on Ubuntu Python 3.8
Q: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 0: invalid start byte
A: Please refer to this YouTube video https://youtu.be/xsxY0-hiWdM Don’t forget to subscribe to the channel and more tutorials are coming.
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?
Q: NameError: name ‘runfile’ is not defined. Can you help?
Q: ‘ascii’ codec can’t decode byte 0xb2 in position 20: ordinal not in range(128) What cause this error?
Q: The error message is “from IBridgePy import IBCpp ImportError: DLL load failed: The specified module could not be found.” What can I do?
Q: I am using IBridgePy on Ubuntu but see the error message of “undefined symbol: GENERAL_NAME_free”. Can you help?
Q: [Errno 10038] An operation was attempted on something that is not a socket
Q: [Errno 10093] Either the application has not called WSAStartup, or WSAStartup failed
Q: IBridgePy on Ubuntu has errorCode = 509, error message: Exception caught while reading socket – Inappropriate ioctl for device
Q: ImportError: dynamic module does not define module export function (PyInit_IBCpp)
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.
Q: “Kernel died, restarting” It happened many time. Why? How to prevent it?
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.
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.
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.
Q: Can you give me an example for Futures Option symbology ? Say ES Dec 16 2200 call?
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.
Q: When display_all is called the Positions ‘Latest_profit’ is always NA?
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.
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.
Q: Is there any way for me to save history data using IBridgePy?
A: Yes. Please check out the tutorial. https://ibridgepy.com//2017/06/17/export-historical-data-and-save-them-to-csv-files/
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.
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.
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.
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?
Q: I got an error: No market data permissions for AMEX STK / ISLAND STK. What should I do?
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:
context.portfolio.orderStatusBook[orderId].avgFillPrice
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:
- Try to place an order when the market is open.
- Place an order for a security with high liquidity.
- Check IB Gateway status and make sure the connection to IB server is good.
- If you are using a demo account, you may change to your paper account.
- 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.
Q: Do you support live trading for institutional account holders?
A: Yes, we support both individual account holders and institutional account holders.
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/setting.py –> BROKER_CLIENT –> IB_CLIENT –> port
Q: Will IBridgePy collect my trading secrets or other information without my permission?
A: IBridgePy does NOT collect any information without user’s permission.
Q: With all due respect, how can we trust you if you shut down like Quantopian?
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.
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.
Q: What is “pacing violation”?
A: https://interactivebrokers.github.io/tws-api/historical_limitations.html#gsc.tab=0
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.
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.
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.
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.
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”
Q: How can I set IBridgePy to wait for a longer time to retrieve more historical data from IB server?
Q: How do I know what version of IBridgePy I am using?
A: Run any sample code, the version of IBridgePy will be printed when displaying the account information and when it encounters any errors.
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
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.