Repaints Identify Gap-Up Pattern For ThinkOrSwim

Repaints

halcyonguy

Moderator - Expert
VIP
Lifetime
The author states:
The reason I am going for the gaps between three bars only is because I saw the strategy "The Fair Value Gap" theory and I found it helpful. So, I wanted to see how well it works for trading. Thank you so much for your precious time!
1716478188955.png

here you go
add down gap signals
i set the offset to be 2, so it looks at 3 bar groups and checks for a gap between the 1st and 3rd bars.
add horizontal lines and clouds, in the gap areas.
(count gaps and draw with multiple plots, so the clouds of adjacent gaps don't interact)
changed the arrows to point in the direction of gaps. they appear on 1st bar.
can turn arrows off

mod note:
This script uses future bars. It waits 2 bars into the future for confirmation. Then repaints the 2nd previous bar. Because this study does not update in real time. It cannot be used in scans, watchlists, or conditional orders
Code:
# 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);
#
 
Last edited by a moderator:

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

here you go

add down gap signals
i set the offset to be 2, so it looks at 3 bar groups and checks for a gap between the 1st and 3rd bars.
add horizontal lines and clouds, in the gap areas.
(count gaps and draw with multiple plots, so the clouds of adjacent gaps don't interact)
changed the arrows to point in the direction of gaps. they appear on 1st bar.
can turn arrows off


Code:
# 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);
#
Thank you, @halcyonguy . The reason I am going for the gaps between three bars only is because I saw the strategy "The Fair Value Gap" theory and I found it helpful. So, I wanted to see how well it works for trading. Thank you so much for your precious time!
 
here you go

add down gap signals
i set the offset to be 2, so it looks at 3 bar groups and checks for a gap between the 1st and 3rd bars.
add horizontal lines and clouds, in the gap areas.
(count gaps and draw with multiple plots, so the clouds of adjacent gaps don't interact)
changed the arrows to point in the direction of gaps. they appear on 1st bar.
can turn arrows off


Code:
# 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);
#
Thanks for this script. I have been reading a lot about order imbalance and importance for the retail trader to improve the odds vs the big guys. I added to my intraday charts on 10/9/24 and will see if this improves the odds.
 
here you go

add down gap signals
i set the offset to be 2, so it looks at 3 bar groups and checks for a gap between the 1st and 3rd bars.
add horizontal lines and clouds, in the gap areas.
(count gaps and draw with multiple plots, so the clouds of adjacent gaps don't interact)
changed the arrows to point in the direction of gaps. they appear on 1st bar.
can turn arrows off


Code:
# 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);
#
I just started using this to see if it helps in day trading. Is it possible to add this as a column to a Watch List when an arrow appears? I didnt see it as a choice. Many thanks
 
1/8/25 Update...This signal does improve the odds for day traders. I am using it on a 10 minute time frame after a signal alert on a daily time frame. I set up a scan on this script last week for both a 10 and 15 minute period. It seems when the scan alert brings a symbol up it happened like 30 minutes ago. So, you miss part of the gains. Suggestions please?
 
Last edited by a moderator:
mod note:
This script uses future bars. It waits 2 bars into the future for confirmation. Then repaints the 2nd previous bar back. Because this study does not update in real time, it is not useful in scans, watchlists, or conditional orders

FYI, all the Fair Value Gap scripts on this forum repaint and lag
By definition, they have to wait until the future to define the gaps, and then they go back in history and paint a signal where they occurred.
@TechGuy
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
265 Online
Create Post

Similar threads

Similar threads

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