Gap Fill Indicator for ThinkorSwim - Highlight Potential Gap

BenTen

Administrative
Staff member
Staff
VIP
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:

jerrydinvestor

New member
2019 Donor
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:

BenTen

Administrative
Staff member
Staff
VIP
@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.
 

Mlentsch0

New member
VIP
@jerrydinvestor Is there a way to add this to a watch list scan as far as ones that have percentage and ones that dont? sorry still pretty new to trading but I think this will help greatly
 

xexykid

New member
@WIZARD Would it be possible to EXTEND the gap cloud to today if the gap is not filled (eg- A gap has formed 5 days back and we are trading below that gap since. So that gap cloud is seen until today)? Thanks
 

Ahmar824

Member
@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);
 

XeoNoX

Well-known member
VIP
@cswu1211 Would be interesting to see the formula, one method would be you can just grab the strike that the gap is at for the day or week and use the option chain probability to get the probability.
 

Thomas

Active member
VIP
would be interesting to see the formula, one method would be you can just grab the strike that the gap is at for the day or week and use the option chain probabilaty to get the probability.
Read through the thread, I think it's there in the first chart description?
 

jay2

Member
@Billions I don't have this script for gap fill odds. i discovered it on someone else's chart and wondering if someone can formulate one in here.
 

RickK

Active member
Hey all,
I've modified a script to suit my needs and for some reason I've gotten into a bit of trouble.
The original script is named TS_GapFill, but I've modified it to only display the day's
Half Gap between previous day's close and current day's open.

Two questions:
1. Not sure why my chart label is not displaying. Any ideas? It's probably something very simple
that I'm overlooking.
2. Regarding the calculation, am I right or wrong to set the MarketCloseTime to 15:59 instead
of 14:00? Doesn't the calculation use the 'close' of the 15:59 candle? I know this
seems like splitting hairs, but when applying to something like /ZS soybeans (which has
a very odd market closing time), there can be a great difference between the close of a
candle and the open of the next candle.

Thanks. Here's the code:
Code:
#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 #)

# This indicator has been modified from the original TS_GapFill to only display the instrument's
# daily half gap, a yellow line accompanied by a yellow label.  To change it back to the
# original, just un comment the commented plots below.

# New name: TS_HalfGap

# orig name: TS_GapFill
# http://www.thinkscripter.com
# [email protected]
# Last Update 28 Jan 2010

input marketOpenTime = 0930;
input marketCloseTime = 1559;

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);

#AddLabel(gapUp or gapDown, Concat(percentRemaining, " % Gap Remaining"), Color.WHITE);

###################################
#AddLabel(yc, " yc = " +  yc, Color.White);
#AddLabel(o, " o = " +  o, Color.White);


plot hg_plot = hg;
hg_plot.SetStyle(Curve.LONG_DASH);
hg_plot.SetDefaultColor(Color.yellow);

AddLabel(hg, "hgap=" +  hg, Color.yellow);

#end
 
Last edited:

Leo1015

Member
VIP
Hi @jerrydinvestor or @BenTen , I uploaded your indicator into TOS https://tos.mx/S5EYxw. I do get the violet colored gap, but I'm not getting the green and red shading. Could you advise what I'm doing wrong. Thanks.
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[/U [QUOTE="je...unfurl="true"]https://tos.mx/S5EYxw [/QUOTE]
 
Last edited:

Similar threads

Top