# Gap Stats Labels for ThinkorSwim

#### Sir_Gallywix

##### New member
Hello,
I was wondering if anyone had a script that would show the gap stats of a company. Like how many times it has gapped up above 20% the average spike % after open does it close below or above open. Thanks.

Something like this.

Last edited by a moderator:
d12
Gap Stats Labels revised 10/4/22
Full revised script below

Ruby:
``````# Archive Name: Earnings Statistics Paris
# Archive Section: Scripts-Earnings
# Suggested Tos Name: EarningsStatistics_Paris
# Archive Date: 5.07.2018
# Archive Notes:
# Earnings Statisques
# Paris
# 5.6.2018

# This study displays the following earnings related info/stats:
#
# ++ Vertical lines that mark each earnings cycle and earnings date
# ++ Bubble that measures the gap and percentage moves post earnings
# ++ Total number of gaps ups versus gap downs
# ++ Average gap ups versus gap downs
# ++ Average percentage moves up versus percentage moves down
# ++ If the next earnings cycle happens within the next "ExpansionBars",
#    the earnings date is displayed, otherwise it is not displayed

input ExpansionBars = 11;
input BubbleOffset = 2;

def bn = BarNumber();
def Month = GetMonth();
def Day = GetDayOfMonth(GetYYYYMMDD());
def isBefore = HasEarnings(EarningTime.BEFORE_MARKET);
def isAfter  = HasEarnings(EarningTime.AFTER_MARKET);
def isToday  = isBefore or isAfter or (HasEarnings() and !isBefore and !isAfter);
def EarningsBN  = if HasEarnings(EarningTime.BEFORE_MARKET) then bn[1]
else if HasEarnings(EarningTime.AFTER_MARKET) then bn
else EarningsBN[1];
def DayAfterBN = if bn - EarningsBN == 1 then bn else Double.NaN;

AddVerticalLine(isToday, "        " + Month + "/" + Day + " Earnings", Color.ORANGE, Curve.SHORT_DASH);

def EGap = fold y = 0 to 1
while DayAfterBN and bn > 1 do
if (bn - DayAfterBN) <= 1 then open - close[1]
else Double.NaN;

def EPct = fold z = 0 to 1
while DayAfterBN and bn > 1 do
if (bn - DayAfterBN) <= 1 then (open - close[1]) / close[1]
else Double.NaN;

def gapUp = EGap > 0;
def gapDn = EGap < 0;

def gapUpTotal = if EGap > 0 then gapUpTotal[1] + EGap else gapUpTotal[1];
def gapDnTotal = if EGap < 0 then gapDnTotal[1] + EGap else gapDnTotal[1];
def pctUpTotal = if EGap > 0 then pctUpTotal[1] + EPct else pctUpTotal[1];
def pctDnTotal = if EGap < 0 then pctDnTotal[1] + EPct else pctDnTotal[1];

AddChartBubble(BarNumber() != 1 and gapUp, high[1] + TickSize() * BubbleOffset, "GapUp: \$" + EGap + "\nPctUp: " + AsPercent(EPct), Color.LIGHT_GREEN);

AddChartBubble(BarNumber() != 1 and gapDn, low[1] - TickSize() * BubbleOffset, "GapDn: \$" + EGap + "\nPctDn: " + AsPercent(EPct), Color.PINK);

AddLabel(BarNumber() != 1, "# Earnings Gap Up: " + TotalSum(gapUp), Color.LIGHT_GREEN);
AddLabel(1, "# Earnings Gap Down: " + TotalSum(gapDn), Color.PINK);
AddLabel(1, "Average Gap Up: " + Round(gapUpTotal / TotalSum(gapUp), 2), Color.LIGHT_GREEN);
AddLabel(1, "Average Gap Down: " + Round(gapDnTotal / TotalSum(gapDn), 2), Color.PINK);
AddLabel(1, "Average Pct Up: " + AsPercent(pctUpTotal / TotalSum(gapUp)), Color.LIGHT_GREEN);
AddLabel(1, "Average Pct Down: " + AsPercent(pctDnTotal / TotalSum(gapDn)), Color.PINK);

###--- Process next earnings date
def Earnings = AbsValue(GetEventOffset(Events.EARNINGS, 0));
def DTE = if IsNaN(Earnings) then 0 else Earnings;
def NextEarningsBN = if !IsNaN(Earnings) then bn + Earnings else Double.NaN;
def EarningsBNX = bn == HighestAll(NextEarningsBN);
def NextEarningsMth = HighestAll(if EarningsBNX then Month else Double.NaN);
def NextEarningsDay = HighestAll(if EarningsBNX then Day   else Double.NaN);

