After months of studying quant finance and systems architecture, I’ve built a complete algorithmic trading engine in C++.
I’m Johannes, 18, and for a long time, I’ve been fascinated by the world of Quantitative Finance. After countless hours studying theory and learning from online resources, I decided it was time to build something real.
It’s no longer just a simulator; it’s now a system that connects to a live brokerage, handles real-time data, and places trades for me. It’s a crucial step on a long learning journey.
The Core Architecture
A proper trading engine needs to be modular and flexible. My system is built around several key components designed to work together for both backtesting and live trading.
1. The Dual-Mode Engine: Backtesting vs. Live Trading
The engine can be launched in one of two modes from the command line: backtest
or live
.
- Backtest Mode: Uses historical data from CSV files to simulate how a strategy would have performed, generating a full performance report with metrics like Sharpe Ratio and Volatility.
- Live Mode: Connects to the Alpaca brokerage API, fetches live market data, and executes real (paper) trades based on the strategy’s signals.
This dual-mode capability is essential for developing and testing strategies before deploying them.
2. The Pluggable Strategy Core
The “brain” of the operation is built on a flexible design. An abstract StrategyBase
class defines a common interface, allowing me to “plug in” any number of different trading strategies. So far, I’ve implemented two:
- Simple Moving Average (SMA) Crossover: A classic trend-following strategy that buys or sells when short-term and long-term price averages cross.
- Bollinger Band Strategy: A “mean-reversion” strategy. It assumes that if a price moves too far from its average, it will likely return. Bollinger Bands create a dynamic channel around the price to identify these moments. The logic is to buy when the price hits the lower band (oversold) and sell when it hits the upper band (overbought).
The bands are calculated as:
\(Upper Band = SMA + (N \times StdDev)\)\(Lower Band = SMA – (N \times StdDev)\) where N is the number of standard deviations (typically 2).
3. The Portfolio and Execution System
This component was also designed with a base interface to handle both simulated and live portfolios.
- The
BacktestPortfolio
tracks positions and cash in memory during simulations. - The
LivePortfolio
is the real-world connection. When it receives an order, it makes an authenticated API request to Alpaca to place a trade in my paper trading account.
To handle credentials securely, the engine reads API keys from a config.json
file, which is ignored by Git to ensure my secrets are never committed to the repository.
The Scientific Method in Action
With this framework, I can scientifically test different strategies. Running both on historical AAPL data produced dramatically different results:
SMA Crossover (50/200 Day)
- Total Return: -1.1%
- Sharpe Ratio: -2.04
- Conclusion: This strategy failed on this dataset.
Bollinger Bands (20-day, 2 StdDev)
- Total Return: +7.1%
- Sharpe Ratio: 1.13
- Conclusion: A success! A Sharpe Ratio above 1.0 is widely considered a good result, indicating a strong return for the risk taken.
The Road Ahead
Building a live engine has been an incredible learning experience. While it functions as intended, a professional system would need more polish, including:
- Advanced Risk Management: Implementing dynamic position sizing and automated stop-losses.
- Robust Error Handling: Adding logic to gracefully handle API downtime or network disconnects.
- Real-time State Management: Constantly syncing the engine’s internal portfolio state with the broker’s data.
- Comprehensive Logging: Writing a detailed log of every action for debugging and review.
Follow Along
This project has solidified my understanding of both software architecture and financial markets, and I’m excited to continue building on this foundation.
Check out the full code on GitHub and follow along as I build more: https://github.com/JohannesMeyerYC/AlgorithmicTradingEngine
Stay Calculated!