Bill Williams Fractal Indicator for ThinkorSwim

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);
 
Last edited by a moderator:
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);

Line code added to the bottom of your code

Screenshot 2023-10-19 120254.png
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);
 
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.

Original 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:
Ruby:
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
 
Last edited by a moderator:
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, 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​

in the addlabel() , the word brilliant looks odd
addlabel(yes, brilliant

i don't think it belongs , but will test something first,

add a comma after it,.. nope , still error.

i add a def at top of code to see if it complains of a duplicate define,
def brilliant = 0;
nope. no error , was not defined , so it doesn't belong. i delete that def.

i delete the word brilliant
it works
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
522 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

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.

How do I get started?

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.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top