Fair Value Gaps Issue

Again I stick in a problem I am unable to solve. In the 2 first exemples I get only the desired result for the last candle, in the 3.case there is no result at all. In most cases I used code written by fxshrat which compiles well, but of course my purpose is to get the indications for all occurences not only for the last one. Here follow the 3 trials I made:


_SECTION_BEGIN("Fair Value Gaps");
function BullRect(i)
{
Offset = 3;
GfxSetZOrder(-5);
GfxSetCoordsMode(1);
GfxSetBkMode(1);
GfxSelectPen(colorLightBlue, 1, 2);
GfxSelectSolidBrush( ColorRGB( 190, 190, 255 ) );
i = Barcount;
lvbi = Status( "lastvisiblebarindex" );
GfxRectangle(i-2, H[i-2], i, L[i-1]); 
}

function BearRect(i)
{
GfxSetZOrder(-5);
GfxSetCoordsMode(1);
GfxSetBkMode(1);
GfxSelectPen(colorLightBlue, 1, 2);
GfxSelectSolidBrush( ColorRGB( 190, 190, 255 ) );
i = Barcount; 
GfxRectangle(i-2, L[i-2], i, H[i-1]); 
}

length = Param("Periods", 20, 1, 300, 1 );
Multiplier=Param("Multiplier", 2.0,!.0, 10.0, 0.1);
MinDist = Param("MinDist", 1.0,0.1, 100.0, 0.1 );
bi = BarIndex();
range = H-L;
AvgRange = MA(range,length);
MinRange = AvgRange*Multiplier;
for(i=2;i<BarCount;i++)
{
FVGBull = range[i-1]>=MinRange AND L[i] - H[i-2] >= MinDist;
FVGBear = range[i-1]>=MinRange AND L[i-2]-H[i] >= MinDist;
xx=SelectedValue(bi[i]);
bull1=Cum(SelectedValue(ValueWhen(FVGBull,xx)));
bear1=Cum(SelectedValue(ValueWhen(FVGBear,xx)));
result = IIf(bull1,BullRect(i),IIf(bear1,BearRect(i),Null));
}
Title = "";
_SECTION_END();

_SECTION_BEGIN("Fair Value Gaps 2");
function BullRect()
{
Offset = 3;
GfxSetZOrder(-5);
GfxSetCoordsMode(1);
GfxSetBkMode(1);
GfxSelectPen(colorLightBlue, 1, 2);
GfxSelectSolidBrush( ColorRGB( 190, 190, 255 ) );
i = Barcount;
lvbi = Status( "lastvisiblebarindex" );
GfxRectangle(i-2, H[i-2], i, L[i-1]); 
}

function BearRect()
{
GfxSetZOrder(-5);
GfxSetCoordsMode(1);
GfxSetBkMode(1);
GfxSelectPen(colorLightBlue, 1, 2);
GfxSelectSolidBrush( ColorRGB( 190, 190, 255 ) );
i = Barcount; 
GfxRectangle(i-2, L[i-2], i, H[i-1]); 
}

length = Param("Periods", 20, 1, 300, 1 );
Multiplier=Param("Multiplier", 2.0,!.0, 10.0, 0.1);
MinDist = Param("MinDist", 1.0,0.1, 100.0, 0.1 );
bi = BarIndex();
lastbar = bi == LastValue(bi);
range = H-L;
AvgRange = MA(range,length);
MinRange = AvgRange*Multiplier;

Buy = Ref(range,-1)>=MinRange AND L - Ref(H,-2) >= MinDist;
Sell = Ref(range,-1)>=MinRange AND Ref(L,-2)-H >= MinDist;

NotInTrade = Flip(Sell, Buy);
sfutlow = Valuewhen( Sell, ValueWhen( Buy OR lastbar, Ref(LowestSince(Sell, L), -1), 0 ) );
sll = IIf( NotInTrade, sfutlow, Null );
sfuthigh = ValueWhen( Sell, ValueWhen(Buy OR lastbar, Ref(HighestSince(Sell, H),-1), 0) );
shh = IIf( NotInTrade, sfuthigh, Null );

