Bruce
Can this indicator be plotted in TC?
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 2.5
'# delay = UserInput.Integer = 5
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme As Single
Static tstop As Single
Static state As Boolean
Static index As Integer
If isFirstBar Then
TR = Price.High - Price.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
index = CurrentIndex
If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
extreme = Price.Low
tstop = System.Math.Max(Price.High, extreme + factor * ATR)
state = False
Else
extreme = Price.High
tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
state = True
End If
Else
TR = (Price.High - Price.Low + _
System.Math.Abs(Price.High - Price.Last(1)) + _
System.Math.Abs(Price.Last(1) - Price.Low)) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If Price.Low < tstop AndAlso _
CurrentIndex - index >= delay - 1 Then
AddToOutput(Price.Bar.DateValue(index), extreme)
state = False
extreme = Price.Low
index = CurrentIndex
tstop = System.Math.Max(Price.High, extreme + factor * ATR)
End If
Else
If Price.High > tstop AndAlso _
CurrentIndex - index >= delay - 1 Then
AddToOutput(Price.Bar.DateValue(index), extreme)
state = True
extreme = Price.High
index = CurrentIndex
tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
End If
End If
If state Then
If Price.High >= extreme Then
extreme = Price.High
index = CurrentIndex
End If
tstop = extreme - factor * ATR
Else
If Price.Low <= extreme Then
extreme = Price.Low
index = CurrentIndex
End If
tstop = extreme + factor * ATR
End If
End If
If isLastBar Then AddToOutput(Price.Bar.DateValue(index), extreme)
Plot = Single.NaN
|