Home » Technical Analysis Functions » Understanding ta.cross() Function in Pine Script

Understanding ta.cross() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the syntax, usage, and practical applications of ta.cross() to enhance your trading strategies with Pine Script.

Syntax of ta.cross()

The syntax for ta.cross() in Pine Script is as follows:

ta.cross(series1, series2) → series bool
  • series1 (series int/float): The first data series you’re analyzing. This could be a moving average, a price series like close, or any other numerical series.
  • series2 (series int/float): The second data series for comparison. Similar to series1, this can be any numerical series within your chart.

The function returns a series bool (boolean series), where the value is true at the point where series1 crosses over series2, indicating a crossover event. Otherwise, the value is false.

Understanding the Arguments

The ta.cross() function accepts two primary arguments:

  • source1: This represents the first data series in your analysis. It’s essential to ensure this series is numerical, as it’s being compared to another series for crossing points.
  • source2: The second data series, against which source1 is compared. The function checks each point on your chart to determine if source1 has crossed over source2.


The function yields a boolean series, with true signifying that a cross has occurred at a specific bar, and false indicating no cross.

Practical Example

To illustrate the use of ta.cross(), let’s consider a simple moving average (SMA) crossover strategy:

indicator("Simple MA Crossover", overlay=true)

// Define moving averages
fastMA = ta.sma(close, 9)
slowMA = ta.sma(close, 21)

// Detect crossover
crossover = ta.cross(fastMA, slowMA)

// Plotting
plot(fastMA, color=color.red)
plot(slowMA, color=color.blue)
bgcolor(crossover ? color.new(color.green, 90) : na)

Walkthrough of Code

  1. //@version=5
    • This line specifies the version of Pine Script being used. Version 5 is the latest, offering the most up-to-date features and functionalities of the language.
  2. indicator("Simple MA Crossover", overlay=true)
    • This line declares a new indicator named “Simple MA Crossover”. The overlay=true parameter means that this indicator will be plotted directly on the price chart, allowing for easy visualization of the moving averages in relation to the price action.
  3. // Define moving averages
    • This comment line introduces the section where moving averages are defined, serving as a clear demarcation for code organization.
  4. fastMA = ta.sma(close, 9)
    • Defines the “fast” moving average (fastMA) by calculating the simple moving average (SMA) of the closing prices (close) over the last 9 periods. The fast moving average is more responsive to recent price changes.
  5. slowMA = ta.sma(close, 21)
    • Defines the “slow” moving average (slowMA) by calculating the SMA of the closing prices over the last 21 periods. The slow moving average is less responsive to price changes, providing a smoother line that tracks the longer-term trend.
  6. // Detect crossover
    • This comment indicates the section of the code that deals with detecting the crossover between the fast and slow moving averages.
  7. crossover = ta.cross(fastMA, slowMA)
    • This line creates a boolean series named crossover that returns true at bars where the fastMA crosses over the slowMA. This is used to identify potential trading signals when the shorter-term trend (fastMA) overtakes the longer-term trend (slowMA).
  8. // Plotting
    • This comment signifies the beginning of the code section dedicated to plotting the moving averages and highlighting crossover points on the chart.
  9. plot(fastMA, color=color.red)
    • Plots the fast moving average on the chart with a red line, allowing users to visually track the fastMA in relation to the price and the slowMA.
  10. plot(slowMA, color=color.blue)
    • Plots the slow moving average with a blue line, enabling a visual comparison between the short-term and long-term trends represented by the fastMA and slowMA, respectively.
  11. bgcolor(crossover ? color.new(color.green, 90) : na)
    • This line changes the background color of bars where a crossover occurs to a semi-transparent green, using a conditional statement. If crossover is true (indicating that the fastMA has crossed above the slowMA), the background color for that bar is set to green with 90% transparency. Otherwise, no background color is applied (na).

Key Features and Takeaways

  • Function Usability: ta.cross() is versatile, usable with any numerical series in Pine Script, including price data, moving averages, or custom indicators.
  • Syntax and Application: Its straightforward syntax makes it easy to implement and understand, even for beginners in Pine Script programming.
  • Practical Application: This function is crucial for creating strategies based on crossovers, such as moving average crossovers, MACD line signal crossovers, and more.

By integrating ta.cross() into your Pine Script strategies, you can efficiently identify key crossover events that are indicative of potential trend reversals or confirmations. This function’s simplicity and power make it an indispensable tool in the Pine Script programmer’s toolkit.

Leave a Comment