Count the number of bars between successive highs

thanks Robert for being so generous with your contributions.

I would appreciate some help.

I have the output of a band pass filter. I have this programed and it is working well. Lets call the output array Wave.

I am trying to ‘count’ the number of bars between the successive highs and (and also for the lows) of the output. The output wave is fairly smooth, so determining the highs and lows should be easy, as the slope of the output changes. Perhaps like:

HighWave = Wave < Wave[1] and Wave[1] >= Wave[2]; # Location of Highs
LowWave = Wave > Wave[1] and Wave[1] <= Wave[2]; # Location of Lows

But now, I don’t see how to compute the number of bars between successive highs and also for the Lows using TS built in functions. Anyone know how to do this? In English, what I want to do is:

DeltaHigh = index or location of HighWave when it is TRUE – index or location of HighWave when it was last TRUE.

and similarly for DeltaLow.

Once I have the number(s), I would like to ‘print’ the number near each high and low. This does not seem to be possible with ThinkScript. If not possible, perhaps I could put the numbers for the last two Highs in the Label with addLabel, and then the last two Lows in another Label.

Thanks for your help.

Charles

You will need to use a recursive variable for the counter. That just means that it’ll be referencing its own last value ( count[1] )

Also, take a look at the AddChartBubble function.

Imgur

# +--------------------------------------------------------------+
# |  Example: Count the number of bars between successive highs  |
# |                         Robert Payne                         |
# |                    funwiththinkscript.com                    |
# +--------------------------------------------------------------+

# define a peak as a high point greater than the 5 highs on either side
# note that this is just an arbitrary definition for example purposes
def peak = high > Highest(high[1], 5) and high > Highest(high[-5], 5);

# count the number of bars since the last peak
def count = if peak[1] then 1 else count[1] + 1;

# mark each peak with a down arrow
plot peakArrow = peak;
     peakArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

# show the number of bars (the count) since the last peak
AddChartBubble(peak, high + 1, count, Color.WHITE);

Leave a Reply