Back to Community
Beginner question

Hi guys!

Probably a silly question but according to this backtest...
Beginning of 2007 SPY is at 72.01%,
Beginning of 2008 SPY is at 78.66%
Beginning of 2009 SPY is at 13.95%

But the draw down SPY between 2008-2009 was less than 2009s beginning % subtracted from 2008s beginning %. Why is that?

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS


def initialize(context):
    context.currently_holding = []
   
    set_benchmark(symbol('SPY'))
There was a runtime error.
6 responses

Maybe of help here in looking into it, but be sure to check this code carefully. Before a run, click in margin on line numbers, to set breakpoints.
Now, one thing I noticed, the Risk Metrics tab here says -.01. The backtest -.84, while the custom chart would be -.24 unless I'm mistaken.
Founder on drawdown: https://www.quantopian.com/posts/calculation-of-cumulative-max-drawdown
Others: https://www.google.com/search?q=drawdown+site:quantopian.com

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    context.spy_max_pct  = 0
    context.drawdown_max = 0

    for i in range(1, 391):    # every minute
        schedule_function(dd, date_rules.every_day(), time_rules.market_open(minutes=i))

def before_trading_start(context, data):  # merely here to record starting spy price
    if 'spy_prc_ori' not in context:
        context.spy_prc_ori = data.current(sid(8554), 'price')
        context.spy_prc_prv = context.spy_prc_ori

def dd(context, data):    # drawdown
    if not context.portfolio.positions:
        order(sid(8554), 1)    # owning 1 share throughout
        
    if data.current(sid(8554), 'price') > context.spy_prc_prv:
        return    # to make this run a little faster

    spy_now             = data.current(sid(8554), 'price')
    now_v_ori           = spy_now / context.spy_prc_ori
    now_v_ori_pct       = 100 * now_v_ori
    context.spy_max_pct = max(context.spy_max_pct, now_v_ori_pct)
    dd_now              = context.spy_max_pct - now_v_ori_pct

    if dd_now > context.drawdown_max:
        context.drawdown_max = dd_now
        record(spy_drawdown_max = context.drawdown_max)

    context.spy_prc_prv  = spy_now
There was a runtime error.

Very interesting, thank you.

@Blue Seahawk I'm not sure I phrased my question correctly, how can the total return of SPY that peaks at 92% fall to -11% when the market fell about 1/2 of that?

Joshua,

Maximum Drawdown(MDD) is expressed in percentage terms and computed as:

MDD = (Trough Value – Peak Value) / Peak Value

What you see on the graph(1/1/2008 79.23%) is cumulative return.

To get value or equity you need to add initial investment (100%)

1/1/2008 Value = 79.23% + 100% = 179.23%

1/1/2009 Value = 13.95% + 100% = 113.95%

For the period from 1/1/2008 to 1/1/2009

MDD = ( 113.95% - 179.23%)/179.23% = -36.42% not (13.95% - 79.23% = -65.28%).

this was exactly what I needed. thank you so much

As runnable code for a drawdown value one can use for making decisions could be useful to some.