Home » Technical Analysis Functions » Understanding the ta.sma Function in Pine Script

Understanding the ta.sma Function in Pine Script

Photo of author
Published on

Pine Script is the unique scripting language developed by TradingView for crafting technical analysis indicators and strategies. Among its myriad functions, one that is crucial for traders is the Simple Moving Average (SMA) function, represented in Pine Script as ta.sma.

What is a Simple Moving Average (SMA)?

The Simple Moving Average (SMA) is a popular technical analysis tool that smoothens out data by creating a constantly updated average price. In simpler terms, the SMA provides an average of a stock’s price over a specific period.

Syntax and Arguments of ta.sma

ta.sma(source, length) → series float

Arguments:

  • source (series int/float): This refers to the series of values that will be processed. For instance, it can be the closing prices (close) of a stock over a specific number of bars.
  • length (series int): Represents the number of bars, or the length, for which the SMA is calculated.

Basic Example

To understand how this function works in practice, let’s break down a basic example:

//@version=5
indicator("ta.sma")
plot(ta.sma(close, 15))
  • @version=5: This specifies that we’re using Pine Script version 5.
  • indicator("ta.sma"): This sets the name of the indicator on the chart.
  • plot(ta.sma(close, 15)): Here, we’re plotting the SMA of the close prices of the last 15 bars.

Pine Script Built-in vs. Custom SMA Calculation

Interestingly, you can also calculate the SMA using a more manual approach in Pine Script, although it’s 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))

Here:

  • pine_sma(x, y) =>: We define a custom function to calculate SMA.
  • Inside the function, a loop runs from 0 to y-1 (total bars minus one), accumulating the sum of the past y values of x (source values like close prices), and then divides it by y to get the average.
  • Finally, we plot the SMA using our custom function.

Returns and Remarks

The ta.sma function, after processing, will return the Simple Moving Average of the source for length bars back.

Important Remark: Any na values within the source series are ignored. This ensures that the SMA computation remains accurate without incorporating ‘Not Available’ values.

Unique Use-Case: Using ta.sma for Cross-Over Strategy

One popular trading strategy is the Moving Average Cross-Over. Let’s craft a basic example using ta.sma:

//@version=5
indicator("SMA Cross-Over Strategy", overlay=true)

short_period = 9
long_period = 21

short_sma = ta.sma(close, short_period)
long_sma = ta.sma(close, long_period)

plot(short_sma, color=color.red)
plot(long_sma, color=color.blue)

buy_signal = ta.crossover(short_sma, long_sma)
sell_signal = ta.crossunder(short_sma, long_sma)

plotshape(series=buy_signal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=sell_signal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")
 ta.sma Function in Pine Script
  • We have defined two SMA periods: short (9) and long (21).
  • We then plot both SMAs on the chart in different colors.
  • Using the ta.crossover and ta.crossunder functions, we can detect when the short SMA crosses over the long SMA (buy signal) and when it crosses under (sell signal).
  • Finally, we plot these buy and sell signals on the chart using plotshape.

Key Takeaway

The ta.sma function in Pine Script allows traders and developers to efficiently compute the Simple Moving Average on their charts, aiding in various technical analysis strategies. While Pine Script provides a built-in function, understanding its manual computation deepens one’s grasp of its workings and potential applications.

Conclusion

Mastering the ta.sma function and its applications can significantly boost a trader’s technical analysis toolkit. Whether it’s for simple price tracking or complex strategies like the Moving Average Cross-Over, this function remains indispensable in the world of Pine Script.

Leave a Comment