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
ta.sma(source, length) → series floatArguments
| Parameter | Type | Description |
|---|---|---|
| source | series int/float | Series of values to process. |
| length | series int | Number of bars (length). |
Returns
Simple moving average of source for length bars back.
Remarks
na values in the source series are ignored.
Code Examples
//@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.
Related Pine Script Functions
ta.ema() - Exponential Moving Average
Learn how to use ta.ema() in Pine Script. Syntax, parameters, code examples, and trading applications for the Exponential Moving Average function.
ta.wma() - Weighted Moving Average
Learn how to use ta.wma() in Pine Script. Syntax, parameters, code examples, and trading applications for the Weighted Moving Average function.
ta.rma() - Relative Moving Average
Learn how to use ta.rma() in Pine Script. Syntax, parameters, code examples for Wilder's smoothing method used in RSI calculation.
ta.hma() - Hull Moving Average
Learn how to use ta.hma() in Pine Script. Syntax, parameters, code examples for the fast, low-lag Hull Moving Average.
ta.vwma() - Volume Weighted Moving Average
Learn how to use ta.vwma() in Pine Script. Syntax, parameters, code examples for the Volume Weighted Moving Average.