Bill Williams Fractal Indicator from TOS

I see there have been several conversions done from TOS thinkscripts, was wondering if someone could help with converting this excellent fractal indicator based on Bill Williams work. Here is the TOS thinkscript. Thank you in advance!

# Bill Williams Fractal Template
# Coded By: Rigel May 2018

#Define “n” as the number of periods and keep a minimum value of 2 for error handling.
input n=2;

# Williams Fractals are a 5 point lagging indicator that will draw 2 candles behind.
# The purpose of the indicator is to plot points of trend reversals.
# Often these are paired with trailing stop indicators such as Parabolic SAR, Volatility Stop, and SuperTrend.

# Down pointing fractals occur over candles when:
# High(n-2) < High(n)
# High(n-1) < High(n)
# High(n + 1) < High(n)
# High(n + 2) < High(n)
#dnFractal = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n])

def isupfractal = if low < low[1] and low < low[2] and low < low[-1] and low < low[-2] then low else double.nan;
# Up pointing fractals occur under candles when:
# Low(n-2) > Low(n)
# Low(n-1) > Low(n)
# Low(n + 1) > Low(n)
# Low(n + 2) > Low(n)
#upFractal = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])
def isdownfractal = if high > high[1] and high > high[2] and high > high[-1] and high > high[-2] then high else double.nan;
# Plot the fractals as shapes on the chart.

plot upfractal = if( isupfractal, isupfractal+ (1 * tickSize()) , double.nan);
upfractal.SetPaintingStrategy(paintingStrategy.ARROW_UP);
plot downfractal = if( isdownfractal, isdownfractal - (1 * tickSize()), double.nan);
downfractal.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);plot Data = close;

Aloha Dennis, as far as I know we can not add an indicator to bars in the past, which would be required to get this indicator working. @Victor_Vins am I correct in this assumption and do you have a sense for if this might be possible at some point?

1 Like

Any further word on building this indicator? It is an excellent tool to monitor price action.

1 Like

Great scalping tool as well. Many will use stops just above or below a fractal, so when a price breaks a fractal you will have a quick burst of buying. Good to jump on these momentum trades

I took a stab at it. I have never used the indicator so there could bugs or edge cases I haven’t considered.

@latter-day-trader

Could you add the ability to control the color of the fractal “up” and “Down” arrows individually instead of all the same color for a little more visual control?

Thanks

@MadCat

Done. You may need to remove and re-add the indicator to see the new color options.

@latter-day-trader

Thanks, that works well.

I need help porting this indicator from tos to tradovate. Do you think you can assist?

#Plot yesterday’s high / low
input Showchartbubbles = yes;
input ShowTodayOnly = yes;
def Today = if GetDay() == GetLastDay() then 1 else 0;
plot Yhigh = if ShowTodayOnly and !Today then Double.NaN else high(period = “day” )[1];
plot Ylow = if ShowTodayOnly and !Today then Double.NaN else low(period = “day” )[1];

Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.UPTICK);
Yhigh.SetLineWeight(2);
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.DOWNTICK);
Ylow.SetLineWeight(2);

declare once_per_bar;

input PlotPreMktLinesHrsPastOpen = yes;
def bar = BarNumber();

def nan = Double.NaN;
def vHigh = high;
def vLow = low;

def PMhrs = RegularTradingStart (GetYYYYMMDD()) > GetTime();
def RMhrs = RegularTradingStart (GetYYYYMMDD()) < GetTime();
def PMStart = RMhrs[1] and PMhrs;
def PMHigh = CompoundValue(1, if PMStart then vHigh else if PMhrs then Max(vHigh, PMHigh[1]) else PMHigh[1], 0);
def PMLow = CompoundValue(1, if PMStart then vLow else if PMhrs then Min(vLow, PMLow[1]) else PMLow[1], 0);
def highBar = if PMhrs and vHigh == PMHigh then bar else nan;
def lowBar = if PMhrs and vLow == PMLow then bar else nan;
def PMHighBar = if bar == HighestAll(highBar) then PMHigh else PMHighBar[1];
def PMLowBar = if bar == HighestAll(lowBar) then PMLow else PMLowBar[1];

plot PMH = if PlotPreMktLinesHrsPastOpen and PMHighBar != 0
then PMHighBar
else nan;
plot PML = if PlotPreMktLinesHrsPastOpen and PMLowBar != 0
then PMLowBar
else nan;
plot PMMid = if PlotPreMktLinesHrsPastOpen and PMHighBar != 0 and PMLowBar != 0
then (PMHighBar + PMLowBar) / 2
else nan;

AddChartBubble(ShowChartBubbles and bar == HighestAll(highBar),
PMHigh,
“PM High”,
Color.Gray,
1);

AddChartBubble(ShowChartBubbles and bar == HighestAll(lowBar),
PMLow,
“PM Low”,
Color.Gray,
0);

AddChartBubble(ShowChartBubbles and bar == Max(HighestAll(highBar), HighestAll(lowBar)),
(PMHighBar + PMLowBar) / 2,
“PM Mid”,
Color.Gray,
1);

addcloud(pmh, yhigh,Color.light_gray, Color.Light_gray);
AddCloud(pml, ylow, Color.light_gray, Color.Light_gray);

I can’t help. Sorry. You can’t pay me enough to look at Thinkscript. :joy: Good luck.

1 Like