Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

How to create a indicator to display Max drawdown for the last 60day, or 90days? Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
abfs
Posted : Wednesday, April 7, 2010 6:27:44 PM
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
StockGuy
Posted : Wednesday, April 7, 2010 10:11:08 PM

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?
abfs
Posted : Wednesday, April 7, 2010 10:36:08 PM
Registered User
Joined: 12/30/2004
Posts: 28

Yes, it measures the maximum loss from a price peak to price low.

jas0501
Posted : Thursday, April 8, 2010 1:26:28 AM
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

 

jas0501
Posted : Thursday, April 8, 2010 3:14:38 PM
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

abfs
Posted : Thursday, April 8, 2010 11:20:35 PM
Registered User
Joined: 12/30/2004
Posts: 28
thanks
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.