InTrade = Flip(Buy, Sell);
bfutlow = Valuewhen( Buy, ValueWhen( Sell OR lastbar, Ref(LowestSince(Buy, L), -1), 0 ));
bll = IIf( InTrade, bfutlow, Null );
bfuthigh = ValueWhen( Buy, ValueWhen(Sell OR lastbar, Ref(HighestSince(Buy, H),-1), 0) );
bhh = IIf( InTrade, bfuthigh, Null );

IIf(buy,BullRect(),IIf(sell,BearRect(),Null));
Title = "";
_SECTION_END();`

_SECTION_BEGIN("Fair Value Gaps 3");
function fxMxStopLines(sig, targetarray) {
	/// @link https://forum.amibroker.com/t/plot-horizontal-line-until-crossed/10802/32
	/// by fxshrat@gmail.com
	Version(6.0);
	bir = Status("barvisible" );// chart area array	
	cums = Cum(IIf(bir, sig, 0));
	lastcum = LastVisibleValue(cums);
	stop_mat = Matrix(3, Max(1,lastcum), 0);
	colnum = MxGetSize(stop_mat, 1);
	for ( i = 0, n = 0; i < BarCount; i++ ) {
		if ( bir[i] ) {
			if ( sig[i] ) {				
				if ( n < colnum ) {
					stop_mat[0][n] = i;// start bar
					stop_mat[1][n] = BarCount-1; // end bar
					stop_mat[2][n] = targetarray[i]; // level				
					for ( j = i; j < BarCount; j++ ) {
						if ( L[j] < stop_mat[2][n] ) {
							stop_mat[1][n] = j; // end bar
							break;
						}
					}
				}
				n++;
			}
		}
	}		
	printf( "#lines: %g, BarCount: %g", colnum, BarCount );
	GfxSelectPen( colorRed, 1, 0 );	
	for ( n = 0; n < colnum; n++ ) {
		GfxMoveTo(x1 = stop_mat[0][n], y = stop_mat[2][n] );
		GfxLineTo(x2 = stop_mat[1][n], y ); 
		GfxTextOut(StrFormat("%g", y), x2+0.25, y ); 
		//GfxTextOut(StrFormat("%g bars", x2-x1), x2+0.25, y );    
	}
	return stop_mat;
}

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );
length = Param("Periods", 20, 1, 300, 1 );
Multiplier=Param("Multiplier", 2.0,!.0, 10.0, 0.1);
MinDist = Param("MinDist", 1.0,0.1, 100.0, 0.1 );
bi = BarIndex();
lastbar = bi == LastValue(bi);
range = H-L;
AvgRange = MA(range,length);
MinRange = AvgRange*Multiplier;

Buy = Ref(range,-1)>=MinRange AND L - Ref(H,-2) >= MinDist;
Sell = Ref(range,-1)>=MinRange AND Ref(L,-2)-H >= MinDist;

GfxSetZOrder( 1 );
GfxSetCoordsMode( 1 );
GfxSetBkMode(2);
GfxSetTextAlign(0|0);
GfxSelectFont("Arial", 8, 500 );	
GfxSetBkColor( colorRed);
GfxSetTextColor( colorLightYellow );
GfxSelectPen( colorRed, 1, 0 );

targetarraybuy = L - Ref(H,-2);
mat = fxMxStopLines(Buy, targetarraybuy);
targetarraysell = Ref(L,-2)-H;
mat2 = fxMxStopLines(Sell, targetarraysell);
Title = "";
_SECTION_END();

Thank you for your assistance !

Hi
you should send the afl seperated and not 3 in one block
i insert your code to the afl editor as i try to help you and the only i can see now is 8 errors :shushing_face:

i didn't understand what you try to do.

Are you after to code something like this ?

Thank you for your answer. I'm sorry that I couldn't find the way to edit the codes correctly. Every module starts with '_SECTION_BEGIN' and ends with '_SECTION_END();'. I can't identify well what your script is doing, but indeed it looks similar to what I would like to achieve for the Fair Value Gaps which is to draw those gaps into the future until they are either touched by the first candle(s) or until the end. All those lines or rectangles should be horizontal ...
All those 3 modules compile without errors on my side.