AddLabel(BarNumber() != 1 and DTE < ExpansionBars and DTE > 0, "Upcoming Earnings Date: " + NextEarningsMth + "/" + NextEarningsDay, Color.YELLOW);
# END STUDY``````

Last edited by a moderator:
There are different gap stats. Check to see which one you're looking for.

### Earnings Statistics

Code:
``````# Archive Name: Earnings Statistics Paris

# Archive Section: Scripts-Earnings

# Suggested Tos Name: EarningsStatistics_Paris

# Archive Date: 5.07.2018

# Archive Notes:

# Earnings Statisques

# Paris

# 5.6.2018

# This study displays the following earnings related info/stats:

#

# ++ Vertical lines that mark each earnings cycle and earnings date

# ++ Bubble that measures the gap and percentage moves post earnings

# ++ Total number of gaps ups versus gap downs

# ++ Average gap ups versus gap downs

# ++ Average percentage moves up versus percentage moves down

# ++ If the next earnings cycle happens within the next "ExpansionBars",

#    the earnings date is displayed, otherwise it is not displayed

input ExpansionBars = 11;

input BubbleOffset = 2;

def bn = BarNumber();

def Month = GetMonth();

def Day = GetDayOfMonth(GetYYYYMMDD());

def isBefore = HasEarnings(EarningTime.BEFORE_MARKET);

def isAfter  = HasEarnings(EarningTime.AFTER_MARKET);

def isToday  = isBefore or isAfter or (HasEarnings() and !isBefore and !isAfter);

def EarningsBN  = if HasEarnings(EarningTime.BEFORE_MARKET) then bn[1]

else if HasEarnings(EarningTime.AFTER_MARKET) then bn

else EarningsBN[1];

def DayAfterBN = if bn - EarningsBN == 1 then bn else Double.NaN;

AddVerticalLine(isToday, "        " + Month + "/" + Day + " Earnings", Color.ORANGE, Curve.SHORT_DASH);

def EGap = fold y = 0 to 1

while DayAfterBN do

if (bn - DayAfterBN) <= 1 then open - close[1]

else Double.NaN;

def EPct = fold z = 0 to 1

while DayAfterBN do

if (bn - DayAfterBN) <= 1 then (open - close[1]) / close[1]

else Double.NaN;

def gapUp = EGap > 0;

def gapDn = EGap < 0;

def gapUpTotal = if EGap > 0 then gapUpTotal[1] + EGap else gapUpTotal[1];

def gapDnTotal = if EGap < 0 then gapDnTotal[1] + EGap else gapDnTotal[1];

def pctUpTotal = if EGap > 0 then pctUpTotal[1] + EPct else pctUpTotal[1];

def pctDnTotal = if EGap < 0 then pctDnTotal[1] + EPct else pctDnTotal[1];

AddChartBubble(gapUp, high[1] + TickSize() * bubbleOffset, "GapUp: \$" + EGap + "\nPctUp: " + AsPercent(EPct), Color.LIGHT_Green);

AddChartBubble(gapDn, low[1] - TickSize() * bubbleOffset, "GapDn: \$" + EGap + "\nPctDn: " + AsPercent(EPct), Color.PINK);

AddLabel(1, "# Earnings Gap Up: " + TotalSum(gapUp), Color.LIGHT_GREEN);

AddLabel(1, "# Earnings Gap Down: " + TotalSum(gapDn), Color.PINK);

AddLabel(1, "Average Gap Up: " + Round(gapUpTotal/TotalSum(gapUp),2), Color.LIGHT_GREEN);

AddLabel(1, "Average Gap Down: " + Round(gapDnTotal/TotalSum(gapDn),2), Color.PINK);

AddLabel(1, "Average Pct Up: " + AsPercent(pctUpTotal/TotalSum(gapUp)), Color.LIGHT_GREEN);

AddLabel(1, "Average Pct Down: " + AsPercent(pctDnTotal/TotalSum(gapDn)), Color.PINK);

###--- Process next earnings date

def Earnings = AbsValue(GetEventOffset(Events.Earnings, 0));

def DTE = if isNaN(Earnings) then 0 else Earnings;

def NextEarningsBN = if !isNaN(Earnings) then bn + Earnings else Double.NaN;

def EarningsBNX = bn == HighestAll(NextEarningsBN);

def NextEarningsMth = HighestAll(if EarningsBNX then Month else Double.NaN);

def NextEarningsDay = HighestAll(if EarningsBNX then Day   else Double.NaN);

AddLabel(DTE < ExpansionBars and DTE > 0, "Upcoming Earnings Date: " + NextEarningsMth + "/" + NextEarningsDay, Color.YELLOW);

# END STUDY``````

### Gap Stats

