A moving average is probably the first indicator everyone codes in Pine Script. On TradingView, the three most used ones come as built-in functions: ta.sma (simple), ta.ema (exponential) and ta.wma (weighted). This guide shows the syntax of each one, when it makes sense to use which, and includes ready-to-paste code for you to drop into the editor and adapt — including an honest crossover signal, with no promise of easy profit.

Already have the signal on the chart and want to turn it into real automation?

See how to connect signals to Python →

Syntax of the three functions

They all follow the same pattern: you pass the source (usually close) and the period (the famous “length”). They return a series, meaning a new value on every bar.

//@version=5 indicator(“Médias Móveis”, overlay=true) length = input.int(20, “Período”) mediaSimples = ta.sma(close, length) // SMA mediaExponencial = ta.ema(close, length) // EMA mediaPonderada = ta.wma(close, length) // WMA plot(mediaSimples, “SMA”, color=color.blue) plot(mediaExponencial, “EMA”, color=color.orange) plot(mediaPonderada, “WMA”, color=color.green)

Note the overlay=true: it makes the lines appear on top of the price, not in a separate pane. Without it, the averages would sit outside the chart’s scale.

What is the difference between SMA, EMA and WMA?

All three smooth the price, but they distribute the “weight” of each bar differently. The SMA treats all bars equally — it is the most stable and the one that lags the most. The EMA gives more weight to recent bars, reacting faster to changes (and generating more noise). The WMA also prioritizes recent data, with a linear decay, sitting between the two in terms of speed.

Practical summary: want stability and fewer false signals? SMA. Want to react fast to reversals? EMA. Want a middle ground? WMA. None of them is “better” — it depends on the asset, the timeframe and your strategy.

Ready code: moving average crossover with alert

The classic use is the crossover of two averages with different periods. When the fast one crosses above the slow one, it is a bullish signal; below, a bearish one. Here is how to build it with ta.crossover and ta.crossunder and also fire an alert:

//@version=5 indicator(“Cruzamento de EMAs”, overlay=true) rapidaLen = input.int(9, “EMA rápida”) lentaLen = input.int(21, “EMA lenta”) emaRapida = ta.ema(close, rapidaLen) emaLenta = ta.ema(close, lentaLen) plot(emaRapida, “EMA rápida”, color=color.aqua) plot(emaLenta, “EMA lenta”, color=color.fuchsia) compra = ta.crossover(emaRapida, emaLenta) venda = ta.crossunder(emaRapida, emaLenta) plotshape(compra, title=”Compra”, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) plotshape(venda, title=”Venda”, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small) if compra alert(“Cruzamento de ALTA”, alert.freq_once_per_bar_close) if venda alert(“Cruzamento de BAIXA”, alert.freq_once_per_bar_close)
Watch out for repaint: use alert.freq_once_per_bar_close and evaluate signals at the bar close. Signals that show up mid-candle can vanish when it closes, creating an illusion of accuracy.

ta.ma: choosing the type dynamically

If you want to make the moving average type selectable by the user, you can use input.string with a conditional. It is cleaner than duplicating code:

tipo = input.string(“EMA”, “Tipo”, options=[“SMA”,”EMA”,”WMA”]) len = input.int(20, “Período”) media = switch tipo “SMA” => ta.sma(close, len) “EMA” => ta.ema(close, len) “WMA” => ta.wma(close, len) plot(media, “Média”, color=color.yellow)

FAQ

Which period should I use?
There is no magic number. 9/21 and 50/200 are popular, but the ideal setting depends on the asset and the timeframe. Test it on historical data before trusting it.

Can I apply the average to the RSI instead of the price?
Yes. Just pass the desired series as the source, for example ta.sma(ta.rsi(close, 14), 9).

Is a moving average crossover a profitable strategy?
It is a didactic starting point, not a ready-made system. Averages lag by nature and generate many false signals in sideways markets. Treat them as a filter, not as a standalone trigger.

Does it work in Pine Script v6?
Yes. ta.sma, ta.ema and ta.wma are still available; just adjust the version tag at the top of the script.

Disclaimer: binary options and leveraged trading are extremely high-risk products and you can lose all the capital you invest. This content is educational and does not constitute an investment recommendation, an offer or a guarantee of results. Indicators and moving average crossovers do not predict the future. Always test on a demo account before trading with real money.

Similar Posts