November 12, 2007
New Highs/New Lows Indicator (v3)
Scan can be used, in combination with AddToComposite (ATC), to create customized broad market indicators.
1) Run the following code as a Scan against the constituents of an index (for this example an EOD database comprising only constituents of the the Dow Jones Index “^DJI” has been used).
Note: Since there are no other symbols in the database All symbols were selected in Automatic Analysis and the filter was not used i.e. instructions 1-3 (inclusive) in the formula were bypassed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // Breadth of Stocks // Develops composites that can be plotted or manipulated as you wish // How to Run // 1) Select Group - Stocks or Market -NYSE,Nasdq, AMEX // 2) Select an issue with a long history in the current ticker window // 3) Set APPLY TO to use filter Market - Stocks // 4) Set RANGE to one bar... 1 n last quotations // 5) Press SCAN // adapted from code by Dick Areehoi //===================52 Weeks New High - Lows ================================== Buy = Sell = 0;//allows Scan to run //Find New Highs over the past 52 weeks. AddToComposite(IIf(C > HHV(Ref(C, -1), 250), 1,0), "~StocksNewHi","X"); //Same for lows next. AddToComposite(IIf(C < LLV(Ref(C, -1), 250), 1,0), "~StocksNewLo","X"); //Assign (define) variables for plots NewHigh = Foreign("~StocksNewHi","X"); NewLow = Foreign("~StocksNewLo","X"); DIFF_H_L = NewHigh-NewLow; Plot(NewHigh,"NewHigh",colorGreen,styleThick|styleHistogram); Plot(NewLow*-1,"NewLow",colorRed,styleThick|styleHistogram); Plot(0,"",1); //Plot(Diff_H_L,"Diff_H_L",colorBlack,styleLine) |
Nothing will be reported in the AA Results pane by the scan.

However two pseudo tickers will have been added to the All symbols Tree.

2) Insert S_52WeekHiLo, as an indicator, then modify S_52WeekHiLo to plot only the difference (comment out the high and low plots and un-comment the difference plot) and save it as S_52weekHiLoDiff.

3) Insert S_52weekHiLoDiff as an indicator (it will automatically open in a new pane).

The Hi/Lo plots are breadth indicators for the broad market (^DJI in this example) and might produce signals that are suitable for trading the index. Different periods for the indicator, and/or offsetting the Hi/Lo periods, might produce some interesting variations on the theme. Another strategy might be to use the broad market indicator for timing entries to single issues, e.g. a ’bull’ period in the breadth indicator coincides with a ’bull’ period for AIG (a constituent stock of the ^DJI).
CAUTION: THIS IS A VERY FAVORABLE SECTION OF THE CHART FOR THE BREADTH INDICATOR/STOCK PERFORMANCE COMBINATION. IT DOESN’T AUTOMATICALLY FOLLOW THAT IT WILL FORM THE BASIS OF A GOOD TRADING SYSTEM. “ONE SWALLOW A SUMMER DOES NOT MAKE”.

Note: In the AIG screenshot, although the upper chart shows prices for the current symbol the lower charts still plotting the global or breadth indicators, since they are referencing the foreign composite symbols (the current symbol shown in the indicator titles is not relevant). Also, the chart has been changed to Weekly view (in Weekly view the breadth indicators report the number of stocks making new highs or lows on the last day of the week).
The current chart can be ‘marked’ to provide a visual cue as to when that particular issue makes a new high.
To ‘mark’ charts when the price moves into new territory:
1) Save the following formula as P_NewHiLo.
2) Manually enter the number of bars required for the look-back period.
Note: The Hi and Lo plots can use different periods. For this example half the Hi look-back period was used for the Lo.
3) Left click on the formula, in the Chart Tree, and, holding the mouse button down, drag and drop it on the price chart, as an overlay.
1 2 3 4 5 | //P_NewHiLo //Overlay on a price chart. PlotShapes( IIf(H > HHV(Ref(H, -1), 250) AND BarIndex() > 250, shapeSmallCircle,0) , colorGreen,0,High,10); PlotShapes( IIf(L < LLV(Ref(L, -1), 125) AND BarIndex() > 125, shapeSmallCircle,0) , colorRed,0,Low,-10); |

Explorer can be used to find which symbols are making new highs or lows, on any given day, or over any date range e.g. the solitary issue that made a new low on 11/10/2005 can be isolated.

To report on issues that a making a new High or Low:
1) Run an exploration using the following formula.
1 2 3 4 5 6 7 8 9 | //X_NewHiLo //NewHi = H > HHV(Ref(H, -1),250); NewLo = L < LLV(Ref(L, -1),125); Filter = NewLo; //AddColumn(NewHi, "NewHi"); AddColumn(NewLo, "NewLo"); |
2) Select All symbols, set the date range to the day, inclusive, and click on Explore.

GM is reported as the stock that is at a new 125 day low.
Clicking on the “GM” row, in the results pane, will bring up the chart for a visual confirmation.