Code:
``````#vimes - gap stas - chat discussion 2018-07-12
#only evaulates the first opening bar so set the chart to desired period
# for evaulation of gap fills in the first opening range of the chart period

declare hide_on_daily;

input mingap = 10; #hint: minimum gap to trade in pts (i.e identify gap trades of at least 6 /es pts or 10 /ym points)
input maxgap = 60;#hint: maxium gap to evaulate (filter to not analyze larger moves which might indicate bullish continuation
input usethreshold = yes;#hint: apply threshold? yes will only consider gaps less than threshold; no will evaulate every gap

def o = if GetYYYYMMDD() > GetYYYYMMDD()[1] then open else o[1];
def c = if GetYYYYMMDD() > GetYYYYMMDD()[1] then close[1] else c[1];

#helper scripts
script retrace {
input retrace_per = 0.5;
input o = open;
input c = close;
def retracement = if o > c then o - (o - c) * retrace_per else o + (c - o) * retrace_per;
plot retrace = retracement;
}

script guf {
input Gap = 1;
input tgt = close;
plot guf = if Gap and low <= tgt then 1 else 0;
plot percfill = totalsum(guf)/totalsum(gap)*100;
}
script gdf {
input gap = 1;
input tgt = close;
plot gdf = if gap and high >= tgt then 1 else 0;
plot percfill = totalsum(gdf)/totalsum(gap)*100;
}

script pts {
input gf = 1;
input tgt = close;
plot pts = if gf and open > tgt then open - tgt else if gf and open < tgt then tgt - open else 0;
def cnt = TotalSum(gf);
plot totpts = totalsum(pts);
plot avgpts = totpts / cnt;
def exc = if gf and open > tgt then high - open else if gf and open < tgt then open - low else 0;
plot avgexc = TotalSum(exc) / cnt;
plot maxexc = highestAll(exc);

}
#retrace levels
plot retrace100 = retrace(1,o,c);
plot retrace75 = retrace(0.75,o,c);
plot retrace50 = retrace(0.5, o, c);
plot retrace25 = retrace(0.25, o, c);
def retrace10 = retrace(0.1,o,c);

def newday = GetYYYYMMDD() <> GetYYYYMMDD()[1] ; #only analyzing the first bar of the day based on the chart period
def GapUP = if o > c and newday and usethreshold and o-c>=mingap and o-c<=maxgap then 1
else if o > c and newday and !usethreshold then 1 else 0;
def GapDN = if o < c and newday and usethreshold and c-o>mingap and c-o<=maxgap then 1
else if O < c and newday and !usethreshold then 1 else 0;

def cntGapUpDays = TotalSum(GapUP);
def cntGapDnDays = TotalSum(GapDN);
#50% retrace
#def guf50 = if GapUP and low <= retrace50 then 1 else 0;
def guf100 = guf(gapup,retrace100);
def guf75 = guf(gapup,retrace75);
def guf50 = guf(gapup,retrace50);
def guf25 = guf(gapup,retrace25);
def guf10 = guf(gapup,retrace10);

def gdf100 = gdf(gapdn,retrace100);
def gdf75 = gdf(gapdn,retrace75);
def gdf50 = gdf(gapdn,retrace50);
def gdf25 = gdf(gapdn,retrace25);
def gdf10 = gdf(gapdn,retrace10);

def pguf100 = guf(gapup,retrace100).percfill;
def pguf75 = guf(gapup,retrace75).percfill;
def pguf50 = guf(gapup,retrace50).percfill;
def pguf25 = guf(gapup,retrace25).percfill;

def pgdf100 = gdf(gapdn,retrace100).percfill;
def pgdf75 = gdf(gapdn,retrace75).percfill;
def pgdf50 = gdf(gapdn,retrace50).percfill;
def pgdf25 = gdf(gapdn,retrace25).percfill;

def ptsguf100 = pts(guf100, retrace100);
def totptsguf100 = pts(guf100, retrace100).totpts;
def avgptsguf100 = pts(guf100, retrace100).avgpts;
def avgexcguf100 = pts(guf100, retrace100).avgexc;
def maxexcguf100 = pts(guf100, retrace100).maxexc;

def ptsguf75 = pts(guf75, retrace75);
def totptsguf75 = pts(guf75, retrace75).totpts;
def avgptsguf75 = pts(guf75, retrace75).avgpts;
def avgexcguf75 = pts(guf75, retrace75).avgexc;
def maxexcguf75 = pts(guf75, retrace75).maxexc;

def ptsguf50 = pts(guf50, retrace50);
def totptsguf50 = pts(guf50, retrace50).totpts;
def avgptsguf50 = pts(guf50, retrace50).avgpts;
def avgexcguf50 = pts(guf50, retrace50).avgexc;
def maxexcguf50 = pts(guf50, retrace50).maxexc;

