# Help making Pre-Market Gap Scanner more efficient

0
0

Hello, I currently have a pre-market gap scanner I feel that is inefficient; it’s not pulling enough stock that are down about 2.0% or more in pre-market.
At times it returns stocks that are gapping up – which I find weird.

Is there a way I can refactor this pre-market gap scanner code to make it more efficient and accurate?
The code is below along with the criteria.

1. Stocks from \$3 ~ \$500
2. Min Volume 200,000

3. In 3 min aggregation
```input minimumchange = -2; def todaypremarket = if getlastDay() == GetDay() and secondsfromtime(0930) < 0 then 1 else 0; def timecondprice = if todaypremarket then close else double.nan; def firstbar = lowestall(if todaypremarket then barnumber() else double.nan); def firstbarprice = if isnan(if barnumber() == firstbar then open else double.nan) then firstbarprice[1] else if barnumber() == firstbar then open else double.nan; def price = if todaypremarket then close else price[1]; def change = price - firstbarprice; def result = if isnan((change/price) * 100) then result[1] else (change/price) * 100; def todays_premarket_percent_change = result; plot scan = if todays_premarket_percent_change <= minimumchange then 1 else 0;```

Any help will greatly be appreciated.

Marked as spam
Posted by (Questions: 2, Answers: 3)
Asked on April 9, 2018 8:40 pm
1271 views
 ▲ 0 ▼ ✔ Private answer Rather than trying to make this code work, I suggest you take a look at the several other posts on this topic and try them. https://www.hahn-tech.com/ans/after-hours-gap-scanner/ https://www.hahn-tech.com/ans/barchart-style-gap-upgap-down-tos-scan/ https://www.hahn-tech.com/ans/scan-gap-within-last-week/ https://www.hahn-tech.com/ans/gap-to-previous-day-close-scan/ Marked as spam Posted by Pete Hahn (Questions: 35, Answers: 1789) Answered on April 9, 2018 9:45 pm Lucius submitted his response as a new answer instead of a comment. I am including the contents of his response here. In his original response he included code from each of the four posts I listed in my answer. The code is being omitted here so his response will fit within the limits of the comment field. I have also broken up the response into four sections, one for each of the links he is commenting on. (Pete Hahn at April 10, 2018 6:56 am) Thank you so much for your response Pete. I attempted to reply as a comment but my response was cut off. I normally trade stocks intraday with 1min and 5min charts and am usually up early hours of pre-market looking for stocks that have gapped down by at least 2% from previous day’s close. I have looked at those before but were able to produce the desired results. Please see my comments below. 1 – After Hours Gap Scanner – [https://www.hahn-tech.com/ans/after-hours-gap-scanner/] This one seems promising. It appears to only return results for stocks that are gapping up. How can I include a percentage variable to only show stocks that has gapped down in pre-market by 2%? What would be the recommended aggregation for this? (Pete Hahn at April 10, 2018 6:56 am) 2 – Barchart Style Gap Up/Gap Down TOS Scan [https://www.hahn-tech.com/ans/barchart-style-gap-upgap-down-tos-scan/] The following was stated in that thread: “This single line of code will compute the difference between today’s close and yesterday’s close, when applied to a daily time frame.” I an seeking to produce results where the current pre-market price has gapped down by 2% from yesterday’s close. (Pete Hahn at April 10, 2018 6:56 am) 3 – Scan for gap down within the last week [https://www.hahn-tech.com/ans/scan-gap-within-last-week/] This is returning undesired results. It’s returning stocks such as the following near or above yesterday’s close as of 4/10/2018 – 8:50 AM \$YNDX \$MBT \$ACAD I’ve even went as far as modifying the variable to: input price2 = close; Similar undesired results returned. (Pete Hahn at April 10, 2018 6:56 am) 4 – Gap to Previous day close scan [https://www.hahn-tech.com/ans/gap-to-previous-day-close-scan/] This may be a bit too much for what I am seeking and am having difficulty understanding what it’s producing: Any help will greatly be appreciated. Thank you (Pete Hahn at April 10, 2018 6:57 am) ▲ 0 ▼ ✔ Private answer Based on feedback from Lucius on each of the 4 gap scanners I listed in my original answer I have additional details to provide. From your comments it seems the After Hours Gap Scanner fits your needs the best: https://www.hahn-tech.com/ans/after-hours-gap-scanner/ You indicated that you needed help converting that code to search for gaps down instead of gaps up. (I only had to adjust the percent_change and flip the greater than to less than). There is no “recommended aggregation period,” experiment and use whatever works best for you. Here is the code from that post, with my adjustments applied to the author’s original. I have also included the conversion to search for gaps down. ```def percent_change = -2.00; input marketClose = 1600; def closeCounter = SecondsTillTime(marketClose); def regSessEnd = closeCounter[-1] == 0; rec priorDayClose = if regSessEnd then close else priorDayClose[1]; # comment out yest_closing_price and replace with priorDayClose #def yest_closing_price = close[500]; def afterhours_percent_change = 100 * ((close - priorDayClose) / priorDayClose ) ; # comment out scan for greater than and replace with scan for less than #plot scan = afterhours_percent_change >= percent_change; plot scan = afterhours_percent_change <= percent_change;``` Marked as spam Posted by Pete Hahn (Questions: 35, Answers: 1789) Answered on April 10, 2018 7:10 am Pete, thank you so much for providing this. I will test this out and provide feedback. (lucius fox at April 11, 2018 4:34 am) Hi Pete, thank you so much for providing this. After several weeks of review, this is returning the results I am looking for. Is there a way to exclude “hard-to-borrow” and 5-ticker stocks from this scan? Stocks like SSDOY, ZYZZZ, DSEEY are being populated in my scan; taking up space in my scanner. I have my scan limited to 10 results due to screen real estate. Hopefully you received your rock star contribution about a month ago. 🙂 (lucius fox at May 2, 2018 6:05 am) Awesome. Glad to know you got things working. And thanks so much for the contribution. As to refining the results further, removing 5-character tickers and hard-to-barrow. The only thing you have to work with are the two filters at the very top of the scan. Next to the word “Scan In:” at the top left. You can select from a category, a personal watchlist or a public watchlist. In your case, you will need to create a personal watchlist that excludes all the ticker symbols you want to avoid. Then select that as one of the two main filters at the top left. (Pete Hahn at May 2, 2018 7:54 am) How would I change the code to search for 5% gap up? Change the -2 to a 5? Or a +5? Also is there something I need to do about the ><. Also how does this differ from the AfterHours_Percent_Change Study in thinkorswim? (James Bennett at August 9, 2018 10:07 pm) You never need to include a plus sign for positive numbers. So just a plain 5 will due. If you look carefully at the code, check the last three lines. There is s comment that explains there are two different scan signals. One is commented out, the other is active. Just move the comment mark “#’. As to the built in version. I was not aware of it. This code is result of of a previous request and most of it was copied from what the viewers posted. As I compare this result to the built in version I find they are very much the same. So the author of that original post must have started out with code from the built in version and tried to modify it. (Pete Hahn at August 10, 2018 7:42 am) Are there benefits to using this versus the built in version? Just trying to see what would be most effective. Thanks so much for all of your work. You have a true talent. (James Bennett at August 10, 2018 2:45 pm) Honestly if I had known about the built in version I would have used that one in my response to this post. It is possible it was not there at the time of this post. I usually try to check for built-in solutions before writing code. But I don’t remember this one. (Pete Hahn at August 10, 2018 2:49 pm) I wonder if they made it off of yours? (James Bennett at August 10, 2018 4:38 pm)