Bar gapping box

Rd2der

New member
Please address the problem. I am an amateur I am attempting to draw a box for the bar gaping. my code in embedded in this screen shot (box effort not included because, the code drew a line, rather than a box, under each candle). The arrow indicates the last bar of a gap. The indicator looks left two bars from arrow. I would like to draw a box, rather than an arrow. TOS code link included, may need to squeeze chart to view. Examples are included in the image. Thank you http://tos.mx/ZE2KNyn

RVty9AW.png

Ruby:
#Gap fill
#Roger 2/8/2022

#BearGap


def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot BEARS =
    IsDown[2] and
    IsDown[1] and
    IsDown[0] and
    low[2] > high[0];


BEARS.SetPaintingStrategy(PaintingStrategy.BOOLEAN_arrow_UP);
BEARS.SetDefaultColor(GetColor(253));

# Bull Gap

def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
plot BULLS =
    BUIsUp[2] and
    BUIsUp[1] and
    BUIsUp[0] and
    high[2] < low[0];

BULLS.SetPaintingStrategy(PaintingStrategy.BOOLEAN_arrow_DOWN);
BULLS.SetDefaultColor(GetColor(253));


# End of code
 
Last edited:
Please address the problem. I am an amateur I am attempting to draw a box for the bar gaping. my code in embedded in this screen shot (box effort not included because, the code drew a line, rather than a box, under each candle). The arrow indicates the last bar of a gap. The indicator looks left two bars from arrow. I would like to draw a box, rather than an arrow. TOS code link included, may need to squeeze chart to view. Examples are included in the image. Thank you http://tos.mx/ZE2KNyn

RVty9AW.png

Ruby:
#Gap fill
#Roger 2/8/2022

#BearGap


def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot BEARS =
    IsDown[2] and
    IsDown[1] and
    IsDown[0] and
    low[2] > high[0];


BEARS.SetPaintingStrategy(PaintingStrategy.BOOLEAN_arrow_UP);
BEARS.SetDefaultColor(GetColor(253));

# Bull Gap

def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
plot BULLS =
    BUIsUp[2] and
    BUIsUp[1] and
    BUIsUp[0] and
    high[2] < low[0];

BULLS.SetPaintingStrategy(PaintingStrategy.BOOLEAN_arrow_DOWN);
BULLS.SetDefaultColor(GetColor(253));


# End of code

i think i understand your intent.
. bear gaps, from low[2] to high[0]
. bull gaps, from high[2] to low[0]

how many gap rectangles do you think you want to see on the chart at once?

the rectangles will be drawn with addcloud.
each rectangle will need its own set of a few formulas, to hold the price values and to draw it.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddCloud

i have to say, i was confused by your words and had to read it several times and study your code. those 'gaps' are not what most consider a gap.
 
i think i understand your intent.
. bear gaps, from low[2] to high[0]
. bull gaps, from high[2] to low[0]

how many gap rectangles do you think you want to see on the chart at once?

the rectangles will be drawn with addcloud.
each rectangle will need its own set of a few formulas, to hold the price values and to draw it.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddCloud

i have to say, i was confused by your words and had to read it several times and study your code. those 'gaps' are not what most consider a gap.
I apologize for the confusion. What I have tried to do is locate gaping within three bars. Let's use bears bars as an example. Bar 1 closes at $10.00 and bar 3, which is a bear bar high is at $9.50 (which is were the arrow pointing up is now). My desire is to draw a box, beginning close of bar 1 at $10.00 and draw the box (void) to bar 3 at $9.50. If possible extend the box to the right three of four bars. If not, that is ok also. Thank you very much for your consideration.
 
Last edited:
I apologize for the confusion. What I have tried to do is locate gaping within three bars. Let's use bears bars as an example. Bar 1 closes at $10.00 and bar 3, which is a bear bar high is at $9.50 (which is were the arrow pointing up is now). My desire is to draw a box, beginning close of bar 1 at $10.00 and draw the box (void) to bar 3 at $9.50. If possible extend the box to the right three of four bars. If not, that is ok also. Thank you very much for your consideration.


