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

Understanding ta.change() Function in Pine Script

Photo of author
Published on

This article delves into the syntax, overloads, and practical applications of ta.change(), illustrated with an example that tracks daily bars and color changes in trading charts.

Syntax & Overloads

The ta.change() function comes with six overloads, allowing it to handle different data types and scenarios:

  1. ta.change(source) → series int
  2. ta.change(source) → series float
  3. ta.change(source, length) → series int
  4. ta.change(source, length) → series float
  5. ta.change(source) → series bool
  6. ta.change(source, length) → series bool

The source argument accepts a series of integer, float, or boolean values. The optional length argument specifies the number of bars ago to compare with the current value. When length is omitted, the function compares the current value to its previous value.

Arguments Explained

  • source (series int): The data series to analyze. It can be of type integer, float, or boolean.

Practical Example: Day and Direction Change Indicator

The following example demonstrates how to use ta.change() to create an indicator that highlights the start of a new day and changes in the direction of price bars on a trading chart.

//@version=5
indicator('Day and Direction Change Indicator', overlay = true)
dailyBarTime = time('1D')
isNewDay = ta.change(dailyBarTime)
bgcolor(isNewDay ? color.new(color.green, 80) : na)

isGreenBar = close >= open
directionChange = ta.change(isGreenBar)
plotshape(directionChange, 'Direction Change')
Example

Walkthrough

  • Daily Bar Time Detection: dailyBarTime = time('1D') captures the time of each daily bar.
  • New Day Detection: isNewDay = ta.change(dailyBarTime) checks if the time of the current bar differs from the previous bar, indicating a new day.
  • Background Color: bgcolor(isNewDay ? color.new(color.green, 80) : na) changes the background color to a translucent green at the start of a new day.
  • Direction Change Detection: isGreenBar = close >= open determines if the current bar is green (closing price is higher than the opening price).
  • Plot Direction Change: plotshape(directionChange, 'Direction Change') plots a shape whenever there is a change in the direction of price bars.

Key Features and Takeaways

  • Flexibility: ta.change() supports integer, float, and boolean series, making it adaptable to various data analysis needs.
  • Customization: The function allows for the comparison of current values with those from a specified number of bars ago, offering tailored analysis.
  • Practical Application: It is particularly useful in financial chart analysis for detecting shifts in market trends or identifying the beginning of new time periods.
  • Bool Handling: When applied to boolean values, ta.change() helps in event detection, such as the transition from a bearish to bullish market (or vice versa).

In summary, ta.change() is a powerful function in Pine Script for tracking differences in data points, offering insights into market dynamics and aiding in the development of sophisticated trading indicators.

Leave a Comment