How to Calculate the SMMA in Pine Script: Explained + Code Generator
Struggling with the Smoothed Moving Average (SMMA) in TradingView? This guide breaks down exactly how to calculate the SMMA in Pine Script. Use our simple generator below to create custom, error-free code for your indicators and strategies instantly. Master the `ta.smma` function and level up your trading scripts.
Note: This generated code is a basic indicator template. Always test scripts thoroughly before using them in a live trading environment. Read our disclaimer for more information.
Understanding the Smoothed Moving Average (SMMA)
The Smoothed Moving Average (SMMA) is a technical indicator that provides a clearer view of price trends by reducing market noise. Unlike a Simple Moving Average (SMA) that gives equal weight to all prices in its calculation period, the SMMA uses a more sophisticated approach that considers all historical price data.
The SMMA formula is: SMMAi = (SMMAi-1 × (N-1) + Pricei) / N, where N is the length parameter. This recursive calculation means each new SMMA value incorporates the previous SMMA value, creating a smoother line that's less susceptible to short-term price spikes.
The SMMA is closely related to the Exponential Moving Average (EMA) and Relative Moving Average (RMA). In fact, in Pine Script, the ta.smma() function is identical to ta.rma(), both implementing Wilder's smoothing method.
How to Calculate the SMMA in Pine Script Manually and with `ta.smma`
Method 1: Using the Built-in `ta.smma()` Function (Recommended)
//@version=5
indicator("Simple SMMA", overlay=true)
length = input.int(14, title="Length")
src = input.source(close, title="Source")
// Calculate SMMA using built-in function
smma_value = ta.smma(src, length)
// Plot the result
plot(smma_value, title="SMMA", color=color.blue, linewidth=2)Method 2: Manual Calculation (Educational Purpose)
//@version=5
indicator("Manual SMMA", overlay=true)
length = input.int(14, title="Length")
src = input.source(close, title="Source")
// Manual SMMA calculation
var float smma = na
if bar_index == length - 1
smma := ta.sma(src, length) // Initialize with SMA
else if not na(smma[1])
smma := (smma[1] * (length - 1) + src) / length
plot(smma, title="Manual SMMA", color=color.red, linewidth=2)Practical Example: Plotting the SMMA on a Chart
Here's a complete, copy-paste indicator script that plots the SMMA on your TradingView chart with customizable parameters and visual enhancements:
//@version=5
indicator("Professional SMMA Indicator", shorttitle="Pro SMMA", overlay=true)
// === INPUT PARAMETERS ===
length = input.int(14, title="SMMA Length", minval=1, maxval=500)
src = input.source(close, title="Source")
smma_color = input.color(color.new(color.blue, 0), title="SMMA Color")
show_fill = input.bool(true, title="Show Fill Area")
// === CALCULATIONS ===
smma_value = ta.smma(src, length)
// === PLOTTING ===
smma_plot = plot(smma_value, title="SMMA", color=smma_color, linewidth=2)
// Optional: Add fill between price and SMMA
if show_fill
price_plot = plot(src, color=color.new(color.white, 100), display=display.none)
fill_color = src > smma_value ? color.new(color.green, 85) : color.new(color.red, 85)
fill(price_plot, smma_plot, color=fill_color, title="Price vs SMMA Fill")
// === ALERTS ===
alertcondition(ta.crossover(src, smma_value), title="Price Crosses Above SMMA", message="Price has crossed above the SMMA")
alertcondition(ta.crossunder(src, smma_value), title="Price Crosses Below SMMA", message="Price has crossed below the SMMA")How to use: Copy the code above, open TradingView, go to Pine Editor, paste the code, and click "Add to Chart". You can customize the length, source, and colors in the indicator settings.