# inside_updown_box_mod01b
# https://usethinkscript.com/threads/inside-and-up-down-box.11130/
# Inside and Up/Down box
input candle_levels = {default "wick" , "body" };
input show_big_bar_level_lines = yes;
def bn = barnumber();
def na = double.nan;
# choose price levels for inside bar , wicks or body
def highx;
def lowx;
def t;
switch (candle_levels) {
case "wick":
highx = high;
lowx = low;
t = 1;
case "body":
highx = Max( open, close);
lowx = Min( open, close);
t = 2;
}
addlabel(1, "Inside bar based on " + (if t == 1 then "Wick levels" else "Body levels"), color.yellow);
# ?? original code , body inside bar ?
#def insideBar = close < close[1] and close > open[1] or close < open[1] and close > close[1];
# 'normal' inside bar formula , wicks
#def insideBar = low[1] < low and high[1] > high;
def insideBar = lowx[1] < lowx and highx[1] > highx;
def InsideUp = insideBar[1] and close > highx[1] ;
def InsideDown = insideBar[1] and close < lowx[1] ;
def uphi = if bn == 1 then na else if InsideUp then highx[1] else if InsideUp[-1] then na else uphi[1];
def uplo = if bn == 1 then na else if InsideUp then lowx[1] else if InsideUp[-1] then na else uplo[1];
def dwnhi = if bn == 1 then na else if Insidedown then highx[1] else if Insidedown[-1] then na else dwnhi[1];
def dwnlo = if bn == 1 then na else if Insidedown then lowx[1] else if Insidedown[-1] then na else dwnlo[1];
plot zuphi = uphi;
zuphi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zuphi.SetDefaultColor(Color.light_green);
zuphi.hidebubble();
plot zuplo = uplo;
zuplo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zuplo.SetDefaultColor(Color.light_green);
zuplo.hidebubble();
plot zdwnhi = dwnhi;
zdwnhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zdwnhi.SetDefaultColor(Color.light_red);
zdwnhi.hidebubble();
plot zdwnlo = dwnlo;
zdwnlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zdwnlo.SetDefaultColor(Color.light_red);
zdwnlo.hidebubble();
addcloud(zuphi, zuplo, color.green, color.light_green);
addcloud(zdwnhi, zdwnlo, color.red, color.light_red);
# -------------------------
# show lines from mother bar ,big bar
def x = 4;
def biguphi = if bn == 1 then na else if InsideUp[-2] then highx[0] else if InsideUp[x] then na else biguphi[1];
def biguplo = if bn == 1 then na else if InsideUp[-2] then lowx[0] else if InsideUp[x] then na else biguplo[1];
def bigdwnhi = if bn == 1 then na else if Insidedown[-2] then highx[0] else if Insidedown[x] then na else bigdwnhi[1];
def bigdwnlo = if bn == 1 then na else if Insidedown[-2] then lowx[0] else if Insidedown[x] then na else bigdwnlo[1];
plot z1 = biguphi;
plot z2 = biguplo;
plot z3 = bigdwnhi;
plot z4 = bigdwnlo;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.lime);
z1.hidebubble();
z1.SetHiding(!show_big_bar_level_lines);
z1.SetStyle(Curve.MEDIUM_DASH);
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(Color.lime);
z2.hidebubble();
z2.SetHiding(!show_big_bar_level_lines);
z2.SetStyle(Curve.MEDIUM_DASH);
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.magenta);
z3.hidebubble();
z3.SetHiding(!show_big_bar_level_lines);
z3.SetStyle(Curve.MEDIUM_DASH);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetDefaultColor(Color.magenta);
z4.hidebubble();
z4.SetHiding(!show_big_bar_level_lines);
z4.SetStyle(Curve.MEDIUM_DASH);
# --------------------------
# test stuff
addchartbubble(0, low,
biguphi + "\n" +
biguplo + "\n" +
bigdwnhi + "\n" +
bigdwnlo
, color.yellow, no);
input test_vertical_lines = no;
addverticalline(test_vertical_lines and insideup, "-", color.green);
addverticalline(test_vertical_lines and insidedown, "-", color.red);
#