There are several topics about detecting lows and highs backwards from the current bar, for instance:
However, I did not find the answer to my question from these.
The test data:
15 13 12 13 6 4 5 3 7 11 15 12 10 8 9 10 11
I would like to find out the first preceding bar that is less than 0.9 x of the current bar LOW_1, then the first bar larger than LOW_1 preceding LOW_1, let us call it HIGH_1 and so on. The first part is easy with the new good function:
_low_bar_1 = BarsSinceCompare(C, "<", C*0.9);
That is 2 for the last bar (11), meaning that the LOW_1 is 2 bars back, which is true (9). After that, I initially thought that the HIGH_1 could be found as following:
_high_bar_1 = BarsSinceCompare(Ref(C, -_low_bar_1), ">", Ref(C, -_low_bar_1));
As far as I understand, Ref(C, -_low_bar_1)
should be the C array shifted to the right by 2 and therefore, the _high_bar_1 value for the last bar should be 2 as well, and the final answer 2+2=4 (the previous value larger than 9 is 10 in the test data, occupying the 4th position if we count backwards).
However, for some reason I do not comprehend, the value of high_bar_1 is 6!
_low_bar_1 = BarsSinceCompare(C, "<", C*0.9);
_high_bar_1 = BarsSinceCompare(Ref(C, -_low_bar_1), ">", Ref(C, -_low_bar_1));
Plot(_low_bar_1, "low 1", colorRed);
Plot(_high_bar_1, "high 1", colorGreen);
Could anybody please explain where am I mistaken. Why does high_bar_1 = 6? What should I do differently to achieve my goal?