Get the the bar index of the first backtest/exploration day

Hello, is there a straight way to retrieve the bar index of the first bar used into an exploration?

I've tried using "BeginValue(array)" to workaround, but this work only after manual setting the range into the AA window.

My Idea:
I need to setup an exploration that performs some price calculations relative to the close of the day I start the exploration. To achieve this I'm considering to setup a loop from the bar that the exploration starts and then showing the result using

Filter = Status("LastBarInRange");

as filter to show only the results of the calculation.

bi = BarIndex();
fbr = Status("FirstBarInRange");
first_bar = Valuewhen(fbr, bi);

BeginValue is chart function and retrieves first value of selected range (selected via range markers).

1 Like

Thank you fxshrat, very fast and precious help.

For anyone else that could need to do the same:
I've tried this as it is but the Valuewhen function returns an array instead of an integer or a boolean values. Unfortunately the For loop - I'm gonna use - doesn't accept arrays as part of the statement.

I've tried to force the casting but it doesn't work.

Finally I've managed to give my answer pointing the last element of the array so:

bi = BarIndex();
fbr = Status("FirstBarInRange");
first_bar = Valuewhen(fbr, bi)[BarCount-1];

It is incorrect.

First of all to get number you may use LastValue() function.

bi = BarIndex();
fbr = Status("FirstBarInRange");
first_barindex = Valuewhen(fbr, bi);// array
first_bar = LastValue(first_barindex );// element of array

Secondly you should think about whether you actually require looping. I doubt it.

1 Like

I didn't mind the LastValue function, thank you :+1:

Really I was wondering the same because the calculations are not complex at all. But I'm not very skilled with vectorization yet... For example: how can I calculate the maximum and the minimum value of the closes between the beginning and the ending of the exploration?

I've managed doing this:

bi = BarIndex();
fbr = Status("FirstBarInRange");
lbr = Status("LastBarInRange");
firstBarArray = ValueWhen(fbr, bi);
lastBarArray = ValueWhen(lbr, bi);
firstBar = LastValue(firstBarArray);
lastBar = LastValue(lastBarArray);

top = 0;
bottom = 10^8;

for (i=firstBar; i <= lastBar; i++)
{
	if (C[i] > top){top = C[i];}
	if (C[i] < bottom){bottom = C[i];}
	
}

As I guessed.. you do not need (slower) loop for that.

bir = Status("barinrange");
hh = Highest(IIf(bir, C, 0));
ll = Lowest(IIf(bir, C, 1e9));

Filter = Status("lastbarinrange");
AddColumn(ll, "LL(Close)", 1.2);
AddColumn(hh, "HH(Close)", 1.2);
2 Likes

Wow, brillant! Thank you very much fxshrat. only 6 lines, so elegant. Super! :+1:

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.