2022-2026

Algorithmic Trading System

PythonTypeScriptFastAPIPostgreSQLZMQInteractive Brokers APIAsyncsDockerPandas
Architecture Diagram
Architecture Diagram

Abstract

This is a project I have been working on, since university. The stock market had always seemed like this really big game to me where thousands of people participate, and the prize is something more than pride, more than glory, it's something everyone wants... money.

Infrastructure Breakdown

We control a brokage account using Interactive Brokers Python API. The IB gateway acts as, well, network gateway between my home servers and IB and the Exchange's severs. All micro-services including the gateway are hosted on a single PC in my basement. So everything in the architecture diagram is a logical seperation. To manage the IB gateway, I use open source code called, IBC (IB controller) it handles auto login, and timeout recovery but interating with the GUI of the gateway. It's not perfect, but it adds some robustness to our most critical failure point.

Mirco-services are divided into producers and consumers. Producers connection directly to the gateway, and act as a central handler for market data and order management. This was the best approach due to how the IB API is designed. Orders are required to have a globally unique order ID, so it is better to have a single entity handle it. It is also ineffecient to request duplicated market data, so instead of each strategy request market data individually, it is also the responsibilty of a central "Master IB Client."

Designing for Failure on a budget

In a professional software development setting, we are constantly thinking of how things could fail, from a software level, to a compute level. We have failovers, graceful restarts, state restoration. Although I try to keep the code as professional as possible, in a personal project setting we have diminishing returns. How effective is having docker auto rebuilds when everything is hosted on my 12 year old PC? The chances are if one container died, everything is dead. Therefore, coding from a trading perspective means assuming your power could go out at any moment. At the most simple level, this means only sending bracket orders, or at the very least, stop limit orders.

System Visibility