Line that crosses the less bars


I am trying to find a quick (see array operations) way to find the horizontal line that crosses the less amount of bars, within a range of bars and within a range of prices. It is very easy to spot by eye, I want to make it an algo.

More or less I know where those are (close to Support/resistance, but I need to take care of breakouts too), so I could make a small loop and step 1-2 pips and examine that line. I want to avoid this.

Anybody has an idea how to do this with arrays?


to be honest, your post isn't precise and the points are ambiguos.

Thanks for the feedback. Here are some clarifications:

The red line is supposed to be a support that turned into resistance and it was again crossed upwards by the last white bar.

The red line touches 4 tails: 3 low tails and one high tail. It also crosses 2 bars, one black bar and one white (bullish) bar.

If I move the red line up, it will touch/cross many bars that belong to that congestion. I do not want this. If I move it down, it will cross those bars to the lower right. I also do not want this.

I drew this bar by hand. I want to find an automated way to calculate it.

Hi @fxshrat

I am not looking to count the crosses of a predefined line. I want to find the price value where the horizontal line will have the less crosses. ie I want the algo to determine where is the support/resistance line.

Most of the time the line(s) with the less crosses of bars is/are the line(s) near highest high or lowest low of a (chart) range (if there is single lowest/highest value).

Don't you see it? Each range has a highest high or lowest low. That's the one(s).

(If there would be more same lowest/highest values side by side then it would/could turn out differently)


If I wanted the highest/lowest values I would not have asked in the forum. I am not interested in the "bingo" lines. I want the "nope" line automatically adjusted to the optimal price value where it has the less crosses.

The key is in my initial post:

within a range of prices will not necessarily be at the high/low of the chart. It will be close to a support/resistance level that was breached.

Who knows what you want. No one does (except you).

If you want... then start coding!
Do something instead of just waiting.
You have to iterate vertically between lowest and highest value of a range of bars. There is no vertical array from price x to price y but just horizontal array from bar x to bar y.

Within a range of bars there always is at least one highest and lowest! And if you look for less then most of the time it will be 1 cross. So you would have to work with minimum threshold being higher than that if you don't like that.

Yes, I thought so. Thanks for confirming it cannot be done with arrays.