'Trend Tracking Indicator
Dim Period As Integer
Option Explicit
Option Base 1
Dim Z As Integer
Dim UpPivotValue As Double
Dim UpPivot As Double
Dim UpPivotAverage As Double
Dim DownPivotAverage As Double
Dim DownPivot As Double
Dim DownPivotValue As Double
Dim LateralBars As Integer
Dim PivotNumber As Integer
Dim UpFind As Integer
Dim DownFind As Integer
Dim UpPivotAverageReserve As Double
Dim Avg() As Double
Dim CommonAvg As Double
Dim AvgCounter As Double
Dim DefinitiveAvg As Double
Dim Inidicator_Value As Double
Dim nPeriod As Integer
Dim Control_Time As Double
Dim Control_Date As Double
Dim FinalControl As Boolean
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Dim MovilAvg As DataIdentifier
Public APP As OscUserApp
Implements Indicator
Public Sub Indicator_OnInitCalculate()
With APP
FinalControl = True
.StartBar = Period * 20 + 1
nPeriod = Period * 6
PivotNumber = Period - 1
LateralBars = Period
DefinitiveAvg = 0
Inidicator_Value = 0
CommonAvg = 0
Control_Time = 0
Control_Date = 0
MovilAvg = .GetIndicatorIdentifier(AvSimple, Data, 3, PriceClose)
ReDim Avg(nPeriod)
AvgCounter = 1
' Controls if the value introduced in Period is within the allowed rank
If Period <= 1 Or Period > 10 Then
Msg = "It has introduced a value for Period not between 1 and 10, do you want to continue calculating this indicator?"
' Define message.
Style = vbOKCancel + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Error of Parameter" ' Define title.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbCancel Then
FinalControl = False
End If
End If
End With
End Sub
Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
With APP
UpPivotAverage = 0
DownPivotAverage = 0
UpFind = 0
DownFind = 0
For Z = 1 To PivotNumber
DownPivot = .GetSwingLow(Data, Z, PriceLow, LateralBars, Period * 20)
UpPivot = .GetSwingHigh(Data, Z, PriceHigh, LateralBars, Period * 20)
If UpPivot <> NullValue Then
UpPivotAverage = UpPivotAverage + UpPivot
UpFind = UpFind + 1
End If
If DownPivot <> NullValue Then
DownPivotAverage = DownPivotAverage + DownPivot
DownFind = DownFind + 1
End If
Next Z
UpPivotAverage = UpPivotAverage / UpFind
DownPivotAverage = DownPivotAverage / DownFind
If .High > UpPivotAverage Then
CommonAvg = DownPivotAverage
End If
If .Low < DownPivotAverage Then
CommonAvg = UpPivotAverage
End If
If .CurrentBar = .StartBar Then
For Z = 1 To nPeriod
Avg(Z) = CommonAvg
Next Z
End If
If .Time > Control_Time Or .Date > Control_Date Then
If AvgCounter <= nPeriod Then
Avg(AvgCounter) = CommonAvg
AvgCounter = AvgCounter + 1
Else
AvgCounter = 1
Avg(AvgCounter) = CommonAvg
AvgCounter = AvgCounter + 1
End If
Control_Time = .Time
Control_Date = .Date
End If
DefinitiveAvg = 0
For Z = 1 To nPeriod
DefinitiveAvg = DefinitiveAvg + Avg(Z)
Next Z
DefinitiveAvg = DefinitiveAvg / nPeriod
Inidicator_Value = .GetIndicatorValue(MovilAvg) - DefinitiveAvg
.SetIndicatorValue Inidicator_Value, 1
.SetIndicatorValue 0, 2
End With
End Sub
Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
Period = ParamList(1)
End Sub
Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
Dim i As Long
i = APP.StartBar
If StartBar > i Then
i = StartBar
End If
While Not APP.ShouldTerminate And i <= FinalBar And FinalControl
APP.CurrentBar = i
Indicator_OnCalculateBar i
i = i + 1
Wend
End Sub
Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
Set APP = Application
End Sub