Pre-Market Gap from Previous Close Scanner for ThinkorSwim

Status
Not open for further replies.

BenTen

Administrative
Staff member
Staff
VIP
Here is a pre-market gap scanner for ThinkorSwim that looks for stocks with a 1% gap up or down from the previous close. Mobius shared this in the thinkScript lounge.

Code:
# Scan PreMarket: Scan for 1 Percent gap from Previous Close
# Scan at 5 min aggregation or less
# Mobius
# Chat Room Request

def PrevClose = if SecondsTillTime(1555) == 0 and
                   SecondsFromTime(1555) == 0
                then close
                else PrevClose[1];
def ScanActive = if SecondsTillTime(0930) >= 0 and
                    SecondsFromTime(0730) > 0
                 then 1
                 else 0;
def ll = if ScanActive and !ScanActive[1]
         then low
         else if !ScanActive
         then double.nan
         else if ScanActive and low < ll[1]
         then low
         else ll[1];
def hh = if ScanActive and !ScanActive[1]
         then high
         else if !ScanActive
         then double.nan
         else if ScanActive and high > hh[1]
         then high
         else hh[1];
# Comment out (#) Plot NOT wanted
plot gapUP = if ScanActive and ll > PrevClose * 1.01
             then 1
             else 0;
#plot gapDN = if ScanActive and hh < PrevClose * .99
#             then 1
#             else 0;

Here is another one:

14:34 davidanderson: Hello guys, wondering if anyone here has the code for column that would show %gap pre market (like total, since yesterday's close at 4pm). it's (the number that corresponds to After_Hours_Percent_change. but I can't express it in a column. thank you
14:35 Mobius: sectors that are stable.. not out of favor with the general trading world.
14:35 bigworm: so not a sector that is really volatilie
14:42 AlphaInvestor: Column must be set to 1 minute, with Extended Hours Session turned on
14:42 AlphaInvestor: This isn't really a "gap" just a change after-hours or pre-market

Code:

Code:
# PM_AH_Chg

#09:26 GapnGo: This worked as a watchlist column until we had the last big TOS update.
#09:27 GapnGo: anyone know what the problem is with the script

#09:37 Mobius: Gap.. Since the introduction of GetTime(), watchlists and mobile applications seem to work more reliably using it rather than SecondsTillTime() or SecondsFromTime()

#09:52 Mobius: Gap.. I think this will get it going

def Post = getTime() > RegularTradingEnd(getYYYYMMDD());
def Pre = getTime() < RegularTradingStart(getYYYYMMDD());
def Closed  = Post or Pre;
def DayClose1 = if getTime() crosses RegularTradingEnd(getYYYYMMDD())
               then close
               else DayClose1[1];



#09:39 Mobius: Alpha.. I don't know why there'd be a difference but using cross vs crosses above does make a difference with some equities.

def DayClose2 = if getTime() crosses above RegularTradingEnd(getYYYYMMDD())
               then close
               else DayClose2[1];
#addLabel(1, "Prev Close = " + DayClose2);


def Change = Round((close - DayClose2), 2);
def Percent = Round(((close - DayClose2) / DayClose2) * 100, 2);

plot Xtended = If(Closed, Percent, 0);

AssignBackgroundColor(Color.BLACK);
Xtended.AssignValueColor(if !Closed then Color.YELLOW else if Percent > 0 then Color.GREEN else if Percent < 0 then Color.RED else color.gray);
 

Texan57

New member
2019 Donor
I can't seem to get this sucker to work, are there any other configurations I need to set? I'm essentially just trying to create scans that show me premarket gap up, low float (outstanding shares), relative volume, and lower price.
 

BenTen

Administrative
Staff member
Staff
VIP
No configurations needed. Pay attention to the following lines:

Code:
plot gapUP = if ScanActive and ll > PrevClose * 1.01
             then 1
             else 0;
#plot gapDN = if ScanActive and hh < PrevClose * .99
#             then 1
#             else 0;

By default this is scanning for gap ups. If you want to scan for gap downs then add # in front of the plot gapUp and remove the # for plot gapDN.

Something like this:

Code:
#plot gapUP = if ScanActive and ll > PrevClose * 1.01
#             then 1
#             else 0;
plot gapDN = if ScanActive and hh < PrevClose * .99
             then 1
             else 0;
 

wilmanv

Member
VIP
How Add scan script to scan options on ThinkorSwim, thanks

Code:
# PreMarket Gap Down
 # Mobius
 # Scan Aggregation should be set to 5 min
 
def price = close;
 def last = if secondsFromTime(1600) == 0 and
               secondsTillTime(1600) == 0
            then price[1]
            else last[1];
 def LabelTime = if SecondsFromTime(0800) > 0 and
                    SecondsTillTime(1000) >= 0
                 then 1
                 else 0;
 def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
 def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
 def Highprice = if isRollover and beforeStart
               then price
                else if beforeStart and price > Highprice[1]
                     then price
                else Highprice[1];
 plot PreMarketGapDn = if highPrice < (last * .99) # 1% Gap Down
                       then 1
                       else 0;

Code:
# Premarket Gap-Up Scan
# Run Scan at premarket on one minute aggregation.
# Will not be accurate after hours or prior to midnight
# Mobius

def MarketClosePrice = if getTime() crosses RegularTradingEnd(getYYYYMMDD())
                       then close
                       else MarketClosePrice[1];
plot PreMarketScan = getTime() < RegularTradingStart(getYYYYMMDD()) and
close(priceType = "LAST") > MarketClosePrice * 1.01;
# End Scan Code
 
Last edited by a moderator:

BenTen

Administrative
Staff member
Staff
VIP
@wilmanv Switch over to your Scanner tab, select a filter, under the thinkScript Editor, paste the code above in there.

HOf3wEk.png
 

tomsk

Well-known member
VIP
Just to be sure that EVERYONE understands how to interpret this, at the end of the scan there is a section of code like so:

# Comment out (#) the ONE not needed
plot BullScan = close crosses above SHigh;
#plot BearScan = close crosses below SLow;

Notice that there are some lines that are commented out with (#) - Those would be ignored by the scanner, only for human consumption
In the above example only the bullish scan is enabled.

In order to turn this into a bearish scan, just switch the commented lines like so

# Comment out (#) the ONE not needed
#plot BullScan = close crosses above SHigh;
plot BearScan = close crosses below SLow;

Compare those differences - it really is that simple.

Just REMEMBER that there can only be one active plot defined or the scanner would complain.

@BenTen many thanks
 
Last edited by a moderator:

tomsk

Well-known member
VIP
@Trismagistus The scan code that @BenTen posted at the beginning of this thread does work
All you need to do is to run this before the market opens at aggregations of 5 minutes or less
Pay attention as to whether you want to scan for gap up or gap down
The code is configured for both.

Since the scanner only accepts a single plot statement you'll need to comment out the scan you do not wish
I have written some instructions on how to best manage this.
Read this for further info

https://usethinkscript.com/threads/how-to-use-the-tos-scanner.284/#post-10505
 

HighBredCloud

Well-known member
VIP
@BenTen Yeah...perhaps you're right...I did read about it repainting. I just wanted to know that I was able to input everything correctly into the scan as I have never done this before. ANYWAYS...do you know of ANY good scans such as % gainers and losers but ones that show up practically at opening bell...and not hit later on such as 15 to 20 min into the market open such as the % gainers/losers found in default TOS scan... I guess what I am looking for is stocks that are going either up or down premarket and continue on that trend once opening bell rings. Would the HULL scan be more suitable for such a scan or are there better scans out there that I am not aware of? I
 

tomsk

Well-known member
VIP
In the past several years I have seen several versions of premarket gap up and gap down scans that Mobius had posted. You might like to check out the OneNote archive that @markos posted before. I'm almost positive it is archived there.
 

HighBredCloud

Well-known member
VIP
@tomsk I will look for them...Thanks for the suggestion. Would I import them the same way as this particular scan and under conditions also select "True" as BenTen suggested? Sorry for all the questions...I never used custom scans before...only ones I set that met my criteria...
 

tomsk

Well-known member
VIP
You will find that all the scans that Mobius writes are simple and effective and undoubtedly produce results. Hence you will not need to reference them as a separate study but import it directly into the scanner. Since you're new to scans, you might like to read up this great tutorial that @markos wrote.
 

HighBredCloud

Well-known member
VIP
@markos Not sure if you want to talk about the following here or if it would be better to make a separate post...but after reading the ONEDRIVE...I think it would be important to include and touch upon the differences between the LAST price vs CLOSE price when scanning as well as VOLUME vs AVG VOLUME...Something to note would also be what is best setting is as far as periods go...default is set to 50 SMA...

As far as the custom scans that were developed by many here. I think it would be nice to clarify if it's better to run the scan as a study as shown in the video or simply imported the study using the ThinkScript as suggested by some coders...It can be a bit confusing for a new user to the scanner especially with what conditions need to be added for the scans to work properly.
 

RPrado

Member
Lifetime
Guys, I am having trouble getting this scanner to work. I tried during pre-mkt hours today, right after 7:30 but nothing was shown. Can anyone help me out? Thanks.
 

tomsk

Well-known member
VIP
@RPrado Try running the scan again tomorrow premarket hours
According to the notes in the scan code scan this at aggregation period of 5 min or less,
 

darkelvis

Member
To add on to my 90/120/180 scalp strategy I'm looking to make a scanner that will scan for stocks that are up at least 80% from yesterday's close, which is pretty simple (https://tos.mx/D8xAFIC), but I'm trying, I'm trying to find a way that it will scan during PM and AH, and for the life of me I can't think of anything I can add to it that will allow PM/AH scanning. Any ideas from any thinkscript wizards here?
 

tomsk

Well-known member
VIP
@darkelvis Here is your pre market gap up scan that i have set at 80% above yesterday's close. Unless you are looking at penny stocks, chances of that happening on main board stocks in the S&P 500 or NASDAQ are pretty slim. I adjusted the percentage down to 5% and obtained 1 result - MS.

Here then is your scan code to be run pre market which is set to 80%. Make sure you set your aggregation period to 5 minutes or less. I set mine to 1 min.

Code:
# Pre Market Gap Up Scan
# tomsk
# 1.16.2020

# Run Scan at premarket on one minute aggregation.

def lastPrice = if getTime() crosses RegularTradingEnd(getYYYYMMDD()) then close else lastPrice[1];
plot scan = getTime() < RegularTradingStart(getYYYYMMDD()) and close > lastPrice * 1.8;
# Pre Market Gap Up Scan
 

ripcityman

New member
I used this scanner this morning ( 1/21/2020). I had no issues finding stocks. In fact I had had to add market cap and price filters to reduce my list to less than 200. This morning was a big down day, and I was surprised that I saw so many stocks that had gapped up. When I charted them however, almost all of them except GOLD were lower than Mondays close. I made sure the last two lines of the code were commented out ( .99 or gap down). I also ended up commenting out the 1.02 lines and ran the scan for gap down. Still got all down stocks. I am using the exact same code as in the top of this post. Any advise would be appreciated.

Would this scan run using ON Demand?? I tried that but no results.
 
Status
Not open for further replies.

Similar threads

Top