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.
This approach is useful for highlighting periods of low volatility or consolidation, which can be significant in various trading strategies.
Check out this related content for more information:
https://www.prorealcode.com/topic/tradingrange-recognize-and-breakout/#post-143282
Visit Link