I would be remiss if I didn't take the time to explain this code is full of inconsistencies, redundancies and unused items. Please do not take this too harshly. You are obviously here to learn something so I am going to teach. It may feel like I am stepping on some toes. So stand firm.
Debugging the Code
The code is extremely difficult to read. I took 20 minutes to fix everything. It's very important that you reduce everything to the bare minimum so you can properly debug the code. For Thinkorswim, the only debugging tool we have is the chart. So we take your code, fix up the many mistakes and plot it on a chart to find out what exactly is taking place under the hood.
camelCase versus under_score
It's a matter of preference. So use whichever method is easiest for you to read. But do your best to avoid mixing both in the same code. It really slows down the process of reading the code. I have corrected the code to use only camelCase.
Unused Statements
I understand that during development there tends to be several lines of code that end up not being used. When you get results you don't expect the first step is to remove all the lines that don't get used for the final result. During this process, many solutions are found. I have removed all but the most essential lines of code from your example.
Redundancies
There was only one case of this in your code.
def c = priorDayClose;
plot x = 100*(l-c)/c > 2;
But I have corrected it for the debug version.
Excessive Abbreviations
Always avoid using single characters for variable names. You have two options when writing code to make it is easiest to read and maintain. One is to use a comment for every line of code. The other is to use variable names that clearly explain what each variable contains. Lean towards the latter of two but don't hesitate to use comment lines where appropriate.
Spacing Between Mathematic and Logic Operators
Never write any statement like this:
plot x = 100*(l-c)/c > 2;
That is very difficult to read. Instead you should write it this way:
plot value = 100 * (gapPrice - priorDayClose) / priorDayClose > 2;
Plotting on a Chart to Troubleshoot the Code
Once I have corrected all these bits and pieces I can now add this to a new chart study and plot two of your most important variables so we can see what it looks like:
input marketClose = 1600;
input marketOpen = 930;
def closeCounter = SecondsTillTime(marketClose);
def openCounter = SecondsTillTime(marketOpen);
def regSessEnd = closeCounter[-1] == 0;
def regSessStart = openCounter[-1] == 0;
rec priorDayClose = if regSessEnd then close else priorDayClose[1];
rec dayOpen = if regSessStart then open else dayOpen[1];
def secondsPre = SecondsFromTime(0400);
def isPre = secondsPre >= 0 and openCounter > 0;
plot gapPrice = if isPre then close else dayOpen;
plot test1 = priorDayClose;
#plot value = 100 * (gapPrice - priorDayClose) / priorDayClose > 2;
Screenshot below shows the result. Now that you have a debug version of your scan you can go through your list of stocks and plot them. You should soon identify exactly what is causing the unexpected results. (you will likely find the errors are due to stocks that have little or no trade data for the extended hours session)
Your question title needs to describe the context. "Robust Premarket Scanner" does not describe the context of your request. You have a custom scan that finds stocks based on premarket and regular session gaps greater than X percent. Your question title should reflect that. Otherwise how are the rest of our viewers going to know what your post is about when searching through a list of over 1,000 posts trying to find a solution? Anyone searching for a custom scan that picks up premarket AND regular session gaps will NEVER find your post.
Let's see what the best question title looks like for this post:
"Gaps greater than x percent premarket and regular session"
I'll go ahead and update that before posting the solution. Doing so will break the original URL link to this post. But it will still appear in your list of posted questions.