def ptsguf25 = pts(guf25, retrace25);
def totptsguf25 = pts(guf25, retrace25).totpts;
def avgptsguf25 = pts(guf25, retrace25).avgpts;
def avgexcguf25 = pts(guf25, retrace25).avgexc;
def maxexcguf25 = pts(guf25, retrace25).maxexc;

def ptsgdf100 = pts(gdf100, retrace100);
def totptsgdf100 = pts(gdf100, retrace100).totpts;
def avgptsgdf100 = pts(gdf100, retrace100).avgpts;
def avgexcgdf100 = pts(gdf100, retrace100).avgexc;
def maxexcgdf100 = pts(gdf100, retrace100).maxexc;

def ptsgdf75 = pts(gdf75, retrace75);
def totptsgdf75 = pts(gdf75, retrace75).totpts;
def avgptsgdf75 = pts(gdf75, retrace75).avgpts;
def avgexcgdf75 = pts(gdf75, retrace75).avgexc;
def maxexcgdf75 = pts(gdf75, retrace75).maxexc;

def ptsgdf50 = pts(gdf50, retrace50);
def totptsgdf50 = pts(gdf50, retrace50).totpts;
def avgptsgdf50 = pts(gdf50, retrace50).avgpts;
def avgexcgdf50 = pts(gdf50, retrace50).avgexc;
def maxexcgdf50 = pts(gdf50, retrace50).maxexc;

def ptsgdf25 = pts(gdf25, retrace25);
def totptsgdf25 = pts(gdf25, retrace25).totpts;
def avgptsgdf25 = pts(gdf25, retrace25).avgpts;
def avgexcgdf25 = pts(gdf25, retrace25).avgexc;
def maxexcgdf25 = pts(gdf25, retrace25).maxexc;

def p10to25up = totalsum(guf25)/totalsum(guf10 or guf25)*100;
def p25to50up = totalsum(guf50)/totalsum(guf25 or guf50)*100;
def p50to75up = totalsum(guf75)/totalsum(guf50 or guf75)*100;
def p75to100up = totalsum(guf100)/totalsum(guf75 or guf100)*100;

def p10to25dn = totalsum(gdf25)/totalsum(gdf10 or gdf25)*100;
def p25to50dn = totalsum(gdf50)/totalsum(gdf25 or gdf50)*100;
def p50to75dn = totalsum(gdf75)/totalsum(gdf50 or gdf75)*100;
def p75to100dn = totalsum(gdf100)/totalsum(gdf75 or gdf100)*100;

def prof10to25dn = if gdf10 and gdf25 then retrace25-retrace10 else 0;
def prof25to50dn = if gdf25 and gdf50 then retrace50-retrace25 else 0;
def prof50to75dn = if gdf50 and gdf75 then retrace75-retrace50 else 0;
def prof75to100dn = if gdf75 and gdf100 then retrace100-retrace75 else 0;

def prof10to25up = if guf10 and guf25 then retrace10-retrace25 else 0;
def prof25to50up = if guf25 and guf50 then retrace25 - retrace50 else 0;
def prof50to75up = if guf50 and guf75 then retrace50 - retrace75 else 0;
def prof75to100up = if guf75 and guf100 then retrace75 - retrace100 else 0;

def c25u = totalsum(guf25);
def c50u = totalsum(guf50);
def c75u = totalsum(guf75);
def c100u = totalsum(guf100);

def ldr10to25up = totalsum(prof10to25up)/c25u;
def ldr25to50up = totalsum(prof25to50up)/c50u;
def ldr50to75up = totalsum(prof50to75up)/c75u;
def ldr75to100u = totalsum(prof75to100up)/c100u;

def c25d = totalsum(gdf25);
def c50d = totalsum(gdf50);
def c75d = totalsum(gdf75);
def c100d = totalsum(gdf100);

def ldr10to25dn = totalsum(prof10to25dn)/c25d;
def ldr25to50dn = totalsum(prof25to50dn)/c50d;
def ldr50to75dn = totalsum(prof50to75dn)/c75d;
def ldr75to100d = totalsum(prof75to100dn)/c100d;

addlabel(!usethreshold,"Considering all gaps at the open there are a total of up/down gaps =, "
+ astext(cntGapUpDays) + "/" +  astext(cntgapdndays)
,color.cyan);

addlabel(usethreshold,"Considering min/max gaps at the open of: " + astext(mingap) + "/" + astext(maxgap)
+", there are a total up/down gaps = " + astext(cntGapUpDays) + "/" +  astext(cntgapdndays)
,color.cyan);

