ES EN

Case Study: Building an Automated Trading Engine

A step-by-step journey through engineering decisions, architecture, backtesting, observability, and operating an automated trading system.

Theory and principles come to life and demonstrate their value when applied to building concrete and challenging projects. Developing an automated trading engine from scratch is an excellent example of how these concepts of performance, architecture, design, and development philosophy intertwine in practice:

  1. From Spreadsheet to Functional Code Prototype: The journey, like many projects, began with a phase of exploration and idea validation. Spreadsheets (Excel) were used to model trading strategies with historical stock price data. This rapid prototyping approach allowed for iterating on the logic, adjusting parameters, and obtaining an initial validation of the strategies' feasibility. The next natural step was to port this logic to a script (Python, for example), consuming the same historical data. This not only confirmed the reproducibility of the results (eliminating manual spreadsheet errors) but also quickly highlighted Excel's limitations in handling larger data volumes, more complex automation, and the need for a more robust and scalable execution.
  2. From Monolithic Script to an Event-Driven Microservices Architecture: The initial script, as often happens, grew organically into a hard-to-manage monolith: it mixed the logic for downloading market data, simulating strategies, executing orders (broker mocks), and analyzing results. Every change in one part of the system risked breaking another. The solution was to adopt a modular approach, separating responsibilities into independent services (microservices or well-defined services), each focused on a specific domain:
  1. Rigorous Backtesting, the Harsh Reality of the Real Market, and the Surprise ofSlippage: With historical data and the new modular architecture, backtesting simulations showed almost perfect profit curves, generating initial optimism. However, the transition to the real market (even in 'paper trading' mode or with small amounts) introduced a series of non-trivial challenges that idealized backtesting does not always capture:
  1. Strategy Parameter Optimization with Biological Inspiration (Evolutionary Algorithms): Each trading strategy depends on a set of carefully tuned parameters: the size of the price analysis window, entry and exit thresholds, stop-loss and take-profit levels, etc. Manually testing all possible combinations of these parameters is computationally infeasible due to the enormous combinatorial search space. To address this optimization problem, aGenetic Programming (a type of Evolutionary Algorithm) engine was implemented:
  1. Total Observability: 'If You Can't Measure It, You Can't Manage (or Improve) It': An automated trading system that potentially handles real money and operates without constant supervision would be a recipe for disaster without a robust monitoring and observability system. A stack of tools was implemented for this purpose:
  1. Agile Deploys, Resilience, and Disaster Recovery Strategies: A hotfix that takes too long to deploy or a system failure that cannot be recovered quickly can cost real money. While Kubernetes is a powerful solution for container orchestration, its cost and complexity in the cloud can be high for a personal or small project. A lighter but effective strategy was chosen:

This journey through building an automated trading engine practically illustrates that starting with simple prototypes, progressively modularizing to scale, modeling and preparing for real-world uncertainty and imperfections, automating repetitive tasks like optimization and monitoring, and ensuring fast and reliable deployment and recovery processes are fundamental lessons in quality software engineering.