find gaps, more than some %. reduce gap when price crosses into it for ThinkOrSwim

Hey,

I'm not going to have time to complete this, but here's code that shows the nearest previous gap. I'm pretty sure it shouldn't be incredibly hard to modify it to show the previous N gaps. I hope this helps.

Code:
# Finntech 8/26/23
# [email protected]
#
# Draws morning regular trading hours price gaps from yesterday's close to today's open.
# Gap will update as the gap fills. When the gap is completely filled it will show the
# nearest next unfilled gap.
#
# TODO: 1) add support for extending previous 3 gaps (or input n if possible)
#       2) improve computing the daily high / low until now


# helper function to compute gap
script gap {
    input dailyOpen = open;
    input yesterdayClose = close;
    input dailyHigh = high;
    input dailyLow = low;
 
    def difference = yesterdayClose - dailyOpen;
    def half = dailyOpen + difference / 2;
    def gapUp = if yesterdayClose < dailyOpen then 1 else 0;
    def gapDown = if yesterdayClose > dailyOpen then 1 else 0;
    def remaining = if gapUp then Max(dailyLow - yesterdayClose, 0) else if gapDown then Max(yesterdayClose - dailyHigh, 0) else 0;
    def percent = 100 * remaining / AbsValue(difference);
    def gapFilled = percent == 0;
    def halfGapFilled = percent <= 50;

    plot isGapUp = if (gapFilled) then Double.Nan else gapUp;
    plot day = if (gapFilled) then Double.NaN else getDay();
    plot gapRemaining = if (gapFilled) then Double.NaN else percent;
    plot gapHigh = if (gapFilled) then Double.NaN else if gapUp then dailyLow else yesterdayClose;
    plot gapLow = if (gapFilled) then Double.NaN else if gapUp then yesterdayClose else dailyHigh;
    plot gapHalf = if (halfGapFilled or gapFilled) then Double.NaN else half;
}

rec dailyOpen = open(period = "DAY")[0];
rec dailyClose = close(period = "DAY")[0];
rec dailyHigh = high(period = "DAY")[0];
rec dailyLow = low(period = "DAY")[0];
rec targetDay = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).day;
rec checkRemaining = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).gapRemaining;
rec targetDayC = CompoundValue(1, if isNaN(targetDay) then targetDayC[1] else targetDay, targetDay[1]);
# TODO: find a better way to do this with recursion or fold. there seems to be an issue with passing recursive values
#       into functions. it might be passing by value or limited to dataholders for reference and not functions. would
#       be nice to be able to use lambdas...
#rec highUntilNow = if (getDay() > targetDay) then min(highUntilNow[1], min(dailyHigh[-1], min(dailyHigh[1], dailyHigh[0]))) else min(dailyHigh[-1], dailyHigh[0]);
#def daysBack = getDay() - targetDay;

#rec highUntilNow = if daysBack == 0 then dailyHigh else if isNan(daysBack) then highUntilNow[1] else max(highUntilNow, highUntilNow[-1]);
#rec highUntilNow = if isNan(checkRemaining) then dailyHigh else max(dailyHigh, fold k = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempHigh = dailyHigh while !isNan(daysback) and !isNan(getValue(dailyHigh, -k)) and tempHigh > dailyHigh do max(tempHigh, getValue(dailyHigh, -k)));
rec highUntilNow = max(dailyHigh[-10], max(dailyHigh[-9], max(dailyHigh[-8], max(dailyHigh[-7], max(dailyHigh[-6], max(dailyHigh[-5], max(dailyHigh[-4], max(dailyHigh[-3], max(dailyHigh[-2], max(dailyHigh, dailyHigh[-1]))))))))));
#rec lowUntilNow = if (getDay() > targetDay) then min(lowUntilNow[1], min(dailyLow[-1], min(dailyLow[1], dailyLow[0]))) else min(dailyLow[-1], min(dailyLow[-1], dailyLow[0]));
#rec lowUntilNow = if isNan(checkRemaining) then dailyLow else min(dailyLow, fold j = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempLow = dailyLow while !isNan(daysBack) and !isNan(getValue(dailyLow, -j)) and tempLow < dailyLow do min(tempLow, getValue(dailyLow, -j)));
rec lowUntilNow = min(dailyLow[-10], min(dailyLow[-9], min(dailyLow[-8], min(dailyLow[-7], min(dailyLow[-6], min(dailyLow[-5], min(dailyLow[-4], min(dailyLow[-3], min(dailyLow[-2], min(dailyLow[-1], dailyLow[0]))))))))));