if you want to draw a box from a close, why is low in your bears formula and not close?
low[2] > high[0]

--------------------------------------------------------

here is a preliminary study, to see if this is what you are after.
it draws lines from low and high levels, based on the last condition in bears and bulls formulas.
bull lines are drawn until the next bull. same with bear.

one problem is, if 2 signals are one after the other, not sure of the origin of lines.


Ruby:
# gaps_alt_3bar

def na = double.nan;
def bn = barnumber();

# -------------------------------------------------------------
#Gap fill
#Roger 2/8/2022

#BearGap
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def BEARS =
    IsDown[2] and
    IsDown[1] and
    IsDown[0] and
    low[2] > high[0];


# Bull Gap
def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
def BULLS =
    BuIsUp[2] and
    BuIsUp[1] and
    BuIsUp[0] and
    high[2] < low[0];


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

input show_arrows = yes;

plot z1 = show_arrows and bears;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.red);
#z1.SetDefaultColor(GetColor(253));

plot z2 = show_arrows and bulls;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#z2.SetDefaultColor(GetColor(253));
z2.SetDefaultColor(Color.green);

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

#  bear bars
#   Bar 1 closes at $10.00
#   bar 3, bear bar high is at $9.50
#    (which is where the arrow pointing up is now).
 
#  My desire is to draw a box,
#   beginning close of bar 1 at $10.00,
#    and draw the box (void) to bar 3 at $9.50.
#      If possible extend the box to the right three of four bars.


# bear box levels
#  low[2] > high[0];

def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

plot z3 = beartop;
plot z4 = bearbot;
z3.SetDefaultColor(Color.red);
z4.SetDefaultColor(Color.red);
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.hidebubble();
z4.hidebubble();



# bear start points
#  low[2] > high[0];

plot z5 = if bears[-2] then low[0] else na;
z5.SetPaintingStrategy(PaintingStrategy.POINTS);
z5.SetDefaultColor(Color.red);
z5.setlineweight(3);
z5.hidebubble();

plot z6 = if bears[0] then high[0] else na;
z6.SetPaintingStrategy(PaintingStrategy.POINTS);
z6.SetDefaultColor(Color.red);
z6.setlineweight(3);
z6.hidebubble();


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

# bull
#  high[2] < low[0];

def bulltop = if bulls[-2] then high[0] else bulltop[1];
def bullbot = if bulls[-2] then low[-2] else bullbot[1];

plot z7 = bulltop;
plot z8 = bullbot;
z7.SetDefaultColor(Color.green);
z8.SetDefaultColor(Color.green);
z7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z7.hidebubble();
z8.hidebubble();



# bull start points
#  high[2] < low[0];

plot z9 = if bulls[-2] then high[0] else na;
z9.SetPaintingStrategy(PaintingStrategy.POINTS);
z9.SetDefaultColor(Color.green);
z9.setlineweight(3);
z9.hidebubble();

plot z10 = if bulls[0] then low[0] else na;
z10.SetPaintingStrategy(PaintingStrategy.POINTS);
z10.SetDefaultColor(Color.green);
z10.setlineweight(3);
z10.hidebubble();
#


bull gap - green lines
. green dots - at the bar level a line is drawn at
bear gap - red lines
. red dots - at the bar level a line is drawn at
h8wZYzo.jpg
 
if you want to draw a box from a close, why is low in your bears formula and not close?
low[2] > high[0]

--------------------------------------------------------

here is a preliminary study, to see if this is what you are after.
it draws lines from low and high levels, based on the last condition in bears and bulls formulas.
bull lines are drawn until the next bull. same with bear.

one problem is, if 2 signals are one after the other, not sure of the origin of lines.


Ruby:
# gaps_alt_3bar

