syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
How do I demo Quantopian Zipline?

I found out about Zipline at this URL:

http://quantopian.com

The first step to learning Zipline is to write a shell script. Here is a demo script:
#!/bin/bash

# /a/ks/zl/zl20.bash

run_algo.py -f zl20.py --data-frequency daily \
  --start 1990-01-01 \
  --end 2014-12-11   \
  --capital_base 360000 \
  --output zl20_output.pickle \
  --symbols ^GSPC

exit

Next, write a python script. Here is a demo script:
# zl20.py

# Buy on 27th sell on 4th

from zipline     import api
from collections import deque
import numpy  as np
import pandas as pd
import pdb
import datetime

def initialize(context):
    ct        = context
    ct.tkr    = api.symbol('^GSPC')

def handle_data(context, data):
    ct = context
    adate = data[ct.tkr].datetime
    dom   = int(datetime.datetime.strftime(adate,'%d'))
    if (dom > 0) & (dom < 5):
        api.order_target_percent(ct.tkr, 1.0)
    if (dom > 4) & (dom < 27):
        api.order_target_percent(ct.tkr, 0.0)
    if dom > 26:
        api.order_target_percent(ct.tkr, 1.0)
    api.record(GSPC=data[api.symbol('^GSPC')].price*1000.0)
The above script implements a very simple trading algorithm.

Then, write another python script which helps us visualize the results:
# /a/ks/zl/zl20_analyze.py

# This file is called by:
# zl20.bash
# After zl20.bash runs zl20.py
# The calling is done automatically for me by the zipline api.

import matplotlib.pyplot as plt

def analyze(context, perf):
    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax2 = plt.subplot(212, sharex=ax1)
    perf.GSPC.plot(ax=ax2)
    plt.gcf().set_size_inches(18, 8)
    plt.show()
Next, run the shell script which should call both the Python scripts. When the shell script runs it should look like this:
dan@hp /a/ks/zl $ ./zl20.bash 
^GSPC

from zipline     import api
from collections import deque
import numpy  as np
import pandas as pd
import pdb
import datetime

def initialize(context):
    ct        = context
    ct.tkr    = api.symbol('^GSPC')

def handle_data(context, data):
    ct = context
    adate = data[ct.tkr].datetime
    dom   = int(datetime.datetime.strftime(adate,'%d'))
    if (dom > 0) & (dom < 5):
        api.order_target_percent(ct.tkr, 1.0)
    if (dom > 4) & (dom < 27):
        api.order_target_percent(ct.tkr, 0.0)
    if dom > 26:
        api.order_target_percent(ct.tkr, 1.0)
    api.record(GSPC=data[api.symbol('^GSPC')].price*1000.0)

import matplotlib.pyplot as plt

def analyze(context, perf):
    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax2 = plt.subplot(212, sharex=ax1)
    perf.GSPC.plot(ax=ax2)
    plt.gcf().set_size_inches(18, 8)
    plt.show()
[2014-12-14 09:56] INFO: Performance: Simulated 6288 trading days out of 6288.
[2014-12-14 09:56] INFO: Performance: first open: 1990-01-02 14:31:00+00:00
[2014-12-14 09:56] INFO: Performance: last close: 2014-12-11 21:00:00+00:00
Also, a plot should appear which looks like this:

The top-plot shows performance of the trading-algo.

The bottom-plot shows performance of buy and hold strategy.

When I analyze the above plot I see that the trading-algo fails to perform better than buy and hold between 1990 and today.

If, however, I look at the duration between 2001 and today, I see that the trading-algo performs better than buy and hold.

Finally, between 2009 and today, I see that buy and hold was more lucrative.

Zipline is well suited for backtesting trading algos for GSPC which is the symbol for the S and P 500.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me