#Indicator
'**************************************************************
'*   Automatic Trendlines (AutomaticTrendlines.txt)
'*     by Jeremy Williams
'*	   September 12,2006
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*     November 2006
'*
'*  Summary: 
'*
'*      This indicator plots Automatic Trendlines based on price  
'*  movements. For more information see "Building Automatic Trendlines"
'*  in the November 2006 edition of Technical Analysis of Stocks 
'*  and Commodities.
'*
'*  Parameters:
'*
'*  Adjustment_Type    = Determines which methodology to use when
'*                       calculating the Pivot Points. 
'*                   	    0: ATR's (Wilder's Volatility)
'*					 	    1: Percent
'*
'*	MagnitudeInATRs    = Specifies the number of ATR's price must move 
'*                       off a recent high or low in order for that 
'* 					     point to be classified as a pivot
'*
'*  MagnitudeInPercent = Specifies the percentage price must move off
'*                       a recent high or low in order for that point
'*                       to be classified as a pivot
'*
'*  ATRPeriods         = Specifies the number of ATR's used for the pivot
'*                       points calculation 
'*
'************************************************************** 


#Param "MeasurementType",0,0,1
#Param "MagnitudeInATRs",1,0,5
#Param "MagnitudeInPercent",10,0,100
#Param "ATRPeriods",14,2,100

' Mock array of Pivot Points
Dim HighPivot1			As Single
Dim HighPivot2			As Single
Dim LowPivot1			As Single
Dim LowPivot2			As Single
Dim RunningHigh			As Single
Dim RunningLow			As Single

' Mock array of Pivot Positions
Dim HighPivot1Bar		As Integer
Dim HighPivot2Bar		As Integer
Dim LowPivot1Bar		As Integer
Dim LowPivot2Bar		As Integer
Dim RunningHighBar		As Integer
Dim RunningLowBar		As Integer

' Control Variables for the Pivot System
Dim PivotThreshold		As Single
Dim CurrentPivotType	As Integer
Dim PivotCount			As Integer
Dim FirstPivotFound		As Boolean

'Variables for Plotting of Pivots and Trendlines
Dim x1					As Integer
Dim x2					As Integer
Dim y1					As Single
Dim y2					As Single
Dim Slope				As Single

'Determine Threshold for Classification as a Pivot
If MeasurementType = 0 Then
	PivotThreshold = MagnitudeInATRs*ATR(ATRPeriods)
Else
	PivotThreshold = MagnitudeInPercent*Close/100
End If

'Initialize the Pivot System for current bar
If Bar = 0 Then
	FirstPivotFound = False
	PivotCount = 0
	RunningHigh = Close
	RunningLow = Close
	RunningHighBar = 0
	RunningLowBar = 0
	CurrentPivotType = 0
	HighPivot1 = 0
	HighPivot2 = 0
	LowPivot1 = 0
	LowPivot2 = 0
	HighPivot1Bar = 0
	HighPivot2Bar = 0
	LowPivot1Bar = 0
	LowPivot2Bar = 0
Else 
	FirstPivotFound = FirstPivotFound[1]
	PivotCount = PivotCount[1]
	RunningHigh = RunningHigh[1]
	RunningLow = RunningLow[1]
	RunningHighBar = RunningHighBar[1]+ 1
	RunningLowBar = RunningLowBar[1] + 1
	CurrentPivotType =CurrentPivotType[1]
	If Close > RunningHigh Then
		RunningHigh = Close
		RunningHighBar = 0
	Else If Close < RunningLow Then
		RunningLow = Close
		RunningLowBar = 0
	End If
	HighPivot1 = HighPivot1[1]
	HighPivot2 = HighPivot2[1]
	LowPivot1 = LowPivot1[1]
	LowPivot2 = LowPivot2[1]
	HighPivot1Bar = HighPivot1Bar[1] + 1
	HighPivot2Bar = HighPivot2Bar[1] + 1
	LowPivot1Bar = LowPivot1Bar[1] + 1
	LowPivot2Bar = LowPivot2Bar[1] + 1
End If

'Calculate the Pivot Point Arrays
If Not FirstPivotFound Then
	If RunningHigh - RunningLow > PivotThreshold And RunningHighBar > RunningLowBar Then
		FirstPivotFound = True
		HighPivot1 = RunningHigh
		HighPivot1Bar = RunningHighBar
		CurrentPivotType = -1
		RunningLow = Close
		RunningLowBar = 0
	Else If RunningHigh - RunningLow > PivotThreshold And RunningLowBar > RunningHighBar Then
		FirstPivotFound = True
		LowPivot1 = RunningLow
		LowPivot1Bar = RunningLowBar
		CurrentPivotType = 1
		RunningHigh = Close
		RunningHighBar = 0
	End If
Else If CurrentPivotType = 1 And RunningHigh - Close > PivotThreshold Then
	HighPivot2 = HighPivot1
	HighPivot2Bar = HighPivot1Bar
	HighPivot1 = RunningHigh
	HighPivot1Bar = RunningHighBar
	PivotCount = PivotCount + 1
	RunningLow = Close
	RunningLowBar = 0
	CurrentPivotType = -1
	
Else If CurrentPivotType = -1 And Close - RunningLow > PivotThreshold Then
	LowPivot2 = LowPivot1
	LowPivot2Bar = LowPivot1Bar
	LowPivot1 = RunningLow
	LowPivot1Bar = RunningLowBar
	PivotCount = PivotCount + 1
	RunningHigh = Close
	RunningHighBar = 0
	CurrentPivotType = 1
	
End If
		
' Calculate Trendlines
If PivotCount >= 4 Then
	If Close > HighPivot1 And LowPivot1 > LowPivot2  Then 'And Close[1] < HighPivot1
		
		X1 = Bar-LowPivot2Bar
		X2 = Bar-LowPivot1Bar
		Y1 = LowPivot2
		Y2 = LowPivot1
		Slope = (Y2 - Y1)/(X2 - X1)
		Y2=Slope*(Bar-X1)+Y1
		PlotPriceTrendLine("Low Pivot" ,X1,Y1,Bar,Y2)
	Else If Close < LowPivot1 And HighPivot1 < HighPivot2  Then 'And Close[1] > LowPivot1
		X1 = Bar-HighPivot2Bar
		X2 = Bar-HighPivot1Bar
		Y1 = HighPivot2
		Y2 = HighPivot1
		Slope = (Y2 - Y1)/(X2 - X1)
		Y2=Slope*(Bar-X1)+Y1
		PlotPriceTrendLine("High Pivot" ,X1,Y1,Bar,Y2)
	End If
End If
	PlotHist("Slope",Slope,0)

Return slope     ' Return the value calculated by the indicator