As usual for such cases you would have to iterate. You have to store breakout level and retest level and then reset if broken/crossed by price or if number of bars reached (in your case 20 bars -> see picture below).
BTW your picture is showing retest level at Open but not at High or Close.
function fxMxBuyBreakoutAfterRetest( startlevel ) {
/// Buy at breakout from 1st retest level
/// by fxshrat@gmail.com
/// available from here only:
/// https://forum.amibroker.com/t/how-to-buy-at-the-high-close-of-the-retest-candle/10905/5
/// (commercial use prohibited!)
local Breakout, BreakoutLevel, breakoutprice, startbar, mat, L_crss;
local Retest, RetestLevel, retestprice, bars, retest_cond, Buy_crss;
//
Version(6.10);
Buy = 0;
Breakout = Cross(C, startlevel);
breakoutprice = retestprice = startbar = Retest = 0;
BreakoutLevel = RetestLevel = bars = Null;
for ( i = 1; i < BarCount; i++ ) {
if (breakoutprice == 0 && Breakout[i]) {
breakoutprice = startlevel[i];
startbar = i;
retestprice = 0;
} else
Breakout[i] = 0;
//
L_crss = L[i] < breakoutprice AND L[i-1] >= breakoutprice;
retest_cond = breakoutprice > 0 AND L_crss AND i > startbar+1;
//
if (retest_cond) {
Retest[i] = 1;
retestprice = H[i];
breakoutprice = startbar = 0;
}
//
if ( i >= startbar+20 )
breakoutprice = startbar = 0;
//
Buy_crss = C[i] > retestprice AND C[i-1] <= retestprice;
if ( Buy_crss AND retestprice > 0 ) {
Buy[i] = 1;
retestprice = 0;
}
//
if ( breakoutprice > 0 ) {
BreakoutLevel[i] = breakoutprice;
bars[i] = i-startbar+1;
}
//
if ( retestprice > 0 )
RetestLevel[i] = retestprice;
}
//
VarSet("Buy", Buy);
mat = Matrix(BarCount, 5);
mat = MxSetBlock(mat, 0, BarCount-1, 0, 0, Breakout);
mat = MxSetBlock(mat, 0, BarCount-1, 1, 1, BreakoutLevel);
mat = MxSetBlock(mat, 0, BarCount-1, 2, 2, Retest);
mat = MxSetBlock(mat, 0, BarCount-1, 3, 3, RetestLevel);
mat = MxSetBlock(mat, 0, BarCount-1, 4, 4, bars);
return mat;
}
Resistance = Ref(HHV(H,20),-1);
mat = fxMxBuyBreakoutAfterRetest( Resistance );
list = "Breakout,BreakOutLevel,Retest,RetestLevel,bars";
for( i = 0; i < 5; i++ )
VarSet( StrExtract(list, i ), MxGetBlock(mat, 0, BarCount-1, i, i, True) );
Sell = 0;
//Plot( Resistance, "Resistance", colorOrange );
Plot( BreakoutLevel, "BreakoutLevel", colorPaleGreen );
Plot( Retestlevel, "Retest_level", colorGreen );
Plot( C, "", colorDefault, styleCandle );
PlotShapes( Breakout * shapeSmallCircle, colorPaleGreen, layer = 0, y = L );
PlotShapes( Retest * shapeSmallCircle, colorGreen, layer = 0, y = H, 12 );
PlotShapes( Buy * shapeSmallUpTriangle, colorBrightGreen, layer = 0, y = L, -24 );
//PlotShapes( Sell * shapeSmallDownTriangle, colorRed, layer = 0, y = H, -24 );
_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 ) );
//
bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );
PlotTextSetFont( "", fnt = "Arial", fntsize = 8, lvb, 0, -1 );
barscond = Ref(BreakoutLevel,-1) && IsNull(BreakoutLevel) && Ref(bars,-1) == 20;
for ( i = fvb+1; i <= lvb; i++ ) {
if ( barscond[i] )
PlotText( StrFormat( " #bars:%g", bars[ i-1 ] ), i, BreakoutLevel[i-1], colorPaleGreen, colorDefault, -fntsize/2 );
if ( Breakout[i])
PlotText( "HHV-Breakout", i, L[i], colorPaleGreen, colorDefault, -30 );
if ( Retest[i])
PlotText( "Retest", i, H[i], colorGreen, colorDefault, 20 );
if ( Buy[i])
PlotText( "Buy", i, L[i], colorBrightGreen, colorDefault, -40 );
}
Below is picture.
In case you wonder why some movements below HHV breakout level are not crosses (see yellow marker).... well, simply because in your upper code of 1st post you use
Retest = Cross(BreakoutLevel,L);
So Cross means BreakoutLevel > L and Ref(BreakoutLevel <= L,-1)
That's why in the loop
L_crss = L[i] < breakoutprice && L[i-1] >= breakoutprice;
And that's why cross occurs a few bars later in those examples (if you look carefully). Cross is different then just breaking line.
Next thing is... you didn't say anything about Sell signal and whether resetting levels after signal. So I left Sell signals outside of function. And that's why there're just multiple buy signals since you only said Buy the 1st retest level.
Also as you can see there is 20 bar timer included to reset after being reached (-> no retest (cross) from above within 20 bars).
and another picture....
So AFAICS it does what you are looking for.