Amibroker Q & A: How to Create the 52 Week High/Low Index with AddToComposite


Free Beginners Course  |  Free Intermediate Course  |  Free Amibroker Course Home

Amibroker Q & A: Using AddToComposite to Create the 52 Week High/Low Index

This week’s question comes from Richard on Twitter, who would like to look at the AddToComposite function in Amibroker.

This function is very cool – it allows us to create our own index, and even turn it into an indicator.

In this video we create the 52 week High/Low index, which shows us how many stocks are making new 52 week highs or lows, and we plot it in our very own indicator so we can look for divergences.  Dr Alexander Elder introduced this concept to the wider world in his book “Trading for a living”, with the idea that if the indicator is moving up, but our 52 week high.low index is moving down, it might be a chance to sell (and vice versa).

Check out the video below, and please leave a comment at the bottom!

(The video above has the latest code in it, thank you to both Jan and James for letting me know, and Howard Bandy and the forum user “Trash” at Aussie Stock Forums for assisting with the correct code!)

Happy Trending everyone,

– Dave McLachlan

November 22, 2015  Tags: , , , , ,   Posted in: Amibroker Q & A

9 Responses

  1. Craig - December 27, 2015

    How did you make the actual indicator like you have in the vid? I tried using a loop of CategoryGetSymbols of the asx200 watchlist and count++ if the ticker is at a new 52-week high or new 52-week low. Works reasonably well but getting errant signals.

  2. David McLachlan - December 28, 2015

    Hey Craig – just plotting (Plot function) the Array we created, after using “Foreign” to import the AddToComposite ticker into that Array.

    Then, once you’ve saved your code, you can just insert it onto any chart.

    Happy Trending,


  3. Craig - December 29, 2015

    Same thing without scanning.Just add to chart…

    function Months(m){return 252/(12/m);}
    IndexName = ParamStr(“Index Name”, “ASX All Ordinaries”);
    period = months(Param(“Period”, 12, 1, 12, 1));
    IndexName = ParamStr(“Index Name”, “ASX All Ordinaries”);
    IndexNumber = CategoryFind(IndexName, categoryWatchlist);
    list = CategoryGetSymbols(categoryWatchlist, IndexNumber);
    newhighs = 0;
    newlows = 0;
    for( i = 0; (sym = StrExtract(list, i)) != “”; i++ ){
    shigh = Foreign(sym, “H”);
    slow = Foreign(sym, “L”);
    newhigh = shigh > Ref(HHV(shigh,period),-1);
    newlow = slow < Ref(LLV(slow,period),-1);
    newhighs = newhighs + IIf(IsNull(shigh), 0, IIf(newhigh, 1, 0));
    newlows = newlows + IIf(IsNull(slow), 0, IIf(newlow, 1, 0));
    hilodiff = newhighs-newlows;
    Plot(hilodiff, _DEFAULT_NAME() + " – " + IndexName, ParamColor("Color",16), ParamStyle("Style",4));

  4. Jan - December 31, 2015

    I’m trying to create composite index but every time I got error after validating the code in AmiBroker 6.00 for the line 5:
    “Error 55. Invalid assignment. The identifier is read-only (constant) and can not be written to.”

    Hi = High > Ref(HHV(High,250),-1);
    Lo = Low < Ref(LLV(Low,250),-1);
    HiLoDifference = Hi – Lo;
    Buy = 0;
    AddToComposite(HiLoDifference,"~HiLoDifference","X",atcFlagDefaults = 7);
    NHNL = Foreign("~HiLoDifference","C",True);
    Plot(NHNL, "New high new low index", colorGreen,styleLine);

    I suppose that something has changed in the newest versions of AmiBroker but not sure.

  5. David McLachlan - December 31, 2015

    Hey Jan,

    You are absolutely correct! And that is my fault. It works on v5.5, but not the latest versions. Upgrading Amibroker is on my to-do list for the New Year.

    In the latest versions of Amibroker, simply replace:

    AddToComposite(HiLoDifference,”~HiLoDifference”,”X “,atcFlagDefaults = 7);
    with this:
    AddToComposite(HiLoDifference,”~HiLoDifference”,”X “);

    The wonderful Howard Bandy and forum user Trash were kind enough to give it a look-in at Aussie Stock Forums, here:

    I’ll also fix the video up, soon.

    Happy trending! Dave

  6. Craig - January 1, 2016

    A good book to read regarding use of the 52-week high low differential is The Nature of Risk by Justin Mamis. An excellent book about risk as well. 🙂

  7. David McLachlan - January 2, 2016

    Hey Craig – awesome, thank you.

    I just read an interview with Justin from the early 90s, and he seemed to be more “subjective” in his analysis…”Rules based discretionary” I guess you’d call it, and from the Edwards & Magee school of chart patterns. That is where I started as well, and I still use it.

    What does he say about using the 52w H/L Index?

    I guess we can always test different uses for it easily too, by using the Array we created in the video in a trading system. I haven’t found anything trade-able myself, just yet.

    Happy trending – Dave

  8. Craig - January 3, 2016

    Mamis used a 10-day MA of the 52wHLI of the DJIA. The 52wHLI as a market breadth indicator may reveal the member stocks’ number of new highs or lows diverging from what the actual index shows. Eg. March-April 2015 of XAO as it went sideways, there were decreasing new highs on the HLI after exuberance in February. I usually only use price and volume, but another way to look at market breadth is helpful.
    Adding this to CategoryGetSymbols version may add insight:
    Plot(newhighs,”Highs”, 27, 2|4|4096);
    Plot(-(newlows),”Lows”, 32, 2|4|4096);

  9. David McLachlan - January 3, 2016

    Awesome, thanks Craig!

    Hey, I mostly use price and volume too. Nice one. My take is that most everything else (i.e. indicators, etc) are interpretations of price and volume (and sometimes Time), so why not go to the source 🙂

    Happy trending – Dave

Leave a Reply