I see you have four individual conditions but you did not explain how you wanted each of these conditions to be matched up.
So my solution includes for variables named signalOne, signalTwo, signalThree and signalFour. The last line is the plot statment for the scan and you can see that it combines signalOne and signalTwo conditions. You can mix and match as needed to get the combination you want to apply. However you will note that signalOne and signalThree are contradictory and likewise for signalTwo and SignalFour. So don't try combining those!
input percentThreshold = 40.0;
def percentOfRangeCurrent = (close - low) / (high - low);
def percentOfRangePrevious = (close - low[1]) / (high[1] - low[1]);
# scan for current close within upper x percent of current range
def signalOne = percentOfRangeCurrent > 1 - percentThreshold * 0.01;
# scan for current close within upper x percent of previous range
def signalTwo = percentOfRangePrevious > 1 - percentThreshold * 0.01;
# scan for current close within lower x percent of current range
def signalThree = percentOfRangeCurrent < percentThreshold * 0.01;
# scan for current close within lower x percent of previous range
def signalFour = percentOfRangePrevious < percentThreshold * 0.01;
plot scan = signalOne and signalTwo;
Just set the time frame to whatever you want to use. The code knows nothing whatsoever about time. The only thing the code knows is to compare the current bar’s close to the current bar’s range and the current bars close compared to the previous bar’s range.