#
# TD Ameritrade IP Company, Inc. (c) 2015-2021
#
#wizard plots
#wizard text: Inputs: sequence count:
#wizard input: sequenceCount
input sequenceCount = 2;
def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
if GetValue(high, -i1, -maxSideLength) > high or (GetValue(high, -i1, -maxSideLength) == high and count1 == 0) then -1
else if GetValue(high, -i1, -maxSideLength) < high then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
if GetValue(high, i2, maxSideLength) > high or (GetValue(high, i2, maxSideLength) == high and count2 >= 1) then -1
else if GetValue(high, i2, maxSideLength) < high then count2 + 1 else count2;
def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
if GetValue(low, -i3, -maxSideLength) < low or (GetValue(low, -i3, -maxSideLength) == low and count3 == 0) then -1
else if GetValue(high, -i3, -maxSideLength) > low then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
if GetValue(low, i4, maxSideLength) < low or (GetValue(low, i4, maxSideLength) == low and count4 >= 1) then -1
else if GetValue(low, i4, maxSideLength) > low then count4 + 1 else count4;
#---------------------------------------
#Conditions
def UpFractal_ = if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else Double.NaN;
def DownFractal_ = if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else Double.NaN;
#------------------------------
#Plot limiting Code for chart bubbles and arrows
input Count = 2;
def cond = UpFractal_;
def dataCount = CompoundValue(1, if !IsNaN(cond) then dataCount[1] + 1 else dataCount[1], 0);
def data = if HighestAll(dataCount) - dataCount <= Count - 1 then UpFractal_ else Double.NaN;
def cond1 = DownFractal_;
def dataCount1 = CompoundValue(1, if !IsNaN(cond1) then dataCount1[1] + 1 else dataCount1[1], 0);
def data1 = if HighestAll(dataCount1) - dataCount1 <= Count - 1 then DownFractal_ else Double.NaN;
#--------------------------------
#Bubbles
input showbubbles = yes;
input movebubblesupdown = 8;
AddChartBubble(showbubbles and data, data + movebubblesupdown * TickSize(), astext(data));
AddChartBubble(showbubbles and data1, data1 - movebubblesupdown * TickSize(), astext(data1), Color.LIGHT_GREEN, No);
#-------------------------------------
#Arrows
input showarrows = yes;
plot upfractal = if showarrows then data else double.nan;
upfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
upfractal.SetDefaultColor(GetColor(3));
upfractal.SetLineWeight(2);
plot downfractal = if showarrows then data1 else double.nan;
downfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
downfractal.SetDefaultColor(GetColor(4));
downfractal.SetLineWeight(2);
#-----------------------------
#Alerts
Alert(upfractal[sequenceCount], "DN", Alert.BAR, Sound.Chimes);
Alert(downfractal[sequenceCount], "UP", Alert.BAR, Sound.Chimes);