Max drawdown calc
Forums › ProRealTime English forum › ProBuilder support › Max drawdown calc
- This topic has 13 replies, 3 voices, and was last updated 8 years ago by Nicolas.
-
-
09/22/2016 at 1:43 PM #13542
Hi All,
I am trying to calculate the max drawdown for a strategy in order to display this on a backtest.
This is what I have so far:
1234567891011121314151617181920212223242526// max drawdown - wiponce highestequity=startcapitalonce lowestequity=startcapitalonce mddall=0equity=startcapital+strategyprofitif equity>highestequity thenhighestequity=equitylowestequity=equitynewpeakbar=barindexendifif equity<lowestequity and barindex>newpeakbar thenlowestequity=equitymdd=(lowestequity-highestequity)/highestequity*100endifif mdd<mddall thenmddall=mddendifgraph mddallNot sure how right or wrong this is currently.
Any help would be appreciated.
Stef
09/22/2016 at 1:46 PM #13544You should investigate on how to calcule the MAE (Max Adverse Excursion), which is the opposite of the MFE. You’ll find a way to calculate MFE in my trailing stop function here: http://www.prorealcode.com/blog/learning/trailing-stop-max-favorable-excursion-mfe/
The max drawdown of a strategy is already displayed on the backtest result window, so why are you looking to another way to find it?
09/22/2016 at 2:58 PM #13547@Nicolas. Max drawdown is displayed as a single value only. I would like to see how it developed over time – visually. By calculating it yourself, you will also be able to see if a large system drawdown was the result of a single bad year/month or many bad months/years. If a single occurrence, you can investigate it further – before simply eliminating a system because of that.
Have you looked at/tried my code – I don’t think it is necessarily that far off?
Stef
09/22/2016 at 3:12 PM #13548No I’m sorry, I didn’t test your code already. But the MAE is the complete inverse of my previous code of the MFE, it should be something like this:
123456789101112131415//resetting variables when no trades are on marketif not onmarket thenMAXPRICE = 0MINPRICE = closeendif//case SHORT orderif shortonmarket thenMAXPRICE = MAX(MAXPRICE,close) //saving the MAE of the current tradeendif//case LONG orderif longonmarket thenMINPRICE = MIN(MINPRICE,close) //saving the MAE of the current tradeendifWith this little code snippet you already have the minimum or maximum price met when you are on market. You could make a difference of these 2 values with the POSITIONPRICE and then transform this difference into money. Done, you have your max $ drawdown.
09/22/2016 at 4:01 PM #13549Tx Nicolas. Unless I am understanding you incorrectly, I do not think that we are trying to calculate the same thing.
I am not trying to calculate the high-low of a single trade, I am trying to calculate the largest difference between any highest peak and subsequent lowest trough.
As in:
http://www.robeco.com/en/professionals/insights/markets/2015/03/the-formula-maximum-drawdown.jsp
http://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp
Stef
09/22/2016 at 6:18 PM #13553Hmmm ok, you are right it’s rather different from what I thought in the first place.
If I refer to the investopedia explanation, it deals with money, but are only the closed profit or the closed profit + floating one? (made by current trades on market).
09/22/2016 at 6:26 PM #1355609/23/2016 at 5:39 AM #1357909/23/2016 at 6:15 AM #13580@John S.
You can see the peak to valley troughs on the equity graph yes, but it is not that simple to see if one peak to valley trough is bigger than another – especially if your equity curve contains a lot of data (system was backtested over a long time). Also, max drawdown takes into account all the decline until a new high or peak is formed. It might be of interest, for example, to know that your system might take 3/6 months before a new equity high is formed. During that time, your system is in drawdown – you’re not making money.
What I am looking for is more like this (see attachment). It shows the max drawdown as it develops over time (green line), and max drawdown for the whole period (blue line).
What I was hoping (asking) for was for someone to check my calculations to see if they agree.
Stef
09/23/2016 at 7:21 AM #1358609/23/2016 at 7:34 AM #13588I think the most accurate (correct way) would be to include open positions. I did not know how, so I used strategyprofit. It should not make that big a difference either way; i.e. if open positions cannot be included, then closed only ones would be good enough. Whether drawdown is calculated as 16.6% or 17.8%, for example, is not THAT important. I would be more interested to know if drawdown is less than 10%, 20%, 25%, etc.
09/23/2016 at 7:46 AM #13592Yes you can include them, you can use this code snippet in your formulas :
12//floating profitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gainsI made a request to PRT to add a new instruction for the floating profit, because we already have POSITIONPERF but it is in percentage.
Anyway, this variable can added to this line:
1equity=startcapital+strategyprofit+floatingprofit09/23/2016 at 9:02 AM #13601@Nicolas, tx for that!
I wonder how they (PRT) calculate their max drawdown %.
I did some comparisons on different instruments – see attached.
It looks like my calculations are 85%+ accurate on average – or at least 85%+ the same as theirs.
Interesting if you look at the 2nd and 4th results – at least we are both consistent which is a good sign.
Stef
09/23/2016 at 9:06 AM #13604 -
AuthorPosts