Expanding the date range can return a list of the stocks that correspond to ‘bullish’ and ‘bearish’ periods for the NewHiLo Indicator e.g. the following Exploration reports the list of stocks that contributed to the ‘bullish’ NewHiLo indicator in the last quarter of 2005 (approximately).

Clicking on the Date/Time column header will sort the NewHi’s sequentially, by date, to show which companies lead the ^ DJI ‘bull run’, to a new annual high on the 1/09/2006 the ^DJI.

The ‘bulls’ can also be compared to the ‘bears’ from the preceding ‘bearish’ period.

To report on stocks making new lows:
1) Set the date range to match the period marked in red.
2) Change the formula to suit (by un-commenting the NewLo’s code) and then run a fresh exploration.

It can be interesting to track the performance of the ‘bulls’ or the ‘bears’, from that period, over the next few months.
To compare relative performance of the October 2005 ‘bears’:
1) Set ^DJI as the current chart.
2) Insert the Relative Performance formula, from Charts >> Basic Charts as a chart (a Properties of: Relative Performance window will open).

3) Position the cursor in the Tickers input box and enter the symbols, from the AA report, with a comma between each.
4) Click on OK and the performance of each, relative to the current ticker, will be plotted.
5) Position the selector line on the reference date (in this case that is 11/10/2005) and scroll back until the reference day is the first bar in the chart (the relative performance of each symbol will be automatically recalculated to start from the first bar in the chart).

Interestingly, only 3/13 of the ‘bears’ under-perform, relative to the ^DJI, over the next (approx) six months.
CAUTION: RESULTS WILL VARY WITH THE TIME PERIOD SELECTED.
The exclusivity of a new high or low can also be tested.
A variation of the ‘bull side’ code used in P_NewHiNewLo adds a vertical line to the plot if a new 250 bar high is the first for a selected time period.
1 2 3 4 5 6 7 8 9 10 | //P_HigherHi v2 PlotShapes( IIf(H > HHV(Ref(H, -1), 250) AND BarIndex() > 250, shapeSmallCircle,0) , colorGreen,0,High,10); //P_ExclusiveHigherHi NewHi = H > HHV(Ref(H, -1), 250) AND BarIndex() > 250; ExclusivePeriods = Param("ExclusivePeriods",1,1,250,1); ExclusiveNewHi = ValueWhen(NewHi ==1, BarIndex(),1) - ValueWhen(NewHi == 1, BarIndex(),2) >= ExclusivePeriods AND NewHi; Plot(ExclusiveNewHi,"ExclusiveNewHigh",2,2|styleOwnScale); |
In this example the formula has ‘marked’ a new 250 bar high, that is the first one for 182 bars, with a red line.
Note: It would be rare to find a 250 bar new high that is the first occurence in exactly 182 bars so the formula reports, in this example, on any new high that is exclusive for at least 182 bars.
The exclusivity period for any new high can be found by changing the parameter setting.
To change the ExclusivePeriods parameter setting:
1) Right click inside the chart (the Properties window will open).
2) Left click in the space to the right of the default setting (the parameter slider control will appear).
3) Left click and hold the mouse button down while dragging the slider arrow left or right, to change the setting.
Note: The exact number of bars, since the previous 250 bar new high, can be found for any bar by dragging the slider until the plotted ‘marker’ line disappears from the chart and then moving the setting back one period.

The veracity of the code can be cross-checked by plotting barindex() in a separate pane, positioning the selector line on the relevant highs and calculating the difference between the reported barindex() numbers.
1 | Plot(BarIndex(),_DEFAULT_NAME(),1,1); |
For the complete code, that contains considerably more than the vignette provided here, download the attached S_52HiLoMaster file, or refer to the AmiBrokerYahooGroup message #116405 “Getting New High-New Low data into AB using AmiQuote? ” http://finance.groups.yahoo.com/group/amibroker/message/116405
For additional discussion on identifying exclusive new highs, within a defined period, refer to the AmiBrokerYahooGroup message # 116897 ”How to find out when a new 52 week Low was set in the past 90 days?” http://finance.groups.yahoo.com/group/amibroker/message/116897
ATTACHED FILES:
- s_52weekhilo.afl
- s_52weekhilodiff.afl
- s_52weekhilomaster.afl
- s_52weekhilo-v2.afl
p_newhilo.afl
x_newhilo.afl - p_higherhi-v2.afl
version 2 - Oct29/2007 - adds examples of plotshapes for HiLo’s and explore NewHiLo with the corresponding formula files uploaded.
Filed by brian_z at 11:13 am under Scans


(4 votes, average: 4.5 out of 5)
New Highs/NewLows Indicator v2 includes changes to the S_52WeekHiLo formula that add an amnesty to the plot, until after the lookback period has passed, and an input variable to allow easy use of different time periods for the NewHi and NewLo lookbacks.
It also extends the post to include plotting shapes to indicate, on the chart, when the current symbol is making new highs or lows and using Explorer to find stocks that are taking the ^DJI to new highs or lows.
New Highs/NewLows Indicator v3 adds the formula for an exclusivity test for new highs (how many bars since the last one)?