No need for any custom code solution here. This is already included as a built-in filter on Thinkorswim. The scan filter is named "Gap_Within_Time". It checks if there has been a gap greater than the percent you enter within the number of bars you enter.
Adding this filter to the study filter group named "None of the following" will have the effect of inverting this filter so that it returns a list of stocks that have NOT gapped more than the selected percent with the selected number of bars.
Screenshot below shows how to set this up.