This ProBuilder code snippet demonstrates how to track the number of times a stock’s price crosses certain zones defined by recent high and low prices. It uses conditions and counters to tally these crossings, which can be useful for identifying trend strength or reversal signals in trading strategies.
Periods = 40
Bullish = close > open
Bearish = close < open
HH = highest[Periods](close)
LL = lowest[Periods](close)
Rng = (HH - LL) / 4
IF (HH <> HH[1]) OR (LL <> LL[1]) THEN
Tally = 0
ENDIF
HItouch = high >= HH
LOtouch = low <= LL
IF HItouch AND Bullish THEN
Tally = Tally + 1
DrawText("#Tally#",BarIndex,HH + Rng)
ENDIF
IF LOtouch AND Bearish THEN
Tally = Tally + 1
DrawText("#Tally#",BarIndex,LL - Rng)
ENDIF
RETURN HH AS "Top",LL AS "Bottom"
Explanation of the Code:
- Initialization: The code starts by setting the number of periods to 40. It then defines conditions for a bullish (closing price higher than opening price) and bearish (closing price lower than opening price) market.
- High and Low Calculation: It calculates the highest and lowest closing prices over the specified period (40 bars) and divides the range between these two values by four to determine smaller zones or ranges.
- Resetting the Tally: The tally counter is reset to zero whenever there is a change in the calculated high or low values from one period to the next. This ensures that the count starts afresh when new zones are established.
- Detecting Crossings: The code checks if the current high has touched or exceeded the highest value (HItouch) and if the current low has touched or gone below the lowest value (LOtouch).
- Updating the Tally: If a high touch occurs during a bullish period, or a low touch occurs during a bearish period, the tally is incremented by one. Additionally, a text displaying the tally count is drawn on the chart at the respective high or low plus or minus a fraction of the range.
- Output: Finally, the highest and lowest values are returned as "Top" and "Bottom" respectively, which can be used for further analysis or display.
This code is a practical example of how to implement conditional logic and counters in ProBuilder to monitor specific market conditions effectively.