r/interactivebrokers • u/Gousf • 4d ago
General Question Using IBKR API, getting kicked off every night, then cant reconnect in the morning.. what am I doing wrong..
Hey everyone, just to be upfront, I'm not a coder or scripter by nature at all. I've built a lot of my setup purely through Gemini and it worked great for a while to help me place trades faster and pull market data from subscriptions I already pay for, without fumbling around on order tickets and missing fast moves. Lately, though, the reliability has gone way down and it's getting exhausting dealing with it every night.
One thing I can say for sure is that it isn't from me logging in over my own session—I use a separate login specifically for the API setup. Even so, it still ends up getting kicked off or stuck on login/2FA with no push showing up on my phone, and I'm wondering if there's a simpler, less painful way to keep a connection going than fighting this every night.
Has anyone else dealt with this kind of nightly drop/login headache using a separate API login, or found an easier workaround that doesn't require constant babysitting?
If anyone needs to ask more questions to let me help them explain where things are going wrong. Thank you !
•
u/AutoModerator 4d ago
Thank you for your post. This is the UNOFFICIAL subreddit of Interactive Brokers.
There is official contact information to the side of this sub, and you may want to try r/IBKR_Official, which is the OFFICIAL Reddit sub for Interactive Brokers.
Official phone numbers: https://www.interactivebrokers.com/en/support/customer-service.php?p=contact
Official contact: https://www.interactivebrokers.com/en/general/contact/newContact/contact.php
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/lambardar 4d ago edited 4d ago
Today morning I was thinking.. it should be possible to derive the 2FA codes into the future aswell (they are time based), so we could skip the whole monday thing aswell.
Edit: so people have automated the 2FA codes aswell.. but it's not recommended.
3
u/Majestic-Taro8437 4d ago
Some password managers support one-time passcode management, where the app generates the code instead of waiting on a text. I use 1Password and I have IBKR credentials set up this way. I believe 1Password has some options where the vaulted credentials can be accessed programmatically. If you have this set up, maybe your automation can call the password manager to get the one-time passcode and you won’t be dependent on your phone.
2
u/richardabrich 1d ago edited 1d ago
IBKR kills sessions every night around 11:45 PM ET and forces a full 2FA re-login on Sundays, so plain scripts always end up wedged by morning. Switching to headless IB Gateway with IBC (
ExistingSessionDetectedAction=primaryandAutoRestartTime) plus a tiny watchdog that restarts the container when the socket hangs inCLOSE_WAITis what finally stopped the morning lockouts for me. Putting Gateway on Tailscale also means you don't have to leave ports open.One other gotcha: if you start seeing Error 326 on reconnect, loop
clientIdfrombase_idup by 5 instead of hardcoding one ID. Happy to share the retry helper I use inpackages/clearfills-desk/src/clearfills_desk/ibkr.py(https://github.com/clearfills/clearfills).3
u/zoinkinator USA 4d ago
Run ib gateway 10.52 on port 4001 instead of tws.
Interactive Brokers IB Gateway — Automatic Login
IB Gateway supports automatic reconnection and restart, but fully unattended login has limitations, particularly around two-factor authentication (2FA).
For your IBKR API applications running on your computer, there are three distinct settings to consider:
Feature
Purpose
Save username
Avoid reentering your IBKR username
Auto-restart
Restart Gateway automatically during its daily maintenance cycle
Auto-login
Authenticate without manually entering credentials or approving 2FA
Important: Enabling auto-restart does not guarantee automatic login. IBKR can still require authentication, including an IB Key approval.
Recommended IB Gateway settings
Open IB Gateway and log in normally.
Select Configure → Settings.
Select Lock and Exit.
Enable Never lock trading system, if available.
Select Auto Restart instead of Auto Logoff.
Choose a restart time outside your data collection or trading window, such as 11:45 PM ET.
Save the settings.
IBKR documents this configuration for API users. Interactive Brokers FAQ +1
What happens afterward?
Monday–Saturday: Gateway can restart automatically without reauthentication.
Sunday: You must authenticate again after IBKR’s weekly security reset.
Unexpected shutdown or computer reboot: A new manual login may be necessary.
API applications: Your Python processes should detect disconnections and reconnect after Gateway becomes available.
IBKR explicitly does not support fully automatic initial login. Documentation +1
Official documentation: IB Gateway API architecture and restart behavior.
I recommend using Gateway’s built-in Auto Restart, plus a macOS LaunchAgent for process monitoring and Python API reconnection logic. The LaunchAgent cannot eliminate IBKR’s authentication requirement.
1
u/MyOldYuzername 3d ago
I had the same issue. Two separate problems usually get mixed up here.
The nightly drop is IBKR, not you. The gateway restarts once a day no matter what. In the gateway settings pick Auto Restart (not Auto Logoff) and set the time outside your trading hours. On those daily restarts it comes back without asking for 2FA.
The morning "can't reconnect" is usually your script, not the gateway. If the script connects once at startup and never tries again, it's dead after the restart even though the gateway came back fine. Ask Gemini to add reconnect logic that keeps retrying every 30 seconds or so until it gets through.
2FA only bites on the weekly re-login around Sunday, and the push notification is the flaky part. If you set that API login's second factor to an authenticator app code instead of the IB Key push, the login can be automated by IBC, which is what that docker image someone linked already does.
0
u/greenmatrix86 4d ago
This is pretty normal IBKR behavior actually — Gateway force-restarts every night around midnight ET, you can't disable it. The part that's actually fixable is the 2FA push not showing up on reconnect — that's what turns a forced restart into an actual outage.
What's worked for me: stop trying to prevent the nightly kill and build a small watchdog that just detects the gateway's gone unresponsive and force-restarts it. IBC (IBController) handles the login flow and can do a full cold restart instead of trying to resume the old session, which is usually where the hang happens. Push notifications are flaky enough on their own that I wouldn't trust one landing every single night — if you need the gateway up by market open, automate the restart rather than hoping the push shows up in time.