#Stop
'**************************************************************
'*   Bear Range Trailing Stop (BearRangeTrailingStop.txt)
'*     by Jeremy Williams
'*	   Oct. 11, 2007
'*
'*	Adapted from Technical Analysis of Stocks and Commodities
'*     January 2008
'*
'*  Summary: 
'*
'*      This stop uses the RSI to dynamically adjust the cushion
'*  of a trailing profit stop, allowing more advanced forms of 
'*  trade management. For more information see "Profit Locking and
'*  The Relative Price Channel" in the January 2007 issue of 
'*  Technical Analysis of Stocks and Commodities.
'*
'*  Parameters:
'*
'*  BearishPeriods -  Specifies the number of  bearish periods 
'*
'*  Factor- Multiplicative Factor specifying the stop cushion
'*
'*  ChannelPeriods - Specifies the number of periods for the channel
'* 
'*  Overbought  - Specifies the RSI level corresponding to an
'*                  overbought condition
'*
'*  ChannelSmoothing - Specifies the smoothing for the Overbought Channel
'*
'************************************************************** 

#Param "BearishPeriods",21
#Param "Factor"	,3.8
#Param "ChannelPeriods",34
#Param "Overbought",70
#Param "ChannelSmoothing", 1

Dim myRSI		As Single
Dim OB			As Single
Dim Bullish 	As Single
Dim OBSmooth 	As Single
Dim Bearish 	As Single
Dim RangeA		As Single
Dim myStop		As Single
Dim DeltaL		As Single
Dim DeltaA		As Single
Dim myLevel		As Single

'Calculate the bullish band
myRSI = RSI(c,ChannelPeriods)
OB = myRSI - Overbought
OBSmooth = EMA(OB,ChannelSmoothing)
Bullish = c-c*(OBSmooth/100)

' Bear calculation
DeltaL = ABS(l-l[1])
Bearish = SMA(DeltaL,BearishPeriods)

' Calculate Stop level
DeltaA = c-l
RangeA = SMA(DeltaA,BearishPeriods)
myStop = c-Factor*(Bearish +RangeA)

' Update Stop Level and exit if crossed
If Signal = LongSignal and l<myLevel[1] Then
	Signal = ExitSignal

Else 
	myLevel = IIF(myLevel[1]>myStop,myLevel[1],myStop)
	ExitLevel = myLevel
End If

'Plot the stop level and bullish zone.
PlotPrice("StopL",myLevel)
PlotPrice("Bull",Bullish)