rec lastGapDay = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).day;
rec lastGapRemaining = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).gapRemaining;
rec lastGapHigh = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHigh;
rec lastGapLow = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapLow;
rec lastGapHalf = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHalf;

rec lastGapDayC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapDayC[1] else lastGapDay, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).day);
rec lastGapRemainingC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapRemainingC[1] else lastGapRemaining, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapRemaining);
rec lastGapHighC = CompoundValue(1, if isNaN(lastGapHigh) or isNan(checkRemaining) then lastGapHighC[1] else lastGapHigh, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHigh);
rec lastGapLowC = CompoundValue(1, if isNaN(lastGapLow) or isNan(checkRemaining) then lastGapLowC[1] else lastGapLow,gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapLow);
rec lastGapHalfC = CompoundValue(1, if isNaN(lastGapHalf) or isNan(checkRemaining) then lastGapHalfC[1] else lastGapHalf, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHalf);

# draw
plot gapRemaining = lastGapRemainingC;
plot gapHighPlot = lastGapHighC;
plot gapLowPlot = lastGapLowC;
plot gapHalfPlot = lastGapHalfC;

gapHighPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapHighPlot.AssignValueColor(Color.VIOLET);
gapLowPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapLowPlot.AssignValueColor(Color.VIOLET);
gapHalfPlot.SetStyle(Curve.LONG_DASH);
gapHalfPlot.SetDefaultColor(Color.PINK);

gapHighPlot.HideBubble();
gapLowPlot.HideBubble();
gapHalfPlot.HideBubble();



AddCloud(gapHighPlot, gapLowPlot, Color.VIOLET, Color.VIOLET);
AddLabel(yes, "Gap 1: " + gapRemaining + " % remaining, Gap 2: " + "TODO", Color.VIOLET);


# debug
#AddChartBubble(
#    SecondsFromTime(0930) == 0,
 #   "price location" = dailyOpen,
 #   text = "highUntilNow " + highUntilNow + " lowUntilNow " + lowUntilNow +
 #          "\nremainging " + lastGapRemaining[0] + " remaingingC " + lastGapRemainingC[0] +
 ##          "\nlastGapHigh " + lastGapHigh + " lastGapHighC " + lastGapHighC +
 #          "\nlastGapLow " + lastGapLow + " lastGapLowC " + lastGapLowC +
 #          "\nlastGapHalf " + lastGapHalf + " lastGapHalfC " + lastGapHalfC +
 #          "\ntargetDay " + targetDay + " daysBack " + daysBack +
 #          "\ndailyHigh " + dailyHigh + " dailyLow " + dailyLow,
 #   color = Color.YELLOW,
 #   up = no
#);
 
Hey,

I'm not going to have time to complete this, but here's code that shows the nearest previous gap. I'm pretty sure it shouldn't be incredibly hard to modify it to show the previous N gaps. I hope this helps.

Code:
# Finntech 8/26/23
# [email protected]
#
# Draws morning regular trading hours price gaps from yesterday's close to today's open.
# Gap will update as the gap fills. When the gap is completely filled it will show the
# nearest next unfilled gap.
#
# TODO: 1) add support for extending previous 3 gaps (or input n if possible)
#       2) improve computing the daily high / low until now