AddLabel(yes, "Gap up and probability of 100/75/50/25% retrace on the first bar, "
+ astext(pguf100) + " / " + astext(pguf75) + " / " + astext(pguf50) +  " / " + astext(pguf25)
+ "%, Avg pts: " + AsText(avgptsguf100)  + " / " + AsText(avgptsguf75) + " / " + AsText(avgptsguf50) + " / " + AsText(avgptsguf25)
+ " Total pts: " + astext(totptsguf100) + " / " + astext(totptsguf75) + " / " + astext(totptsguf50) + " / " + astext(totptsguf25)
+ ", Avg negative excursion on winning trades: " + AsText(avgexcguf100) + "/" + AsText(avgexcguf75) + "/" + AsText(avgexcguf50) + "/" + AsText(avgexcguf25)
+ ", Max Neg Excursion: " + AsText(maxexcguf100)
, color = Color.yellow);

AddLabel(yes, "Gap down and probability of 100/75/50/25% retrace on first bar, "
+ astext(pgdf100) + " / " + astext(pgdf75) + " / " + astext(pgdf50) +  " / " + astext(pgdf25)
+ "%, Avg pts: " + AsText(avgptsgdf100)  + " / " + AsText(avgptsgdf75) + " / " + AsText(avgptsgdf50) + " / " + AsText(avgptsgdf25)
+ " Total pts: " + astext(totptsgdf100) + " / " + astext(totptsgdf75) + " / " + astext(totptsgdf50) + " / " + astext(totptsgdf25)
+ ", Avg negative excursion on winning trades: " + AsText(avgexcgdf100) + "/" + AsText(avgexcgdf75) + "/" + AsText(avgexcgdf50) + "/" + AsText(avgexcgdf25)
+ ", Max Neg Excursion: " + AsText(maxexcgdf100)
, color = Color.lime);

addlabel(yes,"Probability of 10/25, 25/50, 50/75, 75/100 ladder moves on a gap up (SELL): "
+ astext(p10to25up) + "/" + astext(p25to50up) + "/" + astext(p50to75up) + "/" + astext(p75to100up) + "%"
+ ",AvgWin: " + astext(ldr10to25dn) + " / "+  astext(ldr25to50dn) + " / "+  astext(ldr50to75dn) + " / "+  astext(ldr75to100d)
,color.light_red);

+ astext(p10to25dn) + "/" + astext(p25to50dn) + "/" + astext(p50to75dn) + "/" + astext(p75to100dn) + "%"
+ ",Avg win: " +  astext(ldr10to25up) + " / "+  astext(ldr25to50up) + " / "+  astext(ldr50to75up) + " / "+  astext(ldr75to100u) ,color.green);``````

Hello, I tried using this code for gap stats as I was curious about finding these like Sir_Gallywax and it doesn't have any actual data filled in just the labels. It looks half complete. Is there a way to get the actual gap statistics? Thank you

Hello, I tried using this code for gap stats as I was curious about finding these like Sir_Gallywax and it doesn't have any actual data filled in just the labels. It looks half complete. Is there a way to get the actual gap statistics? Thank you
Welcome @mwalstea Both of the codes above were made by expert level coders.
Did you read the notes inside the script you chose?
What stock did you try it on?
What was the time frame? Thanks

Thank you! Glad to be here. I tried it on SES on a one minute and it displayed the labels but no data, and on a daily as well, but nothing showed at all on the daily.

I copied and pasted the entire code into the "create" tab in thinkorswim studies area. I thought that was the correct thing to do, but you mentioned notes...are the notes all the lines starting with "#" and should I take those out? Thanks

Did you delete the first line before you pasted the code? If yes, then you put it in correctly.
Notes have the # symbol in front of them. Those are not to be erased.
The # sign make then un-noticeable to thinkscript.

Was it the first script or the second?
One script is for Daily and above and the other says "declare hide_on_daily;" which means that it's only visible on less than 1 day time frame.
Put an active stock in like CAT so that you eliminate small stocks which may not have enough past data or history. What do you see?

The second script for Gap Stats and I just deleted the first line. I put in the entire code (without the first line) on CAT and it has the labels but where the data should be it shows "NaN / NaN / NaN" "0.00 / 0.00 / 0.00"

I wish I could get a picture on here but the link refuses to work.

The second script for Gap Stats and I just deleted the first line. I put in the entire code (without the first line) on CAT and it has the labels but where the data should be it shows "NaN / NaN / NaN" "0.00 / 0.00 / 0.00"

I wish I could get a picture on here but the link refuses to work.
Go to imgur.com, post your pic there (free), than click the pic in Imgur for a BB Link. Copy that and paste link directly in here. btw, the # is called a "comment".
Then, post the code that you have. Somethings not right and will need to be looked over. I'm at work and will be in & out for the day.
@horserider @mc01439 @skynetgen ,once it gets posted, could you review if time allows?

