Gap % Remaining in watchlist

CHOZEN

New member
2019 Donor
Hello everyone, I was wondering if someone could write a script that shows the gap % remaining and can be added to a watchlist for ThinkorSwim. Thanks

 
Last edited by a moderator:

markos

Well-known member
VIP
@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:

BenTen

Administrative
Staff member
Staff
VIP
@markos Already posted that indicator here. @CHOZEN was looking for a watchlist column of the % of gap filled.

 
Last edited:

VicD

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

Prison Mike

Member
I cut up the code to use in a watchlist column. Could someone explain why this code won't work on mobile? Haven't figured out mobile watchlist yet.

Code:
input marketOpenTime = 0935;
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;

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

Big Ed

New member
hey guys how can I get this code to look back 2 days ago (or for yesterday)? I know its probably something simple but I dont have any experience writing scripts.



I cut up the code to use in a watchlist column. Could someone explain why this code won't work on mobile? Haven't figured out mobile watchlist yet.

Code:
input marketOpenTime = 0935;
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;

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

Slippage

Well-known member
hey guys how can I get this code to look back 2 days ago (or for yesterday)? I know its probably something simple but I dont have any experience writing scripts.

It already is looking at yesterday's close.
Ruby:
def yesterdaysClose = close(period = "DAY" )[1];

If you want to make it look back a day further you can adjust the index at the end of that line telling it to look back 2 periods instead of 1.
Ruby:
def yesterdaysClose = close(period = "DAY" )[2];
 

Big Ed

New member
It already is looking at yesterday's close.
Ruby:
def yesterdaysClose = close(period = "DAY" )[1];

If you want to make it look back a day further you can adjust the index at the end of that line telling it to look back 2 periods instead of 1.
Ruby:
def yesterdaysClose = close(period = "DAY" )[2];


that didnt' fix it. do I have to fix all the [1] to [2] also in the whole code as well?.. if so I did that, and it still didnt work
 

Slippage

Well-known member
that didnt' fix it. do I have to fix all the [1] to [2] also in the whole code as well?.. if so I did that, and it still didnt work

I'm not clear on what exactly you want to see. If you were starting from scratch, without this code already written, what is it you'd be asking for?
 

Big Ed

New member
I'm not clear on what exactly you want to see. If you were starting from scratch, without this code already written, what is it you'd be asking for?

I want to know when a stock has gapped up and not filled the gap 2 days in a row or gaped down and not filled that gap down 2 days in a row
 

Slippage

Well-known member
This should work for a scan. I'm assuming a scan is what you want.

Ruby:
input minGapPercent = 2;
def buffer = close[2] * minGapPercent / 100;

def gapUp = open[1] > close[2] + buffer;
def gapUpFilled =
  Between(close[2] + .01, low[1], high[1])
  or Between(close[2] + .01, low, high)
;

def gapDown = open[1] < close[2] - buffer;
def gapDownFilled =
  Between(close[2] - .01, low[1], high[1])
  or Between(close[2] - .01, low, high)
;

plot scan = (gapUp and !gapUpFilled) or (gapDown and !gapDownFilled);
 

Big Ed

New member
This should work for a scan. I'm assuming a scan is what you want.

Ruby:
input minGapPercent = 2;
def buffer = close[2] * minGapPercent / 100;

def gapUp = open[1] > close[2] + buffer;
def gapUpFilled =
  Between(close[2] + .01, low[1], high[1])
  or Between(close[2] + .01, low, high)
;

def gapDown = open[1] < close[2] - buffer;
def gapDownFilled =
  Between(close[2] - .01, low[1], high[1])
  or Between(close[2] - .01, low, high)
;

plot scan = (gapUp and !gapUpFilled) or (gapDown and !gapDownFilled);

scan or watchlist is fine. I can make the watchlist dynamic. I did try this after script after hours and didnt return any matches. Does it have to run during trading hours for it to work or is there something wrong with it?
 

Slippage

Well-known member
scan or watchlist is fine. I can make the watchlist dynamic. I did try this after script after hours and didnt return any matches. Does it have to run during trading hours for it to work or is there something wrong with it?

It should work any time. Set the timeframe to D. I ran it just now with Scan in set to All Symbols and it finds thousands. It didn't find very many on the day I wrote it. Depends on what the market is doing. You can play with the minGapPercent value to get more/fewer results.
 

Big Ed

New member
It should work any time. Set the timeframe to D. I ran it just now with Scan in set to All Symbols and it finds thousands. It didn't find very many on the day I wrote it. Depends on what the market is doing. You can play with the minGapPercent value to get more/fewer results.

sorry but nothing is coming up, I have it set to D and just ran it this morning. I have several stocks that have gapped up two days in a row or gapped down 2 days in a row and nothing is coming up. I copied and pasted it as a custom study and ran it with D aggregation
 

Slippage

Well-known member
sorry but nothing is coming up, I have it set to D and just ran it this morning. I have several stocks that have gapped up two days in a row or gapped down 2 days in a row and nothing is coming up. I copied and pasted it as a custom study and ran it with D aggregation
2021-08-13-23-24-36.png

I get 3768 results right now, scanning in All Symbols with no other filters applied.
 

Similar threads

Top