TA.Crossover in Pine Script: Detecting Moving Average Crossovers
The ta.crossover function in Pine Script is a powerful tool used to detect when one data series crosses over another, which is a common signal in trading strategies. It returns true on the bar where the first series moves from being below or equal to the second series to being above it. This makes it ideal for identifying bullish crossovers, such as when a short-term moving average crosses above a long-term moving average, signaling potential upward momentum.
What is ta.crossover in Pine Script?
- Definition:
ta.crossover(source1, source2)
returns a boolean indicating ifsource1
has crossed oversource2
on the current bar. - Use Case: Commonly used to detect moving average crossovers, which traders interpret as buy or sell signals.
- Syntax:
ta.crossover(source1, source2) → series bool
- Example: Detecting when a 14-period SMA crosses above a 50-period SMA:
shortMA = ta.sma(close, 14)
longMA = ta.sma(close, 50)
bullishCrossover = ta.crossover(shortMA, longMA)

How to Use ta.crossover Effectively
- Plotting Signals: You can use
plotshape()
to visually mark crossover points on your chart, helping you quickly spot key trading signals. - Conditional Logic: Combine
ta.crossover
with other conditions to refine your strategy, such as confirming that the crossover aligns with other technical indicators or price levels. - Avoid Multiple Triggers: Since
ta.crossover
triggers only once per crossover event, it prevents repeated signals on consecutive bars, which helps reduce noise in your strategy.
Sample Code to Highlight a Bullish Crossover
//@version=5
indicator("Moving Average Crossover Example", overlay=true)
shortMA = ta.sma(close, 14)
longMA = ta.sma(close, 50)
bullishCrossover = ta.crossover(shortMA, longMA)
plot(shortMA, color=color.red, title="14-period SMA")
plot(longMA, color=color.blue, title="50-period SMA")
plotshape(bullishCrossover, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, text="Bullish")
This script plots two moving averages and marks the bullish crossover with a green label below the bar, making it easy to spot potential buy signals.
The ta.crossover
function is essential for traders and Pine Script developers aiming to build effective, clear, and actionable trading indicators. By mastering this function, you can create strategies that pinpoint critical market moments like moving average crossovers, helping you make better-informed trading decisions.