What Is Algo Trading? How It Works, the US Rules, and a Real Rule Run on SPY
Algorithmic trading means writing a trading rule precisely enough that a computer can execute it. The difficulty is rarely the code — it is that most rules people describe are missing an exit, a size, a timeframe, or a definition of the terms they use.
Algorithmic trading, or algo trading, is trading by a written rule precise enough for a computer to run with no further instruction: what to buy, when, how much and when to get out. The code is usually the easy part; the hard part is finishing the rule.
Banks use algorithms to slice large orders, market makers use them to quote all day, and a retail trader can use one to run a single moving average rule. This page covers how the pieces fit, what US law requires, what SEC data shows about the orders algorithms send, and one simple rule traded on real SPY prices, losses included.
How it works
An algorithm has three layers. A signal decides when to act. An order decides how: market or limit, how big, and how to split it. A risk check sits between the code and the exchange and refuses anything that breaks a limit.
Signal algorithms are what most traders mean: trend following, mean reversion, arbitrage between two prices of the same thing, or a moving average crossover like the example below. Execution algorithms do not choose the trade at all. A VWAP algorithm, for example, spreads a big order across the day so it trades near the day’s average price.
Speed is a separate question. A daily rule that places one order after the close is algorithmic trading; so is a high frequency firm sending and canceling quotes in microseconds. What makes it algorithmic is that the rule, not a person, makes each decision.
Writing a rule a computer can run
Here is a common entry: buy when the close crosses above the 20-day simple average. A computer can run it, but it is only one of the five sentences a complete rule needs.
- Market and chart: SPY, daily bars, regular session.
- Entry: after a close above the 20-day average that follows a close at or below it, buy at the next open.
- Exit: sell at the next open after any close at or below the average. The same sentence covers the loss and the gain.
- Size: $10,000 per trade, whole shares.
- Costs: what the broker and regulators charge on each sale, and the gap between the price the rule saw and the price it got.
Try writing your own approach this way, and the sentence will usually stop somewhere, at “when the structure looks right” or “with enough confirmation”. Each place it stops is a decision you have been making differently each time, and a decision that was never written down cannot be reviewed.
Every number in a rule is a choice. On SPY since 1993 a 5-day average gave 1,121 signals and a 60-day average 275. Try every length, keep the one that looked best, and you have measured which setting fitted the past, which is overfitting.
A worked example
The five sentences above, run on every signal from 1 September to 30 October 2026. Prices are SPY’s real opens and closes from Yahoo Finance. Shares are $10,000 divided by the entry price, rounded down.
| Signal close (vs average) | Bought at the open | Close at or below the average | Sold at the open | Shares | Result before fees |
|---|---|---|---|---|---|
| 3 Sep: $773.17 vs $769.21 | 4 Sep: $772.01 | 8 Sep: $765.96 | 9 Sep: $764.08 | 12 | -$95.16 |
| 21 Sep: $773.50 vs $764.69 | 22 Sep: $774.03 | 29 Sep: $764.20 | 30 Sep: $766.45 | 12 | -$90.96 |
| 2 Oct: $769.64 vs $764.83 | 5 Oct: $769.69 | 14 Oct: $769.60 | 15 Oct: $766.25 | 12 | -$41.28 |
| 15 Oct: $771.79 vs $771.11 | 16 Oct: $773.24 | 16 Oct: $763.25 | 19 Oct: $767.23 | 12 | -$72.12 |
| 21 Oct: $773.09 vs $770.88 | 22 Oct: $766.37 | 22 Oct: $763.96 | 23 Oct: $759.90 | 13 | -$84.11 |
| 28 Oct: $772.31 vs $770.90 | 29 Oct: $768.79 | 29 Oct: $769.36 | 30 Oct: $761.38 | 13 | -$96.33 |
Check one row by hand. The 2 October trade bought 12 shares at $769.69 and sold them at $766.25: 12 x ($766.25 - $769.69) = -$41.28. The SEC’s fee of $20.60 per million dollars of sales adds 12 x $766.25 x 0.0000206 = $0.19. All six trades lost: $479.96 before fees, $481.12 after $1.16 of SEC fees. FINRA’s per-share trading fee is left out; it is $0.00 from 1 October to 31 December 2026 and a fraction of a cent per share before that.
What the table teaches is not the result. Six trades in a sideways two months say nothing about the rule over decades. Two rows show what writing it down exposed. On 2 October the trade was $9.40 a share ahead at the 6 October close of $779.09, and the exit gave all of it back, because the rule only sells after a close below the average. On 22 October SPY opened at $766.37, already under the signal day’s average of $770.88, and the rule bought anyway, because nobody wrote a sentence about a gap through the line. A computer follows the sentence it was given, including the gaps in it.
The original data
How often the entry turned straight back. On Yahoo Finance’s SPY daily bars from 29 January 1993 to 26 October 2026, the close crossed above its 20-day average 515 times. Of the 514 with five sessions to follow, 124 (24.12%) were back at or below the average the very next session, 225 (43.77%) within 3 and 278 (54.09%) within 5. Of all 5,365 closes above the average, signal or not, 33.10% were back at or below it within 5 sessions. A fresh crossing sits right on the line, so it turns back far more often.
By length, the 5-day average reversed within 5 sessions on 71.96% of its signals, the 10-day on 55.11%, the 30-day on 53.90% and the 60-day on 47.64%; no length kept a clear majority of its signals above the line for a week. Nasdaq’s SPY history since October 2016 gives the same 124 signals on the same dates as Yahoo’s bars over those years. Every length is in the crossover file and every 20-day signal in the signal list. One fund, no costs, and a reversal here counts closes, not trade results.
What the order book is made of. The SEC’s MIDAS system records every order, cancel and trade message on the exchanges whose direct feeds it reads. In the second quarter of 2026, 1 April to 30 June (62 sessions), SPY drew 1,350,281,175 cancel messages against 20,472,387 lit trades, the trades against displayed orders (the SEC’s ratio leaves out trades against hidden orders): 65.96 cancels for every lit trade. Orders were placed for 145.95 billion SPY shares and 1.25 billion traded on those exchanges, 0.86%.
The median stock of 3,723 had 16.30 cancels per trade, and 3.81% of its order shares traded; the median stock in the largest tenth by market value had 15.18 and 3.86%. Apple (AAPL) had 25.54 cancels per trade with 3.31% of order shares traded, and Nvidia (NVDA) 21.13 with 3.95%.
People do not cancel a billion orders a quarter; programs that keep re-pricing quotes do. The data does not label who sent each message, so it shows the footprint of automated quoting, not a share of trading done by algorithms. The busiest funds have the highest ratios: QQQ reached 133.28 cancels per trade, with 0.44% of its order shares traded. Every ticker is in the MIDAS file. For a retail rule this matters because the quote you see may be gone before your order arrives; see the order book page.
Is algo trading legal? The US rules
Yes. Running your own rule through a broker is legal. The rules fall on the firms that touch the market, and four are worth knowing:
- SEC Rule 15c3-5, the market access rule, adopted 3 November 2010: a broker that gives anyone access to an exchange must block orders above preset credit or capital limits, or orders that look erroneous, before they reach the market. It ended “naked” access, so your bot’s orders pass your broker’s checks.
- FINRA Regulatory Notice 15-09, 26 March 2015: guidance for member firms on supervising and controlling algorithmic strategies under FINRA Rule 3110.
- FINRA Rule 1220(b)(4)(A): at a member firm, the person mainly responsible for designing, developing or significantly changing an algorithmic strategy in stocks, preferred shares or convertible bonds must register as a Securities Trader.
- CFTC Electronic Trading Risk Principles, effective 11 January 2021: futures exchanges must have rules and pre-trade risk controls for all electronic orders, in full by 12 July 2021. They replaced Regulation AT, a stricter proposal the CFTC withdrew on 25 June 2020.
None of them asks a retail trader to register anything to run a rule in their own account; the broker’s checks apply to the orders instead. Those dates and rule numbers were read on the SEC, FINRA, CFTC and Federal Register sites on 1 November 2026.
Three things a backtest will not tell you
Run a complete rule over history in a backtest and three faults make it look better than live trading would.
Look-ahead. Acting on a close at that close. The close is only known once the bar ends, which is why the worked example buys at the next open.
Survivorship. Testing on today’s index members skips the companies that were removed, usually the ones that did badly.
Costs. A test with no spread or fees trades a market nobody trades in; the scalping page shows why a rule that trades often feels that most.
When it fails
About half the signals reverse within a week
278 of 514 SPY signals from the 20-day average were back across it within 5 sessions, and the rule cannot tell which ones in advance. A precise rule is not a right rule; automation removes hesitation, not losing trades.
The rule does exactly what it says
The 22 October gap and the 2 October giveback were both the rule working as written. Read your sentences for the cases they skip, then decide those cases on paper.
You tuned it on the history you tested it on
Choosing the best setting on a period and then judging it on that same period measures nothing, and it is the most common error in this whole subject.
Questions people ask
Is algo trading really profitable?
Some firms profit from it, but no rule is profitable just because a computer runs it. The six-trade example above lost money, and over 33 years half of its signals turned back within a week; costs and exits decide what an entry is worth.
Is algo trading legal in the USA?
Yes. Your broker must run pre-trade risk checks on your orders under SEC Rule 15c3-5, and FINRA’s registration rule covers people who build algorithms at member firms, not your own account.
Do I need to code to do algorithmic trading?
Not to start. The hard part is writing the five sentences; many charting platforms can run them, and the automate a strategy page covers the steps.
How many signals does a moving average rule give?
It depends on the length. On SPY’s daily chart from 1993 to 2026 the 5-day average gave 1,121 signals, the 20-day 515 and the 60-day 275: about 33, 15 and 8 a year.
Related
Entry and exit is the half of a rule most people leave unspecified, as the 2 October trade showed.
Risk management turns a signal into a position size you can survive.
And moving average is the tool the example rule is built on, including why its length is a choice rather than a setting.
Even if you never run a bot, try writing your strategy as a rule, because the exercise is brutal in a useful way. You may find you cannot finish the sentence: you know what you mean by a good setup and cannot say it in a way a computer would accept. That gap between what you think you do and what you can actually specify is worth more than any indicator.
— Michael Whitman
This page is educational, not financial advice. Test every idea on your own charts before risking money.