Notebook
In [1]:
# Import Zipline, the open source backester, and a few other libraries that we will use
import zipline
from zipline import TradingAlgorithm
from zipline.api import schedule_function, order_target_percent, order_percent, record, symbol, history, add_history,set_commission,set_slippage
from zipline.api import date_rules, time_rules, commission, slippage, order_target, get_datetime
import pytz
from datetime import datetime
import numpy as np
import pandas as pd
from matplotlib import pyplot
from scipy.optimize import minimize
import time
import math
In [2]:
stocks = symbols(['XLF', 'XLE', 'XLU', 'XLK', 'XLB', 'XLP', 'XLY', 'XLI', 'XLV'])
data = get_pricing(
    stocks,
    start_date='2003-01-01',
    end_date = '2015-06-23',
    frequency='daily'
)
data.price.plot()
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f616f94c890>
In [6]:
# Define the algorithm - this should look familiar from the Quantopian IDE
# For more information on writing algorithms for Quantopian
# and these functions, see https://www.quantopian.com/help

def initialize(context):
    schedule_function(trade, 
                      date_rule=date_rules.week_end(), 
                      time_rule=time_rules.market_close(minutes=1), 
                      half_days=False)
    context.lookback = 252*2 # 252*n = n years
    context.stocks = symbols(['XLF', 'XLE', 'XLU', 'XLK', 'XLB', 'XLP', 'XLY', 'XLI', 'XLV'])
    set_commission(commission.PerShare(cost=0.00))
    set_slippage(slippage.VolumeShareSlippage(volume_limit=1, price_impact=0))
    context.i = 0
    add_history(context.lookback, '1d', 'price')

def handle_data(context, data):
    context.i += 1
    if context.i < context.lookback:
        return
    
def trade(context,data):
        if context.i < context.lookback:
            return
        w = estimate(context,data)
    
        # Close all current positions
        for stock in context.portfolio.positions:
            if stock not in w.index:
               order_target(stock, 0)  

       #Order
        for i,n in enumerate(w):
            if w.index[i] in data:
                if w.index[i] in context.portfolio.positions: #If position already exists
                    order_target_percent(w.index[i], n)
                else:
                    order_percent(w.index[i], n)
    
def estimate(context,data):
        # Structure a portfolio such that it maximizes dividend yield while lowering volatility
        # Covar estimation
        price_history = history(context.lookback, frequency="1d", field='price')
        ret = (price_history/price_history.shift(5)-1).dropna(axis=0)[5::5]
        ret.columns = context.stocks
        n = len(ret.columns)
        
        covar = ret.cov()
        
        w = min_var_weights(covar, context.stocks)

        # LOG EVERYTHING
        #esigma = np.sqrt(np.dot(w.T,np.dot(covar,w)))*math.sqrt(52)*100
        #log.info('Expected Volatility: '+str(esigma)+'%')
        #w = pd.Series(np.round(1000*w)/1000)
        #log.info('Number of Stocks: '+str(sum(abs(w)>0)))
        #log.info('Current Leverage: '+str(context.account.leverage))
        #w.index = ret.columns
        #log.info(w)
        #print(get_datetime())
        #print(w)
        return w  #Cash Cushion of X%


def min_var_weights(cov, stocks):
    '''
    Returns a dictionary of sid:weight pairs.
    '''
    cov = pd.DataFrame(2*cov)
    x = np.array([0.]*(len(cov)+1))
    #x = np.ones(len(cov) + 1)
    x[-1] = 1.0
    p = lagrangize(cov)
    weights = np.linalg.solve(p, x)[:-1]
    weights = pd.Series(weights, index=stocks)
    return weights


def lagrangize(df):
    '''
    Utility funcion to format a DataFrame 
    in order to solve a Lagrangian sysem. 
    '''
    df = df
    df['lambda'] = np.ones(len(df))
    z = np.ones(len(df) + 1)
    x = np.ones(len(df) + 1)
    z[-1] = 0.0
    x[-1] = 1.0
    m = [i for i in df.as_matrix()]
    m.append(z)    
    return pd.DataFrame(np.array(m))