Code:
``````#only evaulates the first opening bar so set the chart to desired period
# for evaulation of gap fills in the first opening range of the chart period

declare hide_on_daily;

input mingap = 10; #hint: minimum gap to trade in pts (i.e identify gap trades of at least 6 /es pts or 10 /ym points)
input maxgap = 60;#hint: maxium gap to evaulate (filter to not analyze larger moves which might indicate bullish continuation
input usethreshold = yes;#hint: apply threshold? yes will only consider gaps less than threshold; no will evaulate every gap

def o = if GetYYYYMMDD() > GetYYYYMMDD()[1] then open else o[1];
def c = if GetYYYYMMDD() > GetYYYYMMDD()[1] then close[1] else c[1];

#helper scripts
script retrace {
input retrace_per = 0.5;
input o = open;
input c = close;
def retracement = if o > c then o - (o - c) * retrace_per else o + (c - o) * retrace_per;
plot retrace = retracement;
}

script guf {
input Gap = 1;
input tgt = close;
plot guf = if Gap and low <= tgt then 1 else 0;
plot percfill = totalsum(guf)/totalsum(gap)*100;
}
script gdf {
input gap = 1;
input tgt = close;
plot gdf = if gap and high >= tgt then 1 else 0;
plot percfill = totalsum(gdf)/totalsum(gap)*100;
}

script pts {
input gf = 1;
input tgt = close;
plot pts = if gf and open > tgt then open - tgt else if gf and open < tgt then tgt - open else 0;
def cnt = TotalSum(gf);
plot totpts = totalsum(pts);
plot avgpts = totpts / cnt;
def exc = if gf and open > tgt then high - open else if gf and open < tgt then open - low else 0;
plot avgexc = TotalSum(exc) / cnt;
plot maxexc = highestAll(exc);

}
#retrace levels
plot retrace100 = retrace(1,o,c);
plot retrace75 = retrace(0.75,o,c);
plot retrace50 = retrace(0.5, o, c);
plot retrace25 = retrace(0.25, o, c);
def retrace10 = retrace(0.1,o,c);

def newday = GetYYYYMMDD() <> GetYYYYMMDD()[1] ; #only analyzing the first bar of the day based on the chart period
def GapUP = if o > c and newday and usethreshold and o-c>=mingap and o-c<=maxgap then 1
else if o > c and newday and !usethreshold then 1 else 0;
def GapDN = if o < c and newday and usethreshold and c-o>mingap and c-o<=maxgap then 1
else if O < c and newday and !usethreshold then 1 else 0;

def cntGapUpDays = TotalSum(GapUP);
def cntGapDnDays = TotalSum(GapDN);
#50% retrace
#def guf50 = if GapUP and low <= retrace50 then 1 else 0;
def guf100 = guf(gapup,retrace100);
def guf75 = guf(gapup,retrace75);
def guf50 = guf(gapup,retrace50);
def guf25 = guf(gapup,retrace25);
def guf10 = guf(gapup,retrace10);

def gdf100 = gdf(gapdn,retrace100);
def gdf75 = gdf(gapdn,retrace75);
def gdf50 = gdf(gapdn,retrace50);
def gdf25 = gdf(gapdn,retrace25);
def gdf10 = gdf(gapdn,retrace10);

def pguf100 = guf(gapup,retrace100).percfill;
def pguf75 = guf(gapup,retrace75).percfill;
def pguf50 = guf(gapup,retrace50).percfill;
def pguf25 = guf(gapup,retrace25).percfill;

def pgdf100 = gdf(gapdn,retrace100).percfill;
def pgdf75 = gdf(gapdn,retrace75).percfill;
def pgdf50 = gdf(gapdn,retrace50).percfill;
def pgdf25 = gdf(gapdn,retrace25).percfill;

def ptsguf100 = pts(guf100, retrace100);
def totptsguf100 = pts(guf100, retrace100).totpts;
def avgptsguf100 = pts(guf100, retrace100).avgpts;
def avgexcguf100 = pts(guf100, retrace100).avgexc;
def maxexcguf100 = pts(guf100, retrace100).maxexc;

def ptsguf75 = pts(guf75, retrace75);
def totptsguf75 = pts(guf75, retrace75).totpts;
def avgptsguf75 = pts(guf75, retrace75).avgpts;
def avgexcguf75 = pts(guf75, retrace75).avgexc;
def maxexcguf75 = pts(guf75, retrace75).maxexc;

def ptsguf50 = pts(guf50, retrace50);
def totptsguf50 = pts(guf50, retrace50).totpts;
def avgptsguf50 = pts(guf50, retrace50).avgpts;
def avgexcguf50 = pts(guf50, retrace50).avgexc;
def maxexcguf50 = pts(guf50, retrace50).maxexc;