# helper function to compute gap
script gap {
    input dailyOpen = open;
    input yesterdayClose = close;
    input dailyHigh = high;
    input dailyLow = low;
 
    def difference = yesterdayClose - dailyOpen;
    def half = dailyOpen + difference / 2;
    def gapUp = if yesterdayClose < dailyOpen then 1 else 0;
    def gapDown = if yesterdayClose > dailyOpen then 1 else 0;
    def remaining = if gapUp then Max(dailyLow - yesterdayClose, 0) else if gapDown then Max(yesterdayClose - dailyHigh, 0) else 0;
    def percent = 100 * remaining / AbsValue(difference);
    def gapFilled = percent == 0;
    def halfGapFilled = percent <= 50;

    plot isGapUp = if (gapFilled) then Double.Nan else gapUp;
    plot day = if (gapFilled) then Double.NaN else getDay();
    plot gapRemaining = if (gapFilled) then Double.NaN else percent;
    plot gapHigh = if (gapFilled) then Double.NaN else if gapUp then dailyLow else yesterdayClose;
    plot gapLow = if (gapFilled) then Double.NaN else if gapUp then yesterdayClose else dailyHigh;
    plot gapHalf = if (halfGapFilled or gapFilled) then Double.NaN else half;
}

rec dailyOpen = open(period = "DAY")[0];
rec dailyClose = close(period = "DAY")[0];
rec dailyHigh = high(period = "DAY")[0];
rec dailyLow = low(period = "DAY")[0];
rec targetDay = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).day;
rec checkRemaining = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).gapRemaining;
rec targetDayC = CompoundValue(1, if isNaN(targetDay) then targetDayC[1] else targetDay, targetDay[1]);
# TODO: find a better way to do this with recursion or fold. there seems to be an issue with passing recursive values
#       into functions. it might be passing by value or limited to dataholders for reference and not functions. would
#       be nice to be able to use lambdas...
#rec highUntilNow = if (getDay() > targetDay) then min(highUntilNow[1], min(dailyHigh[-1], min(dailyHigh[1], dailyHigh[0]))) else min(dailyHigh[-1], dailyHigh[0]);
#def daysBack = getDay() - targetDay;

#rec highUntilNow = if daysBack == 0 then dailyHigh else if isNan(daysBack) then highUntilNow[1] else max(highUntilNow, highUntilNow[-1]);
#rec highUntilNow = if isNan(checkRemaining) then dailyHigh else max(dailyHigh, fold k = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempHigh = dailyHigh while !isNan(daysback) and !isNan(getValue(dailyHigh, -k)) and tempHigh > dailyHigh do max(tempHigh, getValue(dailyHigh, -k)));
rec highUntilNow = max(dailyHigh[-10], max(dailyHigh[-9], max(dailyHigh[-8], max(dailyHigh[-7], max(dailyHigh[-6], max(dailyHigh[-5], max(dailyHigh[-4], max(dailyHigh[-3], max(dailyHigh[-2], max(dailyHigh, dailyHigh[-1]))))))))));
#rec lowUntilNow = if (getDay() > targetDay) then min(lowUntilNow[1], min(dailyLow[-1], min(dailyLow[1], dailyLow[0]))) else min(dailyLow[-1], min(dailyLow[-1], dailyLow[0]));
#rec lowUntilNow = if isNan(checkRemaining) then dailyLow else min(dailyLow, fold j = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempLow = dailyLow while !isNan(daysBack) and !isNan(getValue(dailyLow, -j)) and tempLow < dailyLow do min(tempLow, getValue(dailyLow, -j)));
rec lowUntilNow = min(dailyLow[-10], min(dailyLow[-9], min(dailyLow[-8], min(dailyLow[-7], min(dailyLow[-6], min(dailyLow[-5], min(dailyLow[-4], min(dailyLow[-3], min(dailyLow[-2], min(dailyLow[-1], dailyLow[0]))))))))));

rec lastGapDay = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).day;
rec lastGapRemaining = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).gapRemaining;
rec lastGapHigh = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHigh;
rec lastGapLow = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapLow;
rec lastGapHalf = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHalf;

