Moving AveragesPine Script v6

ta.sma()

Simple Moving Average

Computes the simple moving average (SMA): the arithmetic mean of the source series over the last length bars. It is one of the most common ways to smooth price and measure trend.

Syntax

Syntax
ta.sma(source, length) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars (length).

Returns

Simple moving average of source for length bars back.

Remarks

na values in the source series are ignored.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.sma")
plot(ta.sma(close, 15))

// same on pine, but much less efficient
pine_sma(x, y) =>
    sum = 0.0
    for i = 0 to y - 1
        sum := sum + x[i] / y
    sum
plot(pine_sma(close, 15))

Trading Applications

Identify trend direction by comparing price to SMA

Use SMA crossovers (e.g., 50/200 golden cross) for entry/exit signals

Smooth noisy price data for clearer trend visualization

Define dynamic support and resistance levels

Frequently Asked Questions

Generate ta.sma() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.sma() and other built-in functions instantly.