In [7]:
# Analyze is a post-hoc analysis method available on Zipline. 
# It accepts the context object and 'perf' which is the output 
# of a Zipline backtest.  This API is currently experimental, 
# and will likely change before release.

def analyze(context, perf):
    perf.portfolio_value.plot()
In [8]:
# NOTE: This cell will take a few minutes to run.

# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(
    initialize=initialize, 
    handle_data=handle_data
)

# HACK: Analyze isn't supported by the parameter-based API, so
# tack it directly onto the object.
algo_obj._analyze = analyze

# Run algorithm
perf_manual = algo_obj.run(data.price)
2004-12-31 00:00:00+00:00
2005-01-07 00:00:00+00:00
2005-01-14 00:00:00+00:00
2005-01-21 00:00:00+00:00
2005-01-28 00:00:00+00:00
2005-02-04 00:00:00+00:00
2005-02-11 00:00:00+00:00
2005-02-18 00:00:00+00:00
2005-02-25 00:00:00+00:00
2005-03-04 00:00:00+00:00
2005-03-11 00:00:00+00:00
2005-03-18 00:00:00+00:00
2005-03-24 00:00:00+00:00
2005-04-01 00:00:00+00:00
2005-04-08 00:00:00+00:00
2005-04-15 00:00:00+00:00
2005-04-22 00:00:00+00:00
2005-04-29 00:00:00+00:00
2005-05-06 00:00:00+00:00
2005-05-13 00:00:00+00:00
2005-05-20 00:00:00+00:00
2005-05-27 00:00:00+00:00
2005-06-03 00:00:00+00:00
2005-06-10 00:00:00+00:00
2005-06-17 00:00:00+00:00
2005-06-24 00:00:00+00:00
2005-07-01 00:00:00+00:00
2005-07-08 00:00:00+00:00
2005-07-15 00:00:00+00:00
2005-07-22 00:00:00+00:00
2005-07-29 00:00:00+00:00
2005-08-05 00:00:00+00:00
2005-08-12 00:00:00+00:00
2005-08-19 00:00:00+00:00
2005-08-26 00:00:00+00:00
2005-09-02 00:00:00+00:00
2005-09-09 00:00:00+00:00
2005-09-16 00:00:00+00:00
2005-09-23 00:00:00+00:00
2005-09-30 00:00:00+00:00
2005-10-07 00:00:00+00:00
2005-10-14 00:00:00+00:00
2005-10-21 00:00:00+00:00
2005-10-28 00:00:00+00:00
2005-11-04 00:00:00+00:00
2005-11-11 00:00:00+00:00
2005-11-18 00:00:00+00:00
2005-12-02 00:00:00+00:00
2005-12-09 00:00:00+00:00
2005-12-16 00:00:00+00:00
2005-12-23 00:00:00+00:00
2005-12-30 00:00:00+00:00
2006-01-06 00:00:00+00:00
2006-01-13 00:00:00+00:00
2006-01-20 00:00:00+00:00
2006-01-27 00:00:00+00:00
2006-02-03 00:00:00+00:00
2006-02-10 00:00:00+00:00
2006-02-17 00:00:00+00:00
2006-02-24 00:00:00+00:00
2006-03-03 00:00:00+00:00
2006-03-10 00:00:00+00:00
2006-03-17 00:00:00+00:00
2006-03-24 00:00:00+00:00
2006-03-31 00:00:00+00:00
2006-04-07 00:00:00+00:00
2006-04-13 00:00:00+00:00
2006-04-21 00:00:00+00:00
2006-04-28 00:00:00+00:00
2006-05-05 00:00:00+00:00
2006-05-12 00:00:00+00:00
2006-05-19 00:00:00+00:00
2006-05-26 00:00:00+00:00
2006-06-02 00:00:00+00:00
2006-06-09 00:00:00+00:00
2006-06-16 00:00:00+00:00
2006-06-23 00:00:00+00:00
2006-06-30 00:00:00+00:00
2006-07-07 00:00:00+00:00
2006-07-14 00:00:00+00:00
2006-07-21 00:00:00+00:00
2006-07-28 00:00:00+00:00
2006-08-04 00:00:00+00:00
2006-08-11 00:00:00+00:00
2006-08-18 00:00:00+00:00
2006-08-25 00:00:00+00:00
2006-09-01 00:00:00+00:00
2006-09-08 00:00:00+00:00
2006-09-15 00:00:00+00:00
2006-09-22 00:00:00+00:00
2006-09-29 00:00:00+00:00
2006-10-06 00:00:00+00:00
2006-10-13 00:00:00+00:00
2006-10-20 00:00:00+00:00
2006-10-27 00:00:00+00:00
2006-11-03 00:00:00+00:00
2006-11-10 00:00:00+00:00
2006-11-17 00:00:00+00:00
2006-12-01 00:00:00+00:00
2006-12-08 00:00:00+00:00
2006-12-15 00:00:00+00:00
2006-12-22 00:00:00+00:00
2006-12-29 00:00:00+00:00
2007-01-05 00:00:00+00:00
2007-01-12 00:00:00+00:00
2007-01-19 00:00:00+00:00
2007-01-26 00:00:00+00:00
2007-02-02 00:00:00+00:00
2007-02-09 00:00:00+00:00
2007-02-16 00:00:00+00:00
2007-02-23 00:00:00+00:00
2007-03-02 00:00:00+00:00
2007-03-09 00:00:00+00:00
2007-03-16 00:00:00+00:00
2007-03-23 00:00:00+00:00
2007-03-30 00:00:00+00:00
2007-04-05 00:00:00+00:00
2007-04-13 00:00:00+00:00
2007-04-20 00:00:00+00:00
2007-04-27 00:00:00+00:00
2007-05-04 00:00:00+00:00
2007-05-11 00:00:00+00:00
2007-05-18 00:00:00+00:00
2007-05-25 00:00:00+00:00
2007-06-01 00:00:00+00:00
2007-06-08 00:00:00+00:00
2007-06-15 00:00:00+00:00
2007-06-22 00:00:00+00:00
2007-06-29 00:00:00+00:00
2007-07-06 00:00:00+00:00
2007-07-13 00:00:00+00:00
2007-07-20 00:00:00+00:00
2007-07-27 00:00:00+00:00
2007-08-03 00:00:00+00:00
2007-08-10 00:00:00+00:00
2007-08-17 00:00:00+00:00
2007-08-24 00:00:00+00:00
2007-08-31 00:00:00+00:00
2007-09-07 00:00:00+00:00
2007-09-14 00:00:00+00:00
2007-09-21 00:00:00+00:00
2007-09-28 00:00:00+00:00
2007-10-05 00:00:00+00:00
2007-10-12 00:00:00+00:00
2007-10-19 00:00:00+00:00
2007-10-26 00:00:00+00:00
2007-11-02 00:00:00+00:00
2007-11-09 00:00:00+00:00
2007-11-16 00:00:00+00:00
2007-11-30 00:00:00+00:00
2007-12-07 00:00:00+00:00
2007-12-14 00:00:00+00:00
2007-12-21 00:00:00+00:00
2007-12-28 00:00:00+00:00
2008-01-04 00:00:00+00:00
2008-01-11 00:00:00+00:00
2008-01-18 00:00:00+00:00
2008-01-25 00:00:00+00:00
2008-02-01 00:00:00+00:00
2008-02-08 00:00:00+00:00
2008-02-15 00:00:00+00:00
2008-02-22 00:00:00+00:00
2008-02-29 00:00:00+00:00
2008-03-07 00:00:00+00:00
2008-03-14 00:00:00+00:00
2008-03-20 00:00:00+00:00
2008-03-28 00:00:00+00:00
2008-04-04 00:00:00+00:00
2008-04-11 00:00:00+00:00
2008-04-18 00:00:00+00:00
2008-04-25 00:00:00+00:00
2008-05-02 00:00:00+00:00
2008-05-09 00:00:00+00:00
2008-05-16 00:00:00+00:00
2008-05-23 00:00:00+00:00
2008-05-30 00:00:00+00:00
2008-06-06 00:00:00+00:00
2008-06-13 00:00:00+00:00
2008-06-20 00:00:00+00:00
2008-06-27 00:00:00+00:00
2008-07-11 00:00:00+00:00
2008-07-18 00:00:00+00:00
2008-07-25 00:00:00+00:00
2008-08-01 00:00:00+00:00
2008-08-08 00:00:00+00:00
2008-08-15 00:00:00+00:00
2008-08-22 00:00:00+00:00
2008-08-29 00:00:00+00:00
2008-09-05 00:00:00+00:00
2008-09-12 00:00:00+00:00
2008-09-19 00:00:00+00:00
2008-09-26 00:00:00+00:00
2008-10-03 00:00:00+00:00
2008-10-10 00:00:00+00:00
2008-10-17 00:00:00+00:00
2008-10-24 00:00:00+00:00
2008-10-31 00:00:00+00:00
2008-11-07 00:00:00+00:00
2008-11-14 00:00:00+00:00
2008-11-21 00:00:00+00:00
2008-12-05 00:00:00+00:00
2008-12-12 00:00:00+00:00
2008-12-19 00:00:00+00:00
2008-12-26 00:00:00+00:00
2009-01-02 00:00:00+00:00
2009-01-09 00:00:00+00:00
2009-01-16 00:00:00+00:00
2009-01-23 00:00:00+00:00
2009-01-30 00:00:00+00:00
2009-02-06 00:00:00+00:00
2009-02-13 00:00:00+00:00
2009-02-20 00:00:00+00:00
2009-02-27 00:00:00+00:00
2009-03-06 00:00:00+00:00
2009-03-13 00:00:00+00:00
2009-03-20 00:00:00+00:00
2009-03-27 00:00:00+00:00
2009-04-03 00:00:00+00:00
2009-04-09 00:00:00+00:00
2009-04-17 00:00:00+00:00
2009-04-24 00:00:00+00:00
2009-05-01 00:00:00+00:00
2009-05-08 00:00:00+00:00
2009-05-15 00:00:00+00:00
2009-05-22 00:00:00+00:00
2009-05-29 00:00:00+00:00
2009-06-05 00:00:00+00:00
2009-06-12 00:00:00+00:00
2009-06-19 00:00:00+00:00
2009-06-26 00:00:00+00:00
2009-07-02 00:00:00+00:00
2009-07-10 00:00:00+00:00
2009-07-17 00:00:00+00:00
2009-07-24 00:00:00+00:00
2009-07-31 00:00:00+00:00
2009-08-07 00:00:00+00:00
2009-08-14 00:00:00+00:00
2009-08-21 00:00:00+00:00
2009-08-28 00:00:00+00:00
2009-09-04 00:00:00+00:00
2009-09-11 00:00:00+00:00
2009-09-18 00:00:00+00:00
2009-09-25 00:00:00+00:00
2009-10-02 00:00:00+00:00
2009-10-09 00:00:00+00:00
2009-10-16 00:00:00+00:00
2009-10-23 00:00:00+00:00
2009-10-30 00:00:00+00:00
2009-11-06 00:00:00+00:00
2009-11-13 00:00:00+00:00
2009-11-20 00:00:00+00:00
2009-12-04 00:00:00+00:00
2009-12-11 00:00:00+00:00
2009-12-18 00:00:00+00:00
2009-12-31 00:00:00+00:00
2010-01-08 00:00:00+00:00
2010-01-15 00:00:00+00:00
2010-01-22 00:00:00+00:00
2010-01-29 00:00:00+00:00
2010-02-05 00:00:00+00:00
2010-02-12 00:00:00+00:00
2010-02-19 00:00:00+00:00
2010-02-26 00:00:00+00:00
2010-03-05 00:00:00+00:00
2010-03-12 00:00:00+00:00
2010-03-19 00:00:00+00:00
2010-03-26 00:00:00+00:00
2010-04-01 00:00:00+00:00
2010-04-09 00:00:00+00:00
2010-04-16 00:00:00+00:00
2010-04-23 00:00:00+00:00
2010-04-30 00:00:00+00:00
2010-05-07 00:00:00+00:00
2010-05-14 00:00:00+00:00
2010-05-21 00:00:00+00:00
2010-05-28 00:00:00+00:00
2010-06-04 00:00:00+00:00
2010-06-11 00:00:00+00:00
2010-06-18 00:00:00+00:00
2010-06-25 00:00:00+00:00
2010-07-02 00:00:00+00:00
2010-07-09 00:00:00+00:00
2010-07-16 00:00:00+00:00
2010-07-23 00:00:00+00:00
2010-07-30 00:00:00+00:00
2010-08-06 00:00:00+00:00
2010-08-13 00:00:00+00:00
2010-08-20 00:00:00+00:00
2010-08-27 00:00:00+00:00
2010-09-03 00:00:00+00:00
2010-09-10 00:00:00+00:00
2010-09-17 00:00:00+00:00
2010-09-24 00:00:00+00:00
2010-10-01 00:00:00+00:00
2010-10-08 00:00:00+00:00
2010-10-15 00:00:00+00:00
2010-10-22 00:00:00+00:00
2010-10-29 00:00:00+00:00
2010-11-05 00:00:00+00:00
2010-11-12 00:00:00+00:00
2010-11-19 00:00:00+00:00
2010-12-03 00:00:00+00:00
2010-12-10 00:00:00+00:00
2010-12-17 00:00:00+00:00
2010-12-23 00:00:00+00:00
2010-12-31 00:00:00+00:00
2011-01-07 00:00:00+00:00
2011-01-14 00:00:00+00:00
2011-01-21 00:00:00+00:00
2011-01-28 00:00:00+00:00
2011-02-04 00:00:00+00:00
2011-02-11 00:00:00+00:00
2011-02-18 00:00:00+00:00
2011-02-25 00:00:00+00:00
2011-03-04 00:00:00+00:00
2011-03-11 00:00:00+00:00
2011-03-18 00:00:00+00:00
2011-03-25 00:00:00+00:00
2011-04-01 00:00:00+00:00
2011-04-08 00:00:00+00:00
2011-04-15 00:00:00+00:00
2011-04-21 00:00:00+00:00
2011-04-29 00:00:00+00:00
2011-05-06 00:00:00+00:00
2011-05-13 00:00:00+00:00
2011-05-20 00:00:00+00:00
2011-05-27 00:00:00+00:00
2011-06-03 00:00:00+00:00
2011-06-10 00:00:00+00:00
2011-06-17 00:00:00+00:00
2011-06-24 00:00:00+00:00
2011-07-01 00:00:00+00:00
2011-07-08 00:00:00+00:00
2011-07-15 00:00:00+00:00
2011-07-22 00:00:00+00:00
2011-07-29 00:00:00+00:00
2011-08-05 00:00:00+00:00
2011-08-12 00:00:00+00:00
2011-08-19 00:00:00+00:00
2011-08-26 00:00:00+00:00
2011-09-02 00:00:00+00:00
2011-09-09 00:00:00+00:00
2011-09-16 00:00:00+00:00
2011-09-23 00:00:00+00:00
2011-09-30 00:00:00+00:00
2011-10-07 00:00:00+00:00
2011-10-14 00:00:00+00:00
2011-10-21 00:00:00+00:00
2011-10-28 00:00:00+00:00
2011-11-04 00:00:00+00:00
2011-11-11 00:00:00+00:00
2011-11-18 00:00:00+00:00
2011-12-02 00:00:00+00:00
2011-12-09 00:00:00+00:00
2011-12-16 00:00:00+00:00
2011-12-23 00:00:00+00:00
2011-12-30 00:00:00+00:00
2012-01-06 00:00:00+00:00
2012-01-13 00:00:00+00:00
2012-01-20 00:00:00+00:00
2012-01-27 00:00:00+00:00
2012-02-03 00:00:00+00:00
2012-02-10 00:00:00+00:00
2012-02-17 00:00:00+00:00
2012-02-24 00:00:00+00:00
2012-03-02 00:00:00+00:00
2012-03-09 00:00:00+00:00
2012-03-16 00:00:00+00:00
2012-03-23 00:00:00+00:00
2012-03-30 00:00:00+00:00
2012-04-05 00:00:00+00:00
2012-04-13 00:00:00+00:00
2012-04-20 00:00:00+00:00
2012-04-27 00:00:00+00:00
2012-05-04 00:00:00+00:00
2012-05-11 00:00:00+00:00
2012-05-18 00:00:00+00:00
2012-05-25 00:00:00+00:00
2012-06-01 00:00:00+00:00
2012-06-08 00:00:00+00:00
2012-06-15 00:00:00+00:00
2012-06-22 00:00:00+00:00
2012-06-29 00:00:00+00:00
2012-07-06 00:00:00+00:00
2012-07-13 00:00:00+00:00
2012-07-20 00:00:00+00:00
2012-07-27 00:00:00+00:00
2012-08-03 00:00:00+00:00
2012-08-10 00:00:00+00:00
2012-08-17 00:00:00+00:00
2012-08-24 00:00:00+00:00
2012-08-31 00:00:00+00:00
2012-09-07 00:00:00+00:00
2012-09-14 00:00:00+00:00
2012-09-21 00:00:00+00:00
2012-09-28 00:00:00+00:00
2012-10-05 00:00:00+00:00
2012-10-12 00:00:00+00:00
2012-10-19 00:00:00+00:00
2012-10-26 00:00:00+00:00
2012-11-02 00:00:00+00:00
2012-11-09 00:00:00+00:00
2012-11-16 00:00:00+00:00
2012-11-30 00:00:00+00:00
2012-12-07 00:00:00+00:00
2012-12-14 00:00:00+00:00
2012-12-21 00:00:00+00:00
2012-12-28 00:00:00+00:00
2013-01-04 00:00:00+00:00
2013-01-11 00:00:00+00:00
2013-01-18 00:00:00+00:00
2013-01-25 00:00:00+00:00
2013-02-01 00:00:00+00:00
2013-02-08 00:00:00+00:00
2013-02-15 00:00:00+00:00
2013-02-22 00:00:00+00:00
2013-03-01 00:00:00+00:00
2013-03-08 00:00:00+00:00
2013-03-15 00:00:00+00:00
2013-03-22 00:00:00+00:00
2013-03-28 00:00:00+00:00
2013-04-05 00:00:00+00:00
2013-04-12 00:00:00+00:00
2013-04-19 00:00:00+00:00
2013-04-26 00:00:00+00:00
2013-05-03 00:00:00+00:00
2013-05-10 00:00:00+00:00
2013-05-17 00:00:00+00:00
2013-05-24 00:00:00+00:00
2013-05-31 00:00:00+00:00
2013-06-07 00:00:00+00:00
2013-06-14 00:00:00+00:00
2013-06-21 00:00:00+00:00
2013-06-28 00:00:00+00:00
2013-07-05 00:00:00+00:00
2013-07-12 00:00:00+00:00
2013-07-19 00:00:00+00:00
2013-07-26 00:00:00+00:00
2013-08-02 00:00:00+00:00
2013-08-09 00:00:00+00:00
2013-08-16 00:00:00+00:00
2013-08-23 00:00:00+00:00
2013-08-30 00:00:00+00:00
2013-09-06 00:00:00+00:00
2013-09-13 00:00:00+00:00
2013-09-20 00:00:00+00:00
2013-09-27 00:00:00+00:00
2013-10-04 00:00:00+00:00
2013-10-11 00:00:00+00:00
2013-10-18 00:00:00+00:00
2013-10-25 00:00:00+00:00
2013-11-01 00:00:00+00:00
2013-11-08 00:00:00+00:00
2013-11-15 00:00:00+00:00
2013-11-22 00:00:00+00:00
2013-12-06 00:00:00+00:00
2013-12-13 00:00:00+00:00
2013-12-20 00:00:00+00:00
2013-12-27 00:00:00+00:00
2014-01-03 00:00:00+00:00
2014-01-10 00:00:00+00:00
2014-01-17 00:00:00+00:00
2014-01-24 00:00:00+00:00
2014-01-31 00:00:00+00:00
2014-02-07 00:00:00+00:00
2014-02-14 00:00:00+00:00
2014-02-21 00:00:00+00:00
2014-02-28 00:00:00+00:00
2014-03-07 00:00:00+00:00
2014-03-14 00:00:00+00:00
2014-03-21 00:00:00+00:00
2014-03-28 00:00:00+00:00
2014-04-04 00:00:00+00:00
2014-04-11 00:00:00+00:00
2014-04-17 00:00:00+00:00
2014-04-25 00:00:00+00:00
2014-05-02 00:00:00+00:00
2014-05-09 00:00:00+00:00
2014-05-16 00:00:00+00:00
2014-05-23 00:00:00+00:00
2014-05-30 00:00:00+00:00
2014-06-06 00:00:00+00:00
2014-06-13 00:00:00+00:00
2014-06-20 00:00:00+00:00
2014-06-27 00:00:00+00:00
2014-07-11 00:00:00+00:00
2014-07-18 00:00:00+00:00
2014-07-25 00:00:00+00:00
2014-08-01 00:00:00+00:00
2014-08-08 00:00:00+00:00
2014-08-15 00:00:00+00:00
2014-08-22 00:00:00+00:00
2014-08-29 00:00:00+00:00
2014-09-05 00:00:00+00:00
2014-09-12 00:00:00+00:00
2014-09-19 00:00:00+00:00
2014-09-26 00:00:00+00:00
2014-10-03 00:00:00+00:00
2014-10-10 00:00:00+00:00
2014-10-17 00:00:00+00:00
2014-10-24 00:00:00+00:00
2014-10-31 00:00:00+00:00
2014-11-07 00:00:00+00:00
2014-11-14 00:00:00+00:00
2014-11-21 00:00:00+00:00
2014-12-05 00:00:00+00:00
2014-12-12 00:00:00+00:00
2014-12-19 00:00:00+00:00
2014-12-26 00:00:00+00:00
2015-01-02 00:00:00+00:00
2015-01-09 00:00:00+00:00
2015-01-16 00:00:00+00:00
2015-01-23 00:00:00+00:00
2015-01-30 00:00:00+00:00
2015-02-06 00:00:00+00:00
2015-02-13 00:00:00+00:00
2015-02-20 00:00:00+00:00
2015-02-27 00:00:00+00:00
2015-03-06 00:00:00+00:00
2015-03-13 00:00:00+00:00
2015-03-20 00:00:00+00:00
2015-03-27 00:00:00+00:00
2015-04-02 00:00:00+00:00
2015-04-10 00:00:00+00:00
2015-04-17 00:00:00+00:00
2015-04-24 00:00:00+00:00
2015-05-01 00:00:00+00:00
2015-05-08 00:00:00+00:00
2015-05-15 00:00:00+00:00
2015-05-22 00:00:00+00:00
2015-05-29 00:00:00+00:00
2015-06-05 00:00:00+00:00
2015-06-12 00:00:00+00:00
2015-06-19 00:00:00+00:00
In [ ]:
perf_manual = perf_manual[perf_manual.index >= datetime(2005,1,1)]
In [ ]:
perf_manual.returns.plot()
In [ ]:
    def calculate_max_drawdown(rets):
        compounded_returns = []
        cur_return = 0.0
        for r in rets:
            try:
                cur_return += math.log(1.0 + r)
            # this is a guard for a single day returning -100%, if returns are
            # greater than -1.0 it will throw an error because you cannot take
            # the log of a negative number
            except ValueError:
                log.debug("{cur} return, zeroing the returns".format(
                    cur=cur_return))
                cur_return = 0.0
            compounded_returns.append(cur_return)

        cur_max = None
        max_drawdown = None
        for cur in compounded_returns:
            if cur_max is None or cur > cur_max:
                cur_max = cur

            drawdown = (cur - cur_max)
            if max_drawdown is None or drawdown < max_drawdown:
                max_drawdown = drawdown

        if max_drawdown is None:
            return 0.0

        return 1.0 - math.exp(max_drawdown)
In [ ]:
calculate_max_drawdown(perf_manual.returns)
In [ ]:
 
In [ ]: