Just change the sequence line in the code to 3 or what ever number you want.How can i chance this to a 3 bar count
I use the Williams Fractal
https://usethinkscript.com/threads/bill-williams-fractal-indicator-for-thinkorswim.600/
to make entries when the price crosses above the upfractal or below the downfractal. I need a way to create a horizontal line from the plotted indicator upfractal that stays visible until the next upfractal is plotted. Same for the downside. This is the code I'm using to start with:
# Bill Williams fractal indicator
# written by Mike Lapping
#
# Make sure that you change the settings for this indicator so that it plots arrows
# up for upfractal plots
# down for downfractal plots
#
# can be used and modified by anyone for any reason. do not sell.
declare upper;
def isupfractal;
def isdownfractal;
# Looking for high and low series of equalities
# checking for possible fractal formation
rec hicount = If (high == high[1], hicount[1] + 1, 0);
rec hivalid = If (
(hicount[1] == 0 and hicount == 1 and high > high[2] and high > high[3]) or
(hicount[1] and hicount and hivalid[1]) or
(hicount[2] and hivalid[2] and high == high[2] and high > high[1]), 1, 0);
rec locount = If (low == low[1], locount[1] + 1, 0);
rec lovalid = If (
(locount[1] == 0 and locount == 1 and low < low[2] and low < low[3]) or
(locount[1] and locount and lovalid[1]) or
(locount[2] and lovalid[2] and low == low[2] and low < low[1]), 1, 0);
# Checking for a traditional or non-standard up fractal
isupfractal = If(
((hicount and hivalid) or (high > high[1] and high > high[2])) and
high > high[-1] and high > high[-2], high, 0);
# Checking for a traditional or non-standard down fractal
isdownfractal = If(
((locount and lovalid) or (low < low[1] and low < low[2])) and
low < low[-1] and low < low[-2], low, 0);
plot upfractal = If(isupfractal, isupfractal + (1 * TickSize()), Double.NaN);
upfractal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
upfractal.SetLineWeight(5);
plot downfractal = If(isdownfractal, isdownfractal - (1 * TickSize()), Double.NaN);
downfractal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
downfractal.SetLineWeight(5);
Code:# Bill Williams fractal indicator # written by Mike Lapping # # Make sure that you change the settings for this indicator so that it plots arrows # up for upfractal plots # down for downfractal plots # # can be used and modified by anyone for any reason. do not sell. declare upper; def isupfractal; def isdownfractal; # Looking for high and low series of equalities # checking for possible fractal formation rec hicount = If (high == high[1], hicount[1] + 1, 0); rec hivalid = If ( (hicount[1] == 0 and hicount == 1 and high > high[2] and high > high[3]) or (hicount[1] and hicount and hivalid[1]) or (hicount[2] and hivalid[2] and high == high[2] and high > high[1]), 1, 0); rec locount = If (low == low[1], locount[1] + 1, 0); rec lovalid = If ( (locount[1] == 0 and locount == 1 and low < low[2] and low < low[3]) or (locount[1] and locount and lovalid[1]) or (locount[2] and lovalid[2] and low == low[2] and low < low[1]), 1, 0); # Checking for a traditional or non-standard up fractal isupfractal = If( ((hicount and hivalid) or (high > high[1] and high > high[2])) and high > high[-1] and high > high[-2], high, 0); # Checking for a traditional or non-standard down fractal isdownfractal = If( ((locount and lovalid) or (low < low[1] and low < low[2])) and low < low[-1] and low < low[-2], low, 0); plot upfractal = If(isupfractal, isupfractal + (1 * TickSize()), Double.NaN); upfractal.SetPaintingStrategy(PaintingStrategy.TRIANGLES); upfractal.SetLineWeight(5); plot downfractal = If(isdownfractal, isdownfractal - (1 * TickSize()), Double.NaN); downfractal.SetPaintingStrategy(PaintingStrategy.TRIANGLES); downfractal.SetLineWeight(5); def up = if isupfractal then high else up[1]; plot upline = up; upline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL); def dn = if isdownfractal then low else dn[1]; plot dnline = dn; dnline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
# RSI with Williams Fractal Pivots
########START Code
# Chat Room 04.20.2018
declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input New_HI_LO = 30;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
plot NEWHIGH = if RSI == Highest(RSI, New_HI_LO) then RSI else Double.NaN;
NEWHIGH.SetPaintingStrategy(PaintingStrategy.POINTS);
NEWHIGH.SetDefaultColor(Color.LIGHT_GREEN);
NEWHIGH.SetLineWeight(1);
plot NEWLOW = if RSI == Lowest(RSI, New_HI_LO) then RSI else Double.NaN;
NEWLOW.SetPaintingStrategy(PaintingStrategy.POINTS);
NEWLOW.SetDefaultColor(Color.LIGHT_RED);
NEWLOW.SetLineWeight(1);
# Fractals
# 1/5/17 Amalia added Aggregation periods?
def H = RSI;
def L = RSI;
input sequenceCount = 2;
def maxSideLength = sequenceCount + 1;
def upRightSide = fold i1 = 1 to maxSideLength
with count1
while count1 != sequenceCount and
count1 != -1
do if GetValue(H, -i1) > H or
(GetValue(H, -i1) == H and
count1 == 0)
then -1
else if GetValue(H, -i1) < H
then count1 + 1
else count1;
def upLeftSide = fold i2 = 1 to maxSideLength
with count2
while count2 != sequenceCount and
count2 != -1
do if GetValue(H, i2) > H or
(GetValue(H, i2) == H and
count2 >= 1)
then -1
else if GetValue(H, i2) < H
then count2 + 1
else count2;
def downRightSide = fold i3 = 1 to maxSideLength
with count3
while count3 != sequenceCount and
count3 != -1
do if GetValue(L, -i3) < L or
(GetValue(L, -i3) == L and
count3 == 0)
then -1
else if GetValue(H, -i3) > L
then count3 + 1
else count3;
def downLeftSide = fold i4 = 1 to maxSideLength
with count4
while count4 != sequenceCount and
count4 != -1
do if GetValue(L, i4) < L or
(GetValue(L, i4) == L and
count4 >= 1)
then -1
else if GetValue(L, i4) > L
then count4 + 1
else count4;
plot UpFractal = if upRightSide == sequenceCount and
upLeftSide == sequenceCount and
RSI > OverBought
then RSI
else Double.NaN;
plot DownFractal = if downRightSide == sequenceCount and
downLeftSide == sequenceCount and
RSI < OverSold
then RSI
else Double.NaN;
UpFractal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
UpFractal.SetDefaultColor(Color.RED);
UpFractal.SetLineWeight(2);
DownFractal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownFractal.SetDefaultColor(Color.GREEN);
DownFractal.SetLineWeight(2);
def Pre_hi = if !IsNaN(UpFractal) then UpFractal else Pre_hi[1];
plot high_line = Pre_hi;
high_line.SetPaintingStrategy(PaintingStrategy.DASHES);
high_line.SetDefaultColor(Color.CYAN);
def Pre_lo = if !IsNaN(DownFractal) then DownFractal else Pre_lo[1];
plot lo_line = Pre_lo;
lo_line.SetPaintingStrategy(PaintingStrategy.DASHES);
lo_line.SetDefaultColor(Color.MAGENTA);
addlabel(yes, brilliant
if RSI >= OverBought && RSI >= RSI[1]
then "RSI OB & Rising: " +round(RSI, 2)
else if RSI >= OverBought && RSI < RSI[1]
then "RSI OB & Falling: " +round(RSI, 2)
else if RSI <= OverSold && RSI < RSI[1]
then "RSI OS & Falling: " +round(RSI, 2)
else if RSI <= OverSold && RSI >= RSI[1]
then "RSI OS & Rising: " +round(RSI, 2)
else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]
then "RSI Rising: " +round(RSI, 2)
else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]
then "RSI Falling: " +round(RSI, 2)
else "",
if RSI >= OverBought && RSI >= RSI[1]
then color.dark_orange
else if RSI >= OverBought && RSI < RSI[1]
then color.yellow
else if RSI <= OverSold && RSI < RSI[1]
then createcolor(000, 100, 100)
else if RSI <= OverSold && RSI >= RSI[1]
then color.cyan
else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]
then color.green
else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]
then color.red
else color.gray);
plot Buffer = if RSI <= OverSold then RSI - 10 else if RSI >= OverBought then RSI + 10 else Double.NaN;
Buffer.SetDefaultColor(Color.BLACK);
Buffer.HideTitle();
Buffer.HideBubble();
####END Code
This indicator
https://usethinkscript.com/threads/bill-williams-fractal-indicator-for-thinkorswim.600/#post-21876
was posted five years ago and the OP is long gone. I'm getting an invalid statement for the section in bold below. I'm sure it's a simple fix.. I tried to figure it out, but I'm not a coder. If someone could fix it, I'll go back to the original post and add it for the next person.
orignal code:
Ruby:# RSI with Williams Fractal Pivots ########START Code # Chat Room 04.20.2018 declare lower; input length = 14; input over_Bought = 70; input over_Sold = 30; input price = close; input averageType = AverageType.WILDERS; input New_HI_LO = 30; def NetChgAvg = MovingAverage(averageType, price - price[1], length); def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length); def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0; plot RSI = 50 * (ChgRatio + 1); plot OverSold = over_Sold; plot OverBought = over_Bought; RSI.DefineColor("OverBought", GetColor(5)); RSI.DefineColor("Normal", GetColor(7)); RSI.DefineColor("OverSold", GetColor(1)); RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal")); OverSold.SetDefaultColor(GetColor(8)); OverBought.SetDefaultColor(GetColor(8)); plot NEWHIGH = if RSI == Highest(RSI, New_HI_LO) then RSI else Double.NaN; NEWHIGH.SetPaintingStrategy(PaintingStrategy.POINTS); NEWHIGH.SetDefaultColor(Color.LIGHT_GREEN); NEWHIGH.SetLineWeight(1); plot NEWLOW = if RSI == Lowest(RSI, New_HI_LO) then RSI else Double.NaN; NEWLOW.SetPaintingStrategy(PaintingStrategy.POINTS); NEWLOW.SetDefaultColor(Color.LIGHT_RED); NEWLOW.SetLineWeight(1); # Fractals # 1/5/17 Amalia added Aggregation periods? def H = RSI; def L = RSI; input sequenceCount = 2; def maxSideLength = sequenceCount + 1; def upRightSide = fold i1 = 1 to maxSideLength with count1 while count1 != sequenceCount and count1 != -1 do if GetValue(H, -i1) > H or (GetValue(H, -i1) == H and count1 == 0) then -1 else if GetValue(H, -i1) < H then count1 + 1 else count1; def upLeftSide = fold i2 = 1 to maxSideLength with count2 while count2 != sequenceCount and count2 != -1 do if GetValue(H, i2) > H or (GetValue(H, i2) == H and count2 >= 1) then -1 else if GetValue(H, i2) < H then count2 + 1 else count2; def downRightSide = fold i3 = 1 to maxSideLength with count3 while count3 != sequenceCount and count3 != -1 do if GetValue(L, -i3) < L or (GetValue(L, -i3) == L and count3 == 0) then -1 else if GetValue(H, -i3) > L then count3 + 1 else count3; def downLeftSide = fold i4 = 1 to maxSideLength with count4 while count4 != sequenceCount and count4 != -1 do if GetValue(L, i4) < L or (GetValue(L, i4) == L and count4 >= 1) then -1 else if GetValue(L, i4) > L then count4 + 1 else count4; plot UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount and RSI > OverBought then RSI else Double.NaN; plot DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount and RSI < OverSold then RSI else Double.NaN; UpFractal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); UpFractal.SetDefaultColor(Color.RED); UpFractal.SetLineWeight(2); DownFractal.SetPaintingStrategy(PaintingStrategy.ARROW_UP); DownFractal.SetDefaultColor(Color.GREEN); DownFractal.SetLineWeight(2); def Pre_hi = if !IsNaN(UpFractal) then UpFractal else Pre_hi[1]; plot high_line = Pre_hi; high_line.SetPaintingStrategy(PaintingStrategy.DASHES); high_line.SetDefaultColor(Color.CYAN); def Pre_lo = if !IsNaN(DownFractal) then DownFractal else Pre_lo[1]; plot lo_line = Pre_lo; lo_line.SetPaintingStrategy(PaintingStrategy.DASHES); lo_line.SetDefaultColor(Color.MAGENTA);
Want to add:
addlabel(yes, brilliantif RSI >= OverBought && RSI >= RSI[1]then "RSI OB & Rising: " +round(RSI, 2)else if RSI >= OverBought && RSI < RSI[1]then "RSI OB & Falling: " +round(RSI, 2)else if RSI <= OverSold && RSI < RSI[1]then "RSI OS & Falling: " +round(RSI, 2)else if RSI <= OverSold && RSI >= RSI[1]then "RSI OS & Rising: " +round(RSI, 2)else if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]then "RSI Rising: " +round(RSI, 2)else if RSI < OverBought && RSI > OverSold && RSI < RSI[1]then "RSI Falling: " +round(RSI, 2)else "",if RSI >= OverBought && RSI >= RSI[1]then color.dark_orangeelse if RSI >= OverBought && RSI < RSI[1]then color.yellowelse if RSI <= OverSold && RSI < RSI[1]then createcolor(000, 100, 100)else if RSI <= OverSold && RSI >= RSI[1]then color.cyanelse if RSI < OverBought && RSI > OverSold && RSI >= RSI[1]then color.greenelse if RSI < OverBought && RSI > OverSold && RSI < RSI[1]then color.redelse color.gray);plot Buffer = if RSI <= OverSold then RSI - 10 else if RSI >= OverBought then RSI + 10 else Double.NaN;Buffer.SetDefaultColor(Color.BLACK);Buffer.HideTitle();Buffer.HideBubble();####END Code
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.