Gap Indicator, Watchlist, Label, Scan for ThinkorSwim - Highlight Potential Gap

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
This indicator will overlay potential gaps on your chart. Highlighted so it's easier to see as the stock gap up, gap down, or filling the gap.

smM8Ipj.png


thinkScript Code

Rich (BB code):
# TS_GapFill
# [www.thinkscripter.com]
# [email protected]
# Last Update 28 Jan 2010

input marketOpenTime = 0930;
input marketCloseTime = 1615;

def yesterdaysClose = close(period = "DAY" )[1];

def secondsFromOpen =  secondsFromTime(marketOpenTime);
def secondsTillClose = secondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;

rec regularHoursOpen = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(getDay() != getDay()[1]) then open else regularHoursOpen[1];

def newDay = if getDay() != getDay()[1] then 1 else 0;

rec regHoursHigh = if newDay then high else if marketOpen then
if high > regHoursHigh[1] then high else regHoursHigh[1] else high;
rec regHoursLow = if newDay then low else if marketOpen then
if low < regHoursLow[1] then low else regHoursLow[1] else low;

def yc = if marketOpen then yesterdaysClose else double.nan;
def o = if marketOpen then regularHoursOpen else double.nan;
def hg = o + (yc - o) / 2;

def gapUp = if yc < o then 1 else 0;
def gapDown = if yc > o then 1 else 0;

def gapRemaining = if gapUp then
Max(regHoursLow - yc, 0) else
if gapDown then Max(yc - regHoursHigh, 0) else 0;

def percentRemaining = 100 * gapRemaining / AbsValue(yc - o);
def gapFilled = if percentRemaining == 0 then 1 else 0;
def halfGapFilled = if percentRemaining <= 50 then 1 else 0;

plot gH = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then regHoursLow else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then yc else double.nan;
plot gL = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then yc else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then regHoursHigh else double.nan;
plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1]
then hg else double.nan;

gH.SetPaintingStrategy(paintingStrategy.DaSHES);
gH.AssignValueColor(if gapDown then color.darK_red else color.dark_green);
gL.SetPaintingStrategy(paintingStrategy.DASHES);
gL.AssignValueColor(if gapDown then color.darK_red else color.dark_green);
hGF.setStyle(curve.LONG_DASH);
hGF.SetDefaultColor(color.dark_gray);
gH.hideBubble();
gL.hideBubble();
hgF.hideBubble();

AddCloud(gH, gL, color.violet, color.violet);
#AddChartLabel(gapUp or gapDown, concat(percentRemaining, " % Gap Remaining" ), color.white);

Shareable Link

https://tos.mx/ujmdv8
 
Last edited:
Here's a small tweak to the original code. With this tweak, the Potential gap remaining is shown in the form of a number on the top left side.

To improve it further I added a condition in the gap fill. If the gap is already filled then the label will be in red. Indicating that very less likely the stock will go up. If the label is green and it shows percentage that means the stock has room for potential gap fill.

Gap filled and red color

uqZPjCW.png


Gap remaning with % and Green color

d7BcbQe.png


There are multiple ways to use this indicator. If the gap is already filled I would place my bet's on buying puts(or refrain from buying any calls) but it can get risky since 0% gap does not mean that stock cannot run up anymore.

When Gap % is in green we can buy calls and wait for upward price direction(Potential gap fill) can be seen (Bull move).**

https://tos.mx/S5EYxw
 
Last edited:
@CHOZEN Maybe this will work...GapFill_TS

