#MAXIMUM DRAWDOWN SERIES#
Pnl - A pandas Series representing period percentage returns.ĭrawdown, duration - Highest peak-to-trough drawdown and duration. The function then simply returns the maximum of each of the two Series: # performance.pyĬalculate the largest peak-to-trough drawdown of the PnL curveĪs well as the duration of the drawdown. If this value is negative then the duration is increased for every bar that this occurs until the next HWM is reached. The drawdown is then simply the difference between the current HWM and the equity curve.
Then the current high water mark (HWM) is established by determining if the equity curve exceeds all previous peaks. The function starts by creating two pandas Series objects representing the drawdown and duration at each trading "bar". There is some subtlety required in the interpretation of the drawdown duration as it counts trading periods and thus is not directly translateable into a temporal unit such as "days". The former is the aforementioned largest peak-to-trough drop, while the latter is defined as the number of periods over which this drop occurs. The create_drawdowns function below actually provides both the maximum drawdown and the maximum drawdown duration. While the Sharpe ratio characterises how much risk (as defined by asset path standard deviation) is being taken per unit of return, the "drawdown" is defined as the largest peak-to-trough drop along an equity curve. Return np.sqrt(periods) * (np.mean(returns)) / np.std(returns) Returns - A pandas Series representing period percentage returns. The create_sharpe_ratio function operates on a pandas Series object called returns and simply calculates the ratio of the mean of the period percentage returns and the period percentage return standard deviations scaled by the periods factor: # performance.pyĭef create_sharpe_ratio(returns, periods=252):Ĭreate the Sharpe ratio for the strategy, based on aīenchmark of zero (i.e. If you trade on a minutely basis, then this factor must be set to $252*6.5*60=98280$. Thus you need to set periods to $252*6.5 = 1638$, which is the number of US trading hours within a year. However, if your strategy trades within the hour you need to adjust the Sharpe to correctly annualise it. Usually this value is set to 252, which is the number of trading days in the US per year. It has a single parameter, that of the number of periods to adjust for when scaling up to the annualised value. Note that the Sharpe ratio is a measure of risk to reward (in fact it is one of many!).
![maximum drawdown maximum drawdown](https://www.stockportfolioorganizer.com/wp-content/uploads/2017/01/maximum-drawdown-chart.png)
As with most of our calculation-heavy classes we need to import NumPy and pandas: # performance.py The first task is to create a new file performance.py, which stores the functions to calculate the Sharpe ratio and drawdown information. In this article we will implement the Sharpe ratio, maximum drawdown and drawdown duration as measures of portfolio performance for use in the Python-based Event-Driven Backtesting suite. The former quantities the highest peak-to-trough decline in an equity curve performance, while the latter is defined as the number of trading periods over which it occurs. The maximum drawdown and drawdown duration are two additional measures that investors often uses to assess the risk in a portfolio. Where $R_a$ is the returns stream of the equity curve and $R_b$ is a benchmark, such as an appropriate interest rate or equity index. In that article I outline that the (annualised) Sharpe ratio is calculated via: We've already considered the Sharpe Ratio in a previous article.
#MAXIMUM DRAWDOWN HOW TO#
In this article we are going to discuss how to assess the performance of a strategy post-backtest using the previously constructed equity curve DataFrame in the Portfolio object. In the last article on the Event-Driven Backtester series we considered a basic ExecutionHandler hierarchy.