# Fractals Indicator Of Mike Lapping for ThinkorSwim

#### ykd2018x

##### Member
2019 Donor
here is the code for fractals , i hope everybody know what fractals are and importance of fractals in trading

this version of study i use and like the most , i found it somewhere online i don't know now , sharing here as i have mentioned them in another
RSI technique of trading which i use so far and using till the date for my personal trading

if you are trading with RSI TECHNIQUE & USING REVERSAL INDICATOR , AND YOU PLACE THE STOP LOSS AS SUGGESTED IN THE PIC , 99.99% PRICES ARE NEVER GONNA HIT YOUR STOP LOSS , BUT u must take a good care of your exit with a profit , time frame you decide , if the stop hit in trending market it is itself a reverse the position indicator where u can go double opposite side

yogesh

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.

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);
# if the last bar high is the same as this bar's high, increment
# otherwise set false(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) ;
# set hivalid to true(1)
# if we are entering an equality series, check if the 2 bars preceding the first equal bar # are lower than the current bar
# or if the last bar was an equal bar and this bar is an equal bar and the current equal
# series is valid
# or if we skipped over a lower bar but two bars ago we had an equal bar and that was a
# valid fractal
# otherwise it is false

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);
# Ok this is complicated, basically its checking if there were a series of equal bars and
# if the two bars before the first equal bar were lower
# or if the last 2 bars were lower than the current bar
# and if the two following 2 bars are lower than the current bar

# 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.POINTS);

plot downfractal = if( isdownfractal, isdownfractal - (1 * tickSize()), double.nan);
downfractal.SetPaintingStrategy(paintingStrategy.POINTS);

# This business with the tickSize() function is basically putting the arrow further away fro
# from the bar so that the fractal is easier to read``````

Last edited by a moderator:

#### skynetgen

##### Well-known member
Mind clarifying please which reversal indicator you are using

#### ykd2018x

##### Member
2019 Donor
Mind clarifying please which reversal indicator you are using
the earliest normal version as been posted here many versions i cann't clearly explain too

#### skynetgen

##### Well-known member
Well studying your system. Its promising imho. There are couple of things which make it harder to generate timely reliable signals: Reversal indicator is repainting and Fractal signal is lagging by 2 bars

#### ykd2018x

##### Member
2019 Donor
Well studying your system. Its promising imho. There are couple of things which make it harder to generate timely reliable signals: Reversal indicator is repainting and Fractal signal is lagging by 2 bars
in longer time frame its okay

also repaiting trend reversal
i mean its stop if hits is itself a reversal indicator

i.e. if the overall trend is up with point high ema and u see a red reversal u can do 2 things avoid it always if doesnt match with RSI or take it always but go long if stop is hit

nothing more can be done abt it by nobody i believe

#### Gildes

##### New member
VIP
Hi, I am trying to get an alert on my watchlist when a fractal has formed using this script, I know I might get a delay on the alert.
all ideas are welcome.

Thank you

Code:
``````# 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);``````

Last edited by a moderator:

#### Nick

##### Active member
2019 Donor
VIP
Thanks Yogesh

I am wondering, can anyone out there write me a scan, to tell me which stock has closed above or below its most recent fractal, based on the code kindly provided by Yogesh?

Thanks again.

#### diazlaz

##### Well-known member
2019 Donor
VIP
Hi @Nick

BW is built into the ToS Platform - no need for a custom indicator or scan.

Patterns, See below:

#### ykd2018x

##### Member
2019 Donor
I don't think there should be any such system or way to make such scan

so far fractals are good for below things on charts
1. creating actual and good trendlines those works
2. to see the market trend and movement
3. to understand where the buyers and sellers can be meeting together
4. to see the nature going on , on the charts and how swing highs and swing lows are so easily visible in up and down trend ,

actually they make the picture more clear , I cannot imagine a chart without fractals now and whenever in intraday there is a move without fractals either up or down side , I try to stay away of it.