def na = double.nan;
def bn = barnumber();

# -------------------------------------------------------------
#Gap fill
#Roger 2/8/2022

#BearGap
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def BEARS =
    IsDown[2] and
    IsDown[1] and
    IsDown[0] and
    low[2] > high[0];


# Bull Gap
def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
def BULLS =
    BuIsUp[2] and
    BuIsUp[1] and
    BuIsUp[0] and
    high[2] < low[0];


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

input show_arrows = yes;

plot z1 = show_arrows and bears;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.red);
#z1.SetDefaultColor(GetColor(253));

plot z2 = show_arrows and bulls;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#z2.SetDefaultColor(GetColor(253));
z2.SetDefaultColor(Color.green);

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

#  bear bars
#   Bar 1 closes at $10.00
#   bar 3, bear bar high is at $9.50
#    (which is where the arrow pointing up is now).
 
#  My desire is to draw a box,
#   beginning close of bar 1 at $10.00,
#    and draw the box (void) to bar 3 at $9.50.
#      If possible extend the box to the right three of four bars.


# bear box levels
#  low[2] > high[0];

def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

plot z3 = beartop;
plot z4 = bearbot;
z3.SetDefaultColor(Color.red);
z4.SetDefaultColor(Color.red);
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.hidebubble();
z4.hidebubble();



# bear start points
#  low[2] > high[0];

plot z5 = if bears[-2] then low[0] else na;
z5.SetPaintingStrategy(PaintingStrategy.POINTS);
z5.SetDefaultColor(Color.red);
z5.setlineweight(3);
z5.hidebubble();

plot z6 = if bears[0] then high[0] else na;
z6.SetPaintingStrategy(PaintingStrategy.POINTS);
z6.SetDefaultColor(Color.red);
z6.setlineweight(3);
z6.hidebubble();


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

# bull
#  high[2] < low[0];

def bulltop = if bulls[-2] then high[0] else bulltop[1];
def bullbot = if bulls[-2] then low[-2] else bullbot[1];

plot z7 = bulltop;
plot z8 = bullbot;
z7.SetDefaultColor(Color.green);
z8.SetDefaultColor(Color.green);
z7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z7.hidebubble();
z8.hidebubble();



# bull start points
#  high[2] < low[0];

plot z9 = if bulls[-2] then high[0] else na;
z9.SetPaintingStrategy(PaintingStrategy.POINTS);
z9.SetDefaultColor(Color.green);
z9.setlineweight(3);
z9.hidebubble();

plot z10 = if bulls[0] then low[0] else na;
z10.SetPaintingStrategy(PaintingStrategy.POINTS);
z10.SetDefaultColor(Color.green);
z10.setlineweight(3);
z10.hidebubble();
#


bull gap - green lines
. green dots - at the bar level a line is drawn at
bear gap - red lines
. red dots - at the bar level a line is drawn at
h8wZYzo.jpg
Outstanding! I am very grateful for all your hard work. I did attempt to use the addcloud statement but the cloud was constant. I want you to
know, I tried for weeks to accomplish what you have done in minutes. Thank you, so very much and once again I apologize for the confusion! Highest regards.
 
Outstanding! I am very grateful for all your hard work. I did attempt to use the addcloud statement but the cloud was constant. I want you to
know, I tried for weeks to accomplish what you have done in minutes. Thank you, so very much and once again I apologize for the confusion! Highest regards.

welcome. Working with offsets can be confusing.
Your bears and bulls formulas are triggered on the 3rd bar of the pattern.
if you want to start a line on bar one, when one of those formulas is true, you need to look at a future bar, using an offset of -2.

for learning offsets, I would suggest start with something simple, see if you can read a value from x bars away.

The current bar has an offset of 0.
Future bars have negative offsets.
Bars in the past have positive offsets.

