Building a Basic Algorithmic Backtesting Engine in C++: SMA Crossover

Putting calculus and probability to work. I built a functional backtesting engine in C++ using the Simple Moving Average (SMA) Crossover strategy.

I am Johannes, 18, and I am pushing hard toward a career in Quantitative Finance. To stay accountable (and share some cool insights), I am documenting my learning journey every day. After diving into calculus, probability, and the theoretical side of things, I decided to put some of that knowledge to work. I built a functional algorithmic trading engine from scratch.

Now, this is not some high-frequency trading engine ready to take on Jane Street. This is a backtesting engine, meaning it uses historical data to simulate how a trading strategy would have performed in the past. It is the absolute foundational step for any quant aspiring to build automated trading systems.

An algorithmic trading engine needs a few key pieces to operate:

You cannot trade without data. My MarketDataLoader is a C++ class designed to read historical stock prices (like Apple's, from a CSV file). It takes in all those numbers (date, open, high, low, close, volume) and turns them into structured data my program can understand. Making sure it could handle the raw CSV data without crashing if something was a bit off required careful parsing.

This is where the algorithmic part truly shines. My Strategy class houses the brain of the operation. For this first version, I used a classic: the Simple Moving Average (SMA) Crossover. Here is the basic idea: