Pre-market Gap from Previous Close Scanner for ThinkorSwim

BenTen

BenTen

Administrative
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;
 
Texan57

Texan57

New member
2019 Donor
I can't seem to get this sucker to work, are there any other configurations I need to set?
 
BenTen

BenTen

Administrative
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

wilmanv

Member
Warehouse
How Add scan script to scan options on ThinkorSwim, thanks
 
Last edited by a moderator:
BenTen

BenTen

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

 
T

Trismagistus

New member
I get "no matching symbols" when I run this script (as is) in a custom scanner. I'm not clear on the effects of the PrevClose and ScanActive variables. Does this script need to be run before/after US market hours? Or are there any other environmental conditions that need to be set (or need to be true/false) in order for this script to produce output?

I have a screenshot of what I'm seeing, if it'd help. Thanks in advance. -- Tris
 
T

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

 
R

RPrado

Member
VIP
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.
 
T

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,
 
D

dvorakm

New member
2019 Donor
VIP
Do you check the "include extended hours trading session" for this scan?
 
R

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.
 
R

ripcityman

New member
Would this scan run using ON Demand?? I tried that but no results.
 
E

ExactlyMyPoint

New member
OK, OK, I am an OCD programmer. I modified it slightly to make it a bit more gooder and you don't have to delete anything. Just set the inputs in your scan.

-------------------------------------------------------------------

Code:
input percentGap = 0.5;
input direction = {default Up, Down};

def dnColor = 5;    # red
def upColor = 6;    # green

def arrowColor;
def arrowDirection;

def marketOpen    = 0930;
def marketPreOpen = 0730;
def marketClose   = 1555;

def PrevClose = if SecondsTillTime(marketClose) == 0 and
                   SecondsFromTime(marketClose) == 0
                then close
                else PrevClose[1];
def ScanActive = if SecondsTillTime(marketOpen) >= 0 and
                    SecondsFromTime(marketPreOpen) > 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];

def isPlot;
def gapChange;

switch (direction)
{
    case Up:
        arrowColor = upColor;
        arrowDirection = PaintingStrategy.BOOLEAN_ARROW_UP;
        gapChange = 1.0 + (percentGap / 100.0);

        isPlot = if ScanActive and ll > PrevClose * gapChange
                 then 1
                 else 0;

    case Down:
        arrowColor = dnColor;
        arrowDirection = PaintingStrategy.BOOLEAN_ARROW_DOWN;
        gapChange = 1.0 - (percentGap / 100.0);

        isPlot = if ScanActive and hh < PrevClose * gapChange
                 then 1
                 else 0;
}

plot GapPlot = (isPlot == 1);

GapPlot.SetPaintingStrategy(arrowDirection);
GapPlot.SetDefaultColor(GetColor(arrowColor));
 
Last edited:

Top