def ptsguf25 = pts(guf25, retrace25);
def totptsguf25 = pts(guf25, retrace25).totpts;
def avgptsguf25 = pts(guf25, retrace25).avgpts;
def avgexcguf25 = pts(guf25, retrace25).avgexc;
def maxexcguf25 = pts(guf25, retrace25).maxexc;

def ptsgdf100 = pts(gdf100, retrace100);
def totptsgdf100 = pts(gdf100, retrace100).totpts;
def avgptsgdf100 = pts(gdf100, retrace100).avgpts;
def avgexcgdf100 = pts(gdf100, retrace100).avgexc;
def maxexcgdf100 = pts(gdf100, retrace100).maxexc;

def ptsgdf75 = pts(gdf75, retrace75);
def totptsgdf75 = pts(gdf75, retrace75).totpts;
def avgptsgdf75 = pts(gdf75, retrace75).avgpts;
def avgexcgdf75 = pts(gdf75, retrace75).avgexc;
def maxexcgdf75 = pts(gdf75, retrace75).maxexc;

def ptsgdf50 = pts(gdf50, retrace50);
def totptsgdf50 = pts(gdf50, retrace50).totpts;
def avgptsgdf50 = pts(gdf50, retrace50).avgpts;
def avgexcgdf50 = pts(gdf50, retrace50).avgexc;
def maxexcgdf50 = pts(gdf50, retrace50).maxexc;

def ptsgdf25 = pts(gdf25, retrace25);
def totptsgdf25 = pts(gdf25, retrace25).totpts;
def avgptsgdf25 = pts(gdf25, retrace25).avgpts;
def avgexcgdf25 = pts(gdf25, retrace25).avgexc;
def maxexcgdf25 = pts(gdf25, retrace25).maxexc;

def p10to25up = totalsum(guf25)/totalsum(guf10 or guf25)*100;
def p25to50up = totalsum(guf50)/totalsum(guf25 or guf50)*100;
def p50to75up = totalsum(guf75)/totalsum(guf50 or guf75)*100;
def p75to100up = totalsum(guf100)/totalsum(guf75 or guf100)*100;

def p10to25dn = totalsum(gdf25)/totalsum(gdf10 or gdf25)*100;
def p25to50dn = totalsum(gdf50)/totalsum(gdf25 or gdf50)*100;
def p50to75dn = totalsum(gdf75)/totalsum(gdf50 or gdf75)*100;
def p75to100dn = totalsum(gdf100)/totalsum(gdf75 or gdf100)*100;

def prof10to25dn = if gdf10 and gdf25 then retrace25-retrace10 else 0;
def prof25to50dn = if gdf25 and gdf50 then retrace50-retrace25 else 0;
def prof50to75dn = if gdf50 and gdf75 then retrace75-retrace50 else 0;
def prof75to100dn = if gdf75 and gdf100 then retrace100-retrace75 else 0;

def prof10to25up = if guf10 and guf25 then retrace10-retrace25 else 0;
def prof25to50up = if guf25 and guf50 then retrace25 - retrace50 else 0;
def prof50to75up = if guf50 and guf75 then retrace50 - retrace75 else 0;
def prof75to100up = if guf75 and guf100 then retrace75 - retrace100 else 0;

def c25u = totalsum(guf25);
def c50u = totalsum(guf50);
def c75u = totalsum(guf75);
def c100u = totalsum(guf100);

def ldr10to25up = totalsum(prof10to25up)/c25u;
def ldr25to50up = totalsum(prof25to50up)/c50u;
def ldr50to75up = totalsum(prof50to75up)/c75u;
def ldr75to100u = totalsum(prof75to100up)/c100u;

def c25d = totalsum(gdf25);
def c50d = totalsum(gdf50);
def c75d = totalsum(gdf75);
def c100d = totalsum(gdf100);

def ldr10to25dn = totalsum(prof10to25dn)/c25d;
def ldr25to50dn = totalsum(prof25to50dn)/c50d;
def ldr50to75dn = totalsum(prof50to75dn)/c75d;
def ldr75to100d = totalsum(prof75to100dn)/c100d;

addlabel(!usethreshold,"Considering all gaps at the open there are a total of up/down gaps =, "
+ astext(cntGapUpDays) + "/" +  astext(cntgapdndays)
,color.cyan);

addlabel(usethreshold,"Considering min/max gaps at the open of: " + astext(mingap) + "/" + astext(maxgap)
+", there are a total up/down gaps = " + astext(cntGapUpDays) + "/" +  astext(cntgapdndays)
,color.cyan);

