#WilliamsFractals_Plots_Limited_when_Crossed
script wf {
input n = 0;
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;
def bn = BarNumber();
def UpFractal = if IsNaN(close) then UpFractal[1] else if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else UpFractal[1];
def DownFractal = if IsNaN(close) then downFractal[1] else if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else downfractal[1];
#Use thisday snippet to define Each DownFractal
def ymd1 = DownFractal;
def ok1 = !IsNaN(close);
def capture1 = ok1 and ymd1 != ymd1[1];
def dayCount1 = if capture1 then dayCount1[1] + 1 else dayCount1[1];
def thisDay1 = (HighestAll(dayCount1) - dayCount1) ;
#Use thisday snippet to define Each UpFractal
def ymd = UpFractal;
def ok = !IsNaN(close);
def capture = ok and ymd != ymd[1];
def dayCount = if capture then dayCount[1] + 1 else dayCount[1];
def thisDay = (HighestAll(dayCount) - dayCount) ;
def hh = if thisDay == n and thisDay[1] == n + 1
then high
else double.nan ;
def hhbn = if thisDay == n and thisDay[1] == n + 1
then bn
else double.nan ;
def hhcross = if bn == HighestAll(hhbn)
then bn else
if bn > HighestAll(hhbn) and
!(high[-1] crosses HighestAll(hh))
then hhcross[1] + 1
else 0 ;
plot hhplot = if if hhcross > 0
then Between(bn, HighestAll(hhbn), HighestAll(hhcross))
else bn >= HighestAll(bn)
then HighestAll(hh)
else Double.NaN;
#Use thisday snippet to define Each DownFractal
def ll = if thisDay1 == n and thisDay1[1] == n + 1
then low
else double.nan;
def llbn = if thisDay1 == n and thisDay1[1] == n + 1
then bn
else double.nan;
def llcross = if bn == HighestAll(llbn)
then bn else
if bn > HighestAll(llbn) and
!(low[-1] crosses HighestAll(ll))
then llcross[1] + 1
else 0 ;
plot llplot = if if llcross > 0
then Between(bn, HighestAll(llbn), HighestAll(llcross))
else bn >= HighestAll(bn)
then highestAll(ll)
else Double.NaN;
}
input lineweight = 3;
DefineGlobalColor("U", Color.GREEN);
DefineGlobalColor("D", Color.red);
plot x0 = wf(0);
plot x1 = wf(1);
plot x2 = wf(2);
plot x3 = wf(3);
plot x4 = wf(4);
plot x5 = wf(5);
plot x6 = wf(6);
plot x7 = wf(7);
plot x8 = wf(8);
plot x9 = wf(9);
x0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x0.SetLineWeight(lineweight);
x1.SetLineWeight(lineweight);
x2.SetLineWeight(lineweight);
x3.SetLineWeight(lineweight);
x4.SetLineWeight(lineweight);
x5.SetLineWeight(lineweight);
x6.SetLineWeight(lineweight);
x7.SetLineWeight(lineweight);
x8.SetLineWeight(lineweight);
x9.SetLineWeight(lineweight);
x0.SetDefaultColor(GlobalColor("U"));
x1.SetDefaultColor(GlobalColor("U"));
x2.SetDefaultColor(GlobalColor("U"));
x3.SetDefaultColor(GlobalColor("U"));
x4.SetDefaultColor(GlobalColor("U"));
x5.SetDefaultColor(GlobalColor("U"));
x6.SetDefaultColor(GlobalColor("U"));
x7.SetDefaultColor(GlobalColor("U"));
x8.SetDefaultColor(GlobalColor("U"));
x9.SetDefaultColor(GlobalColor("U"));
plot y0 = wf(0).llplot;
plot y1 = wf(1).llplot;
plot y2 = wf(2).llplot;
plot y3 = wf(3).llplot;
plot y4 = wf(4).llplot;
plot y5 = wf(5).llplot;
plot y6 = wf(6).llplot;
plot y7 = wf(7).llplot;
plot y8 = wf(8).llplot;
plot y9 = wf(9).llplot;
y0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
y0.SetLineWeight(lineweight);
y1.SetLineWeight(lineweight);
y2.SetLineWeight(lineweight);
y3.SetLineWeight(lineweight);
y4.SetLineWeight(lineweight);
y5.SetLineWeight(lineweight);
y6.SetLineWeight(lineweight);
y7.SetLineWeight(lineweight);
y8.SetLineWeight(lineweight);
y9.SetLineWeight(lineweight);
y0.SetDefaultColor(GlobalColor("D"));
y1.SetDefaultColor(GlobalColor("D"));
y2.SetDefaultColor(GlobalColor("D"));
y3.SetDefaultColor(GlobalColor("D"));
y4.SetDefaultColor(GlobalColor("D"));
y5.SetDefaultColor(GlobalColor("D"));
y6.SetDefaultColor(GlobalColor("D"));
y7.SetDefaultColor(GlobalColor("D"));
y8.SetDefaultColor(GlobalColor("D"));
y9.SetDefaultColor(GlobalColor("D"));
#