What does a moving average actually measure?
Short answerA moving average measures where price has been, on average, over a fixed number of recent candles. Each time a candle closes, the oldest close drops out and the newest one comes in, so the line slides along the chart. It does not predict anything. Because it averages the past, it always lags: a 20-candle simple moving average describes price as it was about 9.5 candles ago. That lag is the price of a smoother line, and it is why moving-average signals arrive after a turn and repeat themselves in sideways markets.
The simple moving average (SMA) is the one indicator in Valmy's v1 strategy language, so this lag shapes any rule you build with it. A rule such as "buy when the 5-candle SMA crosses above the 20-candle SMA" can act only after the crossing candle has closed, cannot signal before the 21st candle of history, and trades every crossing, including the false ones in a range. Once you can see the lag, you can pick a lookback knowing what it costs, and you will not expect the rule to buy at the low.
Valmy noteValmy's v1 strategy language includes the simple moving average and no other indicator (as of 26 November 2026). The SMA sections below explain how a Valmy strategy evaluates it. The exponential moving average (EMA) is taught here for reading charts.
Reading guideFor the basics, read the next section, the SMA section with its step-by-step calculation, and part 1 of the exercise at the end. Sections marked "Going further" (the EMA, stocks versus crypto and the research) add depth and can wait.
What a moving average measures, and why it always lags#
A moving average is the average closing price over the last n candles, recalculated at every close. It summarizes recent price into one smoother line: small up-and-down moves cancel out, and the general direction remains. When the line rises, recent closes are higher on average than the closes that dropped out of the window. When it falls, they are lower.
A few terms used throughout:
- A candle summarizes trading over one period, such as five minutes: its open, high, low and close. Moving averages are usually calculated from closes.
- The lookback or period, written n, is how many candles the average covers. "SMA(20)" means a simple moving average of the last 20 closes.
- Lag is how far behind price the average runs. Every moving average lags because every value it uses is already in the past.
- A crossover happens when one line moves from below another line to above it, or the reverse.
The trade-off is fixed: a longer lookback gives a smoother line that lags more, and a shorter lookback follows price closely but also follows its noise. No setting removes both problems.
Simple moving average (SMA): the plain average of recent closes#
The simple moving average (SMA) is the sum of the last n closes divided by n, with every close weighted equally. Its input is one price series, usually the close (Valmy's illustrative strategy averages closes). Its unit is the price unit of the chart, for example USDT on a BTC/USDT chart. There is no single standard lookback: 20, 50 and 200 are common on charts, and Valmy's illustrative rule uses 5 and 20. The SMA has no creator to credit, since it is the ordinary arithmetic mean applied to a sliding window. An SMA(n) has no value until n candles exist.
Calculating the SMA step by step#
To calculate SMA(5), add the last five closes and divide by 5, then repeat at each new close. The table uses eight invented closes in USDT (synthetic data).
| Candle | Close | Closes in the window | SMA(5) | EMA(5), shown later |
|---|---|---|---|---|
| 1 | 100 | |||
| 2 | 101 | |||
| 3 | 99 | |||
| 4 | 100 | |||
| 5 | 100 | 100, 101, 99, 100, 100 | 100.0 | 100.00 |
| 6 | 106 | 101, 99, 100, 100, 106 | 101.2 | 102.00 |
| 7 | 107 | 99, 100, 100, 106, 107 | 102.4 | 103.67 |
| 8 | 106 | 100, 100, 106, 107, 106 | 103.8 | 104.44 |
- Candle 5: 100 + 101 + 99 + 100 + 100 = 500, and 500 ÷ 5 = 100.0. This is the first value, because candles 1 to 4 do not have five closes yet.
- Candle 6: the close of 100 from candle 1 drops out and 106 comes in. The sum is 506, so SMA(5) = 101.2.
- Candle 7: 512 ÷ 5 = 102.4. Candle 8: 519 ÷ 5 = 103.8.
A shortcut gives the same result: new SMA = previous SMA + (new close − dropped close) ÷ n. At candle 6 that is 100 + (106 − 100) ÷ 5 = 101.2. Notice that price jumped to 106 at candle 6, but the SMA reached only 103.8 two candles later. The average was still carrying the older closes near 100.
How far behind price an SMA sits: the (n − 1)/2 rule#
An n-candle SMA is centered (n − 1)/2 candles in the past. The closes in its window are 0, 1, 2 … n − 1 candles old, and their average age is (n − 1)/2. So SMA(5) describes price about 2 candles ago, SMA(20) about 9.5 candles ago and SMA(200) about 99.5 candles ago. On five-minute candles, SMA(20) runs about 47.5 minutes behind.
Two consequences follow:
- In a steady trend the SMA trails by a fixed distance. If price rises 0.50 USDT every candle, SMA(20) sits 9.5 × 0.50 = 4.75 USDT below the latest close.
- At a turn the SMA keeps moving the old way. After a symmetric peak, SMA(20) keeps rising until its window is centered on the peak, about 9.5 candles later. Figure 3, in the EMA section, shows this on a test series.
The same rule explains why long averages feel "slow": an SMA(200) on daily candles responds to the market as it was roughly 100 trading days ago.
Reading the SMA on a chart: a five-minute crossover#
On a chart, read the SMA's slope for direction, the price's position above or below it for where the latest close stands relative to the recent average, and crossings between a fast and a slow SMA for changes in the short-term average. Figure 1 shows invented five-minute candles (synthetic data) with SMA(5) and SMA(20). The lower panel shows SMA(5) minus SMA(20): when the bars turn from negative to positive, the fast average has crossed above the slow one.
- Warm-up (candles 1 to 20). SMA(20) has no value until candle 20, and a crossover needs a previous value as well, so the first candle at which a cross can be detected is candle 21. On five-minute candles, that is 105 minutes of history.
- Cross above (candle 26). At candle 25, SMA(5) was 99.82 and SMA(20) was 99.86, so the fast average was below. At the close of candle 26, SMA(5) was 99.96 and SMA(20) was 99.85, so it was above. The signal candle closed at 100.34. The lowest close before the signal was 99.09, so the signal came after price had already risen from its low.
- Top and cross below (candles 71 and 78). Price closed at its top, 110.33, on candle 71. SMA(5) crossed below SMA(20) at the close of candle 78, seven candles later, and SMA(20) itself kept rising until candle 79, eight candles after the top, close to the (20 − 1)/2 = 9.5 the rule predicts.
Suppose a trader bought at the earliest possible price after the cross, the open of candle 27 at 100.32, and sold at the open after the cross below, candle 79 at 108.42. The gain was +8.1% before costs and +7.9% after an assumed fee of 0.1% on each side. The move from the lowest close to the top was 11.3%. The entry was 1.2% above the low, and the exit 1.7% below the top close. That gap is the cost of lag in a trend that worked.
How a Valmy strategy evaluates an SMA crossover#
Valmy's v1 specification evaluates a strategy once per closed five-minute candle. Its expression language includes the SMA, comparisons and cross-above/cross-below. The specification's illustrative Spot program buys with a quote amount (a parameter with a default of 20 USDT) when SMA(5) of the close crosses above SMA(20) of the close, and otherwise holds (as of 26 November 2026). Three rules from the specification decide what happens around the cross:
- Warm-up yields hold. If the history is too short for an indicator, the strategy holds and records a warm-up reason. It never treats a missing SMA as zero.
- A crossover uses two fully warmed values of each average. The specification compares current and previous values. In plain terms, the fast average must have been below the slow one at the previous close and be above it at the current close. With SMA(20), the earliest candle at which this can be true is the 21st.
- The order comes after the close. The decision is made when the signal candle has closed, so any order fills at a price available after that close: in a backtest, the earliest execution is simulated on a subsequent candle with the configured spread, slippage and fee model. In Figure 1 the signal candle closed at 100.34, and the earliest fill shown is the open of candle 27 at 100.32, plus costs. The signal candle's close is never available as a fill.
The specification also suggests an initial limit of 1,000 bars of lookback (a proposal, as of 26 November 2026). On five-minute candles, 1,000 bars cover about 3.5 days.
When the SMA misleads: whipsaws in a range#
The SMA misleads most in a sideways market, where price keeps crossing back and forth around the average and every crossing looks like a new trend. These false signals are called whipsaws. The average describes recent price correctly, but a range has no trend for it to follow, and each signal arrives after the short move that caused it.
Figure 2 shows invented five-minute candles (synthetic data, designed to show this failure) moving sideways between closes of 98.78 and 100.69, a range of about 2%. SMA(5) crossed above SMA(20) five times. Consider a rule that buys at the open after each cross above and sells at the open after each cross below, with an assumed fee of 0.1% per side.
- Four round trips were completed, and all four lost money. Together they lost 2.9% after fees.
- The largest loss was the buy at 100.27 after the cross at candle 59, sold at 99.25 after the next cross below: −1.0% before fees.
- The fifth signal, at candle 95, was still open when the sample ended.
The same rule made +7.9% in the trend of Figure 1. Neither result says what the rule will do next: it depends on whether the coming market trends or ranges. Before automating a crossover, test it on both kinds of periods, and see Is this market trending or stuck in a range? for ways to tell them apart.
Exponential moving average (EMA): more weight on the latest closes#
Going further: Valmy's v1 strategies cannot use the EMA, so on the basic route, skip to part 1 of the exercise.
The exponential moving average (EMA) is a moving average that gives the newest close the most weight and each older close a little less, with weights shrinking by a constant factor. Each new value moves part of the way from the previous EMA toward the latest close: EMA = previous EMA + α × (close − previous EMA). The fraction α ("alpha") is set from the period n as α = 2 ÷ (n + 1), so EMA(20) uses α = 0.0952 and EMA(5) uses α = 1/3. Its input is usually the close and its unit is the price unit of the chart. Common periods are 12 and 26 (used by MACD), 20, 50 and 200.
Calculating the EMA step by step#
To calculate EMA(5), start from a seed value and then move one third of the way toward each new close. This lesson uses TA-Lib's documented definition, which seeds the EMA with the SMA of the first n closes. The table in the SMA section shows the same eight closes (synthetic data).
- Seed, candle 5. Following TA-Lib, the first EMA(5) is the SMA of the first five closes: 100.00.
- Candle 6. The close is 106. The distance from the previous EMA is 106 − 100 = 6, and one third of it is 2, so EMA(5) = 102.00.
- Candle 7. The close is 107. EMA(5) = 102 + (107 − 102) ÷ 3 = 103.67.
- Candle 8. The close is 106. EMA(5) = 103.67 + (106 − 103.67) ÷ 3 = 103.67 + 0.78 = 104.44.
After the jump at candle 6, EMA(5) read 102.00 while SMA(5) read 101.2: the EMA reacted more to the newest close. Two candles later the EMA was at 104.44 and the SMA at 103.8.
Why 2 ÷ (n + 1)? With that alpha, the average age of the closes in an EMA is (1 − α) ÷ α = (n − 1)/2, the same as an SMA with the same n. For n = 20 both come to 9.5 candles. EMA(20) and SMA(20) are therefore equally far behind in a steady trend. They differ in how they respond when the trend changes.
Reading the EMA on a chart: same average age, earlier turn#
Read the EMA like the SMA: slope for direction, price above or below it, and crossings. The practical difference is timing. The EMA turns sooner after a change in direction, because the latest closes carry more weight, while in a steady trend it trails by the same distance as an SMA of the same period.
Figure 3 uses a test series built for this lesson (synthetic data): price rises exactly 0.50 USDT per bar to 120 at bar 41, then falls 0.50 per bar.
- During the rise, both lines sit exactly 4.75 below price: 9.5 bars of average age times 0.50 per bar. The lower panel shows the two distance lines on top of each other.
- At the top (bar 41), price turns down but both averages are still below it, so both keep rising for a while.
- The turns: EMA(20) peaks 6 bars after the top at 116.54. SMA(20) peaks 9 bars after the top at 117.50 and holds that value for one more bar, because on a symmetric turn its window is centered on the top between bars 9 and 10.
Real prices are not straight lines, so the gap between the two turns varies, but the order is typical: the EMA turns first, and the SMA turns later.
Why the EMA's starting value differs between platforms#
An EMA depends on every close since it started, so its early values depend on the starting rule, called the seed. TA-Lib seeds with the SMA of the first n closes and reports nothing before then. Other software starts from the first close: pandas, a common Python data library, does this when its EWM function is called with adjust=False, and its default adjust=True uses yet another weighting for early values. Two platforms can also load different amounts of history. In each case, the same chart can show different EMA values near the start of the data.
The difference shrinks by the factor (1 − α) at every candle, because both versions receive the same updates afterward. Figure 4 compares two EMA(20) lines on the same invented five-minute closes (synthetic data).
At candle 20, the SMA-seeded EMA(20) read 94.61 and the first-close version read 93.95, a gap of 0.65 USDT. Each candle multiplies the gap by 19/21. By candle 40 it was 0.09, and from candle 62 it stayed under 0.01. Shrinking a gap to 1% of its starting size takes 47 candles for EMA(20). A longer EMA forgets its seed more slowly, so allow several times n candles of history before comparing EMA values across platforms or using them in a test.
When the EMA misleads#
The EMA misleads in the same places as the SMA, and its speed makes some of them worse. In a range, the faster reaction produces more crossings. In the range of Figure 2 (synthetic data), EMA(5) crossing EMA(20) gave seven buy signals instead of five. Six round trips were completed, all six lost, and the total was −5.1% after the same assumed 0.1% fees, compared with −2.9% for the SMA pair. Faster is not better when there is no trend to catch.
The second trap is reading early EMA values as exact. Near the start of a dataset, or after a platform loads a short history, the EMA still carries its seed. Compare EMAs only after the seed has faded, as described in the previous section.
Stocks and crypto: what a "200-day" average covers#
Going further: read this before comparing an average across markets.
The same setting covers different amounts of time on different markets, because a moving average counts candles, not calendar days. A US stock trades in sessions: the New York Stock Exchange's core session runs from 9:30 a.m. to 4:00 p.m. Eastern time on weekdays, and the exchange is closed on its holidays. In 2026 there are 261 weekdays and 10 NYSE holidays, so 251 trading days. A 200-day SMA on a US stock chart therefore spans about 9.6 months, roughly 291 calendar days, and its daily candles skip weekends and holidays.
Crypto pairs such as those on Binance trade around the clock, every day, apart from rare technical halts. A 200-day SMA on a crypto chart spans 200 calendar days, about 6.6 months. The "same" average is about three months shorter.
Intraday candles change the picture again:
- On five-minute candles, 200 candles cover 16 hours and 40 minutes on a crypto pair.
- A US stock session holds 78 five-minute candles, so 200 of them span about 2.6 sessions and include the overnight gaps between sessions. An overnight gap enters the average as one jump between two closes.
- Valmy strategies are evaluated on five-minute candles. A "200-day" average would need 57,600 five-minute candles, far beyond the suggested 1,000-bar lookback limit, so a Valmy rule describes minutes and hours, not months.
When you read about a "200-day moving average" in stock market commentary, it usually refers to daily candles of an index or a share, not to anything a five-minute crypto strategy can compute.
What the research says about moving-average rules#
Going further: the evidence behind this lesson's caution.
Published tests of moving-average rules are mixed, so a crossover is a hypothesis to test, not a proven way to make money. Brock, Lakonishok and LeBaron (1992) tested moving-average and trading-range rules on the Dow Jones Industrial Average from 1897 to 1986: buy signals were followed by higher returns than sell signals. Sullivan, Timmermann and White (1999) retested nearly 8,000 rule variations on the same index while correcting for data snooping, the chance that some rule looks good only because many rules were tried. Some rules still beat the benchmark over 1897 to 1986, but over 1987 to 1996 the evidence that the best rule added value was scant.
The same study found no evidence of outperformance in S&P 500 index futures over the 13 years after that contract began trading in 1984. These studies used daily stock index data, not five-minute crypto candles, so they say nothing direct about a Valmy rule. They do show why a rule that looks good on one stretch of history needs to be tested on data it was not designed on. How do I turn something I noticed on a chart into a rule I can test? walks through that process.
Check your understanding: SMA and EMA#
Question, part 1 (SMA, basic route). A rule buys when SMA(2) crosses above SMA(4) on closed five-minute candles. The last six closes, in USDT, were 50.0, 49.6, 49.4, 49.8, 50.4 and 50.6 (candles 1 to 6).
- At which candle does the cross happen, and what are the two averages before and at the cross?
- What is the earliest price at which the resulting order can fill?
- Why can't the rule signal at candle 4?
Question, part 2 (EMA, going further). Two platforms show EMA(9) for the same pair. On the previous candle, platform A showed 50.00 and platform B, which started its EMA differently, showed 49.80. The new close is 51.00. What does each platform show now, and how big is the gap?
Show the answers
Answer, part 1.
- At candle 4, SMA(2) = (49.4 + 49.8) ÷ 2 = 49.6 and SMA(4) = (50.0 + 49.6 + 49.4 + 49.8) ÷ 4 = 49.7: the fast average is below. At candle 5, SMA(2) = (49.8 + 50.4) ÷ 2 = 50.1 and SMA(4) = (49.6 + 49.4 + 49.8 + 50.4) ÷ 4 = 49.8: the fast average is above. The cross happens at the close of candle 5. At candle 6 the fast average stays above (50.5 vs 50.05), so there is no new cross.
- The decision exists only once candle 5 has closed at 50.4, so the order can fill no earlier than the open of candle 6, plus spread, slippage and fees. The candle 5 close of 50.4 is not available as a fill price.
- SMA(4) first exists at candle 4. A crossover also needs the previous value of both averages, so candle 5 is the first candle at which a cross can be detected. Before that, the rule holds.
Answer, part 2. α = 2 ÷ (9 + 1) = 0.2. Platform A: 50.00 + 0.2 × (51.00 − 50.00) = 50.20. Platform B: 49.80 + 0.2 × (51.00 − 49.80) = 50.04. The gap shrank from 0.20 to 0.16, that is by the factor 1 − α = 0.8. Neither platform is wrong. They started differently, and the difference fades by 20% per candle.
Related lessons#
- Before this lesson: What does a candle tell me, and what does it hide?
- Next: Which moving average reacts fastest, and what does speed cost?
- Next: What does MACD show, and why does it lag?
- Next: What does RSI measure, and why do "overbought" signals fail?
- See also: Is this market trending or stuck in a range?
- See also: How do I turn something I noticed on a chart into a rule I can test?
- See also: What is look-ahead bias, and how does it get into a backtest?
- See also: What do Bollinger Bands measure, and does touching a band mean anything?
- See also: What changes when an indicator is weighted by volume? VWMA and the money flow index
- See also: Is a rally broad or narrow? Stocks above their moving average and new highs minus new lows