test code to experiment with offsets
it shows,
. the current bar number,
. current close
. close from another bar.
Code:
input offset = -1;
def x = close[offset];
addchartbubble(1, low, 
barnumber() + " bn\n" +
close + " cls[0]\n" +
x + " cls[" + offset + "]"
, color.yellow, no);

this uses "\n" to put text on a new line
 
Thank you and I am sorry that I am not proficient in this type of coding. When I was trying to make it work, I was curious if I needed to reset values or if tos resets. Originally, I was going to try to capture and retain the values of the previous two bars and then do a comparision but, when I tried that, I got lines on every bar, regardless of timeframe. I intend to study, the code you wrote and again, I can not express to you the degree of my sincere appreciation for your time and contribution. My best to you
 
Looks like what you're marking are fair value gaps/market imbalance algos.
I find these to be quite helpful, though I stick to just manually marking them.
 
https://usethinkscript.com/threads/...ount-as-ict-describes-it-in-this-video.10129/

I believe this is very close to what I was looking for based on ICT`s videos like
Any further versions/improvements are very welcome.
Thanks!!
Actually, I have never seen this video or heard of this company. The conversation of the gap was discussed on the Bullish Bears (BB) futures chat room. I watched the video you shared and the difference I see is BB suggest gapping candles should be of the same trend. After watching the video. The green candles in the video, are what we are considering, opposed to the red candles this person is discussing.. Thank you for the video.
 
Last edited:
I cannot figure out how to stop the cloud from bleeding over to the next cloud ( /ES 15 min) . Any and all help appreciated. Thank you in advance!

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

def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
def BULLS =
BuIsUp[2] and
BuIsUp[1] and
BuIsUp[0] and
high[2] < low[0];


def bulltop = if bulls[-2] then high[0] else bulltop[1];
def bullbot = if bulls[-2] then low[-2] else bullbot[1];

plot O3 = bulltop;
plot O4 = bullbot;

O3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O3.SetDefaultColor(Color.Green);
O4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O4.SetDefaultColor(Color.Green);
O3.hidebubble();
O4.hidebubble();


AddCloud(O3, O4, Color.Green, Color.Green );


http://tos.mx/AGz7Erf
 
Last edited by a moderator:
#-------------------------------------------------

def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
def BULLS =
BuIsUp[2] and
BuIsUp[1] and
BuIsUp[0] and
high[2] < low[0];


def bulltop = if bulls[-2] then high[0] else bulltop[1];
def bullbot = if bulls[-2] then low[-2] else bullbot[1];

plot O3 = bulltop;
plot O4 = bullbot;

O3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O3.SetDefaultColor(Color.Green);
O4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O4.SetDefaultColor(Color.Green);
O3.hidebubble();
O4.hidebubble();


AddCloud(O3, O4, Color.Green, Color.Green );


http://tos.mx/AGz7Erf

there may be other ways.
i use 2 addclouds and alternate between them. i make a cloud counter. when the count is odd, use 1 cloud. when the count is even use the other cloud.

https://usethinkscript.com/threads/congestion-zone.10503/#post-93190
 
How to bypass.
In the below, study, I do not want the lines, dots or cloud to print, if the height of beartop and bearbot is less than 2.5 points. I have looked in
TOS manual and on the web and cannot find how to bypass a condition.

def na = Double.NaN;
def bn = BarNumber();

# -------------------------------------------------------------
#Gap fill


#BearGap
def bearIsUp = close > open;
def bearIsDown = close < open;
def bearIsDoji = IsDoji();
def bearavgRange = 0.05 * Average(high - low, 20);
def bears =
bearIsDown[2] and
bearIsDown[1] and
bearIsDown[0] and
low[2] > high[0];


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


def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

plot O1 = beartop;
plot O2 = bearbot;

O1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#O1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
O1.SetDefaultColor(Color.RED);
O2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O2.SetDefaultColor(Color.RED);
O1.HideBubble();
O2.HideBubble();

AddCloud(O1, O2, Color.RED, Color.RED);

plot O1D = if bears[-2] then low[0] else na;
O1D.SetPaintingStrategy(PaintingStrategy.POINTS);
O1D.SetDefaultColor(Color.WHITE);
O1D.SetLineWeight(3);
O1D.HideBubble();

plot O2D = if bears[0] then high[0] else na;
O2D.SetPaintingStrategy(PaintingStrategy.POINTS);
O2D.SetDefaultColor(Color.WHITE);
O2D.SetLineWeight(3);
O2D.HideBubble();
 
Last edited by a moderator:
How to bypass.
In the below, study, I do not want the lines, dots or cloud to print, if the height of beartop and bearbot is less than 2.5 points. I have looked in
TOS manual and on the web and cannot find how to bypass a condition.

def na = Double.NaN;
def bn = BarNumber();

# -------------------------------------------------------------
#Gap fill


#BearGap
def bearIsUp = close > open;
def bearIsDown = close < open;
def bearIsDoji = IsDoji();
def bearavgRange = 0.05 * Average(high - low, 20);
def bears =
bearIsDown[2] and
bearIsDown[1] and
bearIsDown[0] and
low[2] > high[0];


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


def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

plot O1 = beartop;
plot O2 = bearbot;

O1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#O1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
O1.SetDefaultColor(Color.RED);
O2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O2.SetDefaultColor(Color.RED);
O1.HideBubble();
O2.HideBubble();

AddCloud(O1, O2, Color.RED, Color.RED);

plot O1D = if bears[-2] then low[0] else na;
O1D.SetPaintingStrategy(PaintingStrategy.POINTS);
O1D.SetDefaultColor(Color.WHITE);
O1D.SetLineWeight(3);
O1D.HideBubble();

plot O2D = if bears[0] then high[0] else na;
O2D.SetPaintingStrategy(PaintingStrategy.POINTS);
O2D.SetDefaultColor(Color.WHITE);
O2D.SetLineWeight(3);
O2D.HideBubble();


if you don't want something to happen, you would create a new condition and put it in an if then.
def x = if (top-bot) < 2.5 then double.nan else do other stuff

i modified the code from post 14
..skip clouds on small gaps
.....add an input to pick a min gap height , 2.5 default
.....the points are still drawn for all potential gaps
..add gap counter
..alternate between clouds so colors don't run together.


Ruby:
# gap_box_skipht_0

# skip clouds on small gaps
# add gap counter
# alternate between clouds so colors don't run together.

# https://usethinkscript.com/threads/bar-gapping-box.10271/#post-94263
# post14  Rd2der

def na = Double.NaN;
def bn = BarNumber();

# -------------------------------------------------------------
#Gap fill


#BearGap
def bearIsUp = close > open;
def bearIsDown = close < open;
def bearIsDoji = IsDoji();
def bearavgRange = 0.05 * Average(high - low, 20);
def bears =
bearIsDown[2] and
bearIsDown[1] and
bearIsDown[0] and
low[2] > high[0];

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

def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

# check if gap is > a #
input min_gap_ht = 2.5;
def gap_ht = absvalue(beartop - bearbot);
def gap_en = if gap_ht >=  min_gap_ht then 1 else 0;

# add a gap counter
# alternate between 2 clouds, so the colors don't run together
# look for a change in beartop and a big enough gap
def gap_cnt = if bn == 1 then 0
  else if gap_en and (beartop[1] <> beartop[0]) then gap_cnt[1] + 1
  else gap_cnt[1];

def evencld = ( gap_cnt % 2 ) == 0;
def oddcld = !evencld;


#plot O1 = beartop;
#plot O2 = bearbot;
plot O1 = if gap_en then beartop else na;
plot O2 = if gap_en then bearbot else na;


O1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#O1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
O1.SetDefaultColor(Color.RED);
O2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O2.SetDefaultColor(Color.RED);
O1.HideBubble();
O2.HideBubble();

#AddCloud(O1, O2, Color.RED, Color.RED);
AddCloud((if oddcld then O1 else na), O2, Color.RED, Color.RED);
AddCloud((if evencld then O1 else na), O2, Color.RED, Color.RED);

plot O1D = if bears[-2] then low[0] else na;
O1D.SetPaintingStrategy(PaintingStrategy.POINTS);
O1D.SetDefaultColor(Color.WHITE);
O1D.SetLineWeight(3);
O1D.HideBubble();

plot O2D = if bears[0] then high[0] else na;
O2D.SetPaintingStrategy(PaintingStrategy.POINTS);
O2D.SetDefaultColor(Color.WHITE);
O2D.SetLineWeight(3);
O2D.HideBubble();
#

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

input test2 = no;
addchartbubble(test2, low,
bn + "\n" +
beartop + "\n" +
bearbot + "\n" +
gap_ht + " diff\n" +
gap_cnt
, color.yellow, no);
#
 
if you want to draw a box from a close, why is low in your bears formula and not close?
low[2] > high[0]

--------------------------------------------------------

here is a preliminary study, to see if this is what you are after.
it draws lines from low and high levels, based on the last condition in bears and bulls formulas.
bull lines are drawn until the next bull. same with bear.

one problem is, if 2 signals are one after the other, not sure of the origin of lines.


Ruby:
# gaps_alt_3bar

def na = double.nan;
def bn = barnumber();

# -------------------------------------------------------------
#Gap fill
#Roger 2/8/2022

#BearGap
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def BEARS =
    IsDown[2] and
    IsDown[1] and
    IsDown[0] and
    low[2] > high[0];


# Bull Gap
def BuIsUp = close > open;
def BuIsDown = close < open;
def BuIsDoji = IsDoji();
def BuavgRange = 0.05 * Average(high - low, 20);
def BULLS =
    BuIsUp[2] and
    BuIsUp[1] and
    BuIsUp[0] and
    high[2] < low[0];


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

input show_arrows = yes;

plot z1 = show_arrows and bears;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.SetDefaultColor(Color.red);
#z1.SetDefaultColor(GetColor(253));

plot z2 = show_arrows and bulls;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#z2.SetDefaultColor(GetColor(253));
z2.SetDefaultColor(Color.green);

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

#  bear bars
#   Bar 1 closes at $10.00
#   bar 3, bear bar high is at $9.50
#    (which is where the arrow pointing up is now).
 
#  My desire is to draw a box,
#   beginning close of bar 1 at $10.00,
#    and draw the box (void) to bar 3 at $9.50.
#      If possible extend the box to the right three of four bars.


# bear box levels
#  low[2] > high[0];

def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

plot z3 = beartop;
plot z4 = bearbot;
z3.SetDefaultColor(Color.red);
z4.SetDefaultColor(Color.red);
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.hidebubble();
z4.hidebubble();



# bear start points
#  low[2] > high[0];

plot z5 = if bears[-2] then low[0] else na;
z5.SetPaintingStrategy(PaintingStrategy.POINTS);
z5.SetDefaultColor(Color.red);
z5.setlineweight(3);
z5.hidebubble();

plot z6 = if bears[0] then high[0] else na;
z6.SetPaintingStrategy(PaintingStrategy.POINTS);
z6.SetDefaultColor(Color.red);
z6.setlineweight(3);
z6.hidebubble();


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

# bull
#  high[2] < low[0];

def bulltop = if bulls[-2] then high[0] else bulltop[1];
def bullbot = if bulls[-2] then low[-2] else bullbot[1];

plot z7 = bulltop;
plot z8 = bullbot;
z7.SetDefaultColor(Color.green);
z8.SetDefaultColor(Color.green);
z7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z7.hidebubble();
z8.hidebubble();



# bull start points
#  high[2] < low[0];

plot z9 = if bulls[-2] then high[0] else na;
z9.SetPaintingStrategy(PaintingStrategy.POINTS);
z9.SetDefaultColor(Color.green);
z9.setlineweight(3);
z9.hidebubble();

plot z10 = if bulls[0] then low[0] else na;
z10.SetPaintingStrategy(PaintingStrategy.POINTS);
z10.SetDefaultColor(Color.green);
z10.setlineweight(3);
z10.hidebubble();
#


bull gap - green lines
. green dots - at the bar level a line is drawn at
bear gap - red lines
. red dots - at the bar level a line is drawn at
h8wZYzo.jpg
@halcyonguy thanks for doing this...Sorry to revive the thread but is there an easy way to just have it show just the last bull or bear gap? Would help to have just that to redice c;lutter. Thanks!
 
if you don't want something to happen, you would create a new condition and put it in an if then.
def x = if (top-bot) < 2.5 then double.nan else do other stuff

i modified the code from post 14
..skip clouds on small gaps
.....add an input to pick a min gap height , 2.5 default
.....the points are still drawn for all potential gaps
..add gap counter
..alternate between clouds so colors don't run together.


Ruby:
# gap_box_skipht_0

# skip clouds on small gaps
# add gap counter
# alternate between clouds so colors don't run together.

# https://usethinkscript.com/threads/bar-gapping-box.10271/#post-94263
# post14  Rd2der

def na = Double.NaN;
def bn = BarNumber();

# -------------------------------------------------------------
#Gap fill


#BearGap
def bearIsUp = close > open;
def bearIsDown = close < open;
def bearIsDoji = IsDoji();
def bearavgRange = 0.05 * Average(high - low, 20);
def bears =
bearIsDown[2] and
bearIsDown[1] and
bearIsDown[0] and
low[2] > high[0];

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

def beartop = if bears[-2] then high[-2] else beartop[1];
def bearbot = if bears[-2] then low[0] else bearbot[1];

# check if gap is > a #
input min_gap_ht = 2.5;
def gap_ht = absvalue(beartop - bearbot);
def gap_en = if gap_ht >=  min_gap_ht then 1 else 0;

# add a gap counter
# alternate between 2 clouds, so the colors don't run together
# look for a change in beartop and a big enough gap
def gap_cnt = if bn == 1 then 0
  else if gap_en and (beartop[1] <> beartop[0]) then gap_cnt[1] + 1
  else gap_cnt[1];

def evencld = ( gap_cnt % 2 ) == 0;
def oddcld = !evencld;


#plot O1 = beartop;
#plot O2 = bearbot;
plot O1 = if gap_en then beartop else na;
plot O2 = if gap_en then bearbot else na;


O1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#O1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
O1.SetDefaultColor(Color.RED);
O2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
O2.SetDefaultColor(Color.RED);
O1.HideBubble();
O2.HideBubble();

#AddCloud(O1, O2, Color.RED, Color.RED);
AddCloud((if oddcld then O1 else na), O2, Color.RED, Color.RED);
AddCloud((if evencld then O1 else na), O2, Color.RED, Color.RED);

plot O1D = if bears[-2] then low[0] else na;
O1D.SetPaintingStrategy(PaintingStrategy.POINTS);
O1D.SetDefaultColor(Color.WHITE);
O1D.SetLineWeight(3);
O1D.HideBubble();

plot O2D = if bears[0] then high[0] else na;
O2D.SetPaintingStrategy(PaintingStrategy.POINTS);
O2D.SetDefaultColor(Color.WHITE);
O2D.SetLineWeight(3);
O2D.HideBubble();
#

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

input test2 = no;
addchartbubble(test2, low,
bn + "\n" +
beartop + "\n" +
bearbot + "\n" +
gap_ht + " diff\n" +
gap_cnt
, color.yellow, no);
#
Thank you so VERY MUCH! I read chapters 5 & 6 in the manual and would of never come up with this. I was working in the are of hide and clearly was chasing a rabbit down a hole. I will keep studying and hopefully will get to where I can help others. Once again, thank you!
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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