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

I cut up the code to use in a watchlist column.

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));
 
Last edited by a moderator:

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

Anyone know how to count the number of arrow signals this indicator creates?

I'm looking to see how many times a certain percent gap up occurs without having to actual count the arrows manually.
It would just post a label like below saying 20 gap up arrows.

Thanks for any help!

Code:
input  gapPercent = 1;
def OpenGapPercent = Round((open - close[1]) / close[1] * 100 , 2) ;


AddLabel(yes, Concat("Gap Up " + "% ", OpenGapPercent  ), Color.red );

plot GapUp = open > close[1] && open > high[1] && OpenGapPercent > gapPercent;
GapUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


p0n8mjU.jpg
 
@STB Adding this code should give you the count of all arrows currently shown on the chart -
Code:
def count = TotalSum(GapUp);
AddLabel(1,"Gap Count = " + count, color.white);
 
  • Like
Reactions: STB
@Pensar one last question, would you know how to add a label for the last gap up day and the current gap day.
For example if the last gap up day was a week ago it would have that days volume and also the current days gap up volume.

Thanks
 
@STB That may be a bit difficult for me to code. I can try, but it would likely take me a good while to figure out (unless I'm overthinking it, have done that before). @mashume, @Welkin, or @diazlaz could likely do so easily, mentioning them here in case they wish to assist. 🙂
 
@Pensar one last question, would you know how to add a label for the last gap up day and the current gap day.
For example if the last gap up day was a week ago it would have that days volume and also the current days gap up volume.

Thanks
like this?
Code:
input  gapPercent = 1;
def bn = barnumber();
def OpenGapPercent = Round((open - close[1]) / close[1] * 100 , 2) ;
AddLabel(yes, "Gap Up " + "% "+OpenGapPercent, Color.red );
plot GapUp = open > close[1] && open > high[1] && OpenGapPercent > gapPercent;
GapUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
def gapbn = if GapUp then bn else gapbn[1];
def gapvol = if GapUp then volume else gapvol[1];
def gapbn2 = if gapbn != gapbn[1] then gapbn[1] else gapbn2[1];
def gapvol2 = if gapvol != gapvol[1] then gapvol[1] else gapvol2[1];
AddLabel(1,AbsValue(bn-gapbn2)+" Days Ago | "+gapvol2+" Vol", Color.GRAY);
AddLabel(1,AbsValue(bn-gapbn)+" Days Ago | "+gapvol+" Vol", Color.WHITE);
 
Here is the code to display gap up / gap down as your watchlist column.
  • Green background = stock has gapped up
  • Red background = stock has gapped down
It also includes the price difference between the previous close and today's opening. In other words, if you see -1.5, that means the ticker has a gap of $1.50 to the downside.

R5zkwac.png


thinkScript Code

Code:
# Gap watchlist column
# Assembled by BenTen at UseThinkScript.com

input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
def close = close(period = aggregationPeriod)[1];

def o = open;
def c = close;

def gu = o > c;
def gd = o < c;

def diff = o - c;
def av = (o + c) / 2;
def d = (diff / av) * 100;
plot gap = d;

AssignBackgroundColor(if gap > 0 then color.dark_green else if gap < 0 then color.dark_red else color.gray);
 
In a nut shell the entire thinkscript feature set is available to you in Studies. A subset of thinkscript is available in conditional orders, custom quotes, study alerts, and study filters. Sadly the only way to discover what functionality is not available is to write your scripts and see if they blow up or they execute without error.

In reading some of the recent posts, it seems that the display column on the left side of TOS can be customized with studies in with the same method that you just described. In this way I would be able to customize a column of what shows up on my watch list. If that can be done then I could have a column displaying the morning gap direction of either "UP" or "DOWN" on my watch list. Is that correct?

That's called a custom quote in TOS parlance. And it may be correct, it depends on the complexity of the script. So here's an example of a Gap Analysis, I define a gap as everything for today is high(lower) than yesterday and so I made that the default. Others think it's simply the OPEN today is higher(lower) than yesterday's close, so that's the option. I divide the custom column in two parts, 1) the main logic of what I want to do I put into a chart study, and 2) in the custom quoted I reference that chart study. So here's the two scripts:

1) GapAnalysisSTUDY.ts

Code:
declare lower;

input gapType = { default highLow, openClose };

