%load_ext zipline
%%zipline --start=2010-1-1 --end=2013-12-31 --bundle=quantopian-quandl -o perf.pickle --capital-base 250 --data-frequency daily
# when the algorithm is run it generates the perf.pickle file with information about the performance of the
# strategy, this datastructure is used later to analyze the performance
import zipline
import pandas as pd
from zipline.api import order_target, record, symbol
def initialize(context):
context.sym = symbol('AAPL')
context.i = 0
def handle_data(context, data):
# Skip first 100 days to get full windows
context.i += 1
if context.i < 100:
return
# Compute averages
# history() has to be called with the same params
# from above and returns a pandas dataframe.
short_mavg = data.history(context.sym, 'price', 50, '1d').mean()
long_mavg = data.history(context.sym, 'price', 100, '1d').mean()
# Trading logic
if short_mavg > long_mavg:
# order_target orders as many shares as needed to
# achieve the desired number of shares.
order_target(context.sym, 1)
elif short_mavg < long_mavg:
order_target(context.sym, 0)
# Save values for later inspection
record(AAPL=data.current(context.sym, "price"),
short_mavg=short_mavg,
long_mavg=long_mavg)
[2018-01-08 20:29:58.547872] WARNING: Loader: Refusing to download new benchmark data because a download succeeded at 2018-01-08 20:11:46.849974+00:00. [2018-01-08 20:30:06.810146] INFO: Performance: Simulated 1006 trading days out of 1006. [2018-01-08 20:30:06.811322] INFO: Performance: first open: 2010-01-04 14:31:00+00:00 [2018-01-08 20:30:06.812132] INFO: Performance: last close: 2013-12-31 21:00:00+00:00
AAPL | algo_volatility | algorithm_period_return | alpha | benchmark_period_return | benchmark_volatility | beta | capital_used | ending_cash | ending_exposure | ... | short_mavg | short_value | shorts_count | sortino | starting_cash | starting_exposure | starting_value | trading_days | transactions | treasury_period_return | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2010-01-04 21:00:00+00:00 | NaN | NaN | 0.000000 | NaN | 0.016960 | NaN | NaN | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 1 | [] | 0.0385 |
2010-01-05 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.019652 | 0.160660 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 2 | [] | 0.0377 |
2010-01-06 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.020370 | 0.140929 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 3 | [] | 0.0385 |
2010-01-07 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.024677 | 0.116833 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 4 | [] | 0.0385 |
2010-01-08 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.028087 | 0.103122 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 5 | [] | 0.0383 |
2010-01-11 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.029523 | 0.096122 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 6 | [] | 0.0385 |
2010-01-12 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.019921 | 0.122315 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 7 | [] | 0.0374 |
2010-01-13 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.028536 | 0.117521 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 8 | [] | 0.0380 |
2010-01-14 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.031317 | 0.110021 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 9 | [] | 0.0376 |
2010-01-15 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.019742 | 0.127235 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 10 | [] | 0.0370 |
2010-01-19 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.032484 | 0.130768 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 11 | [] | 0.0373 |
2010-01-20 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.021985 | 0.138401 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 12 | [] | 0.0368 |
2010-01-21 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | 0.002333 | 0.161774 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 13 | [] | 0.0362 |
2010-01-22 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.020011 | 0.182443 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 14 | [] | 0.0362 |
2010-01-25 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.014986 | 0.177820 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 15 | [] | 0.0366 |
2010-01-26 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.019113 | 0.172271 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 16 | [] | 0.0365 |
2010-01-27 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.014447 | 0.168345 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 17 | [] | 0.0366 |
2010-01-28 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.025754 | 0.168127 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 18 | [] | 0.0368 |
2010-01-29 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.036342 | 0.166993 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 19 | [] | 0.0363 |
2010-02-01 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.021357 | 0.173935 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 20 | [] | 0.0368 |
2010-02-02 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.009512 | 0.175521 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 21 | [] | 0.0367 |
2010-02-03 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.014447 | 0.171993 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 22 | [] | 0.0373 |
2010-02-04 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.044867 | 0.195627 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 23 | [] | 0.0362 |
2010-02-05 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.042893 | 0.191763 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 24 | [] | 0.0359 |
2010-02-08 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.049803 | 0.188526 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 25 | [] | 0.0362 |
2010-02-09 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.037868 | 0.190177 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 26 | [] | 0.0367 |
2010-02-10 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.039752 | 0.186492 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 27 | [] | 0.0372 |
2010-02-11 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.029702 | 0.186456 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 28 | [] | 0.0373 |
2010-02-12 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.030510 | 0.183097 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 29 | [] | 0.0369 |
2010-02-16 21:00:00+00:00 | NaN | 0.000000 | 0.000000 | 0.000000 | -0.015255 | 0.186338 | 0.000000 | 0.0 | 250.00 | 0.000 | ... | NaN | 0 | 0 | NaN | 250.00 | 0.000 | 0.000 | 30 | [] | 0.0366 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2013-11-18 21:00:00+00:00 | 518.629 | 0.254569 | 0.985996 | 0.104758 | 0.610014 | 0.170587 | 0.759821 | 0.0 | -22.13 | 518.629 | ... | 497.31452 | 0 | 0 | 1.268847 | -22.13 | 524.991 | 524.991 | 977 | [] | 0.0267 |
2013-11-19 21:00:00+00:00 | 519.549 | 0.254439 | 0.989676 | 0.105564 | 0.606515 | 0.170506 | 0.759747 | 0.0 | -22.13 | 519.549 | ... | 497.87012 | 0 | 0 | 1.271096 | -22.13 | 518.629 | 518.629 | 978 | [] | 0.0271 |
2013-11-20 21:00:00+00:00 | 515.000 | 0.254359 | 0.971480 | 0.103682 | 0.601490 | 0.170428 | 0.759980 | 0.0 | -22.13 | 515.000 | ... | 498.87022 | 0 | 0 | 1.255655 | -22.13 | 519.549 | 519.549 | 979 | [] | 0.0280 |
2013-11-21 21:00:00+00:00 | 521.135 | 0.254298 | 0.996020 | 0.105146 | 0.614411 | 0.170384 | 0.760374 | 0.0 | -22.13 | 521.135 | ... | 499.89400 | 0 | 0 | 1.274451 | -22.13 | 515.000 | 515.000 | 980 | [] | 0.0279 |
2013-11-22 21:00:00+00:00 | 519.799 | 0.254174 | 0.990676 | 0.103412 | 0.622487 | 0.170312 | 0.760102 | 0.0 | -22.13 | 519.799 | ... | 501.04596 | 0 | 0 | 1.269580 | -22.13 | 521.135 | 521.135 | 981 | [] | 0.0275 |
2013-11-25 21:00:00+00:00 | 523.740 | 0.254070 | 1.006440 | 0.105549 | 0.620872 | 0.170227 | 0.759989 | 0.0 | -22.13 | 523.740 | ... | 502.57062 | 0 | 0 | 1.281286 | -22.13 | 519.799 | 519.799 | 982 | [] | 0.0274 |
2013-11-26 21:00:00+00:00 | 533.400 | 0.254112 | 1.045080 | 0.110331 | 0.621321 | 0.170141 | 0.759944 | 0.0 | -22.13 | 533.400 | ... | 504.18508 | 0 | 0 | 1.310658 | -22.13 | 523.740 | 523.740 | 983 | [] | 0.0271 |
2013-11-27 21:00:00+00:00 | 545.960 | 0.254266 | 1.095320 | 0.115984 | 0.625269 | 0.170057 | 0.760316 | 0.0 | -22.13 | 545.960 | ... | 505.86462 | 0 | 0 | 1.348273 | -22.13 | 533.400 | 533.400 | 984 | [] | 0.0274 |
2013-11-29 18:00:00+00:00 | 556.070 | 0.254307 | 1.135760 | 0.120962 | 0.624192 | 0.169971 | 0.760108 | 0.0 | -22.13 | 556.070 | ... | 507.59484 | 0 | 0 | 1.377648 | -22.13 | 545.960 | 545.960 | 985 | [] | 0.0275 |
2013-12-02 21:00:00+00:00 | 551.230 | 0.254228 | 1.116400 | 0.118998 | 0.619975 | 0.169893 | 0.760319 | 0.0 | -22.13 | 551.230 | ... | 509.32550 | 0 | 0 | 1.362308 | -22.13 | 556.070 | 556.070 | 986 | [] | 0.0281 |
2013-12-03 21:00:00+00:00 | 566.322 | 0.254483 | 1.176768 | 0.127183 | 0.612976 | 0.169824 | 0.758969 | 0.0 | -22.13 | 566.322 | ... | 510.89610 | 0 | 0 | 1.405980 | -22.13 | 551.230 | 551.230 | 987 | [] | 0.0279 |
2013-12-04 21:00:00+00:00 | 565.000 | 0.254359 | 1.171480 | 0.126454 | 0.612796 | 0.169738 | 0.758985 | 0.0 | -22.13 | 565.000 | ... | 512.47088 | 0 | 0 | 1.401453 | -22.13 | 566.322 | 566.322 | 988 | [] | 0.0284 |
2013-12-05 21:00:00+00:00 | 567.901 | 0.254240 | 1.183084 | 0.128586 | 0.605707 | 0.169671 | 0.758628 | 0.0 | -22.13 | 567.901 | ... | 514.25420 | 0 | 0 | 1.409047 | -22.13 | 565.000 | 565.000 | 989 | [] | 0.0288 |
2013-12-06 21:00:00+00:00 | 560.020 | 0.254230 | 1.151560 | 0.122926 | 0.623654 | 0.169670 | 0.756423 | 0.0 | -22.13 | 560.020 | ... | 515.78664 | 0 | 0 | 1.384544 | -22.13 | 567.901 | 567.901 | 990 | [] | 0.0288 |
2013-12-09 21:00:00+00:00 | 566.430 | 0.254162 | 1.177200 | 0.125320 | 0.627782 | 0.169587 | 0.756591 | 0.0 | -22.13 | 566.430 | ... | 517.51628 | 0 | 0 | 1.402324 | -22.13 | 560.020 | 560.020 | 991 | [] | 0.0286 |
2013-12-10 21:00:00+00:00 | 565.550 | 0.254037 | 1.173680 | 0.125475 | 0.621949 | 0.169514 | 0.756569 | 0.0 | -22.13 | 565.550 | ... | 519.34762 | 0 | 0 | 1.399094 | -22.13 | 566.430 | 566.430 | 992 | [] | 0.0281 |
2013-12-11 21:00:00+00:00 | 561.360 | 0.253946 | 1.156920 | 0.125552 | 0.603733 | 0.169533 | 0.756539 | 0.0 | -22.13 | 561.360 | ... | 520.87226 | 0 | 0 | 1.386056 | -22.13 | 565.550 | 565.550 | 993 | [] | 0.0286 |
2013-12-12 21:00:00+00:00 | 560.540 | 0.253821 | 1.153640 | 0.125675 | 0.598439 | 0.169458 | 0.756523 | 0.0 | -22.13 | 560.540 | ... | 522.34870 | 0 | 0 | 1.382990 | -22.13 | 561.360 | 561.360 | 994 | [] | 0.0289 |
2013-12-13 21:00:00+00:00 | 554.430 | 0.253768 | 1.129200 | 0.122688 | 0.598259 | 0.169373 | 0.756590 | 0.0 | -22.13 | 554.430 | ... | 523.82522 | 0 | 0 | 1.363910 | -22.13 | 560.540 | 560.540 | 995 | [] | 0.0288 |
2013-12-16 21:00:00+00:00 | 557.500 | 0.253653 | 1.141480 | 0.122827 | 0.608220 | 0.169313 | 0.756618 | 0.0 | -22.13 | 557.500 | ... | 525.37070 | 0 | 0 | 1.372137 | -22.13 | 554.430 | 554.430 | 996 | [] | 0.0289 |
2013-12-17 21:00:00+00:00 | 554.990 | 0.253541 | 1.131440 | 0.122115 | 0.603105 | 0.169238 | 0.756709 | 0.0 | -22.13 | 554.990 | ... | 526.77212 | 0 | 0 | 1.364066 | -22.13 | 557.500 | 557.500 | 997 | [] | 0.0285 |
2013-12-18 21:00:00+00:00 | 550.770 | 0.253452 | 1.114560 | 0.117159 | 0.630474 | 0.169357 | 0.753604 | 0.0 | -22.13 | 550.770 | ... | 528.22456 | 0 | 0 | 1.350759 | -22.13 | 554.990 | 554.990 | 998 | [] | 0.0289 |
2013-12-19 21:00:00+00:00 | 544.460 | 0.253407 | 1.089320 | 0.114226 | 0.628589 | 0.169274 | 0.753776 | 0.0 | -22.13 | 544.460 | ... | 529.43848 | 0 | 0 | 1.330779 | -22.13 | 550.770 | 550.770 | 999 | [] | 0.0294 |
2013-12-20 21:00:00+00:00 | 549.020 | 0.253311 | 1.107560 | 0.116240 | 0.629217 | 0.169190 | 0.753765 | 0.0 | -22.13 | 549.020 | ... | 530.68296 | 0 | 0 | 1.343563 | -22.13 | 544.460 | 544.460 | 1000 | [] | 0.0289 |
2013-12-23 21:00:00+00:00 | 570.090 | 0.253944 | 1.191840 | 0.124970 | 0.637922 | 0.169122 | 0.755264 | 0.0 | -22.13 | 570.090 | ... | 532.28576 | 0 | 0 | 1.404465 | -22.13 | 549.020 | 549.020 | 1001 | [] | 0.0294 |
2013-12-24 18:00:00+00:00 | 567.670 | 0.253831 | 1.182160 | 0.123332 | 0.641511 | 0.169040 | 0.755169 | 0.0 | -22.13 | 567.670 | ... | 533.77594 | 0 | 0 | 1.396839 | -22.13 | 570.090 | 570.090 | 1002 | [] | 0.0299 |
2013-12-26 21:00:00+00:00 | 563.900 | 0.253735 | 1.167080 | 0.120571 | 0.649856 | 0.168971 | 0.754721 | 0.0 | -22.13 | 563.900 | ... | 535.13824 | 0 | 0 | 1.385202 | -22.13 | 567.670 | 567.670 | 1003 | [] | 0.0300 |
2013-12-27 21:00:00+00:00 | 560.090 | 0.253639 | 1.151840 | 0.118690 | 0.649767 | 0.168887 | 0.754761 | 0.0 | -22.13 | 560.090 | ... | 536.37594 | 0 | 0 | 1.373384 | -22.13 | 563.900 | 563.900 | 1004 | [] | 0.0302 |
2013-12-30 21:00:00+00:00 | 554.520 | 0.253575 | 1.129560 | 0.115997 | 0.649498 | 0.168803 | 0.754829 | 0.0 | -22.13 | 554.520 | ... | 537.43490 | 0 | 0 | 1.356115 | -22.13 | 560.090 | 560.090 | 1005 | [] | 0.0299 |
2013-12-31 21:00:00+00:00 | 561.020 | 0.253513 | 1.155560 | 0.118003 | 0.657304 | 0.168732 | 0.755130 | 0.0 | -22.13 | 561.020 | ... | 538.53658 | 0 | 0 | 1.374173 | -22.13 | 554.520 | 554.520 | 1006 | [] | 0.0304 |
1006 rows × 40 columns
%matplotlib inline
import pyfolio as pf
import pyfolio.plotting as plotting
import matplotlib.pyplot as plt
import pickle
import os
# because pyfolio can't download data from google / yahoo we change it's cache directory
# to fetch our presaved data
os.environ['PYFOLIO_CACHE_DIR'] = "/root/notebooks/.cache/"
results = pd.read_pickle("perf.pickle")
# use pyfolio to extract data
returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(results)
benchmark_rets = pf.utils.get_symbol_rets('SPY', returns.index[1], returns.index[-1])
fig = plt.figure(figsize=(20, 20))
plotting.plot_rolling_returns(returns,factor_returns=benchmark_rets)
<matplotlib.axes._subplots.AxesSubplot at 0x7f0904060588>
/root/miniconda3/envs/zipline/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext]))
import numpy
def vol(returns):
# Return the standard deviation of returns
return numpy.std(returns)
def information_ratio(returns, benchmark):
diff = returns - benchmark
return numpy.mean(diff) / vol(diff)
import empyrical as ep
import matplotlib.pyplot as plt
import numpy as np
yearly_returns = ep.aggregate_returns(returns, 'yearly')
yearly_bench = ep.aggregate_returns(benchmark_rets, 'yearly')
years = np.arange(yearly_returns.index.size)
bar_width = 0.35
# plot returns
fig, ax = plt.subplots()
plt.bar(years, yearly_returns, bar_width, alpha=0.5, label='Strategy')
plt.bar(years+bar_width, yearly_bench, bar_width, alpha=0.5, label='Benchmark')
plt.legend(loc='upper right')
plt.xticks(years + bar_width/2, yearly_returns.index.values)
vals = ax.get_yticks()
ax.set_yticklabels(['{0:.0f}%'.format(x*100) for x in vals])
for i, v in enumerate(yearly_returns):
ax.text(i-0.18, v+0.01, "{0:.1f}%".format(v * 100), color='blue')
for i, v in enumerate(yearly_bench):
ax.text(i+0.18, v+0.01, "{0:.1f}%".format(v * 100), color='green')
plt.show()
# plot years history
monthly_returns = ep.aggregate_returns(returns, 'monthly').values[-12:]
monthly_bench = ep.aggregate_returns(benchmark_rets, 'monthly').values[-12:]
months = np.arange(12)
fig, ax = plt.subplots()
plt.bar(months, monthly_returns, bar_width, alpha=0.5, label='Strategy')
plt.bar(months+bar_width, monthly_bench, bar_width, alpha=0.5, label='Benchmark')
plt.legend(loc='upper right')
vals = ax.get_yticks()
ax.set_yticklabels(['{0:.0f}%'.format(x*100) for x in vals])
plt.show()
# plot return / risk plots
fig, ax = plt.subplots()
ax.set_xlabel('Daily Standard Deviation')
ax.set_ylabel('Daily Return')
plt.plot(returns.std(), returns.mean(), 'o', alpha=0.5, label='Strategy')
plt.plot(benchmark_rets.std(), benchmark_rets.mean(), 'o', alpha=0.5, label='Benchmark')
plt.legend(loc='upper left')
plt.show()
# numbers for table
lastDayOfBenchmark = returns.index[-1]
firstDayOfLastYearOfBenchmark = lastDayOfBenchmark.date().replace(month=1, day=1)
returnsYTD = returns[firstDayOfLastYearOfBenchmark:lastDayOfBenchmark]
benchmarkYTD = benchmark_rets[firstDayOfLastYearOfBenchmark:lastDayOfBenchmark]
absReturnStrategy = ep.cum_returns_final(returns)
absReturnBenchmark = ep.cum_returns_final(benchmark_rets)
absReturnYTD = ep.cum_returns_final(returnsYTD)
absBenchmarkYTD = ep.cum_returns_final(benchmarkYTD)
relReturn = absReturnStrategy - absReturnBenchmark
relReturnYTD = absReturnYTD - absBenchmarkYTD
beta = ep.beta(returns, benchmark_rets)
betaYTD = ep.beta(returnsYTD, benchmark_rets)
stdStrategy = returns.std()
stdBenchmark = benchmark_rets.std()
stdStrategyYTD = returnsYTD.std()
stdBenchmarkYTD = benchmarkYTD.std()
sharpeRatioStrategy = ep.sharpe_ratio(returns)
sharpeRatioBenchmark = ep.sharpe_ratio(benchmark_rets)
sharpeRatioStrategyYTD = ep.sharpe_ratio(returnsYTD)
sharpeRatioBenchmarkYTD = ep.sharpe_ratio(benchmarkYTD)
maxDrawdownStrategy = ep.max_drawdown(returns)
maxDrawdownBenchmark = ep.max_drawdown(benchmark_rets)
maxDrawdownStrategyYTD = ep.max_drawdown(returnsYTD)
maxDrawdownBenchmarkYTD = ep.max_drawdown(benchmarkYTD)
irStrategy = information_ratio(returns, benchmark_rets)
irStrategyYTD = information_ratio(returnsYTD, benchmarkYTD)
from sklearn.metrics import r2_score
r2Strategy = r2_score(benchmark_rets, returns[0:benchmark_rets.size])
r2StrategyYTD = r2_score(benchmarkYTD, returnsYTD)
# display data table
from IPython.display import HTML, display
data = [
['', 'Strategy Overall', 'Bench Overall', 'Strategy YTD', 'Bench YTD'],
['Abs. Return', "{:.2%}".format(absReturnStrategy), "{:.2%}".format(absReturnBenchmark), "{:.2%}".format(absReturnYTD), "{:.2%}".format(absBenchmarkYTD)],
['Rel. Return', "{:.2%}".format(relReturn), 'N/A', "{:.2%}".format(relReturnYTD), 'N/A'],
['Beta', "{0:.2f}".format(beta), 'N/A', "{0:.2f}".format(betaYTD), 'N/A'],
['Std. Dev', "{:.2%}".format(stdStrategy), "{:.2%}".format(stdBenchmark), "{:.2%}".format(stdStrategyYTD), "{:.2%}".format(stdBenchmarkYTD)],
['Sharpe', "{0:.2f}".format(sharpeRatioStrategy), "{0:.2f}".format(sharpeRatioBenchmark), "{0:.2f}".format(sharpeRatioStrategyYTD), "{0:.2f}".format(sharpeRatioBenchmarkYTD)],
['Draw Down', "{:.2%}".format(maxDrawdownStrategy), "{:.2%}".format(maxDrawdownBenchmark), "{:.2%}".format(maxDrawdownStrategyYTD), "{:.2%}".format(maxDrawdownBenchmarkYTD)],
['IR', "{0:.2f}".format(irStrategy), 'N/A', "{0:.2f}".format(irStrategyYTD), 'N/A'],
['R2', "{0:.2f}".format(r2Strategy), 'N/A', "{0:.2f}".format(r2StrategyYTD), 'N/A'],
]
display(HTML(
'<table><tr>{}</tr></table>'.format(
'</tr><tr>'.join(
'<td>{}</td>'.format('</td><td>'.join(str(_) for _ in row)) for row in data)
)
))
data = [
[''] + list(range(1,13)),
['Strategy'] + ["{:.2%}".format(ret) for ret in list(monthly_returns)],
['Benchmark'] + ["{:.2%}".format(ret) for ret in list(monthly_bench)],
['Rel. Return'] + ["{:.2%}".format(ret) for ret in list(monthly_returns - monthly_bench)],
]
display(HTML(
'<table><tr>{}</tr></table>'.format(
'</tr><tr>'.join(
'<td>{}</td>'.format('</td><td>'.join(str(_) for _ in row)) for row in data)
)
))
/root/miniconda3/envs/zipline/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext]))
Strategy Overall | Bench Overall | Strategy YTD | Bench YTD | |
Abs. Return | 115.56% | 76.92% | 11.63% | 32.31% |
Rel. Return | 38.64% | N/A | -20.67% | N/A |
Beta | 0.76 | N/A | 0.14 | N/A |
Std. Dev | 1.60% | 1.06% | 0.96% | 0.70% |
Sharpe | 0.88 | 0.93 | 0.80 | 2.58 |
Draw Down | -29.60% | -18.61% | -12.86% | -5.55% |
IR | 0.02 | N/A | -0.06 | N/A |
R2 | -2.45 | N/A | -1.63 | N/A |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
Strategy | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | -1.11% | -2.57% | -2.25% | 10.11% | 6.67% | 0.93% |
Benchmark | 5.12% | 1.28% | 3.80% | 1.92% | 2.36% | -1.33% | 5.17% | -3.00% | 3.16% | 4.63% | 2.96% | 2.59% |
Rel. Return | -5.12% | -1.28% | -3.80% | -1.92% | -2.36% | 1.33% | -6.28% | 0.43% | -5.41% | 5.48% | 3.70% | -1.67% |