Wednesday, March 31, 2010

Bill Williams Fractal Indicator for Think or Swim

Here's a continuation of the Bill Williams indicator set, the Fractal indicator coded in thinkscript.

When setting this up with TOS, you can change the type of object that appears on the chart. I have found some issue using the up and down arrows, so I plot colored dots. I have not set any default objects in this code, because there is no ideal object to plot on the chart.

Fractal Indicator:

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

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

# 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

19 comments:

  1. It does work, just read the script and familiarize yourself with it. In the "edit studies" panel, change the properties of the upfracal and downfractal type to arrows.

    ReplyDelete
  2. Please, help! How should I put this code to TOS for everything work?

    ReplyDelete
  3. # 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

    ReplyDelete
  4. I've been using your script for years but just came across this and it produces much less fractals than your, any ideas why?
    ### define inputs
    input price = close;

    ### define a Buy Fractal setup
    def buy_fractal_setup_is_true = high[2] < high[1];
    def buy_fractal_peak_is_true = high > high[1] && high > high[-1];
    def buy_fractal_confirm_is_true = high[-1] > high[-2];
    plot buy_fractal = if buy_fractal_setup_is_true and buy_fractal_peak_is_true and buy_fractal_confirm_is_true then price else double.NaN;

    ### define a Sell Fractal setup
    def sell_fractal_setup_is_true = low[2] > low[1];
    def sell_fractal_peak_is_true = low < low[1] && low < low[-1];
    def sell_fractal_confirm_is_true = low[-1] < low[-2];
    plot sell_fractal = if sell_fractal_setup_is_true and sell_fractal_peak_is_true and sell_fractal_confirm_is_true then price else double.NaN;

    ### plot
    buy_fractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
    sell_fractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
    buy_fractal.SetDefaultColor(Color.Red);
    sell_fractal.SetDefaultColor(Color.Green);

    ReplyDelete
  5. BTW thank you for your efforts in the TOS community!

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Is it possible to add labels ? i m specifically looking for this..

    if current price > upfractal - then addlabel "UP"
    if current price < downfractal - then addlabel "DOWN"

    ReplyDelete
  8. Is it possible to add labels ? i m specifically looking for this..

    if current price > upfractal - then addlabel "UP"
    if current price < downfractal - then addlabel "DOWN"

    ReplyDelete
  9. Hi there, I know it's been a few years but maybe somebody will read this and be able to help. All I'm trying to do is for the fractal plots to not be 1 tick away from the high or low, rather I want them to plot exactly on the High or Low. Probably super easy but not when you don't code ;) Thank you!!!

    ReplyDelete
  10. Just what I wanted, thanks a lot!

    ReplyDelete
  11. Hello, I was wondering if you could help me write code to make Horizontal lines at the highest and lowest fractal within a specific set of bars?

    ReplyDelete
  12. i wrote the last comment my email is DEHurlock@gmail.com

    ReplyDelete
  13. i wrote the last comment my email is DEHurlock@gmail.com

    ReplyDelete
  14. This is a three bars counts fractal. how can you make it a five bars count fractal?

    ReplyDelete