Eyl
New member
I found this gap reduction on this forum, which works great.
https://usethinkscript.com/threads/...-price-crosses-into-it-for-thinkorswim.11186/
If anyone can help set up a label for the gap reduction or show me the first one can replicate it for the rest.
I have tried so many different ways just not happening figured I try to ask.
Thank you.
https://usethinkscript.com/threads/...-price-crosses-into-it-for-thinkorswim.11186/
If anyone can help set up a label for the gap reduction or show me the first one can replicate it for the rest.
I have tried so many different ways just not happening figured I try to ask.
Thank you.
Code:
# 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);