Moving averages: SMA and EMA
Is the trend up or down?
The average closing price of the last N days, redrawn every day, so the day-to-day noise is smoothed out.
SMA / EMA on a chart of past prices
Settings: move a slider and the chart redraws
SMA weighs every day equally; EMA weighs recent days more.
Fewer days: hugs the price and turns sooner.
More days: smoother, slower, shows the bigger picture.
What it measures
A moving average is the average closing price of the last N trading days. Each day the oldest close drops out and the newest comes in, so the average moves along with the price. It smooths out the day-to-day noise so the direction is easier to see.
A simple moving average (SMA) gives every day the same weight. An exponential moving average (EMA) gives recent days more weight, so it turns sooner when the price turns. The dashed grey line is the other kind with the same number of days, so you can see the gap between the two.
Traders usually watch two: a fast one (fewer days) and a slow one (more days). When the fast one is above the slow one, prices lately have been higher than over the longer stretch.
How traders read it
- Price above a rising average: the trend is up. Price below a falling average: the trend is down.
- The fast average crossing above the slow one is read as a new uptrend (for the 50 and 200-day pair it is called a golden cross); crossing below as a new downtrend (a death cross). These crossings are marked on the chart.
- The more days, the smoother the line and the later it reacts: a 200-day average shows the big picture, a 20-day one the last month.
- Many traders treat a well-watched average, such as the 200-day, as a level where the price may pause. That is a habit of the crowd, not a law.
The traps
Always late
An average is built from past closes, so it confirms a turn only after it has happened. By the time a crossover appears, part of the move is gone.
Whipsaws when the price goes sideways
When the price drifts sideways the two lines tangle and cross back and forth, and every crossing is a trade that pays costs. Look for the flat stretches on the chart.
Shorter is not better
Fewer days react sooner but give more false signals; more days give fewer signals, later. Moving the sliders swaps one problem for the other.
Test the classic rule: Moving-average crossover
Waiting for prices…
Quick check
Waiting for prices…
Use it in a script
function run(bars, ta, helpers) {
const closes = helpers.closes(bars);
const fast = ta.ema(closes, 20); // the fast average
const slow = ta.ema(closes, 50); // the slow average
const signals = [];
for (let i = 0; i < bars.length; i++) {
if (fast[i] > slow[i]) signals.push(helpers.buy(i)); // fast above slow: be in
else if (fast[i] < slow[i]) signals.push(helpers.sell(i)); // fast below slow: be out
}
return helpers.finalize(signals); // keeps the first buy and sell of each run: the crossovers
}This is the rule tested above, with the settings on your sliders, and it gives exactly the same buy and sell candles. Paste it into the Scripts drawer of the chart workbench, or save it and compare it across stocks in the Strategy Lab. New to scripts? The Writing strategies track starts from zero.
In the reference:ta.smata.emahelpers.finalize