#H/L_2$_Candles_indicator_v1
input use_count_plot_limit = yes;
input count = 2;
input diff = 2.0;
input show_bubbles = yes;
input bubblemover_updown = 3;
def day = if GetDay() != GetDay()[1] then 1 else day[1] + 1;
def up = if GetDay() != GetDay()[1]
then if close > open then 1
else 0
else if GetDay() == GetDay()[1]
then if close > open
then 1
else 0
else up[1];
def bull;
def bear;
# Bull Groups ##################################
bull = if day[1] != 78 and day >= 1 and #No bars from prior day allowed
#1 bar only: red, green, red
up[1] == 0 and up == 1 and up[-1] == 0 and
(high - low) > diff and
#Next bar must be within highest/lowest
high[-1] < highest(high, 1) and low[-1] > lowest(low, 1)
then 1
else if day[1] != 78 and day >= 1 and #No bars from prior day allowed
#2 bars only: red, green, green, red
up[2] == 0 and up[1] == 1 and up == 1 and up[-1] == 0 and
(Highest(high, 2) - Lowest(low, 2)) > diff and
#Next bar must be within highest/lowest
high[-1] < Highest(high, 2) and low[-1] > Lowest(low, 2)
then 1
else if day[3] != 78 and day >= 3 and
#3 bars: green, green, green
up[2] == 1 and up[1] == 1 and up == 1 and
(Highest(high, 3) - Lowest(low, 3)) > diff and
#Next bar must be within highest/lowest
high[-1] < Highest(high, 3) and low[-1] > Lowest(low, 3)
then 1
else 0;
def bull_hl = if day[1] != 78 and day >= 1 and #No bars from prior day allowed
#1 bar only: red, green, red
up[1] == 0 and up == 1 and up[-1] == 0 and
(high - low) > diff and
#Next bar must be within highest/lowest
high[-1] < highest(high, 1) and low[-1] > lowest(low, 1)
then 1
else if day[1] != 78 and day >= 1 and
#2 bars only: red, green, green, red
up[2] == 0 and up[1] == 1 and up == 1 and up[-1] == 0 and
(Highest(high, 2) - Lowest(low, 2)) > diff and
#Next bar must be within highest/lowest
high[-1] < Highest(high, 2) and low[-1] > Lowest(low, 2)
then 2
else if day[3] != 78 and day >= 3 and
#3 bars: green, green, green
up[2] == 1 and up[1] == 1 and up == 1 and
(Highest(high, 3) - Lowest(low, 3)) > diff and
#Next bar must be within highest/lowest
high[-1] < Highest(high, 3) and low[-1] > Lowest(low, 3)
then 3
else 0;
def hbull_ = if bull
then if bull_hl == 1 then Highest(high, 1) else
if bull_hl == 2 then Highest(high, 2)
else Highest(high, 3)
else hbull_[1];
def lbull_ = if bull
then if bull_hl == 1 then Lowest(low, 1) else
if bull_hl == 2 then Lowest(low, 2)
else Lowest(low, 3)
else lbull_[1];
def hbull = if bull
then if bull_hl == 1 then Highest(high, 1) else
if bull_hl == 2 then Highest(high, 2)
else Highest(high, 3)
else if high < hbull_[1] and low > lbull_[1]
then hbull[1] else Double.NaN;
def lbull = if bull
then if bull_hl == 1 then Lowest(low, 1) else
if bull_hl == 2 then Lowest(low, 2)
else Lowest(low, 3)
else if high < hbull_[1] and low > lbull_[1]
then lbull[1] else Double.NaN;
def hbullbar = if bull
then 1 else 0;
def lbullbar = if bull
then 1 else 0;
def dataCounthbull = CompoundValue(1, if hbullbar[1] == 0 and hbullbar then dataCounthbull[1] + 1 else dataCounthbull[1], 0);
def dataCountlbull = CompoundValue(1, if lbullbar[1] == 0 and lbullbar then dataCountlbull[1] + 1 else dataCountlbull[1], 0);
plot hhbull = if use_count_plot_limit and HighestAll(dataCounthbull) - dataCounthbull <= count - 1 or !use_count_plot_limit then hbull else Double.NaN;
hhbull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhbull.SetDefaultColor(Color.CYAN);
plot llbull = if use_count_plot_limit and HighestAll(dataCountlbull) - dataCountlbull <= count - 1 or !use_count_plot_limit then lbull else Double.NaN;
llbull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llbull.SetDefaultColor(Color.CYAN);
AddChartBubble(show_bubbles and hbullbar[1] == 0 and hbullbar, hhbull + TickSize() * bubblemover_updown, hhbull, Color.GREEN);
AddChartBubble(show_bubbles and lbullbar[1] == 0 and lbullbar, llbull - TickSize() * bubblemover_updown, llbull, Color.GREEN, no);
plot fib1bull = llbull + (hhbull - llbull) * .618;
plot fib2bull = llbull + (hhbull - llbull) * .500;
plot fib3bull = llbull + (hhbull - llbull) * .382;
fib1bull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib2bull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib3bull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib1bull.SetDefaultColor(Color.WHITE);
fib2bull.SetDefaultColor(Color.YELLOW);
fib3bull.SetDefaultColor(Color.RED);
# Bear Groups ##################################
bear = if day[1] != 78 and day >= 1 and #No bars from prior day allowed
#1 bar only: green, red, green
up[1] == 1 and up == 0 and up[-1] == 1 and
(high - low) > diff and
#Next bar must be within highest/lowest
high[-1] < highest(high, 1) and low[-1] > lowest(low, 1)
then 1
else if day[1] != 78 and day >= 1 and
#2 bars: green, red, red, green
up[2] == 1 and up[1] == 0 and up == 0 and up[-1] == 1 and
(Highest(high, 2) - Lowest(low, 2)) > diff and
high[-1] < Highest(high, 2) and low[-1] > Lowest(low, 2)
then 1
else if day[3] != 78 and day >= 3 and
#3 bars: red, red, red
up[2] == 0 and up[1] == 0 and up == 0 and
(Highest(high, 3) - Lowest(low, 3)) > diff and
high[-1] < Highest(high, 3) and low[-1] > Lowest(low, 3)
then 1
else 0;
def bear_hl = if day[1] != 78 and day >= 1 and #No bars from prior day allowed
#1 bar only: green, red, green
up[1] == 1 and up == 0 and up[-1] == 1 and
(high - low) > diff and
#Next bar must be within highest/lowest
high[-1] < Highest(high, 2) and low[-1] > Lowest(low, 2)
then 1
else if day[1] != 78 and day >= 1 and
#2 bars: green, red, red, green
up[2] == 1 and up[1] == 0 and up == 0 and up[-1] == 1 and
(Highest(high, 2) - Lowest(low, 2)) > diff and
high[-1] < Highest(high, 2) and low[-1] > Lowest(low, 2)
then 2
else if day[3] != 78 and day >= 3 and
#3 bars: red, red, red
up[2] == 0 and up[1] == 0 and up == 0 and
(Highest(high, 3) - Lowest(low, 3)) > diff and
high[-1] < Highest(high, 3) and low[-1] > Lowest(low, 3)
then 3
else 0;
def hbear_ = if bear
then if bear_hl == 1 then Highest(high, 1) else
if bear_hl == 2 then Highest(high, 2)
else Highest(high, 3)
else hbear_[1];
def lbear_ = if bear
then if bear_hl == 1 then Lowest(low, 1) else
if bear_hl == 2 then Lowest(low, 2)
else Lowest(low, 3)
else lbear_[1];
def hbear = if bear
then if bear_hl == 1 then Highest(high, 1) else
if bear_hl == 2 then Highest(high, 2)
else Highest(high, 3)
else if high < hbear_[1] and low > lbear_[1]
then hbear[1] else Double.NaN;
def lbear = if bear
then if bear_hl == 1 then Lowest(low, 1) else
if bear_hl == 2 then Lowest(low, 2)
else Lowest(low, 3)
else if high < hbear_[1] and low > lbear_[1]
then lbear[1] else Double.NaN;
def hbearbar = if bear
then 1 else 0;
def lbearbar = if bear
then 1 else 0;
def dataCounthbear = CompoundValue(1, if hbearbar[1] == 0 and hbearbar then dataCounthbear[1] + 1 else dataCounthbear[1], 0);
def dataCountlbear = CompoundValue(1, if lbearbar[1] == 0 and lbearbar then dataCountlbear[1] + 1 else dataCountlbear[1], 0);
plot hhbear = if use_count_plot_limit and HighestAll(dataCounthbear) - dataCounthbear <= count - 1 or !use_count_plot_limit then hbear else Double.NaN;
hhbear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hhbear.SetDefaultColor(Color.CYAN);
plot llbear = if use_count_plot_limit and HighestAll(dataCountlbear) - dataCountlbear <= count - 1 or !use_count_plot_limit then lbear else Double.NaN;
llbear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llbear.SetDefaultColor(Color.CYAN);
AddChartBubble(show_bubbles and hbearbar[1] == 0 and hbearbar, hhbear + TickSize() * bubblemover_updown, hhbear, Color.RED);
AddChartBubble(show_bubbles and lbearbar[1] == 0 and lbearbar, llbear - TickSize() * bubblemover_updown, llbear, Color.RED, no);
plot fib1bear = llbear + (hhbear - llbear) * .382;
plot fib2bear = llbear + (hhbear - llbear) * .500;
plot fib3bear = llbear + (hhbear - llbear) * .618;
fib1bear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib2bear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib3bear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib1bear.SetDefaultColor(Color.WHITE);
fib2bear.SetDefaultColor(Color.YELLOW);
fib3bear.SetDefaultColor(Color.RED);