Detecting and Plotting Price Range Boxes in Financial Markets

14 Aug 2020
0 comment
0 attachment

This ProBuilder code snippet is designed to detect and plot rectangular zones in a price chart that fall within a specified percentage deviation over a given period. These zones can help in identifying areas of price consolidation or stability.


percent = 0.3 //max percent deviation
period = 20 //period of lookback
minBar = 3 //minimal bar for the boxes
count=0
hh=0
ll=close*10
for i = period-1 downto 0 do
    dev=abs(close[i+1]-close[i])/close[i+1]
    if dev<=percent/100 then
        //drawarrow(barindex[i],close[i])
        count=count+1
        hh=max(hh,high[i])
        ll=min(ll,low[i])
        if count=1 then
            startbar=barindex[i]
        else
            endbar=barindex[i]
        endif
    else
        break
    endif
next
if count>=minBar then
    drawrectangle(startbar,ll,endbar,hh)
endif
return //(close-close[1])/close,percent/100,count,highll,dev

This code snippet operates by evaluating the price changes between consecutive bars and checking if these changes stay within a defined percentage deviation. If the deviation is within the acceptable range for a sufficient number of consecutive bars, a rectangle is drawn to highlight this zone.

  • Initialization: Variables such as percent, period, and minBar are set to define the maximum percentage deviation, the lookback period, and the minimum number of bars for a valid zone, respectively.
  • Loop through bars: The code iterates backwards through the bars from the most recent to the start of the defined period. For each bar, it calculates the deviation of the closing price from the previous bar’s closing price.
  • Check deviation: If the deviation is less than or equal to the specified percentage, the high and low values are updated to find the maximum and minimum prices in this range, and the count of consecutive bars within the deviation limit is incremented.
  • Rectangle plotting: If the count of bars that meet the deviation criteria reaches the minimum bar threshold, a rectangle is drawn from the start to the end of this range, using the highest and lowest prices encountered.
  • Breaking the loop: If a bar’s price deviation exceeds the specified percentage, the loop breaks, and no further bars are checked.

This approach is useful for highlighting periods of low volatility or consolidation, which can be significant in various trading strategies.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/tradingrange-recognize-and-breakout/#post-143282

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
33
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...