def _gapUp;
def _gapDown;
switch( gapType ) {
  case highLow:
    _gapUp    = low > high[1];
    _gapDown  = high < low[1];
  case openClose:
    _gapUp    = open > close[1];
    _gapDown  = open < close[1];
}

plot GapUp    = _gapUp;
plot GapDown  = _gapDown;

2) Gap custom column:

Code:
def GapUp = GapAnalysis().GapUp;
def GapDown = GapAnalysis().GapDown;
plot Gap = if GapUp then 100 else if GapDown then -100 else 0;
Gap.AssignValueColor( if GapUp then Color.UPTICK else if GapDown then Color.DOWNTICK else CreateColor( 236, 236, 236 ) );
AssignBackgroundColor( if GapUp then Color.UPTICK else if GapDown then Color.DOWNTICK else CreateColor( 236, 236, 236 ) );

Note that in a custom quote you must have one and only one plot. The CreateColor( 236, 236, 236 ) should be replaced with your background color, this one works well with the GunMetal TOS scheme. A lot of people I've seen uses the scheme with the black background, if you're one of those you'd change the Create... to Color.Black. If you use the White scheme, you'd change it to Color.WHITE. This technique of coloring the plot and the background the same color so the numbers disappear, unless a line has focus then the whole line is some TOS chosen color, usually blue.
 
Ben, could you please make a version where it indicates a gap in percents instead of dollars?

Thank you.
 
@Wave Sure, here you go:

Code:
# Gap watchlist column
# Assembled by BenTen at UseThinkScript.com

input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
def close = close(period = aggregationPeriod)[1];

def o = open;
def c = close;

def gu = o > c;
def gd = o < c;

def diff = o - c;
def av = (o + c) / 2;
def d = (diff / av) * 100;

def nr = absValue(o - c) * 100 / o;
plot gap = nr;

AssignBackgroundColor(if d > 0 then color.dark_green else if d < 0 then color.dark_red else color.gray);
 
Ben - Thanks for this! Could you possible share the code for that RV column as well? There's a few I found throughout the site so I'm curious which one you are using there. Thanks.
 
@computernut You can find it here.

Script:

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN;
def PMV = if IsNaN(vol) then PMV[1] else vol;
def AV = AggregationPeriod.DAY;
def x = Average(Volume(period=AV)[1],60);
def y1 = Round((PMV/x),2);
def L = Lg(y1);
def p = if L>=1 then 0 else if L>=0 then 1 else 2;
def y2 = Round(y1,p);
plot z = y2;
z.assignValueColor(if z>=10 then color.CYAN else if z>=1 then createcolor(255,153,153) else createcolor(0,215,0));
 
Hello,

I am trying to make Labels for my charts. ATR and GAP labels.

Problem is my labels are using the Intraday data, I would like to see the information for the daily data. Daily ATR and Daily Gap.

I just need it to pull the daily aggregated information not sure how to do that.
i just want to see the daily gap and daily ATR on the intraday chart and not have the chart data effect the output.

If someone can please help me code this that would be very much appreciated. the frustration is real. Current code below

Current Gap label

plot Gap = round((open-close[1]) / open*100);

Gap.assignValueColor (if Gap>0 then color.LIGHT_GREEN else color.LIGHT_RED);

AddLabel(yes, Gap + "%", color = Color.WHITE);

Current ATR label
input ATRLength = 14;

def ATR = Round(Average(TrueRange(high, close, low), ATRLength), 2);
def iv = Round(close() * (imp_volatility() / 15.87), 3);
AddLabel(yes, Concat("ATR=", ATR), Color.YELLOW);
 
Last edited by a moderator:
@sawyersweetman

Code:
input agg = AggregationPeriod.DAY;
def open_agg = open(period = agg);
def high_agg = high(period = agg);
def low_agg = low(period = agg);
def close_agg= close(period = agg);
def iv_agg = imp_volatility(period = agg);

#Current Gap label
plot Gap = round((open_agg-close_agg[1]) / open_agg*100);

Gap.assignValueColor (if Gap>0 then color.LIGHT_GREEN else color.LIGHT_RED);

AddLabel(yes, Gap + "%", color = Color.WHITE);

#Current ATR label
input ATRLength = 14;

def ATR = Round(Average(TrueRange(high_agg, close_agg, low_agg), ATRLength), 2);
def iv = Round(close_agg * (iv_agg / 15.87), 3);
AddLabel(yes, Concat("ATR=", ATR), Color.YELLOW);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
425 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