'**************************************************************
'*   
'*	Trailing Extreme Value Stop
'*		by Jeremy Williams
'*	    February 10, 2006
'*
'*  Summary: 
'*
'*      This trailiing stop sets its stop value at the lowest low (or highest high)
'*  over a set number of periods before the current day plus a cushion.
'*
'*  Parameters:
'*
'*       Periods: the number of period of lookback
'*
'*       Cushion: the number of ATR's below the lowest low found
'*
'*		 ATR_Periods: the number of periods used to calculate the ATR
'*
'************************************************************** 


#Stop
#Param "Periods",10
#Param "Cushion",1
#Param "ATR_Periods",20

Dim myValue 	As Single
Dim myStop 		As Single

Cushion = Cushion*ATR(ATR_Periods)        ' Convert the cushion from ATR's to points

If Signal = LongSignal then               ' If long signal, calculate lowest low value
	myValue = LLV(Periods)[1]             ' and check to ensure todays low is above this 
	myValue = MAX(myValue,1)	          ' value.
	myStop = myValue - Cushion
 	If L <= myStop then                   ' If stop level is breached sent exit signal
		Signal = ExitSignal
	End if
	
	Plotprice ("Stop",myStop)             ' Plot the stop in the price pane

Else if Signal = ShortSignal then         ' If short signal, calculate highest high value
	myValue = HHV(Periods)[1]             ' and check to ensure todays high is below this 
	myValue = MIN(myValue,1)			  ' value.
 	myStop = myValue+Cushion
 	If H >= myStop then					  ' If stop level is breached send exit signal
		Signal = ExitSignal
	End if
	
	Plotprice ("Stop",myStop)             ' Plot the stop in the price pane
End if