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

halcyonguy

Moderator - Expert
VIP
Lifetime
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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
 
Last edited:

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

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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
This is great!
 
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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
What is DATABUG?
 
Halcyonguy, this is a great script and one I was looking for. Would you be able to modify by not having shading or bubbles, but rather just a green arrow below the bar that is gapping up? I'm also interested in identifying all the gaps up by X% looking back XX Daily bars whether there are 4 gaps or much more. Also, not interested in gap downs, just gap ups.
 
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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
Any easy way to make this work on extended hours charts?
 
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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
can you make the previous gaps extend across the chart from a previous down trend to where it intersects and up trend then when it encounters the up trend have it fill it?
 
completely remove the lines and shading?
yes , it's possible.
would use something like this.
https://usethinkscript.com/threads/remove-horizontal-line-when-crossed-for-thinkorswim.9089/
Hey halcyonguy I’ve been looking for a gap script just like this where when price enters the gap the cloud closes. I’ve been needing a Slight variation of this script for three types of gaps that I work closely with. If you could make this it would be fantastic! Perhaps it does work like this and I'm doing something wrong. I'm looking at gaps between each bar or three bars. 5 minute time frame, on future RTH no Extended hours on.

1. First gap variation are open-close gaps

On bull bars: if the second bars open is greater than the first bars Close. And vice versa for bear bars which would be the second bars open is less than the first bars close.

2. Tail gaps: when there are three bars the first bars high must be less than the third bar is low and vice versa for bear bars the first bars low must be greater than the third bar is high

These tail gaps take priority if they do get filled I would like it to divert to the most common gap which is the last variation

3. Normal gaps: in between three bars the first bars close is less than the third bars open and vice versa for bear bars the first bars close is greater than the third bars open.

Attached below are pictures of these types of gaps
 
Last edited:
Just copy paste the logic and add one to the numbered variable. You can get about 30ish days before thinkorswim starts *****ing about being too long and complex.
 
thanks for this. This is META stock.

I have set MIN GAP PERCENT to %0.0 to see all gaps in daily chart. but however it does not extend the gap into the future (today) unless i set the MIN GAP PERCENT to about %5, which will also not show gaps that are less. Is there a way to be able to see all gaps to extend into the future until it gets filled regardless the percent?
@halcyonguy
 
@halcyonguy thank you, it works perfect.... i made a little modification to have 8 gaps instead of 4... but you did the hard work in this script, i just copy&paste some of your logic. would you mind if i share the modified script.

once again thank you
 
Hi @halcyonguy, thank you for this script. Is there anyway I can convert it into a scan. I am looking to search for a stock which is getting closer to the unfilled gap formed in last 6 months or so. Let's say when the stock is 1% above the old gap then the scanner starts showing the stock. For example Costco left a gap between 457 and 453 on 14th October 2022 and currently price is heading towards it ( right now 1.5% away from it). Is it something we can do it ?
 
@halcyonguy thank you, it works perfect.... i made a little modification to have 8 gaps instead of 4... but you did the hard work in this script, i just copy&paste some of your logic. would you mind if i share the modified script.

once again thank you
Hi, is it possible for you to shares the 8 gaps scripts. Thanks
 
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
ztwydC0.jpg


WFC reduce gaps = no
5PTE0G8.jpg

hal_gaps
Is There a way to combine the reduce gaps feature with
@samer800's https://usethinkscript.com/threads/fvg-fair-value-gaps-bearish-bullish-levels-for-thinkorswim.14500/
# FVG Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp 5/6/2022
# Mod by Sam4COK @ Samer800 08/2022
 
Last edited by a moderator:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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