Yes, there was a problem.
IMO, a code has to work on both, arrays and picking elements of that array (LastValue, SelectedValue, .., subscripts). Once you have array you can still get elements later. Advantage of working array version is that it works for picking any element.
The upper two ones work on choosing specific element only.
Now after breakfast here is working one (works on array as well as picking any element of array).
BTW, @travick you can not just set some random value to SetBarsRequired() without thinking. You have to calculate SetBarsRequired value based on actual requirement of your formula. What if you look back 5 days from 1 minute interval and you have zoomed in to just last day? Right, it will not pick proper looked for value by using 500.
So actual line should be like
SetBarsRequired(daysback*86400/Max(1,Interval()));
Anyway here is whole code (picking datetime, barindex, timenum)
/// @link https://forum.amibroker.com/t/barindex-and-timenum-of-day-before-yesterdays-hhv-volume-bar-in-5-min-tf/10949/7
function PrevArray(Cond, searchedarray, daysback, expandmode) {
arr = Iif(Cond, searchedarray, -1);
to_daily = TimeFrameCompress(arr, inDaily, compressHigh);
to_daily = TimeFrameExpand(Ref(to_daily,-daysback), inDaily, expandFirst);
return to_daily;
}
/// code supposed to be for INTRADAY intervals
bi = BarIndex();
dt = DateTime();
hh_vol = TimeFrameCompress(V, inDaily, compressHigh);
hh_vol = TimeFrameExpand(hh_vol, inDaily, expandFirst);
vol_equal = Almostequal(hh_vol, V);
dt_prev = PrevArray(vol_equal, dt, daysback = 2, expandmode = expandfirst);
printf( DateTimeToStr(SelectedValue(dt_prev)) );
bi_prev = PrevArray(vol_equal, bi, daysback, expandmode);
printf( "\nBi: %g", SelectedValue(bi_prev) );
tn_prev = PrevArray(vol_equal, Timenum(), daysback, expandmode);
printf( "\nTimenum: %g", SelectedValue(tn_prev) );
Plot( V, "V", colorDefault, styleHistogram, 0, 1, 0, 2);
Plot( hh_vol, "V", colorRed, styleHistogram );
Plot( vol_equal, "V", colorOrange, styleHistogram | styleOwnScale, 0, 1, 0, 1 );
Plot( BarIndex(), "BI", colorDefault, styleOwnScale );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
// if you want to decrease bars (TimeFrameCompress deactivates QuickAFL)
// http://www.amibroker.com/kb/2008/07/03/quickafl/
SetBarsRequired(daysback*86400/Max(1,Interval()));
