Files
catalyst/zipline/examples/buyapple.ipynb
T

159 KiB
Raw Blame History

In [1]:
%matplotlib inline
%load_ext zipline
In [2]:
%%zipline --start=2011-1-1 --end=2013-1-1

from zipline.api import order, record, symbol
import matplotlib.pyplot as plt

def initialize(context):
    pass

def handle_data(context, data):
    order(symbol('AAPL'), 10)
    record(AAPL=data[symbol('AAPL')].price)
    
def analyze(context, perf):
    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax2 = plt.subplot(212, sharex=ax1)
    perf.AAPL.plot(ax=ax2)
    plt.gcf().set_size_inches(18, 8)
    plt.show()
Out [2]:
AAPL algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ... short_exposure short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2011-01-03 21:00:00+00:00 329.570 NaN 0.000000e+00 NaN 0.011315 NaN NaN 0.00 10000000.00 0.00 ... 0 0 0 NaN 10000000.00 0.00 0.00 1 [] 0.0336
2011-01-04 21:00:00+00:00 331.290 0.000001 -1.000000e-07 -0.000023 0.009987 0.141748 0.000008 -3313.90 9996686.10 3312.90 ... 0 0 0 -11.224972 10000000.00 0.00 0.00 2 [{u'commission': None, u'amount': 10, u'sid': ... 0.0336
2011-01-05 21:00:00+00:00 334.000 0.000024 2.510000e-06 0.000201 0.015044 0.100231 0.000008 -3341.00 9993345.10 6680.00 ... 0 0 0 230.045324 9996686.10 3312.90 3312.90 3 [{u'commission': None, u'amount': 10, u'sid': ... 0.0350
2011-01-06 21:00:00+00:00 333.730 0.000023 1.870000e-06 0.000059 0.012889 0.099481 0.000072 -3338.30 9990006.80 10011.90 ... 0 0 0 22.913722 9993345.10 6680.00 6680.00 4 [{u'commission': None, u'amount': 10, u'sid': ... 0.0344
2011-01-07 21:00:00+00:00 336.120 0.000051 8.940000e-06 0.000524 0.011021 0.093360 -0.000132 -3362.20 9986644.60 13444.80 ... 0 0 0 97.979577 9990006.80 10011.90 10011.90 5 [{u'commission': None, u'amount': 10, u'sid': ... 0.0334
2011-01-10 21:00:00+00:00 342.455 0.000159 3.418000e-05 0.001676 0.009629 0.086675 -0.000592 -3425.55 9983219.05 17122.75 ... 0 0 0 341.961345 9986644.60 13444.80 13444.80 6 [{u'commission': None, u'amount': 10, u'sid': ... 0.0332
2011-01-11 21:00:00+00:00 341.640 0.000156 3.000500e-05 0.001415 0.013390 0.080133 -0.000694 -3417.40 9979801.65 20498.40 ... 0 0 0 42.612419 9983219.05 17122.75 17122.75 7 [{u'commission': None, u'amount': 10, u'sid': ... 0.0337
2011-01-12 21:00:00+00:00 344.420 0.000160 4.658500e-05 0.001574 0.022518 0.084200 -0.000152 -3445.20 9976356.45 24109.40 ... 0 0 0 61.885447 9979801.65 20498.40 20498.40 8 [{u'commission': None, u'amount': 10, u'sid': ... 0.0340
2011-01-13 21:00:00+00:00 345.680 0.000151 5.530500e-05 0.001660 0.020769 0.082298 -0.000193 -3457.80 9972898.65 27654.40 ... 0 0 0 69.267298 9976356.45 24109.40 24109.40 9 [{u'commission': None, u'amount': 10, u'sid': ... 0.0334
2011-01-14 21:00:00+00:00 348.480 0.000164 7.760500e-05 0.001859 0.028307 0.081684 0.000136 -3485.80 9969412.85 31363.20 ... 0 0 0 92.208331 9972898.65 27654.40 27654.40 10 [{u'commission': None, u'amount': 10, u'sid': ... 0.0335
2011-01-18 21:00:00+00:00 340.650 0.000406 7.035000e-06 -0.000216 0.029722 0.077794 0.000559 -3407.50 9966005.35 34065.00 ... 0 0 0 0.476546 9969412.85 31363.20 31363.20 11 [{u'commission': None, u'amount': 10, u'sid': ... 0.0339
2011-01-19 21:00:00+00:00 338.840 0.000396 -1.116500e-05 -0.000604 0.019306 0.094544 0.000911 -3389.40 9962615.95 37272.40 ... 0 0 0 -0.700708 9966005.35 34065.00 34065.00 12 [{u'commission': None, u'amount': 10, u'sid': ... 0.0337
2011-01-20 21:00:00+00:00 332.680 0.000481 -7.902500e-05 -0.002002 0.017986 0.091418 0.001344 -3327.80 9959288.15 39921.60 ... 0 0 0 -3.490818 9962615.95 37272.40 37272.40 13 [{u'commission': None, u'amount': 10, u'sid': ... 0.0347
2011-01-21 21:00:00+00:00 326.720 0.000539 -1.506450e-04 -0.003148 0.020443 0.087940 0.001184 -3268.20 9956019.95 42473.60 ... 0 0 0 -5.207546 9959288.15 39921.60 39921.60 14 [{u'commission': None, u'amount': 10, u'sid': ... 0.0344
2011-01-24 21:00:00+00:00 337.450 0.000805 -1.125500e-05 -0.001339 0.026399 0.086618 0.002605 -3375.50 9952644.45 47243.00 ... 0 0 0 -0.375267 9956019.95 42473.60 42473.60 15 [{u'commission': None, u'amount': 10, u'sid': ... 0.0343
2011-01-25 21:00:00+00:00 341.400 0.000809 4.394500e-05 -0.000313 0.026669 0.083889 0.002405 -3415.00 9949229.45 51210.00 ... 0 0 0 1.421565 9952644.45 47243.00 47243.00 16 [{u'commission': None, u'amount': 10, u'sid': ... 0.0335
2011-01-26 21:00:00+00:00 343.850 0.000794 8.059500e-05 0.000026 0.031003 0.081822 0.002563 -3439.50 9945789.95 55016.00 ... 0 0 0 2.528766 9949229.45 51210.00 51210.00 17 [{u'commission': None, u'amount': 10, u'sid': ... 0.0345
2011-01-27 21:00:00+00:00 343.210 0.000773 7.025500e-05 -0.000193 0.033316 0.079396 0.002547 -3433.10 9942356.85 58345.70 ... 0 0 0 2.134760 9945789.95 55016.00 55016.00 18 [{u'commission': None, u'amount': 10, u'sid': ... 0.0342
2011-01-28 21:00:00+00:00 336.100 0.000878 -5.071500e-05 -0.001538 0.014869 0.105327 0.004306 -3362.00 9938994.85 60498.00 ... 0 0 0 -1.069324 9942356.85 58345.70 58345.70 19 [{u'commission': None, u'amount': 10, u'sid': ... 0.0336
2011-01-31 21:00:00+00:00 339.320 0.000881 7.145000e-06 -0.001217 0.022646 0.105374 0.004547 -3394.20 9935600.65 64470.80 ... 0 0 0 0.147515 9938994.85 60498.00 60498.00 20 [{u'commission': None, u'amount': 10, u'sid': ... 0.0342
2011-02-01 21:00:00+00:00 345.030 0.000937 1.155350e-04 -0.001015 0.039717 0.115978 0.005065 -3451.30 9932149.35 69006.00 ... 0 0 0 2.318921 9935600.65 64470.80 64470.80 21 [{u'commission': None, u'amount': 10, u'sid': ... 0.0348
2011-02-02 21:00:00+00:00 344.320 0.000917 1.012350e-04 -0.000969 0.036887 0.114251 0.005050 -3444.20 9928705.15 72307.20 ... 0 0 0 1.978514 9932149.35 69006.00 69006.00 22 [{u'commission': None, u'amount': 10, u'sid': ... 0.0352
2011-02-03 21:00:00+00:00 343.440 0.000899 8.265500e-05 -0.001254 0.039328 0.111647 0.005035 -3435.40 9925269.75 75556.80 ... 0 0 0 1.571015 9928705.15 72307.20 72307.20 23 [{u'commission': None, u'amount': 10, u'sid': ... 0.0358
2011-02-04 21:00:00+00:00 346.500 0.000903 1.498750e-04 -0.000675 0.042325 0.109260 0.005094 -3466.00 9921803.75 79695.00 ... 0 0 0 2.788093 9925269.75 75556.80 75556.80 24 [{u'commission': None, u'amount': 10, u'sid': ... 0.0368
2011-02-07 21:00:00+00:00 351.880 0.000959 2.735150e-04 0.000099 0.048830 0.107905 0.005462 -3519.80 9918283.95 84451.20 ... 0 0 0 4.984606 9921803.75 79695.00 79695.00 25 [{u'commission': None, u'amount': 10, u'sid': ... 0.0368
2011-02-08 21:00:00+00:00 355.200 0.000964 3.530950e-04 0.000589 0.053219 0.105957 0.005571 -3553.00 9914730.95 88800.00 ... 0 0 0 6.309501 9918283.95 84451.20 84451.20 26 [{u'commission': None, u'amount': 10, u'sid': ... 0.0375
2011-02-09 21:00:00+00:00 358.160 0.000963 4.269950e-04 0.001566 0.050285 0.104931 0.005217 -3582.60 9911148.35 93121.60 ... 0 0 0 7.486993 9914730.95 88800.00 88800.00 27 [{u'commission': None, u'amount': 10, u'sid': ... 0.0365
2011-02-10 21:00:00+00:00 354.540 0.001001 3.327750e-04 0.000584 0.051072 0.103021 0.005313 -3546.40 9907601.95 95725.80 ... 0 0 0 5.040908 9911148.35 93121.60 93121.60 28 [{u'commission': None, u'amount': 10, u'sid': ... 0.0370
2011-02-11 21:00:00+00:00 356.850 0.000994 3.950450e-04 0.000804 0.056860 0.101753 0.005409 -3569.50 9904032.45 99918.00 ... 0 0 0 5.879790 9907601.95 95725.80 95725.80 29 [{u'commission': None, u'amount': 10, u'sid': ... 0.0364
2011-02-14 21:00:00+00:00 359.180 0.000988 4.601850e-04 0.001206 0.059381 0.099992 0.005427 -3592.80 9900439.65 104162.20 ... 0 0 0 6.733874 9904032.45 99918.00 99918.00 30 [{u'commission': None, u'amount': 10, u'sid': ... 0.0362
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2012-11-16 21:00:00+00:00 527.678 0.040048 2.943771e-02 0.011145 0.081295 0.191082 0.085310 -5277.78 7803736.93 2490640.16 ... 0 0 0 0.581207 7809014.71 2475670.20 2475670.20 473 [{u'commission': None, u'amount': 10, u'sid': ... 0.0158
2012-11-19 21:00:00+00:00 565.730 0.041965 4.739815e-02 0.019182 0.102772 0.191416 0.089783 -5658.30 7798078.63 2675902.90 ... 0 0 0 0.912542 7803736.93 2490640.16 2490640.16 474 [{u'commission': None, u'amount': 10, u'sid': ... 0.0161
2012-11-20 21:00:00+00:00 560.913 0.041954 4.511961e-02 0.017957 0.103503 0.191214 0.089770 -5610.13 7792468.50 2658727.62 ... 0 0 0 0.868836 7798078.63 2675902.90 2675902.90 475 [{u'commission': None, u'amount': 10, u'sid': ... 0.0166
2012-11-21 21:00:00+00:00 561.700 0.041910 4.549255e-02 0.017997 0.106064 0.191018 0.089773 -5618.00 7786850.50 2668075.00 ... 0 0 0 0.874687 7792468.50 2658727.62 2658727.62 476 [{u'commission': None, u'amount': 10, u'sid': ... 0.0169
2012-11-23 18:00:00+00:00 571.500 0.041986 5.014745e-02 0.019648 0.120472 0.191042 0.090365 -5716.00 7781134.50 2720340.00 ... 0 0 0 0.958079 7786850.50 2668075.00 2668075.00 477 [{u'commission': None, u'amount': 10, u'sid': ... 0.0170
2012-11-26 21:00:00+00:00 589.530 0.042349 5.872963e-02 0.024033 0.118198 0.190850 0.090085 -5896.30 7775238.20 2812058.10 ... 0 0 0 1.111665 7781134.50 2720340.00 2720340.00 478 [{u'commission': None, u'amount': 10, u'sid': ... 0.0166
2012-11-27 21:00:00+00:00 584.780 0.042336 5.646378e-02 0.023094 0.112353 0.190692 0.090226 -5848.80 7769389.40 2795248.40 ... 0 0 0 1.068403 7775238.20 2812058.10 2812058.10 479 [{u'commission': None, u'amount': 10, u'sid': ... 0.0164
2012-11-28 21:00:00+00:00 582.940 0.042297 5.558416e-02 0.022251 0.121092 0.190572 0.090047 -5830.40 7763559.00 2792282.60 ... 0 0 0 1.051351 7769389.40 2795248.40 2795248.40 480 [{u'commission': None, u'amount': 10, u'sid': ... 0.0163
2012-11-29 21:00:00+00:00 589.360 0.042302 5.865924e-02 0.023518 0.125879 0.190395 0.090187 -5894.60 7757664.40 2828928.00 ... 0 0 0 1.105093 7763559.00 2792282.60 2792282.60 481 [{u'commission': None, u'amount': 10, u'sid': ... 0.0162
2012-11-30 21:00:00+00:00 585.280 0.042282 5.670074e-02 0.022494 0.126062 0.190197 0.090191 -5853.80 7751810.60 2815196.80 ... 0 0 0 1.067924 7757664.40 2828928.00 2828928.00 482 [{u'commission': None, u'amount': 10, u'sid': ... 0.0162
2012-12-03 21:00:00+00:00 586.190 0.042239 5.713835e-02 0.022891 0.120718 0.190034 0.090136 -5862.90 7745947.70 2825435.80 ... 0 0 0 1.074588 7751810.60 2815196.80 2815196.80 483 [{u'commission': None, u'amount': 10, u'sid': ... 0.0163
2012-12-04 21:00:00+00:00 575.845 0.042339 5.215196e-02 0.020458 0.118802 0.189843 0.090272 -5759.45 7740188.25 2781331.35 ... 0 0 0 0.977756 7745947.70 2825435.80 2825435.80 484 [{u'commission': None, u'amount': 10, u'sid': ... 0.0162
2012-12-05 21:00:00+00:00 538.792 0.044058 3.425526e-02 0.011528 0.120575 0.189649 0.089953 -5388.92 7734799.33 2607753.28 ... 0 0 0 0.605002 7740188.25 2781331.35 2781331.35 485 [{u'commission': None, u'amount': 10, u'sid': ... 0.0160
2012-12-06 21:00:00+00:00 547.244 0.044101 3.834593e-02 0.013389 0.124280 0.189466 0.090110 -5473.44 7729325.89 2654133.40 ... 0 0 0 0.671627 7734799.33 2607753.28 2607753.28 486 [{u'commission': None, u'amount': 10, u'sid': ... 0.0159
2012-12-07 21:00:00+00:00 533.250 0.044312 3.155874e-02 0.009863 0.127564 0.189280 0.089851 -5333.50 7723992.39 2591595.00 ... 0 0 0 0.553361 7729325.89 2654133.40 2654133.40 487 [{u'commission': None, u'amount': 10, u'sid': ... 0.0164
2012-12-10 21:00:00+00:00 529.820 0.044284 2.989166e-02 0.008993 0.127946 0.189086 0.089851 -5299.20 7718693.19 2580223.40 ... 0 0 0 0.525320 7723992.39 2591595.00 2591595.00 488 [{u'commission': None, u'amount': 10, u'sid': ... 0.0163
2012-12-11 21:00:00+00:00 541.388 0.044408 3.552517e-02 0.011454 0.135333 0.188945 0.090287 -5414.88 7713278.31 2641973.44 ... 0 0 0 0.616351 7718693.19 2580223.40 2580223.40 489 [{u'commission': None, u'amount': 10, u'sid': ... 0.0166
2012-12-12 21:00:00+00:00 539.000 0.044371 3.435973e-02 0.010831 0.135842 0.188752 0.090285 -5391.00 7707887.31 2635710.00 ... 0 0 0 0.596696 7713278.31 2641973.44 2641973.44 490 [{u'commission': None, u'amount': 10, u'sid': ... 0.0172
2012-12-13 21:00:00+00:00 529.690 0.044441 2.980704e-02 0.008813 0.128662 0.188619 0.090656 -5297.90 7702589.41 2595481.00 ... 0 0 0 0.519853 7707887.31 2635710.00 2635710.00 491 [{u'commission': None, u'amount': 10, u'sid': ... 0.0174
2012-12-14 21:00:00+00:00 509.794 0.044917 2.005790e-02 0.004093 0.123994 0.188454 0.091242 -5098.94 7697490.47 2503088.54 ... 0 0 0 0.353785 7702589.41 2595481.00 2595481.00 492 [{u'commission': None, u'amount': 10, u'sid': ... 0.0172
2012-12-17 21:00:00+00:00 518.830 0.044977 2.449448e-02 0.005709 0.137337 0.188444 0.091783 -5189.30 7692301.17 2552643.60 ... 0 0 0 0.423858 7697490.47 2503088.54 2503088.54 493 [{u'commission': None, u'amount': 10, u'sid': ... 0.0178
2012-12-18 21:00:00+00:00 533.900 0.045223 3.190882e-02 0.008763 0.150401 0.188421 0.092771 -5340.00 7686961.17 2632127.00 ... 0 0 0 0.540504 7692301.17 2552643.60 2552643.60 494 [{u'commission': None, u'amount': 10, u'sid': ... 0.0184
2012-12-19 21:00:00+00:00 526.310 0.045254 2.816685e-02 0.007229 0.141670 0.188315 0.093108 -5264.10 7681697.07 2599971.40 ... 0 0 0 0.479739 7686961.17 2632127.00 2632127.00 495 [{u'commission': None, u'amount': 10, u'sid': ... 0.0182
2012-12-20 21:00:00+00:00 521.730 0.045237 2.590423e-02 0.005855 0.147936 0.188161 0.092905 -5218.30 7676478.77 2582563.50 ... 0 0 0 0.443299 7681697.07 2599971.40 2599971.40 496 [{u'commission': None, u'amount': 10, u'sid': ... 0.0181
2012-12-21 21:00:00+00:00 519.330 0.045200 2.471613e-02 0.005694 0.137170 0.188099 0.092949 -5194.30 7671284.47 2575876.80 ... 0 0 0 0.424116 7676478.77 2582563.50 2582563.50 497 [{u'commission': None, u'amount': 10, u'sid': ... 0.0177
2012-12-24 18:00:00+00:00 520.168 0.045155 2.513167e-02 0.006004 0.134395 0.187920 0.092924 -5202.68 7666081.79 2585234.96 ... 0 0 0 0.430192 7671284.47 2575876.80 2575876.80 498 [{u'commission': None, u'amount': 10, u'sid': ... 0.0179
2012-12-26 21:00:00+00:00 512.999 0.045179 2.156858e-02 0.004444 0.128964 0.187766 0.093148 -5130.99 7660950.80 2554735.02 ... 0 0 0 0.372948 7666081.79 2585234.96 2585234.96 499 [{u'commission': None, u'amount': 10, u'sid': ... 0.0177
2012-12-27 21:00:00+00:00 515.059 0.045139 2.259436e-02 0.005000 0.127588 0.187581 0.093124 -5151.59 7655799.21 2570144.41 ... 0 0 0 0.388592 7660950.80 2554735.02 2554735.02 500 [{u'commission': None, u'amount': 10, u'sid': ... 0.0174
2012-12-28 21:00:00+00:00 509.589 0.045135 1.986473e-02 0.004146 0.115128 0.187566 0.093394 -5096.89 7650702.32 2547945.00 ... 0 0 0 0.345046 7655799.21 2570144.41 2570144.41 501 [{u'commission': None, u'amount': 10, u'sid': ... 0.0173
2012-12-31 21:00:00+00:00 532.172 0.045762 3.115613e-02 0.008719 0.134021 0.187750 0.095640 -5322.72 7645379.60 2666181.72 ... 0 0 0 0.520644 7650702.32 2547945.00 2547945.00 502 [{u'commission': None, u'amount': 10, u'sid': ... 0.0178

502 rows × 39 columns

In [ ]: