Moving average crossovers, RSI leaving oversold, MACD turning — almost every trading strategy depends on detecting a cross between two series. In Pine Script v5/v6 (2026), that is solved with two built-in functions: ta.crossover and ta.crossunder. This guide shows the exact syntax, ready-to-paste examples for TradingView and the most common mistakes that make a signal “repaint” or fire at the wrong time.

Want to turn these crossover signals into automated trades? Check out our open-source Python bot.

See the bot (Quotex API in Python) →

What each function does

Both functions return a boolean value (true/false) evaluated on every bar:

  • ta.crossover(a, b) → returns true on the bar where series a crosses above b (it was below or equal before, now it is above).
  • ta.crossunder(a, b) → returns true on the bar where a crosses below b.

Note that the cross is a one-off event: it is true only on the exact bar of the cross, not while a stays above b. For that distinction there is the generic function ta.cross(a, b), which detects a cross in either direction.

Basic syntax

//@version=6 indicator(“Cruzamento de Medias”, overlay=true) fast = ta.sma(close, 9) slow = ta.sma(close, 21) compra = ta.crossover(fast, slow) // fast MA crosses above the slow one venda = ta.crossunder(fast, slow) // fast MA crosses below the slow one plot(fast, color=color.aqua) plot(slow, color=color.orange) 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)
Important: the arguments can be a series and a fixed value. For example, ta.crossover(ta.rsi(close,14), 30) fires when the RSI rises and crosses the 30 level — useful for detecting an exit from oversold.

Example with RSI

//@version=6 indicator(“RSI Cruzamentos”) rsi = ta.rsi(close, 14) saiuSobrevenda = ta.crossover(rsi, 30) saiuSobrecompra = ta.crossunder(rsi, 70) plot(rsi, color=color.purple) hline(30, color=color.green) hline(70, color=color.red) bgcolor(saiuSobrevenda ? color.new(color.green, 80) : na) bgcolor(saiuSobrecompra ? color.new(color.red, 80) : na)

Firing alerts on the cross

To automate (including via webhook), combine the cross with alertcondition or alert():

compra = ta.crossover(ta.ema(close,9), ta.ema(close,21)) if compra alert(“Sinal de COMPRA: EMA9 cruzou EMA21 para cima”, alert.freq_once_per_bar_close) alertcondition(compra, title=”Compra EMA”, message=”Cruzamento de alta detectado”)
Avoid repaint: use alert.freq_once_per_bar_close and base decisions on the bar close. Crosses evaluated while the bar is still forming can appear and disappear (repaint) before the close, generating false signals.

Common mistakes

  • Confusing it with a simple comparison: fast > slow is true on every bar where the fast average is above; ta.crossover is true only on the bar of the cross.
  • Argument order: ta.crossover(a, b)ta.crossover(b, a). Direction matters.
  • Forgetting the close: intrabar signals repaint. Confirm at the close for realistic backtests.
  • Using it on series with na: on the first candles the average can be na and the cross does not fire — that is expected.

FAQ

What is the difference between ta.cross and ta.crossover?
ta.cross detects a cross in either direction; ta.crossover only upward and ta.crossunder only downward.

Does it work in Pine Script v5 and v6?
Yes. In v5/v6 use the ta. prefix. In old scripts (v3/v4) it was just crossover() without the namespace.

Can I compare a series with a fixed number?
Yes, as in ta.crossover(ta.rsi(close,14), 50).

Do these signals work for binary options?
They work as a strategy trigger, but a cross on its own generates many false signals. Combine it with a trend filter and always test on a demo account.

Disclaimer: binary options are extremely high-risk products and can result in the total loss of your capital. This content is educational and technical; it does not constitute an investment recommendation or financial advice. Indicators and crosses do not guarantee results. Always test on a demo account before trading with real money.

Similar Posts