Here the code for bearish divergence, pass this a Oscillator and it will return 1 when it finds divergence. This is code for TradersStudio. You can learn more about TradersStudio at
www.TradersStudio.com
'*******************************************************************
'Description: Bearish Divergence
'********************************************************************
' TradersStudio(r) copyright 2004-2007 , All rights reserved
Function BearishDivergence(Price As BarArray, Oscillator As BarArray, Strength, Length) As BarArray
Dim Return
Dim Value1
Dim Value2
Dim Value3
Dim Value4
Dim Condition1
Dim Condition2
Return = 0
Value1 = 0
Value2 = 0
Value3 = 0
Value4 = 0
Condition1 = 0
Condition2 = 0
Value1 = -1
Value2 = -1
Value3 = -1
Value4 = -1
Return = 0
Value1 = SwingHighBar(1, Price, Strength, Length)
If Value1 > -1 Then
Value2 = SwingHighBar(2, Price, Strength, Length)
End If
If Value2 > -1 Then
Value3 = SwingHighBar(1, Oscillator, Strength, Length)
End If
If Value3 > -1 Then
Value4 = SwingHighBar(2, Oscillator, Strength, Length)
End If
If Value4 > -1 Then
Condition1 = Price[Value1] > Price[Value2]
Condition2 = Oscillator[Value3] < Oscillator[Value4]
If Condition1 And Condition2 Then
Return = 1
End If
End If
BearishDivergence = Return
End Function