Ok, now that we have a functional version of the code we can diagnose these issues and explain what is taking place.
First, let's provide a version of the code in it's properly formatted view. Note that I have fixed all the errors created when the author of code failed to place spaces between different elements in the code. For example, this should never be done in any computer language (except for web dev's):
def change = ((close-priorDayClose)/priorDayClose)*100;
This is the proper way to write that statement:
def change = ((close - priorDayClose) / priorDayClose) * 100;
Having added the spaces you will find the code is much easier to read and it will have less likelihood of getting mashed up by HTML parsers when posting code on a website.
Ok. Here is the properly formatted version of the code:
input marketClose = 1600;
def closeCounter = SecondsTillTime(marketClose);
def regSessEnd = closeCounter[-1] == 0;
rec priorDayClose = if regSessEnd then close else priorDayClose[1];
def change = ((close-priorDayClose) / priorDayClose) * 100;
def con1 = change >= 3 or change <= -3;
plot scan = con1;
In order to troubleshoot the issue I lopped off the last two lines and changed the def change
statement to plot change
. This allowed me to plot the values on the lower subgraph of the chart and visualize exactly what was taking place in various steps of the code.
What we find is that for all the stocks that returned unexpected results, the code performed exactly as it is written. It's doing precisely what it was designed to do. The problem is with the design.
The statements used to read the prior day close require that each stock in the scan has trading activity after hours. The code is trying to find the candle with a time stamp of 1600 hours. Since time stamps on Thinkorswim are for the opening time of the candle, this means the first bar AFTER market closes.
If there is no bar on the chart marked with a time stamp of 1600 hours the code simply carries forward the value from the last 1600 candle it was able to find. This explains your results that are less than +3% or greater than -3%. Your watchlist column is reading the correct value, however your scan is picking up the wrong value. Because the most recent market close was not followed by a candle with a 1600 hours time stamp.
The issue you have with the values of 92,233,720,3688,547,760%. Those are stocks for which the code was NEVER able to find a candle with a 1600 hours time stamp. So you are essentially dividing by zero.
To summarize. The problem is with the design of the code. The code will work for all stocks that always have active trading in during the extended hours session. However the code will fail for all stocks that have little or no trading during extended hours session. After all this is a "aftermarket/premarket gap scan". The question you should be asking yourself is, why are you running this scan against a list of stocks that include ones that have little or no trade activity in the extended hours session.
How to fix this?
input marketClose = 1555;
def closeCounter = SecondsTillTime(marketClose);
def regSessEnd = closeCounter == 0;
rec priorDayClose = if regSessEnd then close else priorDayClose[1];
def change = ((close-priorDayClose) / priorDayClose) * 100;
def con1 = change >= 3 or change <= -3;
plot scan = con1;
The one thing you must ensure when applying this code to a scan is that your time for marketClose
must be the time stamp of the last bar that trades during the regular session hours. On a five min chart that value is 1555. However if you change this to a 15 min time frame you need to change the marketClose
input value to 1545.
When using this code it will still fail for stocks that don't have a full day of trading activity. Let's take AACG as an example. That stock has such low trading activity that there are no candles that trade during the market close. Stocks with such low trading activity are simply not going to work.