Home » Technical Analysis Functions » ta.barssince Function in Pine Script

ta.barssince Function in Pine Script

Photo of author
Published on

One of the interesting functions available in Pine Script is the ta.barssince function. This function plays a crucial role in trading algorithms and strategies, allowing users to count the number of bars since a specific condition was met.

In this tutorial, we’ll explore the ta.barssince function in depth, understand its syntax, go through an example, and finally look at a unique use case.

Syntax

The syntax of ta.barssince function is:

ta.barssince(condition) → series int

Here, the condition refers to a logical statement that is evaluated for each bar in the series.

Returns

The function returns the number of bars since the condition was true.

Remarks

If the condition has never been met prior to the current bar, the function returns na. It’s also important to be aware that using this function can cause indicator repainting.

Basic Example

Let’s start by understanding a basic example of ta.barssince:

//@version=5
indicator("ta.barssince")
// get number of bars since last color.green bar
plot(ta.barssince(close >= open))
ta.barssince Function in Pine Script example

Here, we are plotting the number of bars since the last time the closing price was greater than or equal to the opening price.

Unique Use Case: Detecting a Reversal

In this section, we will look at a unique and different use case. Let’s say you want to detect a reversal pattern based on moving averages and plot the number of bars since this pattern occurred.

Code Example

//@version=5
indicator("ta.barssince")
// get number of bars since last color.green bar
plot(ta.barssince(close >= open))
ta.barssince Function in Pine Script

Line-by-line Explanation

  • maShort = ta.sma(close, 10): Calculates the 10-period simple moving average.
  • maLong = ta.sma(close, 30): Calculates the 30-period simple moving average.
  • reversalCondition = maShort > maLong and maShort[1] <= maLong[1]: Checks if a reversal pattern has occurred.
  • plot(ta.barssince(reversalCondition)): Plots the number of bars since the last reversal pattern was detected.

Key Takeaway

The ta.barssince function in Pine Script is an essential tool for traders and algorithmic systems. It allows for tracking how many bars have passed since a specific condition or pattern was met. Its flexibility can be harnessed to detect various market conditions, making it a powerful function in a trading toolkit.

Conclusion

Understanding the ta.barssince function provides traders and developers with a way to create more responsive and informed strategies. By knowing how to apply this function, one can enhance their trading algorithms to recognize key patterns and make better-informed decisions. Happy trading!

1 thought on “ta.barssince Function in Pine Script”

  1. Thank you for the explanation – how can I use this to calculate bars since market open? For example, I don’t want to execute a strategy until at least 5 bars since market open.

    Reply

Leave a Comment