After Hours Gap Scanner


Category:
0
0

I’m trying to put together a scan that will only be run extended hours (pre-market). It will simply compare the current after hours price with the previous trading day close. Here’s what I have:

 

def percent_change = 8.00;
def yest_closing_price = close[500];
def afterhours_percent_change = 100 * ((close - yest_closing_price) / yest_closing_price ) ;
plot scan = afterhours_percent_change >= percent_change;

 

My crude way to get the closing price of the last regular trading session (yest_closing_price)is to manually find the number of bars from the current time to the closing price of the last regular trading session.  Surely there is a more elegant way to do this with code?

RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on January 15, 2018 11:49 am
4649 views
1
Private answer

The solution depends whether you are trying to work with Futures or Stocks. This solution will be provided for use with stocks.

For this solution I am pulling code used in the following video: https://www.hahn-tech.com/thinkorswim-alert-high-low-version-two/

We must use a time value in order to look back and get the prior day close. But even after grabbing that closing value we must then carry that value forward into the next trading session. This requires us to use recursion. First, let’s see what it takes to mark the location of the previous day’s regular session closing price:

input marketClose = 1600;
def closeCounter = SecondsTillTime(marketClose);
def regSessEnd = closeCounter[-1] == 0;

So we have a variable named regSessEnd that is true only on the last regular session bar of the chart. Next, we’ll set the value of a recursive variable to the close of the bar, only when regSessEnd is true. Then for every other bar on the chart we’ll assign the variable’s previous value.

rec priorDayClose = if regSessEnd then close else priorDayClose[1];

It helps if we can visualize things so let’s add one more line of code to plot that priorDayClose variable:

plot data = priorDayClose;

Screenshot is included below showing how this plots.

IMPORTANT NOTE:

The scan engine in Thinkorswim does not use local time zone. So be sure to realize this when setting the time. In this example I have the time input set to 1600, which is Eastern time zone. Which is required for running scans, regardless where you are located. This may be subject to change in the future. So if you run into issues you can’t explain through other means, consider playing around with the time input so see if they have changed this requirement.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on January 15, 2018 2:47 pm
0

Is there a way to see the exact code all put together in a form I could copy paste? Still learning the ins and outs of this thinkscript but this is exactly what I am looking for.

( at August 6, 2018 7:12 am)
0

Sure thing. In fact I already did this in response to a later post. Look for the answer I posted on April 10, 2018. https://www.hahn-tech.com/ans/help-making-pre-market-gap-scanner-more-efficient/

( at August 6, 2018 7:23 am)
0
Just for clarity Pete, the code currently in the original thread should pick up stocks that are gapping up after the 4pm close correct? I want to run this scan at night only after the market session and only want to see fresh stocks moving after hours which means their current price is greater than their close price the day of. I dont want it to see anything that did not start moving the evening before
( at November 19, 2019 4:40 am)
0
Seems to me from the blue line on the screenshot the code captures and holds the 4:00 pm close.
( at November 19, 2019 7:30 am)