rec lastGapDayC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapDayC[1] else lastGapDay, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).day);
rec lastGapRemainingC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapRemainingC[1] else lastGapRemaining, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapRemaining);
rec lastGapHighC = CompoundValue(1, if isNaN(lastGapHigh) or isNan(checkRemaining) then lastGapHighC[1] else lastGapHigh, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHigh);
rec lastGapLowC = CompoundValue(1, if isNaN(lastGapLow) or isNan(checkRemaining) then lastGapLowC[1] else lastGapLow,gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapLow);
rec lastGapHalfC = CompoundValue(1, if isNaN(lastGapHalf) or isNan(checkRemaining) then lastGapHalfC[1] else lastGapHalf, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHalf);

# draw
plot gapRemaining = lastGapRemainingC;
plot gapHighPlot = lastGapHighC;
plot gapLowPlot = lastGapLowC;
plot gapHalfPlot = lastGapHalfC;

gapHighPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapHighPlot.AssignValueColor(Color.VIOLET);
gapLowPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapLowPlot.AssignValueColor(Color.VIOLET);
gapHalfPlot.SetStyle(Curve.LONG_DASH);
gapHalfPlot.SetDefaultColor(Color.PINK);

gapHighPlot.HideBubble();
gapLowPlot.HideBubble();
gapHalfPlot.HideBubble();



AddCloud(gapHighPlot, gapLowPlot, Color.VIOLET, Color.VIOLET);
AddLabel(yes, "Gap 1: " + gapRemaining + " % remaining, Gap 2: " + "TODO", Color.VIOLET);


# debug
#AddChartBubble(
#    SecondsFromTime(0930) == 0,
 #   "price location" = dailyOpen,
 #   text = "highUntilNow " + highUntilNow + " lowUntilNow " + lowUntilNow +
 #          "\nremainging " + lastGapRemaining[0] + " remaingingC " + lastGapRemainingC[0] +
 ##          "\nlastGapHigh " + lastGapHigh + " lastGapHighC " + lastGapHighC +
 #          "\nlastGapLow " + lastGapLow + " lastGapLowC " + lastGapLowC +
 #          "\nlastGapHalf " + lastGapHalf + " lastGapHalfC " + lastGapHalfC +
 #          "\ntargetDay " + targetDay + " daysBack " + daysBack +
 #          "\ndailyHigh " + dailyHigh + " dailyLow " + dailyLow,
 #   color = Color.YELLOW,
 #   up = no
#);
Thank you! Im not a coder I just dabble in this Ive installed what you have and it does work. Im testing it now.
 
find gaps, more than some %.
reduce the gap when price crosses into it.

this will plot 4 gaps at a time.
if a 5th gaps occurs, the 1st gap lines stop and the new gap lines starts.

gaps are determined when a price difference between 2 bars is more than some %.
..choose a minimum gap % , a % of the price.
..default is 1%

determine gap from previous bar price : close or high/low
determine gap from current bar price : high/low

can choose to reduce gaps, when price crosses into a gap.
if gaps are not reduced, lines will extend until another of the same gap seq number occurs (1,2,3,4)



Ruby:
# bargaps_06


def hi = high;
def lo = low;
def opn = open;
def cls = close;
def na = Double.NaN;
def bn = BarNumber();

# pick price level - prev bar
input prev_bar_price = { high_low , default close };

def prevhi;
def prevlo;
switch (prev_bar_price) {
case high_low:
 prevhi = hi[1];
 prevlo = lo[1];
case close:
 prevhi = cls[1];
 prevlo = cls[1];
}

# ----------------------
# current bar
def currhi = hi;
def currlo = lo;

#----------------------------------

input reduce_gaps = yes;

input min_gap_percent = 1.0;
#def per_digits = 1;
AddLabel(1, "find gaps that are > " + min_gap_percent + " % of price", Color.YELLOW);


