You seem to want to look forward if k1 is true earlier than k4.
Data used:
Date,Time,L,H,Aux1,Aux2
2019-01-01,09:00,900,910,0,0
2019-01-01,09:01,888,895,1,0
2019-01-01,09:02,905,915,0,0
2019-01-01,09:03,880,890,0,0
2019-01-01,09:04,870,877,0,1
2019-01-01,09:05,860,855,0,0
2019-01-01,09:06,850,866,1,0
2019-01-01,09:07,810,822,0,1
/// what is it about:
/// @link https://forum.amibroker.com/t/how-to-create-this-array/15240/2
k1 = Aux1;
k4 = Aux2;
k1L = ValueWhen(k1, L);
k4H = ValueWhen(k4, H);
rev_k4h = Reverse(Nz(k4h));// reverse k4h array
cond = Reverse(Nz(k4));// reverse k4
val_when = ValueWhen(cond, rev_k4h);
k4h_rev = Reverse(val_when);// reverse back
// marks 877 etc. at k1 > 0 and k4 == 0
//-> looking forward for k4 being true then
result1 = IIf(k1 AND !k4, k4h_rev, 0);
//
// marks 877 etc. at k1 > 0 and k4 > 0 at same bar
// -> not looking forward then
result1 = IIf(k1 AND k4, k4h, result1);
//
// keeps 877 etc. since k1 > 0
result2 = Nz(ValueWhen(k1>0, result1));
Plot( result1, "result1", colorOrange, styleHistogram);
Plot( result2, "resul2", colorRed);
Upper code seems to work if k4 is true after k1 being true (then it looks forward for k4H value if k4 is true) and it works if k1 and k4 are true at same bar (then it stores k4H at k1 == 1 of same bar).
See picture with different k1 == true positions
if Data 1

if Data 2
