problem with afterhour/premarket gap scan


Category:
0
0
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;

abv is my code used fr scanning ah/pm gappers greater than or lesser than 3 or -3%. problem now is that 1) it is returning OTC names 2) insane gappers with 92,233,720,3688,547,760% (see image attached) or % that are meant to be excluded such as 0.2% 3) aggregation period used seems to affect code even if it shouldnt. can i ask what seems to be the problem and any ideas fr rectifying it?

thanks

Attachments:
Marked as spam
Posted by (Questions: 4, Answers: 2)
Asked on October 31, 2019 3:50 am
280 views
0
Your code cannot be used as it is. The problem is that your code contains several places where there were no spaces between special characters used as greater than or less than. Because there were no spaces the database of this website interpreted those as special HTML tags rather than literal characters.
( at November 1, 2019 12:09 pm)
0
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; sorry this is the corrected code. cant seem to edit the original post
( at November 1, 2019 4:28 pm)
0
Private answer

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.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on November 2, 2019 11:08 am
0
can this script be tested on weekend? or it works only during extended hours..
( at June 20, 2020 7:33 am)
0
The code for this scan looks at candles that appear during the extended trading hours. During the weekend the last bar on the chart is the last bar during the extended trading hours. So the scan is able to return results. Any signals it detects during the weekend will be from the last bar during extended trading hours on Friday night.
( at June 20, 2020 8:16 am)
0
Somthing is wrong... Using your code, the stock i found is none. If i use "def con1 = change == 0", then the stocks i found is ALL...
( at June 20, 2020 6:11 pm)
0
Well then perhaps it doesn't work during the weekend at all. Try using it during the time it was intended before trying to figure out what might be wrong.
( at June 20, 2020 9:55 pm)
0
During the weekend (or not in trading hours) , "close" in your code means the last price at the REGULAR trading hours, instead of the one after market. Do your know how to fix it? Thanks.
( at June 21, 2020 10:21 am)
0
You are not correct. In the Thinkorswim language "close" always refers to the most recent price. "close[1]" refers to the closing price of the previous bar. But "close", by itself, always refers to the current price right now at this very instant regardless of what year it is, what day of the month it is or what phase the moon is in. I just ran this scan myself, at 3pm Eastern on Sunday June 21, 2020 and it returned a list of 1,544 stocks. If you are not getting results you are not following the instructions provide.
( at June 21, 2020 12:00 pm)
0
I have fixed it by setting not by code. Thanks.
( at June 21, 2020 5:05 pm)