//------------------------------------------------------------------------//
//PRC_Range Detector
//version = 0
//09.04.24
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//------------------------------------------------------------------------//
//-----Inputs-------------------------------------------------------------//
length=20//Minimum Range Length
mult=1//Range Width
atrLen=500//ATR Length
//------------------------------------------------------------------------//
//-----Moving average and ATR definition----------------------------------//
n=barindex
atr=averagetruerange[atrlen](close)*mult
ma=average[length,0](close)
//-----Check range--------------------------------------------------------//
count=0
for j=0 to length-1 do
if abs(close[j]-ma) > atr then
count=count+1
else
count=count
endif
next
//-----Box definition-----------------------------------------------------//
if count=0 and count[1]<>count then
//Test for overlap and change coordinates
if n[length] <= $boxright[x] then
maxi = max(ma+atr,$boxtop[x])
mini = min(ma-atr,$boxbot[x])
//Box new coordinates
$boxtop[x]=maxi
$boxbot[x]=mini
$boxright[x]=n
//Line new coordinates
avg=(maxi+mini)/2
$levely1[x]=avg
$levelx2[x]=n
$levely2[x]=avg
//backgroundcolor("Blue",50)
rangedetected=1
//newrange=0
else
maxi=ma+atr
mini=ma-atr
//Set new box
$boxtop[x+1]=maxi
$boxbot[x+1]=mini
$boxright[x+1]=n
$boxleft[x+1]=n[length]
//Set new level
$levely1[x+1]=ma
$levelx2[x+1]=n
$levely2[x+1]=ma
$levelx1[x+1]=n[length]
//Set color
$rbox[x+1]=33
$gbox[x+1]=87
$bbox[x+1]=243
//Highligth first occurrence
//backgroundcolor("grey",50)
$start[x+1]=n
//Set new value for x
x=x+1
rangedetected=1
//newrange=1
endif
//elsif count=0 then
////Extend the box
//$boxright[x]=n
//$levelx2[x]=n
//rangedetected=1
//newrange=0
else
rangedetected=0
endif
//------------------------------------------------------------------------//
SCREENER[rangedetected and close<$boxtop[x] and close>$boxbot[x]]