Q & A

Rent-a-Coder

Contents

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.

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: Does IBridgePy support Python 3.x?

A: Yes. 64-bit Anaconda Python 3.6.

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 and Python 3.6 , not Python 3.5.

Windows: IBridgePy supports Anaconda Python 2.7 32-bit, Anaconda Python 2.7 64-bit and Anaconda Python 3.6 64-bit.

Mac: 64-bit Python 2.7 only, which is the default Python in iOS. Do NOT install Anaconda ! Also, please make sure that python framework comes from /System/Library/Frameworks/Python.framework/Versions/2.7/Python

Ubuntu: 64-bit Python 2.7 only, which is the default Python in Ubuntu Linux. 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 in Windows and not used in Mac or Ubuntu
  3. Check if multiple Python versions have been installed. If it is, uninstall the unused python version. Or, direct the correct Python to execute RUN_ME.py




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.

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

A: Please check out this answer.

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.

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?

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

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?

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.

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. http://www.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?

img_2228

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.

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?

A: See the answer above.

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

A: See the answer above.

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

 

Others

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. In configuration.txt, you will see a link

c=MarketManager(trader, host=remoteHostIP, port=7496, clientId=clientId)

You can change the port accordingly. Alternatively, change the port in your TWS global configuration, under section “API” -> “Settings” -> Socket 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?

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.

security_info

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: request_data() got an unexpected keyword argument ‘historyData’. Why?

A: request_data ( ) is deprecated from version 2.20171004. To request historical data, please use request_historical_data ( ). The documentation is here http://www.ibridgepy.com/ibridgepy-documentation/#request_historical_data.

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.