Zipline is a Pythonic algorithmic trading library that’s easy to understand and use. It’s frequently used for backtesting trading algorithms and is often integrated into larger trading systems. In this article, we'll guide you through the installation and setup of Zipline in modern Python environments.
Why Choose Zipline?
- Simplicity and Pythonic Nature: Zipline is easy to set up and use due to its Pythonic nature.
- Strong Ecosystem: It integrates smoothly with the Python data stack, like pandas and numpy.
- Community Support: It has a strong community and extensive documentation supporting beginners and experts alike.
Setting Up Your Environment
Before you install Zipline, ensure that your system environment is suitable.
Step 1: Installing Anaconda
Anaconda is one of the easiest ways to manage scientific packages, making it ideal for ML and trading applications:
# Download Anaconda from the official website and follow the installation instructions.
Once installed, you can create isolated Python environments easily, which helps in managing package dependencies without conflicts:
conda create -n myenv python=3.8
Activate your new Anaconda environment:
conda activate myenv
Step 2: Installing Dependencies
While in your environment, install necessary dependencies that Zipline will require:
conda install numpy pandas scipy matplotlib
Pip is also an option but note that conda resolves environment complexities better for numeric, scientific, and ML applications.
Installing Zipline
There are multiple ways to install Zipline, however, using with Conda-Forge is recommended as it avoids potential binary dependencies issues:
conda install -c conda-forge zipline
Alternatively, you could use pip but ensure the above scientific packages are installed:
pip install zipline-reloaded
After installing Zipline, verify successful installation by using the simple Zipline command:
zipline version
Setting Up Data
Zipline needs historical data to backtest trading strategies. It provides a clean API to download and manage financial datasets (Zipline runs simulations assuming daily bunched data).
from zipline.data import bundles
bundles.ingest('quandl')
Upon setup, you can ingest data bundles that will be stored and easily accessed for backtesting. In this example, Quandl data, which is free and readily available, is used.
Backtesting a Simple Strategy
Once Zipline is set up, let’s write a simple moving average cross over strategy and backtest it.
First, define your strategy inside a Python script:
from zipline.api import order_target, record, symbol
def initialize(context):
context.asset = symbol('AAPL')
def handle_data(context, data):
short_mavg = data.history(context.asset, 'price', 5, '1d').mean()
long_mavg = data.history(context.asset, 'price', 30, '1d').mean()
if short_mavg > long_mavg:
order_target(context.asset, 10)
else:
order_target(context.asset, 0)
record(AAPL=data.current(context.asset, 'price'),
short_mavg=short_mavg,
long_mavg=long_mavg)
Run the backtest using the Zipline CLI:
zipline run -f --start 2014-1-1 --end 2018-1-1 -o out.pickle
Reviewing the outputs with pandas and matplotlib can give insights into the strategy performance. Remember, while API-specific details change, the fundamental concepts remain quite consistent across updates.
Conclusion
Now you have Zipline set up within your modern Python environment. You can experiment with existing strategies and test your own with real-world data. Zipline provides a prototyping approach that is invaluable in refining a strategy to be production-ready.