input show_gap_clouds = yes;
input show_gap_stats_bubbles = no;

#----------------------------------

# if a gap, set gap high and low, and gap dir
def gaphi;
def gaplo;
def dir;
if bn == 1 then {
    gaphi = hl2;
    gaplo = hl2;
    dir = 0;
} else if prevhi < currlo then {
# gap up , high to low
    gaphi = currlo;
    gaplo = prevhi;
    dir = if gaphi > gaplo then 1 else 0;
} else if prevlo > currhi then {
# gap down , low to high
    gaphi = prevlo;
    gaplo = currhi;
    dir = if gaphi > gaplo then -1 else 0;
} else {
    gaphi = hl2;
    gaplo = hl2;
    dir = 0;
}

# ---------------------------------

# determine the gap , + or - number
def gap = if dir > 0 then round(gaphi - gaplo, 2)
 else if dir < 0 then round(gaplo - gaphi, 2)
 else 0;

# check if gap is > min %
def gap_per = if gap == 0 then 0 else Round((gap / gaplo) * 100, 1);
def gap_en = (absvalue(gap_per) > min_gap_percent);

# ----------------------------------------------

# counter of gaps
def gapqty = 4;

def cnt = if bn == 1 then 0
  else if gap_en then cnt[1] + 1
  else cnt[1];

# sequence counter, thru the qty
#  1,2,3,0,1,2,3,0...
def seq1 = (cnt % gapqty);
# chg to be,  1,2,3,4,1,2,3,4,...
def seq = if seq1 == 0 then gapqty else seq1;

#-----------------------------

# seq thru diff lines, and update 1 at a time
def gap1_en = (gap_en and seq == 1);
def gap2_en = (gap_en and seq == 2);
def gap3_en = (gap_en and seq == 3);
def gap4_en = (gap_en and seq == 4);

def p = (gap1_en or gap2_en or gap3_en or gap4_en);

#---------------------------

# bubble location
def bubble_vert_offset = 0.005;
def bubhiy = (gaphi[0] * ( 1 + bubble_vert_offset));
def bubloy = (gaplo[0] * ( 1 - bubble_vert_offset));

# ----------------------------
# gap1

def gap1dir = if gap1_en then dir else gap1dir[1];

