Setting up an Ubuntu 24.04 EC2 instance for algorithmic trading with Interactive Brokers
Posted on Fri 28 February 2025 in Infrastructure
This article walks you through the steps of setting up a new EC2 instance for algorithmic trading using Ubuntu 24.04 and Interactive Brokers. Running your trading algorithms on EC2 can offer scalability, reliability, and cost efficiency—ideal for managing both paper and live trading accounts.
Inspired by Dimon's How to setup IBC (3.8.1) + TWS (build 976) on headless Ubuntu 18.04 LTS to run TWO accounts (paper + real) in 10 minutes, this guide has been updated for the latest Ubuntu release.
Initial User Setup
Create a new user, set the password and give him sudo privileges.
useradd -d /home/ubuntu -s /bin/bash -m ubuntu && passwd ubuntu && adduser ubuntu sudo
vim /etc/sudoers
and add the line: ubuntu ALL=NOPASSWD: ALL
Next, we will authorize only the ubuntu user to connect via ssh.
sudo su - ubuntu
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
and paste your public key
sudo vim /etc/ssh/sshd_config
and add AllowUsers ubuntu
sudo systemctl restart sshd
Finally, we update ubuntu:
sudo apt update
sudo apt -y upgrade
Also, make sure that the clock is properly synchronized with NTP: timedatectl status
Optional: install zsh and ohmyzsh
Install zsh: sudo apt install zsh
Edit sudo vim /etc/passwd
and set your shell to /usr/bin/zsh
Install ohmyzsh: sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Installing xvfb and VNC
To create a virtual display environment for TWS and IBGateway, install the required packages and set up a virtual framebuffer and VNC server:
sudo apt install -y xvfb x11vnc x11-apps metacity zsh
Start the Virtual Framebuffer: /usr/bin/Xvfb :2 -ac -screen 0 2048x1536x24 &
Prepare the Log File for x11vnc:
sudo touch /var/log/x11vnc.log
sudo chmod a+rw /var/log/x11vnc.log
Run the VNC server.
/usr/bin/x11vnc -ncache 10 -ncache_cr -passwd your_password -display :2 -forever -shared -logappend /var/log/x11vnc.log -bg -noipv6
Use VNC Viewer to connect to your server on port 5900. You can also check that the display is functioning correctly by running:
DISPLAY=:2 xeyes &
DISPLAY=:2 metacity &
You should see something like this:
Installing and configuring TWS
Download the latest offline TWS version (the URL might change, you can get the latest one here:
wget https://download2.interactivebrokers.com/installers/tws/latest-standalone/tws-stable-standalone-linux-x64.sh -P /tmp/
chmod +x /tmp/tws-latest-standalone-linux-x64.sh
Run the installer: DISPLAY=:2 /tmp/tws-latest-standalone-linux-x64.sh
Installing and configuring IBGateway
Get the latest offline IBGateway version (the URL might change, you can get the latest one here:
wget https://download2.interactivebrokers.com/installers/ibgateway/stable-standalone/ibgateway-stable-standalone-linux-x64.sh -P /tmp/
chmod +x /tmp/ibgateway-stable-standalone-linux-x64.sh
Run the installer: DISPLAY=:2 /tmp/ibgateway-stable-standalone-linux-x64.sh
And run IBGateway: DISPLAY=:2 ~/Jts/ibgateway/1030/ibgateway
IBKR forces customers to restart the software once a day.
In order to do this, go to Configure > Settings > Lock and Exit
. Check Auto Restart
and set the restart time (I recommend to choose a time during the Server Reset Times window)
Securing your server
Firewall
Allowing Loopback Connections:
sudo ufw allow in on lo
sudo ufw allow out on lo
Allow Outgoing Connections on the Internal Network:
sudo iptables -A OUTPUT -o ens5 -j ACCEPT
Open ports for SSH (22) and VNC (5900), then set the default policy to deny incoming connections:
sudo ufw allow 22/tcp
sudo ufw allow 5900/tcp
sudo ufw default deny incoming
Enable the firewall: sudo ufw enable
Conclusion
This guide has taken you through setting up an Ubuntu 24.04 EC2 instance for algorithmic trading with Interactive Brokers.
By following these steps, you'll have a robust, secure, and scalable platform for your algorithmic trading needs. As you move forward, consider exploring additional monitoring tools and automation scripts to further streamline your trading operations.
Happy trading and coding!