DYO: New 20 day High and Low counts
|
Q: I have a request to write a DYO that calculates based on closing prices the number of days in the past 20 in which we've made new 20-day highs. I want to subtract from that figure the number of days in the past 20 in which we've made new 20-day lows.
A:
Template: 1303-Counters
This solution uses 2 DYOs. The 1st DYO counts the number of new 20-bar Highs. The 2nd DYO counts the number of new 20-bar Lows. The 2nd DYO will be explained.
Line A is the number of bars being examined, and with Number = 20, the count is across the prior 20 bars.
Line B is initializing the GV used for our counter to zero.
Line C though H is a loop that will cycle Line A number of times. Line C finds the lowest value in the last 20 bars because Number on Line C is a 20. The number 20 is used on both Lines A and C and though the same in this example, they do not have to be the same.
Line D then tests to see if the current bar has the same value as the Lowest Low and when this is True, our counter GV is incremented.
Line F and G are clever in that they decrement the Bar Offset of Line C and D to reference a prior bar moving leftward through the chart data set.
Line H is the loop control the cycles back to Line C 20 times. The loop counter is GV[1], which is decremented, and while the count is greater than zero, the loop back is performed. Final results will be the counter in GV[5], which is shown in the Section 1 panel.
Template: 1303-Counters
Last modified 1/17/08 5:43 PM
|