How to buy at the high/close of the retest candle?

Resistance = Ref(HHV(H,20),-1);
Breakout = Cross(C,Resistance);
BreakoutLevel = Ref(Valuewhen(Breakout,Resistance), -1);
Retest = Cross(BreakoutLevel,L);
Retest = Retest AND Cum(Retest) ==1 ;
Retest = Retest AND BarsSince(Breakout) < 20;

Im trying to buy above the high/close of the retest candle but i cannot build the right code.
and also im try to buy the 1st retest please refer to the code above. Thanks!

image

2 Likes

Hello @markizta69,

Firstly, I haven't addressed your question, merely looked at your code, as I think there's a problem with the way you've specified the Retest condition(s).

Have a look at the following enhancement of your own code, for any symbol with several hundred quotes:

// Insert this code into a chart, and observe the first screen-full of quotes.
// Then scroll through the remaining quotes, one quote at a time. Make a note of ABs behaviour.

// Then, un-comment the following line to force AB to use _all_ prior bars, not just the ones it thinks are needed.
//SetBarsRequired(sbrAll, 0) ;		

Resistance = Ref(HHV(H,20),-1);
Breakout = Cross(C,Resistance);
BreakoutLevel = Ref(Valuewhen(Breakout,Resistance), -1);

Retest1 = Cross(BreakoutLevel,L);
cumRestest1	= Cum(Retest1) ;
Retest2 = Retest1 AND Cum(Retest1) ==1 ;
Retest3 = Retest2 AND BarsSince(Breakout) < 20;


SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Plot(cumRestest1, "cumRestest1", ParamColor("cumRestest1 Color", colorDefault ), ParamStyle("cumRestest1 Style", styleLine)); 

PlotShapes(shapeDigit1 * retest1, colorRed, 0, Low, -10) ;
PlotShapes(shapeDigit2 * retest2, colorGreen, 0, Low, -20) ;
PlotShapes(shapeDigit3 * retest3, colorBlue, 0, Low, -30) ;

What do you notice?

By-the-way, it would be easier to read your messages if you follow the suggestions in this post:
How to use this site

Secondly, we can address your question better, if you can provide a clearer description of what it is that you want:

Hi Sir,

Thank you for your reply.
My problem on the CUM function is that it only account the beginning on the stocks, please see image below.

I try to build another code but it seems the it's buying at the breakout not at the retest.

Resistance = Ref(HHV(H,20),-1);

Breakout = Cross(C,Resistance);
BOHIGH = Ref(Valuewhen(Breakout,Resistance), -1);

LOOKBACK=BarsSince(Breakout);

LOWESTLOWAFTERBO=Ref(LLV(l,LOOKBACK),-1);
HIGHHIGH=HHV(H,-LOOKBACK+1);
GAIN=100* (HIGHHIGH-BOHIGH)/HIGHHIGH ;
Breakout = LOOKBACK > 4 AND c < 1.03 *BOHIGH AND c > 0.97 * BOHIGH 
AND LOWESTLOWAFTERBO < 1.03 *BOHIGH AND LOWESTLOWAFTERBO > 0.97 * BOHIGH AND GAIN > 10 ;

image
image

I'm not going to take the whole code into account yet, but Cum() will give you a full range count and that's why you may be finding it hard.

What I think you might be looking for is SumSince() instead.
https://www.amibroker.com/guide/afl/sumsince.html

Now you can count the number of retest that occurred from a particular condition, say Cross(BreakoutLevel,L)

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.

12 Likes

That code requires minimum version 6.10.

By the way

Version(6.10);

should be placed in global scope (outside function definition)

1 Like

@fxshrat this is an amazing code. I tried putting in multiple resistance for the same code Resistance = Ref(HHV(H,20),-1); and Resistance = Ref(HHV(H,10),-1); but does not work.

Would I need to copy paste the code mutiple times for each resistance- what would be an easier way to do multiple resistances

SetOption("GenerateReport", 1);

period = Optimize(20, 10, 50, 1);
Resistance = Ref(HHV(H,period ),-1);

Then run optimize.

Note: The code has no Sell signal (it is set to zero in code). You need to insert your own one.

1 Like