mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-13 17:42:42 +08:00
Rewrites README.
Cleans up installation notes and adds quickstart code.
This commit is contained in:
committed by
Eddie Hebert
parent
e77cce1dc2
commit
63e19f71c0
@@ -1,76 +1,139 @@
|
||||
Zipline
|
||||
=======
|
||||
|
||||
Zipline is a realtime stream processing system.
|
||||
Zipline is a financial backtester for trading algorithms written in
|
||||
Python. The system is fundamentally event-driven and a close
|
||||
approximation of how live-trading systems operate.
|
||||
|
||||
System Setup
|
||||
==============
|
||||
Zipline is currently used in production as the backtesting engine
|
||||
powering <https://app.quantopian.com> -- a free, community-centered
|
||||
platform that allows development and real-time backtesting of trading
|
||||
algorithms in the web browser.
|
||||
|
||||
Running
|
||||
-------
|
||||
Features
|
||||
========
|
||||
|
||||
Initial `virtualenv` setup::
|
||||
* Ease of use: Zipline tries to get out of your way so that you can
|
||||
focus on algorithm development. See below for a code example.
|
||||
|
||||
$ mkvirtualenv zipline
|
||||
$ workon zipline
|
||||
# Go get coffee, the following will compile a heap of C/C++ code
|
||||
$ ./etc/ordered_pip.sh etc/requirements.txt
|
||||
# And optionally:
|
||||
$ ./etc/ordered_pip.sh etc/requirements_dev.txt
|
||||
* Zipline comes "batteries included" as many common statistics like
|
||||
moving average and linear regression can be readily accessed from
|
||||
within a user-written algorithm.
|
||||
|
||||
* Input of historical data and output of performance statistics is
|
||||
based on Pandas DataFrames to integrate nicely into the existing
|
||||
Python eco-system.
|
||||
|
||||
* Statistic and machine learning libraries like matplotlib, scipy,
|
||||
statsmodels, and sklearn support development, analysis and
|
||||
visualization of state-of-the-art trading systems.
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Since zipline is pure-python code it should be very easy to install
|
||||
and set up with pip:
|
||||
|
||||
```pip install zipline```
|
||||
|
||||
If there are problems installing the dependencies or zipline we
|
||||
recommend installing these packages via some other means. For Windows,
|
||||
the [![Enthought Python Distribution]
|
||||
<http://www.enthought.com/products/epd.php> includes most of the
|
||||
necessary dependencies. On OSX, the [![Scipy Superpack]
|
||||
<http://fonnesbeck.github.com/ScipySuperpack/> works very well.
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
* Python (>= 2.7.2)
|
||||
* numpy (>= 1.6.0)
|
||||
* pandas (>= 0.9.0)
|
||||
* pytz
|
||||
* msgpack-python
|
||||
* iso8601
|
||||
* Logbook
|
||||
* blist
|
||||
|
||||
Quickstart
|
||||
==========
|
||||
|
||||
The following code implements a simple dual moving average algorithm
|
||||
and tests it on data extracted from yahoo finance.
|
||||
|
||||
```python
|
||||
from zipline.algorithm import TradingAlgorithm
|
||||
from zipline.transforms import MovingAverage
|
||||
from zipline.utils.factory import load_from_yahoo
|
||||
|
||||
class DualMovingAverage(TradingAlgorithm):
|
||||
"""Dual Moving Average algorithm.
|
||||
"""
|
||||
def initialize(self, short_window=200, long_window=400):
|
||||
# Add 2 mavg transforms, one with a long window, one
|
||||
# with a short window.
|
||||
self.add_transform(MovingAverage, 'short_mavg', ['price'],
|
||||
market_aware=True,
|
||||
days=short_window)
|
||||
|
||||
self.add_transform(MovingAverage, 'long_mavg', ['price'],
|
||||
market_aware=True,
|
||||
days=long_window)
|
||||
|
||||
# To keep track of whether we invested in the stock or not
|
||||
self.invested = False
|
||||
|
||||
self.short_mavg = []
|
||||
self.long_mavg = []
|
||||
|
||||
|
||||
Develop
|
||||
-------
|
||||
def handle_data(self, data):
|
||||
if (data['AAPL'].short_mavg['price'] > data['AAPL'].long_mavg['price']) and not self.invested:
|
||||
self.order('AAPL', 100)
|
||||
self.invested = True
|
||||
elif (data['AAPL'].short_mavg['price'] < data['AAPL'].long_mavg['price']) and self.invested:
|
||||
self.order('AAPL', -100)
|
||||
self.invested = False
|
||||
|
||||
To run tests::
|
||||
# Save mavgs for later analysis.
|
||||
self.short_mavg.append(data['AAPL'].short_mavg['price'])
|
||||
self.long_mavg.append(data['AAPL'].long_mavg['price'])
|
||||
|
||||
$ nosetests
|
||||
|
||||
Tooling hints
|
||||
================
|
||||
zipline relies heavily on scientific Python components (numpy, scikit, pandas, matplotlib, ipython, etc). Tooling up can be a pain, and it often involves managing a configuration including your OS, c/c++/fortran compilers, python version, and versions of numerous modules. I've found the following tools absolutely indispensable:
|
||||
|
||||
- some kind of package manager for your platform. package managers generally give you a way to search, install, uninstall, and check currently installed packages. They also do a great job of managing dependencies.
|
||||
- Linux: yum/apt-get
|
||||
- Mac OS: homebrew/macport/fink (I highly recommend homebrew: https://github.com/mxcl/homebrew)
|
||||
- Windows: probably best if you use a complete distribution, like: enthought, ActiveState, or Python(x,y)
|
||||
- Python also provides good package management tools to help you manage the components you install for Python.
|
||||
- pip
|
||||
- easy_install/setuptools. I have always used setuptools, and I've been quite happy with it. Just remember that setuptools is coupled to your python version.
|
||||
- virtualenv and virtualenvwrapper are your very best friends. They complement your python package manager by allowing you to create and quickly switch between named configurations.
|
||||
- *Install all the versions of Python you like to use, but install setuptools, virtualenv, and virtualenvwrapper with the very latest python.* Use the latest python to install the latest setuptools, and the latest setuptools to install virtualenv and virtualenvwrapper. virtualenvwrapper allows you to specify the python version you wish to use (mkvirtualenv -p <python executable> <env name>), so you can create envs of any python denomination.
|
||||
|
||||
Mac OS hints
|
||||
-------------
|
||||
|
||||
Scientific python on the Mac can be a bit confusing because of the many independent variables. You need to have several components installed, and be aware of the versions of each:
|
||||
|
||||
- XCode. XCode includes the gcc and g++ compilers and architecture specific assemblers. Your version of XCode will determine which compilers and assemblers are available. The most common issue I encountered with scientific python libraries is compilation errors of underlying C code. Most scientific libraries are optimized with C routines, so this is a major hurdle. In my environment (XCode 4.0.2 with iOS components installed) I ran into problems with -arch flags asking for power pc (-arch ppc passed to the compiler). Read this stackoverflow to see how to handle similar problems: http://stackoverflow.com/questions/5256397/python-easy-install-fails-with-assembler-for-architecture-ppc-not-installed-on
|
||||
- gfortran - you need this to build numpy. With brew you can install with just: ```brew install gfortran```
|
||||
- umfpack - you need this to build scipy. ```brew install umfpack```
|
||||
- swig - you need this to build scipy. ```brew install swig```
|
||||
- hdf5 - you need this to build tables. ```brew install hdf5```
|
||||
data = load_from_yahoo()
|
||||
dma = DualMovingAverage()
|
||||
results = dma.run(data)
|
||||
```
|
||||
|
||||
Style Guide
|
||||
===========
|
||||
|
||||
To ensure that changes and patches are focused on behavior changes, the zipline codebase adheres to PEP-8, <http://www.python.org/dev/peps/pep-0008/>.
|
||||
To ensure that changes and patches are focused on behavior changes,
|
||||
the zipline codebase adheres to PEP-8,
|
||||
<http://www.python.org/dev/peps/pep-0008/>.
|
||||
|
||||
The maintainers check the code using the flake8 script, <https://github.com/jcrocholl/pep8/>, which is included in the requirements_dev.txt.
|
||||
The maintainers check the code using the flake8 script,
|
||||
<https://github.com/jcrocholl/pep8/>, which is included in the
|
||||
requirements_dev.txt.
|
||||
|
||||
Before submitting patches or pull requests, please ensure that your changes pass ```flake8 --ignore=E124,E125,E126 zipline tests```
|
||||
Before submitting patches or pull requests, please ensure that your
|
||||
changes pass ```flake8 --ignore=E124,E125,E126 zipline tests```
|
||||
|
||||
Discussion and Help
|
||||
===================
|
||||
|
||||
Discussion of the project is held at the Google Group, <zipline@googlegroups.com>, <https://groups.google.com/forum/#!forum/zipline>.
|
||||
Discussion of the project is held at the Google Group,
|
||||
<zipline@googlegroups.com>,
|
||||
<https://groups.google.com/forum/#!forum/zipline>.
|
||||
|
||||
Source
|
||||
======
|
||||
The source for Zipline is hosted at <https://github.com/quantopian/zipline>.
|
||||
|
||||
The source for Zipline is hosted at
|
||||
<https://github.com/quantopian/zipline>.
|
||||
|
||||
Build Status
|
||||
============
|
||||
|
||||
[](https://travis-ci.org/quantopian/zipline)
|
||||
|
||||
Contact
|
||||
|
||||
Reference in New Issue
Block a user