Welcome Guest, please sign in to participate in a discussion. | Search | Active Topics | |
Registered User Joined: 12/30/2004 Posts: 28
|
How to create a indicator to display Max drawdown (MDD) for the last 60day, or 90days? I would like to be able to display the value in percentage terms from highest point to the lowest point in the last 30days or 60days.
thanks
|
|
Administration
Joined: 9/30/2004 Posts: 9,187
|
Since you're using the term "drawdown", I assume the low point would have to come after the high point. Is that correct? You're not just looking for the maximum range over the last x days are you?
|
|
Registered User Joined: 12/30/2004 Posts: 28
|
Yes, it measures the maximum loss from a price peak to price low.
|
|
Registered User Joined: 12/31/2005 Posts: 2,499
|
I think this might do the trick, but not very efficiently:
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:dd
'|******************************************************************
'
' not very efficient as it processes upto 60 bars per call
'
'# period = userinput.integer = 60
dim drawdown as single
dim currhigh as single
dim currlow as single
dim lookback as integer
dim dirty as boolean
currhigh = -1
currlow = 99999
drawdown = 0
if currentIndex > period then
lookback = period
else
lookback = currentindex
End If
For i As Integer = lookback To 0 Step -1
dirty = False
If price.low(i) < currlow Then
currlow = price.low(i)
dirty = true
End If
if price.high(i) > currhigh then
currhigh = price.high(i)
currlow = price.low(i)
dirty = true
End If
if dirty andalso currhigh - currlow > drawdown then
drawdown = currhigh - currlow
end if
next i
plot = -drawdown
|
|
Registered User Joined: 12/31/2005 Posts: 2,499
|
Here is a more efficient version with less looping:
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:DrawDown(x) efficient
'|******************************************************************
'# period = userinput.integer = 60
Static drawdown As Single
Static currhigh As Single
Static currlow As Single
Static highOfDDIndex As Integer
Static highIndex As Integer
Dim lookback As Integer
Dim dirty As Boolean
If isfirstbar Then
currhigh = -1
currlow = 99999
drawdown = 0
highOfDDIndex = 0
highIndex = 0
End If
'
' if the index of the high that was used to
' compute the drawdown is outside the period
' then loop over the period
' otherwise
' just check the current bar
'
'
If highOfDDIndex < currentindex - period Then
currhigh = -1
currlow = 99999
drawdown = 0
'
' high has expired
'
If currentIndex > period Then
lookback = period
Else
lookback = currentindex
End If
For i As Integer = lookback To 0 Step -1
dirty = False
If price.low(i) < currlow Then
currlow = price.low(i)
dirty = True
End If
If price.high(i) > currhigh Then
highIndex = currentIndex - i
currhigh = price.high(i)
currlow = price.low(i)
dirty = True
End If
If dirty AndAlso currhigh - currlow > drawdown Then
highOfDDIndex = highIndex
drawdown = currhigh - currlow
End If
Next i
Else
dirty = False
Dim i As Integer = 0
If price.low(i) < currlow Then
currlow = price.low(i)
dirty = True
End If
If price.high(i) > currhigh Then
highIndex = currentIndex - i
currhigh = price.high(i)
currlow = price.low(i)
dirty = True
End If
If dirty AndAlso currhigh - currlow > drawdown Then
highOfDDIndex = highIndex
drawdown = currhigh - currlow
End If
End If
plot = -drawdown
|
|
Registered User Joined: 12/30/2004 Posts: 28
|
thanks
|
|
Guest-1 |