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

Bill Williams Alligator for Think or Swim

Coming from the forex world, Metatrader is well-equipped to handle trading based on Bill Williams understanding of chaos theory and fractal analysis, however Think or Swim is not. I have scoured the web for suitable indicators, but in the end, I had to write my own in thinkscript.

What Think or Swim Has:
Accelerator Oscillator - for the purists who enjoy Bill's older books
Awesome Oscillator - the 5,34 macd where you can count the elliot wave, etc

What TOS lacks:
The Alligator on the chart
Fractal Indicator on the chart


The Alligator

####
# Bill Williams Alligator for Think or Swim
# Mike Lapping 2010

# Extra steps were taken because I cannot find proper
# documentation of the average() function
# can be used and modified by anyone for any reason. do not sell.
#
####

## we will now work on the SSMA called jaws

def JOffset = 8;
def Javg13 = ((high[12 + JOffset] + low[12 + JOffset]) / 2);
def Javg12 = ((high[11 + JOffset] + low[11 + JOffset]) / 2);
def Javg11 = ((high[10 + JOffset] + low[10 + JOffset]) / 2);
def Javg10 = ((high[9 + JOffset] + low[9 + JOffset]) / 2);
def Javg9 = ((high[8 + JOffset] + low[8 + JOffset]) / 2);
def Javg8 = ((high[7 + JOffset] + low[7 + JOffset]) / 2);
def Javg7 = ((high[6 + JOffset] + low[6 + JOffset]) / 2);
def Javg6 = ((high[5 + JOffset] + low[5 + JOffset]) / 2);
def Javg5 = ((high[4 + JOffset] + low[4 + JOffset]) / 2);
def Javg4 = ((high[3 + JOffset] + low[3 + JOffset]) / 2);
def Javg3 = ((high[2 + JOffset] + low[2 + JOffset]) / 2);
def Javg2 = ((high[1 + JOffset] + low[1 + JOffset]) / 2);
def Javg1 = ((high[0 + JOffset] + low[0 + JOffset]) / 2);

def SSMAJaws = ( Javg1 + Javg2 + Javg3 + Javg4 + Javg5 + Javg6 + Javg7 + Javg8 + Javg9 + Javg10 + Javg11 + Javg12 + Javg13) / 13;

Plot Jaws = SSMAJaws;

######### Now working on Lips
def LOffset = 5;
def Lavg8 = ((high[7 + LOffset] + low[7 + LOffset]) / 2);
def Lavg7 = ((high[6 + LOffset] + low[6 + LOffset]) / 2);
def Lavg6 = ((high[5 + LOffset] + low[5 + LOffset]) / 2);
def Lavg5 = ((high[4 + LOffset] + low[4 + LOffset]) / 2);
def Lavg4 = ((high[3 + LOffset] + low[3 + LOffset]) / 2);
def Lavg3 = ((high[2 + LOffset] + low[2 + LOffset]) / 2);
def Lavg2 = ((high[1 + LOffset] + low[1 + LOffset]) / 2);
def Lavg1 = ((high[0 + LOffset] + low[0 + LOffset]) / 2);

def SSMALips = (Lavg1 + Lavg2 + Lavg3 + Lavg4 + Lavg5 + Lavg6 + Lavg7 + Lavg8) / 8;

Plot Lips = SSMALips;

######## Work on teeth
def TOffset = 3;

def Tavg5 = ((high[4 + TOffset] + low[4 + TOffset]) / 2);
def Tavg4 = ((high[3 + TOffset] + low[3 + TOffset]) / 2);
def Tavg3 = ((high[2 + TOffset] + low[2 + TOffset]) / 2);
def Tavg2 = ((high[1 + TOffset] + low[1 + TOffset]) / 2);
def Tavg1 = ((high[0 + TOffset] + low[0 + TOffset]) / 2);

def SSMATeeth = (Tavg1 + Tavg2 + Tavg3 + Tavg4 + Tavg5) / 5;

Plot Teeth = SSMATeeth;

## Set Colors for the balance lines

Jaws.SetDefaultColor(Color.BLUE);
Lips.SetDefaultColor(Color.UPTICK);
Teeth.SetDefaultColor(Color.DOWNTICK);

Remember to time shift the chart at least 9 bars so that you can have the full effect of the alligator. This is done by:

Right click on chart > Style > Settings.

In the new window, go to the "Scale and Axis" tab, half way down, change the value for "Expansion" to 8 or more. The Jaws are projected 8 bars into the future.

Hope everyone benefits from this!

-Mike