Understanding the Basics of Algorithmic Trading
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Why This Matters
From C++ setup to trading thinking
In Course Introduction and Setting Up Development Environment, you prepared your C++ workspace, checked your compiler, and learned to measure early.
Now we shift from tooling to the trading domain: what is an algorithmic trading system actually trying to do?
In this lesson, we will build the mental model you need before writing market-connected code:
- What an algo trading system observes
- How it decides
- What it sends to the market
- What must be tracked after that
The goal is not to build a full strategy yet. The goal is to understand the moving parts so later C++ code has a clear purpose.

Trading System Loop
The core loop
Most algorithmic trading systems can be simplified into one repeating loop:
Observe market -> Decide action -> Send order -> Track result
In C++ terms, this usually becomes a set of fast, predictable components:
- Market input: prices, quotes, trades, timestamps
- Decision logic: rules that produce buy/sell/hold intent
- Execution logic: order placement, cancellation, replacement
- State tracking: current position, pending orders, realized/unrealized profit and loss
A beginner mistake is to think the “algorithm” is only the buy/sell rule. In real systems, the algorithm includes the full operational loop around that rule.

Core Purpose Check
In an algorithmic trading application, what is the main purpose of the trading algorithm?
Market Vocabulary
Terms you will see constantly
Before APIs and live data, you need a clean vocabulary.
- Instrument / Symbol: the thing being traded, such as a stock ticker
- Bid: highest visible price someone is willing to buy at
- Ask: lowest visible price someone is willing to sell at
- Spread: ask minus bid
- Last traded price: price of the most recent completed trade
- Position: how much you currently hold, positive or negative
- Order: instruction sent to the market
- Fill: confirmation that some or all of an order executed
Here is a tiny standalone C++ example that calculates spread and midpoint from a quote.
```cpp
#include <iostream>
#include <iomanip>
#include <string>
struct Quote {
std::string symbol;
double bid;
double ask;
};
int main() {
Quote q{"ABC", 101.25, 101.30};
double spread = q.ask - q.bid;
double midpoint = (q.bid + q.ask) / 2.0;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Symbol: " << q.symbol << '\n';
std::cout << "Bid: " << q.bid << " Ask: " << q.ask << '\n';
std::cout << "Spread: " << spread << '\n';
std::cout << "Midpoint: " << midpoint << '\n';
return 0;
}
```
Orders And Intent
A signal is not the same as an order
A trading system often moves through levels of intent:
Market data -> Signal -> Decision -> Order -> Fill -> Position update
Example:
- Signal: “price looks attractive”
- Decision: “buy 100 shares if risk limits allow”
- Order: “send limit buy order for 100 shares at ₹250.10”
- Fill: “60 shares executed”
- Position update: “now long 60 shares”
This distinction matters because real markets are not instant or perfect. An order may be partially filled, rejected, delayed, or cancelled.

Spread Thinking
True or false: A trading system can ignore the bid-ask spread because the last traded price is always enough for making execution decisions.
Latency Mindset
Fast is useful only when measured
Because you care about millisecond-level performance, C++ is a strong choice. But in trading systems, speed must be measured at specific points:
- Time to receive and parse market information
- Time to compute a decision
- Time to prepare an order
- Time to receive confirmation
You already practiced the idea of Measure Early in the setup lesson. Here is the same mindset applied to a small decision function.
This example does not connect to a market. It simply demonstrates how to measure the time around a decision block.
```cpp
#include <chrono>
#include <iostream>
#include <string>
std::string decide(double bid, double ask, double fairValue) {
double midpoint = (bid + ask) / 2.0;
if (midpoint < fairValue - 0.05) {
return "BUY_INTENT";
}
if (midpoint > fairValue + 0.05) {
return "SELL_INTENT";
}
return "HOLD";
}
int main() {
using Clock = std::chrono::high_resolution_clock;
double bid = 99.90;
double ask = 100.00;
double fairValue = 100.10;
auto start = Clock::now();
std::string action = decide(bid, ask, fairValue);
auto end = Clock::now();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "Decision: " << action << '\n';
std::cout << "Decision time: " << micros << " microseconds\n";
return 0;
}
```
Vocabulary Fill
The difference between the best ask price and the best bid price is called the ____.
Order Lifecycle
Orders have state
In normal application programming, you might call a function and immediately get a result. Trading is different.
An order can move through states:
Created -> Sent -> PartiallyFilled -> Filled
\-> Cancelled / Rejected
Your C++ application must model these states clearly. Otherwise, it may think it owns shares it does not own, or send duplicate instructions by mistake.
Here is a small standalone lifecycle simulation.
```cpp
#include <iostream>
#include <string>
// Simplified order states for learning purposes.
enum class OrderState {
Created,
Sent,
PartiallyFilled,
Filled,
Cancelled
};
std::string toString(OrderState state) {
switch (state) {
case OrderState::Created: return "Created";
case OrderState::Sent: return "Sent";
case OrderState::PartiallyFilled: return "PartiallyFilled";
case OrderState::Filled: return "Filled";
case OrderState::Cancelled: return "Cancelled";
}
return "Unknown";
}
struct Order {
int id;
std::string symbol;
int quantity;
int filledQuantity;
OrderState state;
};
void printOrder(const Order& order) {
std::cout << "Order " << order.id
<< " | " << order.symbol
<< " | filled " << order.filledQuantity << "/" << order.quantity
<< " | state=" << toString(order.state) << '\n';
}
int main() {
Order order{1, "ABC", 100, 0, OrderState::Created};
printOrder(order);
order.state = OrderState::Sent;
printOrder(order);
order.filledQuantity = 40;
order.state = OrderState::PartiallyFilled;
printOrder(order);
order.filledQuantity = 100;
order.state = OrderState::Filled;
printOrder(order);
return 0;
}
```
Concepts Connected
What you should now see
An algorithmic trading application is not just a clever formula. It is a fast, stateful system that repeatedly:
- Reads market conditions
- Applies decision rules
- Converts intent into orders
- Tracks fills and positions
- Measures latency and correctness
For now, keep this mental model:
Correct state + clear rules + measured speed = usable trading foundation
Next, we will move into Data Handling and Management in C++, where these concepts become structured inputs your program can store, validate, and process.
