-
Notifications
You must be signed in to change notification settings - Fork 192
/
calculateMaxDD.m
33 lines (27 loc) · 1.07 KB
/
calculateMaxDD.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function [maxDD maxDDD]=calculateMaxDD(cumret)
% [maxDD maxDDD]=calculateMaxDD(cumret)
% calculation of maximum drawdown and maximum drawdown duration based on
% cumulative COMPOUNDED returns. cumret must be a compounded cumulative return.
% Same as calculateMaxDD_cpd for backward compatibility.
% written by:
% Ernest Chan
%
% Author of “Quantitative Trading:
% How to Start Your Own Algorithmic Trading Business”
%
% www.epchan.com
highwatermark=zeros(size(cumret)); % initialize high watermarks to zero.
drawdown=zeros(size(cumret)); % initialize drawdowns to zero.
drawdownduration=zeros(size(cumret)); % initialize drawdown duration to zero.
for t=2:length(cumret)
highwatermark(t)=max(highwatermark(t-1), cumret(t));
drawdown(t)=(1+cumret(t))./(1+highwatermark(t))-1; % drawdown on each day
if (drawdown(t)==0)
drawdownduration(t)=0;
else
drawdownduration(t)=drawdownduration(t-1)+1;
end
end
maxDD=min(drawdown); % maximum drawdown
maxDDD=max(drawdownduration); % maximum drawdown duration