Simple Moving Average

Simple moving averages are simple the average of the stock price calculated backwards. I mean, the last “n” values of the stock price according to used time frame are added and averaged. The average is plotted at each point and represents current and also past value averages.

“n“ is the number of data points in the past taken into calculating average. “n“ is a variable and can range from 2 to any positive number.

SMA doesn’t give more weight to recent prices. All prices are considered equal in calculating sma.

//@version=6
indicator("Simple Moving Average (SMA)", overlay=true)

// Input for the length of the SMA
// 'defval' sets the default value to 20 periods.
// 'minval' sets the minimum allowed value to 1.
// 'title' provides a user-friendly label for the input.
smaLength = input.int(defval=9, minval=1, title="SMA Length")

// Calculate the Simple Moving Average (SMA)
// 'ta.sma(source, length)' calculates the SMA of the 'source' series
// over the specified 'length'.
// 'close' is used as the default source, representing the closing price of each bar.
smaValue = ta.sma(close, smaLength)

// Plot the SMA on the chart
// 'plot' draws the calculated SMA value on the chart.
// 'color' sets the color of the plot (here, blue).
// 'title' provides a label for the plotted line in the indicator settings.
// 'linewidth' sets the thickness of the plotted line.
plot(smaValue, color=color.blue, title="SMA", linewidth=2)