Contact Info

118 ALJUNIED AVENUE 2, SINGAPORE (380118)

support@cheapforexvps.com

How to Run IB Gateway 24/7 on a Windows VPS

How to Run IB Gateway 24/7 on a Windows VPS

If you trade through Interactive Brokers with your own code, you already know the catch. Your trading bot does not talk to Interactive Brokers directly. It talks to IB Gateway or Trader Workstation (TWS), which must be running and logged in for any order to go through. When Gateway stops, your bot goes blind.

Running IB Gateway on your home computer works until the computer sleeps, Windows restarts for an update, or your internet drops. Running it on a Windows VPS removes those problems, but Interactive Brokers adds a few of its own: a daily restart, a weekly login with two-factor authentication, and settings that are easy to get wrong.

This guide shows you how to set up IB Gateway on a Windows VPS so it stays connected around the clock, with the least possible manual work. It covers installation, the API settings your bot needs, the daily and weekly login cycle, automation with the free IBC tool, and how to keep the whole setup secure.

IB Gateway or TWS: Which Should You Run?

Both programs give your code access to the Interactive Brokers API. The difference is what else they do.

  • Trader Workstation (TWS) is the full trading platform, with charts, order entry and account windows. It uses more memory and is designed for a person sitting in front of it.
  • IB Gateway is a lightweight program with no charts or trading screens. It exists only to connect API programs to Interactive Brokers.

For a bot running 24/7 on a VPS, IB Gateway is usually the better choice. It uses fewer resources and has fewer pop-up windows that can interrupt an unattended session. Use TWS only if you also want to watch charts or trade by hand on the same server.

Why Run IB Gateway on a VPS?

  • It never sleeps. A VPS runs in a data centre with redundant power and network connections, so your Gateway is not affected by your home computer, Wi-Fi or electricity supply.
  • Lower latency. Our broker latency table shows Interactive Brokers at around 1 millisecond from our New York data centre, compared with about 71 milliseconds from London. For most accounts, a New York VPS is the right choice. Read what NY4 is and why a New York VPS helps for the background.
  • Your bot can run on the same server. With the Gateway and your Python, Java or C# program on the same VPS, the connection between them never leaves the machine.

Step 1: Choose the Right VPS Size

IB Gateway runs on Java and needs a reasonable amount of memory, especially if you stream market data for many symbols. Your own bot needs resources too. As a starting point:

  • One Gateway and a light bot with a small watchlist: 4 GB of RAM, such as our Advanced plan.
  • One Gateway and a heavier bot, or a Gateway plus MetaTrader terminals: 8 GB, such as our Specialist plan.
  • Several gateways (for example, live and paper accounts side by side) or data-heavy strategies: 16 GB or more.

Every Cheap Forex VPS plan runs Windows Server with NVMe storage and a dedicated IP address. You can upgrade later if you add strategies.

Step 2: Install IB Gateway

  1. Connect to your VPS with remote desktop. See how to connect to a forex VPS from a Windows PC if you need help.
  2. Inside the VPS, open a web browser and download IB Gateway from the Interactive Brokers website. You will usually see a stable and a latest version. Choose stable for a production bot; it changes less often.
  3. Run the installer and accept the defaults.
  4. Start IB Gateway, choose IB API (not FIX CTCI), select Live Trading or Paper Trading, and log in with your username and password. Approve the login on your phone in the IBKR Mobile app.

Tip: open a paper trading account first and run your whole setup on it for a week. Paper and live accounts use different usernames, so you can test without risking real orders.

Step 3: Configure the API Settings

In IB Gateway, click Configure > Settings > API > Settings. Check the following:

  • Socket port. IB Gateway uses port 4001 for live accounts and 4002 for paper accounts by default. (TWS uses 7496 and 7497.) Your bot must connect to the same port.
  • Read-Only API. If this is ticked, your bot can read data but cannot place orders. Untick it when you are ready to trade.
  • Allow connections from localhost only. Keep this ticked if your bot runs on the same VPS. It means no one on the internet can connect to your Gateway’s API.
  • Trusted IPs. Leave only 127.0.0.1 unless you have a specific reason to add more.
  • Master API client ID. Optional. Useful if you run several programs and want one to see all orders.

Never open the API port to the internet in the Windows firewall. If your bot must run on a different machine, use a secure tunnel or VPN rather than an open port.

Step 4: Understand the Daily and Weekly Login Cycle

This is the part that catches most people out. Interactive Brokers does not allow a Gateway session to run forever without interruption.

The daily restart

Every 24 hours, IB Gateway either restarts or logs off. You choose which in Configure > Settings > Lock and Exit.

  • Auto restart closes and reopens the Gateway at the time you choose, reusing the existing authenticated session. You do not need to approve a new login on your phone. Interactive Brokers recommends this option for API users.
  • Auto logoff closes the Gateway completely. You must log in again, with two-factor authentication, every day.

Set Auto restart, and choose a time when your strategy is not trading and markets you care about are quiet. Your bot will lose its connection for a minute or two at that time each day, so make sure it reconnects automatically.

The weekly login

Auto restart only carries the session for so long. Once a week, Interactive Brokers requires a full login with two-factor authentication. Traders typically see this around the weekend, and Interactive Brokers’ own documentation notes that if you see the message “Soft token=0 received instead of expected permanent”, the weekly re-authentication must be completed by logging in manually.

In practice, that means approving one login request on your phone each week. Plan for it: if nobody approves the login, the Gateway stays logged out and your bot cannot trade until someone does.

No legitimate setting removes the weekly two-factor step. It is a security control at the account level, and that is a good thing for an account holding your money.

Step 5: Automate Logins with IBC

