Using ib_insync

Going off a previous thread, I followed the steps to install ib_insync and run the sample "get_scan_data.py" file on my installation of QR. When I first tried it, it worked perfectly and I was able to install my slightly modified ib_insync version to download positions by model.

However, when I try to run the same scripts (that were working before) it gets stuck and doesn't produce any output. I have tried running the sample "get_scan_data.py" and it doesn't work either. I thought maybe it was my modified version so I installed the original ib_insync version 0.9.37 but it still gets stuck.

It gets stuck on ib.connect('ibg1', 4001, clientId = 1). If I bring up the Gateway GUI it does show the Client 1 connected and looking at the gateway log there is stuff going on.

Thanks for any help.

I'm not able to reproduce this problem so not sure what to suggest. Could there be two instances of the script running? Try a different client ID?

I have no idea what is going on. I had the same problem using ib_insync with TWS on my local machine a couple of months back and the only way I got it to work was by using either Gateway or using TWS stable, not TWS latest. I posted the question on the ib_insync group and the only answer was to change the software version, they didn't know what was wrong. The odd thing is that it was working a couple of weeks ago.

I've tried with different Client ID's and restarting gateway. Is there a way to cancel everything that satellite is running? I usually stop it with Ctrl+C but on the GUI it still shows the client connected so I don't think it ctrl+C stops the script?

So I figure out that it is getting stuck when ib_insync requests the positions in the portfolio, same issue I had before on my machine. I am also getting an error from QR:

"quantrocket.blotter: WARNING IB positions not received in a timely manner, will try again shortly."

Since the positions is where ib_insync gets stuck, I am guessing these are related?

The best way to cancel every satellite script is to restart the container.

Since this has happened inside and outside of QuantRocket it might be worth trying to connect to the IB API in a different way than ib_insync, either using the IB API directly or using ibpythonic. The syntax is less convenient but your issue can probably only be resolved by trial and error.

I think I figured out what the issue is. I am running QR on AWS and I was able to recreate the issue on my local machine. I'll post it here in case anyone runs into the same issue.

QR is using version 9.73 of the IB API, I am using 9.74 on my local machine. When I downgraded to 9.73 I had the same issue as I was having on QR. I have a Financial Advisor account and I currently have an account that is in the process of being opened but is not yet approved. When I log in to TWS I can see the account but it shows as "Pending". I think that is what is causing ib_insync to get stuck. I am assuming it requests positions for that account and since it still doesn't exist it get stuck.

This would explain why it was working before (account hadn't even been started yet) and why it isn't working now. I also was able to connect to another account on QR and my local machine, using the exact same script.

This may be a bug that IB API has that was fixed on a newer version of the API. I think the solution is to wait until QR upgrades to a newer API version but now I know what the issue is so I can work around it for the time being.

You should be able to install 9.74 by adding a line like this to quantrocket.satellite.pip.txt:

http://interactivebrokers.github.io/downloads/twsapi_macunix.974.01.zip#subdirectory=IBJts/source/pythonclient

1 Like