Vineet Dwivedi's Blog

My musings on technology, entrepreneurship and other things.

Automated Trading with IB and Amazon EC2

Jul 18, 2017 3 min read AlgoTrading Technology Vineet Dwivedi

I took Interactive Brokers (IB) account for trading in NSE (India) around 18 months back. My idea was to use IB API and make an automated trading system to run small scalping and market making programs. IB is probably the only one offering API trading for NSE in India. I made a scalping program using IB Java API and was running it from my MacBook. I am not sure, if you know that a MacBook can sometimes behave like a powerful server.

It was working fine in the beginning but in the next 2-3 weeks I realised the limitations of this automated system. Given below are few of them

  1. I need to start IB Gateway or TWS every day and then start the trading program
  2. Those are the days ( Monday to Friday ) when I am in office and will either forget or will not have the time to start the program
  3. Internet connection in my office is shaky and it will go precisely at the time I need to take or square off a position. For more information on why this happens please have a Look at Murphy’s Law on Wikipedia.

I thought how nice it will be to run a small trading server using IB API in Amazon EC2 which will automatically start and stop and will give me a daily summary by mail as soon as the exchange (NSE) closes.

IB with Amazon EC2

I started working on the server part and was able to write a simple automated trading server with my scalping strategy in 4 weeks. The server also has features like risk management, triggers and different allocation to different strategies.

The next challenge was to put this in EC2. EC2 linux does not have a GUI and IB Gateway or TWS needs a GUI. It will throw an error and terminate. Fortunately someone has written a great program called IBController which can start IB Gateway in hands-free mode. This alone was not sufficient because the server needs a GUI. The trick is to use virtual frame buffer and emulate the GUI (which anyway is going to /dev/null). Now I can automate my small trading strategies. Now if the exchange is open only for 30-35 hrs a week, why should I run my server 24/7. I wanted to save some dollars on EC2 hosting. I wrote a small shell script which will

  1. Start the EC2 instance
  2. Assign it an IP (optional)

This script can be run from any 24/7 server or desktop as a cron job. I put this instance in Singapore, which is the nearest EC2 geography to HongKong, where my closest IB server is located. The trading server also has a script which starts the IB Gateway and then starts my ATS (Automated Trading Server or System). As soon as the exchange closes the server is automatically closed but not before sending the summary of all the trades during the day. Yippeeeee!