tocOn this page 7 sections
To run Interactive Brokers TWS or IB Gateway 24/7 on a Windows VPS, install it in a dedicated Windows user session, switch its daily shutdown to Auto restart, stop Windows from ending disconnected RDP sessions, and add an auto-logon plus startup task so it comes back after a reboot. Plan for one manual login each week, because IBKR forces a full sign-in with two-factor authentication on a weekly cycle that no setting removes.
TWS or IB Gateway: pick the right one
Both expose the same socket API to your bot or script. Choose based on whether a human watches the screen:
- IB Gateway: no charts or order windows, lower memory use, built for API-only setups. Default API ports are 4001 (live) and 4002 (paper).
- TWS: the full trading desktop. Use it when you also trade or monitor by hand. Default API ports are 7496 (live) and 7497 (paper).
For an unattended bot, run IB Gateway. Budget at least 2 GB of RAM for Gateway and noticeably more for TWS with several chart windows, on top of what Windows Server itself needs.
Install in a session that stays alive
TWS and Gateway are Java GUI applications. They need an interactive desktop, so they cannot run as a normal Windows service. They live inside your RDP session, which means that session must never be logged off.
- Create a dedicated local user for trading, for example
ibtrader, and install the stable (not beta) build of TWS or IB Gateway while signed in as that user. - When you finish working over RDP, close the Remote Desktop window or use Disconnect. Never choose Sign out, which kills the application.
- Stop Windows from ending idle or disconnected sessions. In
gpedit.mscgo to Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Session Time Limits and set both Set time limit for disconnected sessions and Set time limit for active but idle Remote Desktop Services sessions to Never.
The same policy from an elevated PowerShell prompt:
$k = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services'
if (-not (Test-Path $k)) { New-Item -Path $k | Out-Null }
Set-ItemProperty -Path $k -Name MaxDisconnectionTime -Value 0 -Type DWord
Set-ItemProperty -Path $k -Name MaxIdleTime -Value 0 -Type DWord
gpupdate /force
Switch the daily logoff to auto restart
By default TWS and Gateway log off once a day. In TWS open File > Global Configuration > Lock and Exit (in Gateway: Configure > Settings > Lock and Exit) and select Auto restart instead of Auto logoff. Set the time to a quiet window for the markets you trade, and remember it uses the VPS time zone. Check that with Get-TimeZone and change it with Set-TimeZone -Id "Eastern Standard Time" if you want the schedule to line up with US market hours.
Auto restart reuses your session through the week. When IBKR requires the weekly re-authentication, the app waits at the login screen until you sign in and approve the 2FA prompt. Put a recurring reminder in your calendar for that.
Configure and protect the API
Open Configure > Settings > API > Settings and set:
- Enable ActiveX and Socket Clients: on.
- Socket port: keep the default unless you run several instances, then give each a unique port.
- Allow connections from localhost only: on, if your bot runs on the same VPS.
- Read-Only API: on for monitoring tools; off only for code that places orders.
- Trusted IPs: add
127.0.0.1, plus any remote client address if you must connect from outside.
Confirm the port is listening:
Test-NetConnection 127.0.0.1 -Port 4001
Get-NetTCPConnection -LocalPort 4001 -State Listen
The API has no password of its own, so never open it to the internet. If a client on another machine needs access, allow only that address:
New-NetFirewallRule -DisplayName "IB API from bot host" -Direction Inbound `
-Protocol TCP -LocalPort 4001 -RemoteAddress 203.0.113.10 -Action Allow
Pair this with the basics in our Windows Server VPS hardening checklist, since anyone who reaches your RDP desktop also reaches your brokerage account.
Survive reboots and Windows Update
After a reboot nobody is signed in, so nothing is running. Close that gap in three steps:
- Auto-logon: use Sysinternals Autologon to sign
ibtraderin at boot. It stores the password as an encrypted LSA secret rather than plain text in the registry. - Start at logon: create a Task Scheduler task with the trigger At log on for
ibtraderthat launches Gateway. Our guide to launching apps automatically at sign-in walks through the task settings. - Control update reboots: set active hours to cover your trading window and schedule restarts for the weekend. The approach in auto-restarting MT4 and MT5 after update reboots applies to IBKR apps unchanged.
Gateway still stops at its login screen after a cold start. The open-source IBC project can fill in credentials and handle dialogs for you; if you adopt it, read its documentation carefully and keep its config file readable only by the trading user.
Monitor that it is actually up
A quick check you can schedule every few minutes:
if (-not (Test-NetConnection 127.0.0.1 -Port 4001 -InformationLevel Quiet)) {
Write-EventLog -LogName Application -Source "IBMonitor" -EventId 1001 `
-EntryType Warning -Message "IB API port 4001 is not listening"
}
Register the source once with New-EventLog -LogName Application -Source IBMonitor, then hook the event to an email or chat alert. For more patterns on keeping trading and API bots alive, see our Windows VPS for automation and bots guide.
FAQ
Can I avoid the weekly login completely?
No. IBKR requires a full authentication with two-factor on a weekly cycle. Auto restart covers the daily reset only, so plan a short manual sign-in each week.
Can I run TWS and IB Gateway on the same VPS?
Yes, as long as each uses a different API port and you have the RAM for both. IBKR may not allow the same username to be logged in to two sessions at once, so a second instance often needs a separate user or paper account.
Does minimizing or disconnecting RDP stop the API?
No. Disconnecting leaves the session and Gateway running. Only signing out, a reboot, or a session time limit closes it.
Run this on your own Windows VPS
Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.
Written by the WindowsVPS Team
We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.