Algorithmic Trading Basics
Build and backtest simple trading strategies
1. Trading at the Speed of Light
In 2010, a single algorithmic trading error caused the "Flash Crash," wiping out $1 trillion in market value in minutes. Today, over 70% of US equity trades are executed by algorithms. These bots make split-second decisions using mathematical models, processing millions of data points per second.
π€ Core Concept
Algorithmic trading uses computer programs to execute trades based on predefined rules. The three main types are: Momentum strategies (trend following), Mean reversion (betting on price corrections), and Arbitrage (exploiting price differences). Success requires backtesting, low latency, and rigorous risk management.
π― Interactive: Choose Trading Strategy
2. Building a Moving Average Crossover Strategy
π From Idea to Algorithm: Building Trading Logic
Moving Average Crossovers: The Hello World of Algo Trading
Moving average (MA) crossover strategies are the simplest trend-following systems. The logic: when a fast MA (e.g., 10-day) crosses ABOVE a slow MA (e.g., 50-day), BUY (trend turning up). When fast crosses BELOW slow, SELL (trend turning down).
Parameter Optimization: The Overfitting Trap
Should you use a 10/50 MA crossover? 5/20? 20/200? Optimization finds the best historical parametersβbut also creates overfitting: a strategy that perfectly fits past data but fails on new data. This is the #1 killer of algo strategies.
| Fast/Slow | Total Return | Win Rate | Max DD | Trades |
|---|---|---|---|---|
| 5 / 20 | +45% | 52% | -18% | 38 |
| 10 / 50 | +38% | 58% | -12% | 22 |
| 20 / 100 | +28% | 62% | -9% | 12 |
| β 13 / 37 | +62% | 68% | -7% | 28 |
| 50 / 200 | +18% | 55% | -11% | 5 |
Backtesting: The Reality Check
Backtesting runs your strategy on historical data to see what WOULD have happened. But backtests lie! They don't include real-world friction: commissions, slippage, liquidity constraints, and execution delays.
π Interactive: Strategy Parameters
π Interactive: Backtesting Period
3. Order Execution & Slippage
π Market Microstructure: Where Profits Go to Die
The Three Order Types (And Why Each One Costs You)
Every algo strategy eventually asks: "How do I actually BUY this stock?" The answer determines whether your 20% backtest becomes 15% reality or -5% disaster. Order type is the tradeoff between speed (certainty of execution) and price (slippage cost).
Slippage: The Silent Killer
Slippage is the difference between expected price and execution price. It's invisible in backtests (assumes perfect fills) but brutal in live trading. On a 100-trade strategy, 0.1% average slippage = -10% annual return gone!
π Interactive: Order Types
βοΈ Interactive: Execution Algorithms
TWAP Strategy
Splits a large order into equal slices executed at regular intervals. Example: 10,000 shares over 10 minutes = 1,000 shares per minute.
β‘ Interactive: Latency & HFT
4. Risk Management & Trading Frequency
π The Frequency Paradox: More Trades β More Profit
Trading Frequency Tiers: The Reality Behind the Speed
Algo trading spans from low-frequency (hold days/weeks, 10 trades/month) to high-frequency (hold seconds, 10,000 trades/day). Higher frequency requires exponentially more capital, infrastructure, and sophistication. Most successful retail traders stay low-frequency.
Cost Structure: Why Frequency Matters
Every trade has fixed costs (commissions) and variable costs (slippage). Higher frequency multiplies these costs exponentially. A 0.1% cost/trade strategy making 10 trades/month = -1.2% annual cost (manageable). Same strategy making 1,000 trades/month = -120% annual cost (impossible!).
| Cost Component | Low (10 trades/mo) | Med (100 trades/mo) | High (10K trades/mo) |
|---|---|---|---|
| Commissions | -0.1% | -0.6% | -5% |
| Slippage | -0.5% | -5% | -30% |
| Data Feeds | -0.1% | -1% | -5% |
| Infrastructure | -0.1% | -2% | -10% |
| Opportunity Cost | -0.2% | -3% | -15% |
| TOTAL COST | -1.0% | -11.6% | -65% |
Position Sizing: Risk of Ruin in Algo Trading
Algos don't get emotionalβbut they can still blow up accounts with bad position sizing. The risk of ruin formula shows how many consecutive losses wipe you out. With 5% risk/trade, you're broke in 14 losses. With 1%, you can survive 70 losses!
π Interactive: Trading Frequency
π² Interactive: Position Sizing
π Interactive: Strategy Performance
| Strategy | Win Rate | Sharpe | Max DD | Complexity |
|---|---|---|---|---|
| Momentum | 55% | 1.2 | 15% | Low |
| Mean Reversion | 60% | 0.9 | 12% | Medium |
| Arbitrage | 85% | 2.1 | 5% | High |
| Market Making | 52% | 1.8 | 8% | Extreme |
| Statistical Arb | 58% | 1.5 | 10% | High |
5. Key Takeaways
Backtest Everything
Never trade a strategy without backtesting it on at least 3-5 years of data across multiple market conditions. A strategy that works in bull markets might crater in bear markets. Out-of-sample testing is crucial.
Transaction Costs Kill Returns
Commissions, slippage, and market impact can turn a profitable backtest into a losing live strategy. High-frequency strategies need commissions <$0.001/share. Include realistic costs in backtests.
Speed = Edge in HFT
High-frequency trading requires sub-5ms latency. Firms pay millions for co-location at exchanges and microwave networks between cities. Retail traders can't compete at this speedβfocus on slower strategies.
Overfitting Destroys Strategies
Optimizing parameters on historical data can create a strategy that perfectly fits the past but fails in live trading. Use walk-forward analysis and keep strategies simple with few parameters.
Risk Management is Non-Negotiable
Even with 60% win rate, risking 5% per trade = ruin in 14 losses. Use 0.5-2% risk per trade maximum. Implement circuit breakers to stop trading after X losses. One bad day shouldn't wipe out your account.
Alpha Decays Quickly
Profitable strategies attract competition until returns decline. The average algo strategy's edge decays 20-30% per year as others discover it. Continuously research and adapt or returns will vanish.