7/25/2023 0 Comments Portfolio drawdown pandasThe drawdown caclulation can now be made analogously using the formula above: dd = (p * b0 - b * p0) / (p0 * b0) ![]() p0 = pd.Series(p.ix.values, index=p.index)ī0 = pd.Series(b.ix.values, index=b.index) So, we generate a series of ' whens' captured in cam ( cumulative arg max) and subsequent series of portfolio and benchmark values at those ' whens'. ![]() The difference is that we want to keep track of what the p and b were at this time and not the difference itself. We get this series of cumulative active returns with p - b. Similar to the absolute case, at each point in time, we want to know what the maximum cumulative active return has been up to that point. P0 = pd.Series(p.iloc.values, index=p.index)ī0 = pd.Series(b.iloc.values, index=b.index) This is how we can extend the absolute solution: def max_draw_down_relative(p, b):Ĭam = pmb.expanding(min_periods=1).apply(lambda x: x.argmax()) The active return from period j to period i is: the variables below are assumed to already be in cumulative return space. Starting with a series of portfolio returns and benchmark returns, we build cumulative returns for both. I wanted to follow up by asking how others are calculating maximumĪssumes that the solution will extend on the solution above. The max drawdown is then just the minimum of all the calculated drawdowns. r = returns.add(1).cumprod()Īt each point in time, the current drawdown is calcualted by comparing the current level of the return index with the maximum return index for all periods prior. We start by generating a series of cumulative returns to act as a return index. It takes a return series and gives back the max_drawdown along with the indices for which the drawdown occured. This is what I implemented for max drawdown based on Alexander's answer to question linked above: def max_drawdown_absolute(returns): I wanted to follow up by asking how others are calculating maximum active drawdown? This calculates Max Drawdown. Print(df.tail()) # I use tail, beacause of the size of your window.I recently asked a question about calculating maximum drawdown where Alexander gave a very succinct and efficient way of calculating it with DataFrame methods in pandas. Print('In case you want to check the result data\n') So the code is the following import pandas as pdĭf = df.returns.rolling(180).apply(lambda x: (x.mean() - 0.02) / x.std(), raw = True)ĭf > 0].rolling_SR.plot(style='-', lw=3, color='orange',
0 Comments
Leave a Reply. |