By popular request, here is a study that will display a gap opening and it’s 50% fill line. The plot will dynamically collapse as the gap is filled. It will work on any intraday chart timeframe, 24 hours or regular trading hours so long as the two affected trading days are showing. (Edit: If you would like the % Gap Filled box in the upper left, uncomment out the last line of code by removing the #)



Rich (BB code):
#- TS_GapFill 
#- http://www.thinkscripter.com 
#- [email protected] 
#- Last Update 28 Jan 2010 

input marketOpenTime = 0930; 

input marketCloseTime = 1615; 

def yesterdaysClose = close(period = "DAY")[1]; 

def secondsFromOpen =  secondsFromTime(marketOpenTime); 

def secondsTillClose = secondsTillTime(marketCloseTime); 

def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0; 

rec regularHoursOpen = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or 

(getDay() != getDay()[1]) then open else regularHoursOpen[1]; 

def newDay = if getDay() != getDay()[1] then 1 else 0; 

rec regHoursHigh = if newDay then high else if marketOpen then 

if high > regHoursHigh[1] then high else regHoursHigh[1] else high; 

rec regHoursLow = if newDay then low else if marketOpen then 

if low < regHoursLow[1] then low else regHoursLow[1] else low; 

def yc = if marketOpen then yesterdaysClose else double.nan; 

def o = if marketOpen then regularHoursOpen else double.nan; 

def hg = o + (yc - o) / 2; 

def gapUp = if yc < o then 1 else 0; 

def gapDown = if yc > o then 1 else 0; 

def gapRemaining = if gapUp then 

Max(regHoursLow - yc, 0) else 

if gapDown then Max(yc - regHoursHigh, 0) else 0; 

def percentRemaining = 100 * gapRemaining / AbsValue(yc - o); 

def gapFilled = if percentRemaining == 0 then 1 else 0; 

def halfGapFilled = if percentRemaining <= 50 then 1 else 0; 

plot gH = if (gapUp and !gapFilled and marketOpen and !newDay[-1]) 

then regHoursLow else if (gapDown and !gapFilled and marketOpen and !newDay[-1]) 

then yc else double.nan; 

plot gL = if (gapUp and !gapFilled and marketOpen and !newDay[-1]) 

then yc else if (gapDown and !gapFilled and marketOpen and !newDay[-1]) 

then regHoursHigh else double.nan; 

plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1] 

then hg else double.nan; 

gH.SetPaintingStrategy(paintingStrategy.DaSHES); 

gH.AssignValueColor(if gapDown then color.darK_red else color.dark_green); 

gL.SetPaintingStrategy(paintingStrategy.DASHES); 

gL.AssignValueColor(if gapDown then color.darK_red else color.dark_green); 

hGF.setStyle(curve.LONG_DASH); 

hGF.SetDefaultColor(color.dark_gray); 

gH.hideBubble(); 

gL.hideBubble(); 

hgF.hideBubble(); 

AddCloud(gH, gL, color.gray, color.gray); 

#AddChartLabel(gapUp or gapDown, concat(percentRemaining, " % Gap Remaining"), color.white); 

#end
 
Last edited by a moderator:
@markos Already posted that indicator here. @CHOZEN was looking for a watchlist column of the % of gap filled.

 
Last edited:
This indicator adds a custom column to your watchlist of stocks that show when a gap has formed on the daily chart.
  • Red = Gap down
  • Green = Gap up
  • White = No gap

xYJalTL.png

RSsgb9w.png


thinkScript Code

Code:
# Billy Bob's Better Gap Indicator for Column alert
input Detect_Gaps_By = {default "percent", "dollars"};
input Min_Gap_Size = 1.0;

# Define Candle Body
def bodyTop = Max(open, close);
def bodyBottom = Min(open, close);

# Define a gap and its direction
def MinGapSize;
switch (Detect_Gaps_By) {
case "percent":
MinGapSize = Min(close[1] * (Min_Gap_Size/100),5);
case "dollars":
MinGapSize = Min_Gap_Size;
};

def GapUp = bodyBottom - bodyTop[1] >= MinGapSize;
def GapDown = bodyTop - bodyBottom[1] <= -MinGapSize;

plot isGap = if GapUp then 1 else if GapDown then 2 else 0;
isgap.AssignValueColor(if isgap ==1 then color.green else if isgap == 2 then color.red else color.white);

AssignBACKGROUNDColor(if isgap ==1 then color.green else if isgap == 2 then color.red else color.white);

