Notebook

Notebook to plot the price and drawdown of a particular stock

In [7]:
# Import some methods we will be needing
from quantopian.research import get_pricing, returns
import pyfolio as pf
In [8]:
# First let's make a place where we can easily set the stock and dates we want to check
stock = 'TSLA'
start_date = '1-1-2020'
end_date = '9-1-2020'
In [9]:
# Next let's get both the stock close prices and daily returns over the dates
stock_prices = get_pricing(stock, start_date, end_date, fields='price')
stock_returns = returns(stock, start_date, end_date, price_field='price')

# To get the max drawdown use the empyrical drawdown method
# Apply it over an expanding window of stock_returns
pf.create_returns_tear_sheet(stock_returns)
Start date2020-01-02
End date2020-09-01
Total months8
Backtest
Annual return 1232.986%
Cumulative returns 467.999%
Annual volatility 94.862%
Sharpe ratio 3.21
Calmar ratio 20.36
Stability 0.75
Max drawdown -60.565%
Omega ratio 1.78
Sortino ratio 5.40
Skew -0.02
Kurtosis 1.62
Tail ratio 1.49
Daily value at risk -10.742%
Worst drawdown periods Net drawdown in % Peak date Valley date Recovery date Duration
0 60.57 2020-02-19 2020-03-18 2020-06-08 79
1 17.24 2020-02-04 2020-02-05 2020-02-19 12
2 16.35 2020-07-20 2020-08-11 2020-08-14 20
3 8.77 2020-06-10 2020-06-12 2020-06-30 15
4 5.17 2020-01-14 2020-01-17 2020-01-21 6
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-6f1d37862f95> in <module>()
      5 # To get the max drawdown use the empyrical drawdown method
      6 # Apply it over an expanding window of stock_returns
----> 7 pf.create_returns_tear_sheet(stock_returns)

/venvs/py35/src/pyfolio/pyfolio/plotting.py in call_w_context(*args, **kwargs)
     50         if set_context:
     51             with plotting_context(), axes_style():
---> 52                 return func(*args, **kwargs)
     53         else:
     54             return func(*args, **kwargs)

/venvs/py35/src/pyfolio/pyfolio/tears.py in create_returns_tear_sheet(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)
    575     plotting.plot_monthly_returns_heatmap(returns, ax=ax_monthly_heatmap)
    576     plotting.plot_annual_returns(returns, ax=ax_annual_returns)
--> 577     plotting.plot_monthly_returns_dist(returns, ax=ax_monthly_dist)
    578 
    579     plotting.plot_return_quantiles(

/venvs/py35/src/pyfolio/pyfolio/plotting.py in plot_monthly_returns_dist(returns, ax, **kwargs)
    267         alpha=0.80,
    268         bins=20,
--> 269         **kwargs)
    270 
    271     ax.axvline(

/venvs/py35/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/venvs/py35/lib/python3.5/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   6216             labels = [label]
   6217         else:
-> 6218             labels = [six.text_type(lab) for lab in label]
   6219 
   6220         for (patch, lbl) in zip_longest(patches, labels, fillvalue=None):

TypeError: 'zipline.assets._assets.Equity' object is not iterable

For TSLA over the period 1-1-2020 to 9-1-2020 it looks like the max drawdown was 60%

For AAPL over the period 1-1-2020 to 9-1-2020 it looks like the max drawdown was about 32%

In [ ]: