Find first and last bar within two price values

Modified post:
I am working on tick bars. Within my afl code, I have retrieved the start bar of a set of bars as 57544 and end bar as 64634. In this set of bars, I find the lowest price is 11855.4 and highest price is 11936. Within these bars, I have marked the lower bound price as 11858.3 and upper bound price as 11872.8.

What I am trying to find is the following:

  1. First bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8
  2. Last bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

I tried from my end but without success. Let me put some partial work.

BarIdxArr = BarIndex();
priceBoundArr = IIf(Close >= lowerBound AND Close <= upperBound, 1, 0);
firstBar = ValueWhen(priceBoundArr, BarIdxArr);

Original post:
I have a set of bars with the startBar and endBar values. Within this set of bars, I am trying to find the first bar and last bar of all the bars that lie within two price values.

Could you expand on your query or precisely depict your intentions?

Are you referring to:

  • BeginValue - Value of the array at the begin of the range (AFL 2.3)
  • EndValue - value of the array at the end of the selected range (AFL 2.3)
1 Like

@Cougar My apologies for not being clear.

I tried searching but didn't find it on the forum. I did go through the following link Bars since higher or equal value, but it didn't help.

Let me put the question properly.
I am working on tick bars. Within my afl code, I have retrieved the start bar of a set of bars as 57544 and end bar as 64634. In this set of bars, I find the lowest price is 11855.4 and highest price is 11936. Within these bars, I have marked the lower bound price as 11858.3 and upper bound price as 11872.8.

What I am trying to find is the following:

  1. First bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8
  2. Last bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

I tried from my end but without success. Let me put some partial work.

BarIdxArr = BarIndex();
priceBoundArr = IIf(Close >= lowerBound AND Close <= upperBound, 1, 0);
firstBar = ValueWhen(priceBoundArr, BarIdxArr);

Something like this might help:

bi = BarIndex();

BarRng = /*Your condition for picking bar range from 57544 to 64634*/;
//For example:
//BarRng = bi >= BeginValue( bi ) && bi <= EndValue( bi );
PriceRng = C >= lowerBound  && C <= upperBound;
RngCond = BarRng && PriceRng;

NumBars = Cum( RngCond );
firstBar = ValueWhen( NumBars == 1 && RngCond, bi );
lastBar = ValueWhen( NumBars == LastValue( NumBars ) && RngCond, bi );

(Untested)

1 Like

Thanks @Cougar, I was able to get the desired result with your help. The only thing, I had to add was LastValue for the output from ValueWhen.

Find below my code to get the first and last bars. startBar and endBar are populated within afl depending on the bars selected.

bi = BarIndex();

rangeArr = bi >= startBar AND bi <= endBar AND C >= lowerBound AND C <= upperBound;

rangeCum = Cum(rangeArr);
firstBar = LastValue(ValueWhen(rangeCum == 1 AND rangeArr, bi));
lastBar = LastValue(ValueWhen(rangeCum == LastValue(rangeCum) AND rangeArr, bi));
1 Like