Amibroker Tutorial: Identifying a retest of a breakout level

One of the more common questions that I get asked by clients is how to identify stocks that are retesting a breakout level. In this article I will provide a solution.

The first step is to identify the breakout itself.

A common method for identifying a 50 day breakout is to use:

////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

//////////////////////////////////////////

Note that because Close of the breakout day cannot be higher than the 50 day High, you must use the Ref() function to identify the 50 day as of yesterday.

However, one problem with the above approach is that if a stock keeps making new highs then multiple days will be classified as breakout days.

In the following example the breakouts are identified by the blue bars in the bottom chart pane. Note that many “breakouts” are identified during the period.

But I would argue that a stock which is making consecutive new 50 day highs is not really “breaking out” at all.

Instead, I’d suggest that the only breakouts worth identifying in the following chart are labelled A,B and C:

 

We therefore need to modify our breakout formula.

For the following example, we’ll only consider a stock to be breaking out if there has been no other 50 day highs in the past month (roughly 21 trading days):

////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout;

//////////////////////////////////////////

We now see that only the following breakouts are identified:

 

You could then take this a step further and only identify a breakout if the closing price of the breakout day is at least 1 ATR above the Resistance level…

////////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

//////////////////////////////////////////////////

Note from the following chart that only 2 breakouts are now valid during the period being analysed:

Now that we’ve identified the breakouts, we can start to look for a retest of the breakout level. To do this we can use the ValueWhen() and Cum() functions.

We first need to store the value of the Resistance level on the day of the breakout…

////////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

BreakoutLevel = Ref(Valuewhen(Breakout,Resistance),-1);

//////////////////////////////////////////////////

Then we need to identify a retest of the Breakout Level:

////////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

BreakoutLevel = Ref(Valuewhen(Breakout,Resistance),-1);

Retest = Cross(BreakoutLevel,L);

//////////////////////////////////////////////////

In the following example a blue bar is plotted in the bottom chart pane whenever the stock price retests the breakout level:

 

The problem with the above chart is that over the following few months the stock price retests the original breakout level multiple times.

In practical terms, it is more likely that we’re only interested in identifying the first retest.

To do this we simply need to add one more line of code to our formula:

//////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

BreakoutLevel = Ref(Valuewhen(Breakout,Resistance),-1);

Retest = Cross(BreakoutLevel,L);

Retest = Retest AND Cum(Retest) == 1;

Plot(Retest,”breakout”,colorBlue,styleHistogram|stylethick);

//////////////////////////////////////////////////

Using the Plot() function we can now see that only the first retest of a breakout level is identified by our indicator:

To identify signals we can now run the explorer using this formula:

//////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

BreakoutLevel = Ref(Valuewhen(Breakout,Resistance),-1);

Retest = Cross(BreakoutLevel,L);

Retest = Retest AND Cum(Retest) == 1;

Filter = Retest;

AddColumn(Retest,”Retest”,1.2);

//////////////////////////////////////////////////

During the past 1 month 5 retest signals were identified if analysing stocks belonging to the Russell3000. These setups are seen below:

(For easier analysis the breakout day is identified by a green arrow and the retest day is identified by a blue arrow):

 

One final observation is that in the case of $GCP (the first chart from the 5 above), it can be seen that the retest of the breakout level did not occur for about 6 months following the breakout.

With that said, I presume that most readers are more likely to want to identify a retest of a recent breakout. So one final snippet of code that you might want to employ uses the BarsSince() function.

In the following example, we will only identify a retest of a breakout level if it occurs within one month of the breakout, although as with each of the lines in the formula the parameters are easily modified to better suit your requirements:

//////////////////////////////////////////////////

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

Breakout = Cross(C,Resistance);

Breakout = Ref(Sum(Breakout,21) == 0,-1) AND Breakout AND C > (Resistance + Ref(ATR(5),-1));

BreakoutLevel = Ref(Valuewhen(Breakout,Resistance),-1);

Retest = Cross(BreakoutLevel,L);

Retest = Retest AND Cum(Retest) == 1;

Retest = Retest AND BarsSince(Breakout) < 21;

Filter = Retest;

AddColumn(Retest,”Retest”,1.2);

//////////////////////////////////////////////////

If you’ve got any other Amibroker programming questions that you’d like answered, feel free to contact me at: Llewelynjames@hotmail.co.uk

All the best,

Llewelyn

3 Comments

  • Chris

    Reply Reply October 13, 2017

    Very informative article!

  • Macca

    Reply Reply October 15, 2017

    Hi Llewelyn, Can you please add the following to your code :

    1. Breakout and retest day arrows
    2. Add a column with Breakout dates
    3. Add a column with the number of days between the Breakout and Retest dates

    Many Thanks

    • Llewelyn James

      Reply Reply October 15, 2017

      Hi Macca,

      For plotting the breakout and retest arrows on your charts please include the following at the end of the article formula: (Please note that quotation marks often need to be re-written when copy and pasting to Amibroker from my site)

      //////PLOT CHART and ARROWS////////

      _SECTION_BEGIN(“Price”);
      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”, colordefault, styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
      _SECTION_END();

      PlotShapes(Breakout * shapeDownTriangle, colorGreen, 0, H);
      PlotShapes(Retest * shapeUpTriangle, colorBlue, 0, L);

      /////END/////

      For adding a column with the most recent breakout date and number of days between the breakout and today’s retest, please add the following to the end of the formula from the article:

      ///Further Exploration columns//

      DT = DateTime();
      BODT = ValueWhen(breakout,DT,1);
      BSB = BarsSince(BREAKOUT);

      AddColumn(bsb,”BarsSinceBO”,1.2);
      AddColumn(BODT,”DateOfBreakout”,formatDateTime);

      //////END////////

      I hope that helps.

      Kind regards,

      Llewelyn

Leave A Response

* Denotes Required Field