AddChartBubble(show_gap_stats_bubbles and gap1_en, (if gap1dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap1dir < 0 then Color.RED else if gap1dir > 0 then Color.GREEN else Color.GRAY, (if gap1dir < 0 then yes else no) );
 
def top1 = if (bn == 1 or gap1_en[-1] == 1) then na else if gap1_en then gaphi else top1[1];
def bot1 = if (bn == 1 or gap1_en[-1] == 1) then na else if gap1_en then gaplo else bot1[1];

def top1b = if (bn == 1 or gap1_en[-1] == 1) then na
 else if gap1_en then gaphi
 else if !reduce_gaps then top1b[1]
 else if (lo < top1[0] and lo < top1b[1] and gap1dir == 1) then lo
 else top1b[1];

def bot1b = if (bn == 1 or gap1_en[-1] == 1) then na
 else if gap1_en then gaplo
 else if !reduce_gaps then bot1b[1]
 else if (hi > bot1[0] and hi > bot1b[1] and gap1dir == -1) then hi
 else bot1b[1];

# is the gap < 0 ,  then cancel
def gap1cancel = if gap1_en then 0 else if (top1b - bot1b) < 0 then 1 else 0;

def top1c = if gap1_en then top1
 else if gap1cancel then na
 else top1b[0];

def bot1c = if gap1_en then bot1
 else if gap1cancel then na
 else bot1b[0];


plot z1 = top1c;
z1.AssignValueColor(if gap1dir > 0 then color.green else color.red);
z1.SetStyle(Curve.MEDIUM_DASH);

plot z2 = bot1c;
z2.AssignValueColor(if gap1dir > 0 then color.green else color.red);
z2.SetStyle(Curve.MEDIUM_DASH);

def gap1top = if !show_gap_clouds then na else if gap1dir > 0 then z1 else z2;
def gap1bot = if !show_gap_clouds then na else if gap1dir > 0 then z2 else z1;
AddCloud(gap1top, gap1bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# ----------------------------------
# gap2

def gap2dir = if gap2_en then dir else gap2dir[1];

AddChartBubble(show_gap_stats_bubbles and gap2_en, (if gap2dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap2dir < 0 then Color.RED else if gap2dir > 0 then Color.GREEN else Color.GRAY, (if gap2dir < 0 then yes else no) );
 
def top2 = if (bn == 1 or gap2_en[-1] == 1) then na else if gap2_en then gaphi else top2[1];
def bot2 = if (bn == 1 or gap2_en[-1] == 1) then na else if gap2_en then gaplo else bot2[1];

def top2b = if (bn == 1 or gap2_en[-1] == 1) then na
 else if gap2_en then gaphi
 else if !reduce_gaps then top2b[1]
 else if (lo < top2[0] and lo < top2b[1] and gap2dir == 1) then lo
 else top2b[1];

def bot2b = if (bn == 1 or gap2_en[-1] == 1) then na
 else if gap2_en then gaplo
 else if !reduce_gaps then bot2b[1]
 else if (hi > bot2[0] and hi > bot2b[1] and gap2dir == -1) then hi
 else bot2b[1];

# is the gap < 0 ,  then cancel
def gap2cancel = if gap2_en then 0 else if (top2b - bot2b) < 0 then 1 else 0;

def top2c = if gap2_en then top2
 else if gap2cancel then na
 else top2b[0];

def bot2c = if gap2_en then bot2
 else if gap2cancel then na
 else bot2b[0];

plot z3 = top2c;
z3.AssignValueColor(if gap2dir > 0 then color.green else color.red);
z3.SetStyle(Curve.MEDIUM_DASH);

plot z4 = bot2c;
z4.AssignValueColor(if gap2dir > 0 then color.green else color.red);
z4.SetStyle(Curve.MEDIUM_DASH);

def gap2top = if !show_gap_clouds then na else if gap2dir > 0 then z3 else z4;
def gap2bot = if !show_gap_clouds then na else if gap2dir > 0 then z4 else z3;
AddCloud(gap2top, gap2bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# --------------------------------
# gap3

def gap3dir = if gap3_en then dir else gap3dir[1];

AddChartBubble(show_gap_stats_bubbles and gap3_en, (if gap3dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap3dir < 0 then Color.RED else if gap3dir > 0 then Color.GREEN else Color.GRAY, (if gap3dir < 0 then yes else no) );
 
def top3 = if (bn == 1 or gap3_en[-1] == 1) then na else if gap3_en then gaphi else top3[1];
def bot3 = if (bn == 1 or gap3_en[-1] == 1) then na else if gap3_en then gaplo else bot3[1];

def top3b = if (bn == 1 or gap3_en[-1] == 1) then na
 else if gap3_en then gaphi
 else if !reduce_gaps then top3b[1]
 else if (lo < top3[0] and lo < top3b[1] and gap3dir == 1) then lo
 else top3b[1];

def bot3b = if (bn == 1 or gap3_en[-1] == 1) then na
 else if gap3_en then gaplo
 else if !reduce_gaps then bot3b[1]
 else if (hi > bot3[0] and hi > bot3b[1] and gap3dir == -1) then hi
 else bot3b[1];

# is the gap < 0 ,  then cancel
def gap3cancel = if gap3_en then 0 else if (top3b - bot3b) < 0 then 1 else 0;

def top3c = if gap3_en then top3
 else if gap3cancel then na
 else top3b[0];

def bot3c = if gap3_en then bot3
 else if gap3cancel then na
 else bot3b[0];

plot z5 = top3c;
z5.AssignValueColor(if gap3dir > 0 then color.green else color.red);
z5.SetStyle(Curve.MEDIUM_DASH);

plot z6 = bot3c;
z6.AssignValueColor(if gap3dir > 0 then color.green else color.red);
z6.SetStyle(Curve.MEDIUM_DASH);

def gap3top = if !show_gap_clouds then na else if gap3dir > 0 then z5 else z6;
def gap3bot = if !show_gap_clouds then na else if gap3dir > 0 then z6 else z5;
AddCloud(gap3top, gap3bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# --------------------------------
# gap4

def gap4dir = if gap4_en then dir else gap4dir[1];

AddChartBubble(show_gap_stats_bubbles and gap4_en, (if gap4dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap4dir < 0 then Color.RED else if gap4dir > 0 then Color.GREEN else Color.GRAY, (if gap4dir < 0 then yes else no) );
 
def top4 = if (bn == 1 or gap4_en[-1] == 1) then na else if gap4_en then gaphi else top4[1];
def bot4 = if (bn == 1 or gap4_en[-1] == 1) then na else if gap4_en then gaplo else bot4[1];

def top4b = if (bn == 1 or gap4_en[-1] == 1) then na
 else if gap4_en then gaphi
 else if !reduce_gaps then top4b[1]
 else if (lo < top4[0] and lo < top4b[1] and gap4dir == 1) then lo
 else top4b[1];

def bot4b = if (bn == 1 or gap4_en[-1] == 1) then na
 else if gap4_en then gaplo
 else if !reduce_gaps then bot4b[1]
 else if (hi > bot4[0] and hi > bot4b[1] and gap4dir == -1) then hi
 else bot4b[1];

# is the gap < 0 ,  then cancel
def gap4cancel = if gap4_en then 0 else if (top4b - bot4b) < 0 then 1 else 0;

def top4c = if gap4_en then top4
 else if gap4cancel then na
 else top4b[0];

def bot4c = if gap4_en then bot4
 else if gap4cancel then na
 else bot4b[0];

plot z7 = top4c;
z7.AssignValueColor(if gap4dir > 0 then color.green else color.red);
z7.SetStyle(Curve.MEDIUM_DASH);

plot z8 = bot4c;
z8.AssignValueColor(if gap4dir > 0 then color.green else color.red);
z8.SetStyle(Curve.MEDIUM_DASH);

def gap4top = if !show_gap_clouds then na else if gap4dir > 0 then z7 else z8;
def gap4bot = if !show_gap_clouds then na else if gap4dir > 0 then z8 else z7;
AddCloud(gap4top, gap4bot, Color.LIGHT_GREEN, Color.LIGHT_RED);

# --------------------------------


# ----------------------------------------
# test stuff

input debug_data = 0;

input test_seq_counter = no;
addchartbubble(test_seq_counter and p, hi, seq, color.cyan, yes);

input test_show_all_gaps = no;
addchartbubble(test_show_all_gaps and p, high, gap, (if dir > 0 then color.green else color.red), yes);

input test_gap_hi_lo = no;
addchartbubble(test_gap_hi_lo and dir != 0,lo,
 gaphi + " hi\n" +
 gaplo + " lo\n" +
 dir + " dir"
, (if dir > 0 then color.green else if dir < 0 then color.red else color.gray), no);
#
#


WFC reduce gaps = yes
View attachment 14512

WFC reduce gaps = no
View attachment 14513
hal_gaps
This is great. Thanks for all of the work. I want to view all open gaps regardless of size so I have minimum set to 0.0. However, the highlight of the gaps does not extend into the future until they are filled. Can you point me in the right direction on where to modify the code so that it will do this? Also, is there a way to remove highlighted gaps once they are filled?
 
Hello @halcyonguy
Thanks for the great indicator.
Is it possible to create a scanner to search for Filled Gap? You need to scan and find stocks when the price has filled the gap, as in the picture.
1709967503943.png
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
572 Online
Create Post

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