Skip to content

Technical indicators

Simple Moving Average (SMA)

The average of the last few closes, redrawn at every bar.

The mean of the last periods closes. Every bar in the window counts equally, so the line follows the price with a delay of about half the window.

Traders use it to judge direction: price above a rising average reads as an uptrend, below a falling one as a downtrend. Two averages of different lengths crossing is a common entry rule, as in the moving average crossover guide. A longer window gives a smoother line that turns later.

SMA = (close₁ + close₂ + … + closeₙ) / n

where n is periods and close₁ is the bar that just closed.

Call: sma(periods)

Setting Chart default
periods: Periods 20

What you get: a number.

Warm-up: with sma(20), it asks for 20 bars of history and a run-up of 1, and its first value came on bar 21 of the example data.

Simple Moving Average (SMA) drawn by its own code on example prices

The prices are made up, hourly, and the same on every page, so the chart shows how the indicator behaves, not how a market behaved.

function onIntervalClose() {
if (OPEN_POSITIONS_COUNT === 0 && sma(20) > sma(50) && CLOSE > sma(20)) {
openPosition("BUY", { quantity: 10000, stopLossPips: 25, takeProfitPips: 50 });
}
}