In this post, we will show you how to calculate a custom metric which is the average maximum drawdown of all analyzed securities.We will first calculate the maximum drawdown for each symbol and then average the results. The rules metrics editor: From the menu, click on "Analysis" then "Rules Manager". Select or create a list of rules, then click on "Analyze". In the "Rules Analyzer Settings" form, select "Metrics" in the left panel. Click on "New" to add an empty metric, and then click on the green dot to edit the metric. Calculating the maximum drawdown of a security: Imagine you start with an initial equity of 100$ and that a certain rule generates the following outputs (returns in this case): 10%, 10%, 5%, 10%, 10% To get the different equity values after each return, you need to multiply the last equity number with the following factor: (1 + (Return in percentage / 100)) Your equity sequence will be: 100$ * (1 + (10 / 100)) = 100$ * (1 + 0.1) = 100$ * 1.1 = 110$, then 110$ * 0.9 = 99$, then 103.95$, then 93.55$, then 102.91$. We have to create the maximum equity series by selecting the maximum equity number from the preceding equity values. Equity series: 100 , 110 , 99 , 103.95 , 93.55 , 102.91 Maximum equity series: 100 , 110 , 110 , 110 , 110 , 110 Note that if we add a value of 120 to the equity series, the corresponding maximum equity value will be 120. If we add another value of 110 to the equity series, the last maximum equity series value will stay at 120. Now, for each element of the series, divide the equity series values by the maximum equity series values, subtract the result by one then multiply it by 100. The resulting series is the drawdown series: 0% , 0% , 10% , 5.5% , 14.95% , 6.44% The maximum drawdown is the lowest value of the preceding series which is 14.95%, and this is the maximum you can lose if you have invested in that particular security. In order to calculate our metric, we need to calculate the maximum drawdown for all securities and then average the result. Implementing the metric: Here are the steps to implement the metric:  Loop through the symbols:  Define the variables:  Loop through the symbols' positions  Calculate the current equity and the maximum equity value  Calculate the current and the maximum drawdown  Outside the symbols' positions loop, calculate the sum of the drawdowns and increment a counter  Outside the symbols loop, calculate the average maximum drawdown metric You can download the AMD metric here (Average Maximum Drawdown)
