'How to find bars with condition

I want to find bar (bar index) which meets some condition related to the current bar. For example, among all bars which minimum prices are greater than current minimum price (yellow rectangle on the illustrations) I want to get bar with minimum (or maximum) price difference with current bar (blue arrows).

I tried to use ta.barssince() and similar funtions. But it seems to me that some sort of "series of series" types is needed to do what I want.

Is it possible to do such things with pine script? Or is using loops the only way?

Illustration 1 Illustration 2



Solution 1:[1]

I've found the answer to my question in the pine script documentation:

Loops exist for good reason because even in Pine, they are necessary in some cases. These cases typically include:

  • The manipulation of arrays.
  • Looking back in history to analyze bars using a reference value that can only be known on the current bar, e.g., to find how many past highs are higher than the high of the current bar. Since the current bar’s high is only known on the bar the script is running on, a loop is necessary to go back in time and analyze past bars.
  • Performing calculations on past bars that cannot be accomplished using Pine’s built-in functions, like the Pearson correlation coefficient.

The second case is just mine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 svaor