This script will search for zones where the range between the high and low is lower than avg % nearest rank of last X bars. After X(=30) bars of consolidating range it will signal that the market is in a range and draw the top, bottom and middle of that range. When price breaks the consolidating range the range is cleared, this is where you long/short the breakout. After this it will look for the next range and do the same.
Changed closing of the range to when the bar closes above the range instead of the high or low. This ensures the range is broken instead of bouncing back into the range