# gap_future_xbars_02
#https://usethinkscript.com/threads/identify-gap-up-pattern.18790/
#Identify Gap-Up Pattern
#identify a candle where the low of the candle two positions ahead has a gap up from the high of the current candle.
def na = double.nan;
def bn = barnumber();
def gap_bar_offset = 2;
def uptop = getvalue(low, -gap_bar_offset);
def upbot = high;
def dwntop = low;
def dwnbot = getvalue(high, -gap_bar_offset);
def isgapup = if isnan(uptop) then 0
else if uptop > high then 1
else 0;
def isgapdwn = if isnan(dwnbot) then 0
else if dwnbot < low then 1
else 0;
def y = 0.0004;
input arrows = yes;
plot zup = if arrows and isgapdwn then (high * (1+y)) else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zup.SetDefaultColor(Color.red);
zup.setlineweight(3);
zup.hidebubble();
plot zdwn = if arrows and isgapup then (low * (1-y)) else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_up);
zdwn.SetDefaultColor(Color.green);
zdwn.setlineweight(3);
zdwn.hidebubble();
#-----------------------
# up gaps
# count all up gaps
def upcnt = if bn == 1 then 0 else if isgapup then upcnt[1] + 1 else upcnt[1];
# look at gap ups between first and last bars, in a 3 bar group
# since 3 bars, need 3+1 lines , so the 1st and 3rd lines don't diagonally connect
# create true signals for 4 counters. compare remainders
def upcnt1 = (upcnt % 4) == 0;
def upcnt2 = (upcnt % 4) == 1;
def upcnt3 = (upcnt % 4) == 2;
def upcnt4 = (upcnt % 4) == 3;
def u1startbn = if upcnt[1] != upcnt and upcnt1 then bn else u1startbn[1];
def u1stopbn = if upcnt[1] != upcnt and upcnt1 then bn+gap_bar_offset else u1stopbn[1];
def u2startbn = if upcnt[1] != upcnt and upcnt2 then bn else u2startbn[1];
def u2stopbn = if upcnt[1] != upcnt and upcnt2 then bn+gap_bar_offset else u2stopbn[1];
def u3startbn = if upcnt[1] != upcnt and upcnt3 then bn else u3startbn[1];
def u3stopbn = if upcnt[1] != upcnt and upcnt3 then bn+gap_bar_offset else u3stopbn[1];
def u4startbn = if upcnt[1] != upcnt and upcnt4 then bn else u4startbn[1];
def u4stopbn = if upcnt[1] != upcnt and upcnt4 then bn+gap_bar_offset else u4stopbn[1];
def u1top = if u1startbn[1] != u1startbn then uptop
else if bn >= u1startbn and bn <= u1stopbn then u1top[1]
else na;
def u1bot = if u1stopbn[1] != u1stopbn then upbot
else if bn >= u1startbn and bn <= u1stopbn then u1bot[1]
else na;
def u2top = if u2startbn[1] != u2startbn then uptop
else if bn >= u2startbn and bn <= u2stopbn then u2top[1]
else na;
def u2bot = if u2stopbn[1] != u2stopbn then upbot
else if bn >= u2startbn and bn <= u2stopbn then u2bot[1]
else na;
def u3top = if u3startbn[1] != u3startbn then uptop
else if bn >= u3startbn and bn <= u3stopbn then u3top[1]
else na;
def u3bot = if u3stopbn[1] != u3stopbn then upbot
else if bn >= u3startbn and bn <= u3stopbn then u3bot[1]
else na;
def u4top = if u4startbn[1] != u4startbn then uptop
else if bn >= u4startbn and bn <= u4stopbn then u4top[1]
else na;
def u4bot = if u4stopbn[1] != u4stopbn then upbot
else if bn >= u4startbn and bn <= u4stopbn then u4bot[1]
else na;
plot z1u = u1top;
z1u.SetPaintingStrategy(PaintingStrategy.LINE);
z1u.SetDefaultColor(Color.green);
#z1u.setlineweight(1);
z1u.hidebubble();
plot z2u = u1bot;
z2u.SetPaintingStrategy(PaintingStrategy.LINE);
z2u.SetDefaultColor(Color.green);
#z2u.setlineweight(1);
z2u.hidebubble();
plot z3u = u2top;
z3u.SetPaintingStrategy(PaintingStrategy.LINE);
z3u.SetDefaultColor(Color.green);
#z4u.setlineweight(1);
z3u.hidebubble();
plot z4u = u2bot;
z4u.SetPaintingStrategy(PaintingStrategy.LINE);
z4u.SetDefaultColor(Color.green);
#z4u.setlineweight(1);
z4u.hidebubble();
plot z5u = u3top;
z5u.SetPaintingStrategy(PaintingStrategy.LINE);
z5u.SetDefaultColor(Color.green);
#z5u.setlineweight(1);
z5u.hidebubble();
plot z6u = u3bot;
z6u.SetPaintingStrategy(PaintingStrategy.LINE);
z6u.SetDefaultColor(Color.green);
#z6u.setlineweight(1);
z6u.hidebubble();
plot z7u = u4top;
z7u.SetPaintingStrategy(PaintingStrategy.LINE);
z7u.SetDefaultColor(Color.green);
#z7u.setlineweight(1);
z7u.hidebubble();
plot z8u = u4bot;
z8u.SetPaintingStrategy(PaintingStrategy.LINE);
z8u.SetDefaultColor(Color.green);
#z8u.setlineweight(1);
z8u.hidebubble();
addcloud(z1u,z2u,color.green);
addcloud(z3u,z4u,color.green);
addcloud(z5u,z6u,color.green);
addcloud(z7u,z8u,color.green);
#-----------------------
# down gaps
# count all up gaps
def dwncnt = if bn == 1 then 0 else if isgapdwn then dwncnt[1] + 1 else dwncnt[1];
# look at gap downs between first and last bars, in a 3 bar group
# since 3 bars, need 3+1 lines , so the 1st and 3rd lines don't diagonally connect
# create true signals for 4 counters. compare remainders
def dwncnt1 = (dwncnt % 4) == 0;
def dwncnt2 = (dwncnt % 4) == 1;
def dwncnt3 = (dwncnt % 4) == 2;
def dwncnt4 = (dwncnt % 4) == 3;
def d1startbn = if dwncnt[1] != dwncnt and dwncnt1 then bn else d1startbn[1];
def d1stopbn = if dwncnt[1] != dwncnt and dwncnt1 then bn+gap_bar_offset else d1stopbn[1];
def d2startbn = if dwncnt[1] != dwncnt and dwncnt2 then bn else d2startbn[1];
def d2stopbn = if dwncnt[1] != dwncnt and dwncnt2 then bn+gap_bar_offset else d2stopbn[1];
def d3startbn = if dwncnt[1] != dwncnt and dwncnt3 then bn else d3startbn[1];
def d3stopbn = if dwncnt[1] != dwncnt and dwncnt3 then bn+gap_bar_offset else d3stopbn[1];
def d4startbn = if dwncnt[1] != dwncnt and dwncnt4 then bn else d4startbn[1];
def d4stopbn = if dwncnt[1] != dwncnt and dwncnt4 then bn+gap_bar_offset else d4stopbn[1];
def d1top = if d1startbn[1] != d1startbn then dwntop
else if bn >= d1startbn and bn <= d1stopbn then d1top[1]
else na;
def d1bot = if d1stopbn[1] != d1stopbn then dwnbot
else if bn >= d1startbn and bn <= d1stopbn then d1bot[1]
else na;
def d2top = if d2startbn[1] != d2startbn then dwntop
else if bn >= d2startbn and bn <= d2stopbn then d2top[1]
else na;
def d2bot = if d2stopbn[1] != d2stopbn then dwnbot
else if bn >= d2startbn and bn <= d2stopbn then d2bot[1]
else na;
def d3top = if d3startbn[1] != d3startbn then dwntop
else if bn >= d3startbn and bn <= d3stopbn then d3top[1]
else na;
def d3bot = if d3stopbn[1] != d3stopbn then dwnbot
else if bn >= d3startbn and bn <= d3stopbn then d3bot[1]
else na;
def d4top = if d4startbn[1] != d4startbn then dwntop
else if bn >= d4startbn and bn <= d4stopbn then d4top[1]
else na;
def d4bot = if d4stopbn[1] != d4stopbn then dwnbot
else if bn >= d4startbn and bn <= d4stopbn then d4bot[1]
else na;
plot z1d = d1top;
z1d.SetPaintingStrategy(PaintingStrategy.LINE);
z1d.SetDefaultColor(Color.red);
#z1d.setlineweight(1);
z1d.hidebubble();
plot z2d = d1bot;
z2d.SetPaintingStrategy(PaintingStrategy.LINE);
z2d.SetDefaultColor(Color.red);
#z2d.setlineweight(1);
z2d.hidebubble();
plot z3d = d2top;
z3d.SetPaintingStrategy(PaintingStrategy.LINE);
z3d.SetDefaultColor(Color.red);
#z4d.setlineweight(1);
z3d.hidebubble();
plot z4d = d2bot;
z4d.SetPaintingStrategy(PaintingStrategy.LINE);
z4d.SetDefaultColor(Color.red);
#z4d.setlineweight(1);
z4d.hidebubble();
plot z5d = d3top;
z5d.SetPaintingStrategy(PaintingStrategy.LINE);
z5d.SetDefaultColor(Color.red);
#z5d.setlineweight(1);
z5d.hidebubble();
plot z6d = d3bot;
z6d.SetPaintingStrategy(PaintingStrategy.LINE);
z6d.SetDefaultColor(Color.red);
#z6d.setlineweight(1);
z6d.hidebubble();
plot z7d = d4top;
z7d.SetPaintingStrategy(PaintingStrategy.LINE);
z7d.SetDefaultColor(Color.red);
#z7d.setlineweight(1);
z7d.hidebubble();
plot z8d = d4bot;
z8d.SetPaintingStrategy(PaintingStrategy.LINE);
z8d.SetDefaultColor(Color.red);
#z8d.setlineweight(1);
z8d.hidebubble();
addcloud(z1d,z2d,color.red);
addcloud(z3d,z4d,color.red);
addcloud(z5d,z6d,color.red);
addcloud(z7d,z8d,color.red);
#-------------------------
# misc
#x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
addchartbubble(0, low,
upcnt + " C\n" +
upcnt1 + " 1\n" +
upcnt2 + " 2\n" +
upcnt3 + " 3\n" +
upcnt4 + " 4\n"
#bn + "\n" +
#u1startbn + "\n" +
#u1stopbn + "\n"
, color.yellow, no);
#