Zipline is an excellent system for trading system research and development. In this article, we will only go over the specifics to install and test the installation. Zipline is a complex platform with multiple parts. If you run into any errors, scroll to the troubleshooting installation issues.
You can download Miniconda from the website, or use wget at the terminal. When running the shell script, follow the prompts.
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh $ chmod +x Miniconda3-latest-Linux-x86_64.sh $ ./Miniconda3-latest-Linux-x86_64.sh ... follow the prompts $ conda update conda
We’ll create an environment to install Zipline into, we will then activate it setting it as our current environment, and then install all of the requisite packages. You must explicitly set the python version as shown below.
$ conda create -n env_zipline python=3.5 $ conda activate env_zipline $ conda install -c Quantopian zipline
Let’s add additional useful libraries and ingest data from Quandl. Notice that I am using pip to install Pyfolio. The version of Pyfolio installed with Conda was significantly out of date, and using it will cause issues. If any packages are missing during installation, use conda or pip install in that order. The last line will require that you have a Quandl account with an associated API key. Replace your_key_here with your Quandl API key. Depending upon your internet connection and processing speed, the zipline ingest can take some time.
$ conda install jupyter matplotlib $ conda install -c conda-forge alphalens $ pip install pyfolio $ QUANDL_API_KEY=your_key_here zipline ingest -b quandl $ zipline ingest -b quantopian-quandl
Create a file named dual_moving_average.py with the following:
from zipline.api import order_target, record, symbol def initialize(context): context.i = 0 context.asset = symbol('AAPL') def handle_data(context, data): # Skip first 300 days to get full windows context.i += 1 if context.i < 300: return # Compute averages # data.history() has to be called with the same params # from above and returns a pandas dataframe. short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean() long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="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.asset, 100) elif short_mavg < long_mavg: order_target(context.asset, 0) # Save values for later inspection record(AAPL=data.current(context.asset, 'price'), short_mavg=short_mavg, long_mavg=long_mavg)
Run Zipline using the following command producing the below output:
$ zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle INFO: zipline.finance.metrics.tracker: Simulated 1007 trading days first open: 2014-01-02 14:31:00+00:00 last close: 2017-12-29 21:00:00+00:00
If your system simulated the 1007 [trading days/how-many-trading-days-in-a-year), you’re all set to follow along with the next articles. If you run into any issues, please comment below or message me on Twitter and I’ll try to help.
If you run into any issues while installing Zipline, check the Zipline Github Issues and Zipline Google Groups pages. Also, make sure that your versions are up-to-date and that you have the Quandl bundles installed. We will go over how to create our own bundles in a later article.
$ conda list $ zipline bundles