AddLabel(yes, "Gap up and probability of 100/75/50/25% retrace on the first bar, "
+ astext(pguf100) + " / " + astext(pguf75) + " / " + astext(pguf50) +  " / " + astext(pguf25)
+ "%, Avg pts: " + AsText(avgptsguf100)  + " / " + AsText(avgptsguf75) + " / " + AsText(avgptsguf50) + " / " + AsText(avgptsguf25)
+ " Total pts: " + astext(totptsguf100) + " / " + astext(totptsguf75) + " / " + astext(totptsguf50) + " / " + astext(totptsguf25)
+ ", Avg negative excursion on winning trades: " + AsText(avgexcguf100) + "/" + AsText(avgexcguf75) + "/" + AsText(avgexcguf50) + "/" + AsText(avgexcguf25)
+ ", Max Neg Excursion: " + AsText(maxexcguf100)
, color = Color.yellow);

AddLabel(yes, "Gap down and probability of 100/75/50/25% retrace on first bar, "
+ astext(pgdf100) + " / " + astext(pgdf75) + " / " + astext(pgdf50) +  " / " + astext(pgdf25)
+ "%, Avg pts: " + AsText(avgptsgdf100)  + " / " + AsText(avgptsgdf75) + " / " + AsText(avgptsgdf50) + " / " + AsText(avgptsgdf25)
+ " Total pts: " + astext(totptsgdf100) + " / " + astext(totptsgdf75) + " / " + astext(totptsgdf50) + " / " + astext(totptsgdf25)
+ ", Avg negative excursion on winning trades: " + AsText(avgexcgdf100) + "/" + AsText(avgexcgdf75) + "/" + AsText(avgexcgdf50) + "/" + AsText(avgexcgdf25)
+ ", Max Neg Excursion: " + AsText(maxexcgdf100)
, color = Color.lime);

addlabel(yes,"Probability of 10/25, 25/50, 50/75, 75/100 ladder moves on a gap up (SELL): "
+ astext(p10to25up) + "/" + astext(p25to50up) + "/" + astext(p50to75up) + "/" + astext(p75to100up) + "%"
+ ",AvgWin: " + astext(ldr10to25dn) + " / "+  astext(ldr25to50dn) + " / "+  astext(ldr50to75dn) + " / "+  astext(ldr75to100d)
,color.light_red);

+ astext(p10to25dn) + "/" + astext(p25to50dn) + "/" + astext(p50to75dn) + "/" + astext(p75to100dn) + "%"
+ ",Avg win: " +  astext(ldr10to25up) + " / "+  astext(ldr25to50up) + " / "+  astext(ldr50to75up) + " / "+  astext(ldr75to100u) ,color.green);``````

#### Attachments

• rxTTOWX.png
24.1 KB · Views: 174
Hi @markos, does this look wrong or is that correct? I tried again, same result. Please let me know when you can, thank you!

Hi @markos, does this look wrong or is that correct? I tried again, same result. Please let me know when you can, thank you!

Hello, was anyone able to see what the error was. I tried it again multiple different ways and still no luck. Not sure what I could be doing wrong at this point. Thanks so much.

Hi, I have been away from the computer and will only be on mobile thru next week. Looking at that script, it seems written for /ES or other futures. Less than daily time frame.
Have you checked the Onenote in the Tutorials section?

@markos @mwalstea The so-called "error" as described is rather simple. The Gap Stats study from Vimes that was used is for aggregations less than daily, AND only evaluates the first opening bar. Since you report seeing a bunch of NaN / NaN, etc - that means no data is currently available. Usually for gap stats, I look at Daily aggregations, so use the first post (Author: Paris) that @BenTen posted. I just loaded a Daily aggregation chart of BIDU and it displays properly.

Additionally I looked at some guidance notes from the Author when the study was initially posted in the chatroom
It specifies:

# Vimes: Here is my attempt at a gap fill stats study. It looks only at the first bar of the chart to determine if the gap fills within the aggregation period. It shouldn't be used with extended hours on.

hi guys

just wondering if anyone has or can make a custom code to measure the gap% from prev close to open
coloring the label red for negative and green for positive

@BenTen this is exactly what i was needing earnings wise. seriously youre a god send

@BenTen @markos
is there anyway that we could get post-earnings performance tab? post earnings effect usually causes price to drift up on some stocks or get dragged down like others.
i really like the number of occurrences counter here btw. so im looking for something similar just with average price performance for a week or two after on average.
very impressive stuff guys

Hello,
I was wondering if anyone had a script that would show the gap stats of a company. Like how many times it has gapped up above 20% the average spike % after open does it close below or above open.

Something like this.

Thank you !

Ric
i dont think anyone has figured this out..i tried the above code but it didn't work.

87k+ Posts
249 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?