IB Gateway was designed for a person to click through. IBC is a free, open-source tool (available on GitHub from the IbcAlpha project) that handles the clicking for you. It:

  • Starts IB Gateway and fills in your username and password.
  • Selects live or paper trading mode.
  • Handles common pop-up dialogs that would otherwise block an unattended session.
  • Manages the daily restart at a fixed time.
  • Retries the login if a two-factor request times out, so a late approval on your phone still works.

IBC cannot approve the weekly two-factor request for you. You still tap approve on your phone once a week, but you no longer need to open the VPS to do it.

Setting up IBC on Windows

  1. Download the Windows release of IBC from the official IbcAlpha GitHub page and extract it, usually to C:IBC.
  2. Open the config.ini file in a text editor and enter your username, password and trading mode (paper or live). The file is well commented; read each section.
  3. Set the daily restart time in config.ini to match the Auto restart time in IB Gateway, so the Gateway restarts once a day rather than at two different times.
  4. Open StartGateway.bat and check that the IB Gateway version number and folder match your installation.
  5. Run StartGateway.bat and watch it log in. Approve the two-factor request on your phone.

Protect config.ini

The IBC configuration file stores your Interactive Brokers password in plain text. Treat it carefully:

  • Use a strong, unique password for your VPS, so only you can open the server.
  • Do not copy config.ini to cloud storage, email or shared folders.
  • Use a separate Interactive Brokers username for the API if your account allows it, with only the permissions your bot needs.

Step 6: Start Everything Automatically After a VPS Restart

IB Gateway, like MetaTrader, is a desktop program. It needs a signed-in Windows session to run. After a VPS restart, Windows waits at the sign-in screen, so nothing starts until someone signs in.

To make the whole stack start by itself:

  1. Turn on automatic Windows sign-in with Microsoft’s Autologon tool.
  2. Add StartGateway.bat (or a shortcut to it) to the Startup folder, or create a Task Scheduler task that runs it at log on with a 30-second delay.
  3. Start your bot the same way, with a slightly longer delay so the Gateway is logged in first.

Our step-by-step guide on how to auto-start MT4 after a VPS reboot explains Autologon, the Startup folder and Task Scheduler in detail. The same steps apply to IB Gateway; just point them at StartGateway.bat instead of terminal.exe.

Also stop Windows from restarting at random times. Follow how to stop Windows updates restarting your forex VPS and schedule updates for the weekend, ideally around your weekly two-factor login.

Step 7: Make Your Bot Resilient

Even with perfect settings, the Gateway will disconnect briefly every day. A well-built bot expects this.

  • Reconnect automatically. Detect a lost connection, wait, and reconnect, rather than crashing.
  • Rebuild state after reconnecting. Request open orders and positions again, so your bot knows what happened while it was disconnected.
  • Keep protective orders at the broker. Stop-loss and take-profit orders sent as Good-Till-Cancelled brackets stay active on Interactive Brokers’ servers even if your Gateway is offline.
  • Log everything. Write connection events and errors to a file, so you can see what happened overnight.
  • Get alerts. Have your bot send you a message (by email, Telegram or similar) if it cannot reconnect for more than a few minutes, especially around the weekly login.

A Weekly Routine That Keeps You Online

  1. Weekend: install Windows updates and restart the VPS if needed.
  2. When the weekly login request arrives: approve it on your phone.
  3. Before the market opens: connect to the VPS briefly and check that the Gateway shows a green connection and your bot reports it is connected.
  4. Once a month: check for a new stable version of IB Gateway and IBC, and update both on a quiet day.
  5. Always: disconnect from the VPS when you are finished. Do not sign out, because signing out closes the Gateway and your bot.

Common Problems

My bot cannot connect to the Gateway

Check that the port in your code matches the Gateway’s socket port (4001 live, 4002 paper), that the Gateway is logged in, and that your bot connects to 127.0.0.1.

Orders are rejected or nothing happens

Make sure Read-Only API is unticked, and check that your account has trading permissions for the product you are trading.

The Gateway logs out every day

Switch from Auto logoff to Auto restart in Lock and Exit settings, and make sure IBC’s restart time matches.

The Gateway is logged out on Monday morning

The weekly two-factor login was not approved. Approve it, or reconnect and log in manually. Set a phone reminder for next week.

The VPS feels slow

IB Gateway and your bot may need more memory. Check usage in Task Manager and see why your forex VPS may be running slow, or upgrade your plan.

Ready to move your Interactive Brokers bot off your home PC? Choose a New York VPS plan with a dedicated IP address and 24/7 support.

Frequently Asked Questions

Can IB Gateway run 24/7 without any manual login?

Almost. With Auto restart enabled, it reconnects daily without a new login. Once a week, Interactive Brokers requires a full login with two-factor authentication, which you approve on your phone.

Should I use IB Gateway or TWS on a VPS?

IB Gateway for unattended bots, because it uses fewer resources and has fewer interruptions. TWS only if you also want charts and manual trading on the same server.

Which VPS location is best for Interactive Brokers?

For most accounts, New York. Our latency table shows Interactive Brokers at around 1 millisecond from our New York data centre.

What ports does IB Gateway use?

By default, 4001 for live trading and 4002 for paper trading. TWS uses 7496 and 7497.

Is IBC safe to use?

IBC is a widely used open-source project. Its main risk is that config.ini stores your password in plain text, so protect your VPS with a strong password and never share the file.

What happens to my open positions if the Gateway goes offline?

Your positions stay open at Interactive Brokers. Stop-loss and take-profit orders already sent as Good-Till-Cancelled remain active on IBKR’s servers. Your bot simply cannot place new orders until the Gateway reconnects.

How much RAM does IB Gateway need on a VPS?

It depends on how much market data you stream and what else runs on the server. A 4 GB VPS suits one Gateway and a light bot; heavier setups need 8 GB or more.

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *