# Example Using Addchart to draw a 'Box' around each candle meeting a condition. The 'box' can be extended by the number of ticks entered at the input, tickoffset
# Sleepyz
input tickoffset = 5;
input charttype = ChartType.CANDLE;
def o = open;
def h = high;
def l = low;
def c = close;
def nan = Double.NaN;
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def PatternPlot =
if ((Sum(IsUp, 1)[3] >= 0)) and
((Sum(IsUp, 1)[2] >= 0)) and
((Sum(IsUp, 1)[1] >= 0)) and
((Sum(IsUp, 1)[0] >= 0)) and
Highest(high[3], 1) < Highest(high[2], 1) and
Highest(high[2], 1) < Highest(high[1], 1) and
Highest(high[1], 1) < Highest(high[0], 1) then 1 else 0;
def cancelPatternPlot = if PatternPlot[1] == 1 then 1 else 0;
def condition = PatternPlot == 1 and cancelPatternPlot == 0;
def o1 = if condition then h + TickSize() * tickoffset else nan;
def c1 = if condition then l - TickSize() * tickoffset else nan;
def h1 = if condition then h + TickSize() * tickoffset else nan;
def l1 = if condition then l - TickSize() * tickoffset else nan;
DefineGlobalColor(color = Color.YELLOW, name = "BoxH");
input usepricecolor = yes;
AssignPriceColor(if usepricecolor and condition then GlobalColor("BoxH") else Color.CURRENT);
AddChart(growcolor = GlobalColor("BoxH"), high = h1, low = l1, open = c1, close = o1, type = charttype);
def PatternPlot1 =
if ((Sum(IsDown, 1)[3] >= 0)) and
((Sum(IsDown, 1)[2] >= 0)) and
((Sum(IsDown, 1)[1] >= 0)) and
((Sum(IsDown, 1)[0] >= 0)) and
Lowest(low[3], 1) > Lowest(low[2], 1) and
Lowest(low[2], 1) > Lowest(low[1], 1) and
Lowest(low[1], 1) > Lowest(low[0], 1) then 1 else 0;
def cancelPatternPlot1 = if PatternPlot1[1] == 1 then 1 else 0;
def condition1 = PatternPlot1 == 1 and cancelPatternPlot1 == 0;
def o2 = if condition1 then h + TickSize() * tickoffset else nan;
def c2 = if condition1 then l - TickSize() * tickoffset else nan;
def h2 = if condition1 then h + TickSize() * tickoffset else nan;
def l2 = if condition1 then l - TickSize() * tickoffset else nan;
DefineGlobalColor(color = Color.MAGENTA, name = "BoxL");
AssignPriceColor(if usepricecolor and condition1 then GlobalColor("BoxL") else Color.CURRENT);
AddChart(growcolor = GlobalColor("BoxL"), high = h2, low = l2, open = c2, close = o2, type = charttype);
#Bubbles
input showbubble = yes;
input limitbubbles = yes;
input show_x_bubbles = 3;
def Count = CompoundValue(1, if condition
then Count[1] + 1
else Count[1], 0);
def this = (HighestAll(Count) - Count) + 1;
AddChartBubble(showbubble and
if limitbubbles
then this <= show_x_bubbles and
condition
else condition,
h1,
"H: " + high + "\nO: " + open + "\nC: " + close + "\nL: " + low,
if close > open
then Color.GREEN
else Color.RED);
def Count1 = CompoundValue(1, if condition1
then Count1[1] + 1
else Count1[1], 0);
def this1 = (HighestAll(Count1) - Count1) + 1;
AddChartBubble(showbubble and
if limitbubbles
then this1 <= show_x_bubbles and
condition1
else condition1,
l2,
"H: " + high + "\nO: " + open + "\nC: " + close + "\nL: " + low,
if close > open
then Color.GREEN
else Color.RED, no);
#