Shareable Link

https://tos.mx/wMCwUM
Credit:
 

Attachments

  • xYJalTL.png
    xYJalTL.png
    31.3 KB · Views: 150
  • RSsgb9w.png
    RSsgb9w.png
    27.6 KB · Views: 134
@leonidas I just checked, it doesn't work on the daily for me either. Hourly and anything lower than that shouldn't have any problem.
 
@Charlie.mp3 @Simteaz Under your current Watchlist, left click on the gear icon > Customize > Browse and select Custom Quote. Click any of the Custom Quote and select it. A new window will popup. Paste your code in there and hit okay.
 
@J007RMC That Gap % label...did you get that from a code found here? And does this tell you how much the gap filled on a gap down or how much it gapped up and fell?
 
The indicator shows percent filled and Im pretty dure I found the label here.
Code:
# TS_GapFill
# [email protected]
# Last Update 28 Jan 2010
# Last Update 16 Jan 2019 by Jerry Investor
input marketOpenTime = 0930;
input marketCloseTime = 1615;
def yesterdaysClose = close(period = "DAY" )[1];
def secondsFromOpen =  SecondsFromTime(marketOpenTime);
def secondsTillClose = SecondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;
rec regularHoursOpen = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(GetDay() != GetDay()[1]) then open else regularHoursOpen[1];
def newDay = if GetDay() != GetDay()[1] then 1 else 0;
rec regHoursHigh = if newDay then high else if marketOpen then
if high > regHoursHigh[1] then high else regHoursHigh[1] else high;
rec regHoursLow = if newDay then low else if marketOpen then
if low < regHoursLow[1] then low else regHoursLow[1] else low;
def yc = if marketOpen then yesterdaysClose else Double.NaN;
def o = if marketOpen then regularHoursOpen else Double.NaN;
def hg = o + (yc - o) / 2;
def gapUp = if yc < o then 1 else 0;
def gapDown = if yc > o then 1 else 0;
def gapRemaining = if gapUp then
Max(regHoursLow - yc, 0) else
if gapDown then Max(yc - regHoursHigh, 0) else 0;
def percentRemaining = 100 * gapRemaining / AbsValue(yc - o);
def gapFilled = if percentRemaining == 0 then 1 else 0;
def halfGapFilled = if percentRemaining <= 50 then 1 else 0;
plot gH = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then regHoursLow else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then yc else Double.NaN;
plot gL = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then yc else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then regHoursHigh else Double.NaN;
plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1]
then hg else Double.NaN;
gH.SetPaintingStrategy(PaintingStrategy.DASHES);
gH.AssignValueColor(if gapDown then Color.DARK_RED else Color.DARK_GREEN);
gL.SetPaintingStrategy(PaintingStrategy.DASHES);
gL.AssignValueColor(if gapDown then Color.DARK_RED else Color.DARK_GREEN);
hGF.SetStyle(Curve.LONG_DASH);
hGF.SetDefaultColor(Color.DARK_GRAY);
gH.HideBubble();
gL.HideBubble();
hGF.HideBubble();
AddCloud(gH, gL, Color.VIOLET, Color.VIOLET);
AddLabel(yes, Concat(percentRemaining, " % Gap Remaining" ),if percentRemaining > 0 then Color.green else Color.Red);
 
Hi everyone,

I would like to build a scan that looks for certain criteria but i am not sure if it's possible to do this with the built in TOS scanner tools or if a custom scanner is needed.
Basically, the main criteria i am looking for is to find stocks that have gapped up a certain % intraday within the last 20 days. For example, find all stocks that gapped up at least 30% intraday in the last 20 days. It should also scan with pre-market prices included, not only regular hours.

Is it possible to build a scan like this?

Any help is much appreciated.
 
@zappi

Code:
# TS_GapFill
# [email protected]
# Last Update 28 Jan 2010
# Last Update 16 Jan 2019 by Jerry Investor

input marketOpenTime = 0930;
input marketCloseTime = 1615;

