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
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...