Running IB Gateway on a VPS

25 Jun 2014

Running IB Gateway and your trading app on your home desktop or laptop may be convenient but not proof against disruptions in internet connectivity, power supply or hardware failures. Having IB Gateway run on an infrastructure of a reputable hosting company is much more reliable. This cannot be underestimated when it comes to putting your money on the line. The following tutorial shows how to install IB Gateway on a virtual server with Ubuntu 14.04.

A VPS instance usually comes with root user access and the following snippets assume running commands under root account. If you are logged in as a different user but have superuser access via sudo then run sudo su - to get root shell.

Create directories for IB Gateways. One gateway will run with simulation account and the other one with real trading account.

mkdir -p /algoeye/sim/gw
mkdir -p /algoeye/real/gw
cd /algoeye

Java

Install Java. If you already have java installed then you can skip this step but make sure you modify all JAVA_HOME variables in below scripts.

wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz
tar xvf jdk-8u5-linux-x64.tar.gz jdk1.8.0_05

VNC

IB Gateway is an interactive application not designed to be run on a headless server. As a workaround we will install VNC server and use it to emulate graphical display. You will be able to connect to your server using VNC client and control IB Gateways, change settings, etc.

apt-get -y install ubuntu-desktop tightvncserver xfce4 xfce4-goodies
vncpasswd

Replace contents of /root/.vnc/xstartup with the following

#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
xhost +localhost
/etc/X11/Xsessione

The script which starts and stops VNC can be installed as init script or run from process monitor like Monit. Save the following script into /algoeye/vnc.sh and make it executable with chmod +x /algoeye/vnc.sh

#!/bin/sh

ROOTDIR=$( cd "$( dirname "$0" )" && pwd )
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="VNC"
PIDFILE=$ROOTDIR/vnc.pid
LOGFILE=$ROOTDIR/vnc.log
DISPN=1

stop()
{
    if kill -0 `cat $PIDFILE` >/dev/null 2>&1; then 
        echo "Stopping $NAME with pid: `cat $PIDFILE`\n" >> $LOGFILE;
        kill -9 `cat $PIDFILE`
    fi
}

restart()
{
    exec > $LOGFILE 2>&1
    stop
    echo $$ > $PIDFILE;
    echo "Starting $NAME with pid: $$\n";
    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    export HOME=/root
    export USER=root
    export XAUTHORITY=/root/.Xauthority
    rm -f /tmp/.X$DISPN-lock
    rm -rf /tmp/.X11-unix
    exec /usr/bin/vncserver -geometry 1024x768 :$DISPN -fp /usr/share/fonts/X11/misc -localhost
}

case $1 in
    start)
        restart
    ;;
    stop)
        stop
    ;;
    *)
        echo "usage: vnc.sh {start|stop}" ;;
esac
exit 0

Check if vnc has been installed correctly by executing ./vnc.sh start. You should see process Xtightvnc running.

Gateway

Next step is to download and install IB Gateway and IBController. IBController is a nice utility used to automatically start and log in to your account. Check out https://github.com/ib-controller/ib-controller if you'd like to know more about IBController.

wget https://github.com/ib-controller/ib-controller/releases/download/2.11.0/IBControllerV2-11-0.zip
wget https://download2.interactivebrokers.com/download/unixmacosx_latest.jar
jar xf unixmacosx_latest.jar
mv IBJts/* sim/gw
rm -rf IBJts
sudo apt-get install -y unzip
unzip IBControllerV2-11-0.zip -d sim/gw

Save a copy of IBController.ini - it has a lot of useful information in comments.

cd sim/gw
cp IBController.ini IBController.default.ini

Replace the contents of IBController.ini with the following. Don't forget to replace username and password with your credentials.

LogToConsole=yes
IbLoginId=auser123
IbPassword=password
PasswordEncrypted=no
IbDir=.
ExistingSessionDetectedAction=primary
IbControllerPort=7462

Finally, the script that starts/stops gateway. Save it to gw.sh.

#!/bin/sh
export DISPLAY=:1
ROOTDIR=$( cd "$( dirname "$0" )" && pwd )
INSTANCE=$2
WORKINGDIR=$ROOTDIR/$INSTANCE
JAVA_HOME=/algoeye/jdk1.8.0_05
PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
JAVA=$JAVA_HOME/bin/java
COMMAND="$JAVA -cp jts.jar:total.2013.jar:IBController.jar ibcontroller.IBGatewayController IBController.ini"
PIDFILE=$WORKINGDIR/ibgateway.pid
LOGFILE=$WORKINGDIR/ibgateway.log

stop()
{
   if kill -0 `cat $PIDFILE` >/dev/null 2>&1; then 
       echo "Stopping gateway $INSTANCE with pid: `cat $PIDFILE`\n" >> $LOGFILE;
       kill -9 `cat $PIDFILE`
   fi
}

restart()
{
       stop
       echo "Starting gateway $INSTANCE in $WORKINGDIR with pid: $$\n" >> $LOGFILE;
       echo $$ > $PIDFILE;
       cd $WORKINGDIR;
       exec $COMMAND 1>>$LOGFILE 2>&1
}

case $1 in
     start)
       restart
       ;;
     restart)
       restart
       ;;
     stop)  
       stop
       ;;
     *)  
       echo "usage: gw.sh {start|stop|restart} <instance>" ;;
esac
exit 0

Make sure VNC is up then type gw.sh start sim/gw to start gateway for simulation account. To start real gateway type gw.sh start real/gw.

Enjoy!


comments powered by Disqus