def yesterdaysClose = close(period = "DAY" )[1];

def secondsFromOpen =  SecondsFromTime(marketOpenTime);
def secondsTillClose = SecondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;

rec regularHoursOpen = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(GetDay() != GetDay()[1]) then open else regularHoursOpen[1];

def newDay = if GetDay() != GetDay()[1] then 1 else 0;

rec regHoursHigh = if newDay then high else if marketOpen then
if high > regHoursHigh[1] then high else regHoursHigh[1] else high;
rec regHoursLow = if newDay then low else if marketOpen then
if low < regHoursLow[1] then low else regHoursLow[1] else low;

def yc = if marketOpen then yesterdaysClose else Double.NaN;
def o = if marketOpen then regularHoursOpen else Double.NaN;
def hg = o + (yc - o) / 2;

def gapUp = if yc < o then 1 else 0;
def gapDown = if yc > o then 1 else 0;

def gapRemaining = if gapUp then
Max(regHoursLow - yc, 0) else
if gapDown then Max(yc - regHoursHigh, 0) else 0;

def percentRemaining = 100 * gapRemaining / AbsValue(yc - o);
def gapFilled = if percentRemaining == 0 then 1 else 0;
def halfGapFilled = if percentRemaining <= 50 then 1 else 0;

plot gH = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then regHoursLow else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then yc else Double.NaN;
plot gL = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
then yc else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
then regHoursHigh else Double.NaN;
plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1]
then hg else Double.NaN;

gH.SetPaintingStrategy(PaintingStrategy.DASHES);
gH.AssignValueColor(if gapDown then Color.DARK_RED else Color.DARK_GREEN);
gL.SetPaintingStrategy(PaintingStrategy.DASHES);
gL.AssignValueColor(if gapDown then Color.DARK_RED else Color.DARK_GREEN);
hGF.SetStyle(Curve.LONG_DASH);
hGF.SetDefaultColor(Color.DARK_GRAY);
gH.HideBubble();
gL.HideBubble();
hGF.HideBubble();

AddCloud(gH, gL, Color.VIOLET, Color.VIOLET);
AddLabel(yes, Concat(percentRemaining, " % Gap Remaining" ),if percentRemaining  > 0 then Color.green else Color.Red);
 
@Notorious07 %Change Close to Open With background color aggregate it on daily.

Code:
#%Change Close to Open With background color aggregate it on daily.

plot change = Round(((open("period" = AggregationPeriod.DAY)-close("period" = AggregationPeriod.DAY)[1])/(close("period" = AggregationPeriod.DAY)[1]))*100, 2);

AssignBackgroundColor(if change > 0 then Color.DARK_GREEN else Color.DARK_RED);
 
Is it possible to create a script for a Watchlist column that measures the % between the previous close and market open ? (Gap %)

Code:
#%Change Close to Open With background color aggregate it on daily.

plot change = Round(((open("period" = AggregationPeriod.DAY)-close("period" = AggregationPeriod.DAY)[1])/(close("period" = AggregationPeriod.DAY)[1]))*100, 2);

AssignBackgroundColor(if change > 0 then Color.DARK_GREEN else Color.DARK_RED);


This one is only change from open to current price. Which is way better than %Change column in my opinion that's standard as that measures from Close, this measures from open (if day trading this is more relevant)
Code:
################################
# sdi_pctChgFromOpen: plots the percentage change from the opening price in a way that is appropriate for a watchlist custom column.
#hint: plots the percentage change from the opening price in a way that is appropriate for a watchlist custom column. rev: 1.0 http://www.smallDogInvestor.com 
# author: allen everhart
# date: 22june2013
# copylefts reserved. This is free software. That means you are free
# to use or modify it for your own usage but not for resale. 
# Help me get the word out about my blog by keeping this header

# in place.
plot x=round(100*((close/open)-1),1);
x.assignValueColor( if x < 0 then color.RED else color.Dark_GREEN);
#################
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
462 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top