Tracking Zone Crossings with Counters in ProBuilder

02 Oct 2022
0 comment
0 attachment

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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/stopping-trading-after-crossing-balance/#post-179258

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...