A Pythonic backtester for trading algorithms
Project description
Backtest your Trading Strategies
Version Info | |
---|---|
Test Status | |
Community |
Zipline is a Pythonic event-driven system for backtesting, developed and used as the backtesting and live-trading engine by crowd-sourced investment fund Quantopian. Since it closed late 2020, the domain that had hosted these docs expired. The library is used extensively in the book Machine Larning for Algorithmic Trading by Stefan Jansen who is trying to keep the library up to date and available to his readers and the wider Python algotrading community.
Features
- 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.
- Batteries Included: many common statistics like moving average and linear regression can be readily accessed from within a user-written algorithm.
- PyData Integration: Input of historical data and output of performance statistics are based on Pandas DataFrames to integrate nicely into the existing PyData ecosystem.
- Statistics and Machine Learning Libraries: You can use libraries like matplotlib, scipy, statsmodels, and scikit-klearn to support development, analysis, and visualization of state-of-the-art trading systems.
Note: Release 3.05 makes Zipline compatible with Numpy 2.0, which requires Pandas 2.2.2 or higher. If you are using an older version of Pandas, you will need to upgrade it. Other packages may also still take more time to catch up with the latest Numpy release.
Note: Release 3.0 updates Zipline to use pandas >= 2.0 and SQLAlchemy > 2.0. These are major version updates that may break existing code; please review the linked docs.
Note: Release 2.4 updates Zipline to use exchange_calendars >= 4.2. This is a major version update and may break existing code (which we have tried to avoid but cannot guarantee). Please review the changes here.
Installation
Zipline supports Python >= 3.9 and is compatible with current versions of the relevant NumFOCUS libraries, including pandas and scikit-learn.
Using pip
If your system meets the pre-requisites described in the installation instructions, you can install Zipline using pip
by running:
pip install zipline-reloaded
Using conda
If you are using the Anaconda or miniconda distributions, you install zipline-reloaded
from the channel conda-forge
like so:
conda install -c conda-forge zipline-reloaded
You can also enable conda-forge
by listing it in your .condarc
.
In case you are installing zipline-reloaded
alongside other packages and encounter conflict errors, consider using mamba instead.
See the installation section of the docs for more detailed instructions and the corresponding conda-forge site.
Quickstart
See our getting started tutorial.
The following code implements a simple dual moving average algorithm.
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)
You can then run this algorithm using the Zipline CLI. But first, you need to download some market data with historical prices and trading volumes.
This will download asset pricing data from NASDAQ (formerly Quandl).
This requires an API key, which you can get for free by signing up at NASDAQ Data Link.
$ export QUANDL_API_KEY="your_key_here"
$ zipline ingest -b quandl
The following will
- stream the through the algorithm over the specified time range.
- save the resulting performance DataFrame as
dma.pickle
, which you can load and analyze from Python using, e.g., pyfolio-reloaded.
$ zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle --no-benchmark
You can find other examples in the zipline/examples directory.
Questions, suggestions, bugs?
If you find a bug or have other questions about the library, feel free to open an issue and fill out the template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file zipline_reloaded-3.1.tar.gz
.
File metadata
- Download URL: zipline_reloaded-3.1.tar.gz
- Upload date:
- Size: 12.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a88fb322a1d93cd93c19e2d094bdbe8b89c81a8a345229b9bd0f24f654f624f3 |
|
MD5 | 5b65d9d523e2f227a7a3003505028dce |
|
BLAKE2b-256 | 783452df3fea6315e902c021005968533394f55fede8597aacf63eaa76bc83b5 |
File details
Details for the file zipline_reloaded-3.1-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 712410850d784e3ce5d3215b37677a18dbf30a162b1e0aa8e93278b2f290e7da |
|
MD5 | 897e1d4ffd54f1c732da9751828faf3f |
|
BLAKE2b-256 | 95cc970e88599ec3528376ff9be8743026127df17925a7cdb34939f2320cd075 |
File details
Details for the file zipline_reloaded-3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 11.5 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23a683f465287000fcbe17aaac4faad46b1afe77b30d4b25ce4ec06d0a5a316e |
|
MD5 | 2a592c9a8e776662f7d87bd401fd6496 |
|
BLAKE2b-256 | b96b2c2d7f3fb0769a08cf0e692d9f1c86c05f9492d46c31b8362cacc425ea2f |
File details
Details for the file zipline_reloaded-3.1-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb00a2f8c95f124842d3b531fc2e9d21c2d358bdf8a843bcbd96a5a4c9e6406e |
|
MD5 | 0808e5b5ec3adab57eedd5bfaa0ac837 |
|
BLAKE2b-256 | 10cdb774e9a42dbd5ee59d346427f6b3207b707ee7e9b30a19a9a8181cb6b04b |
File details
Details for the file zipline_reloaded-3.1-cp312-cp312-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp312-cp312-macosx_10_15_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.12, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ed43a8c3fa43ca39f18eaf0e7e8bf4c05bf2efede3811f7256823fcef139521 |
|
MD5 | 33d8fbc237609fd618ac0a2951566204 |
|
BLAKE2b-256 | 555285f653b9f3b0da673d54ce76b7bbb4acf4d54fa2c897e9b657aa4433648e |
File details
Details for the file zipline_reloaded-3.1-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28281a8adcd6f2649b35d5684b0dd322e187a2878fb9e554417419e929900405 |
|
MD5 | ccb5bc5cd915240c58f57f76d5256dc2 |
|
BLAKE2b-256 | fa45feb23129dd4a9233615ad22015df7e175a5a4d7bfb9984d1291ba1bd93b8 |
File details
Details for the file zipline_reloaded-3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 11.7 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9a4601dbbeff63ac51dc44255ea38272040baeac762eb949bd0bedb036702e1 |
|
MD5 | 6d2f52bcc62de720e7ee61bb7b621f7a |
|
BLAKE2b-256 | 8eadced5b8cb5a746c2a5e91db7d58f4af17765a82599b7a960ed5976ec5139a |
File details
Details for the file zipline_reloaded-3.1-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92278bf4b4a4d26f091d31c45b5411876ab924597c9e6f7ac3e6bb2616c03023 |
|
MD5 | 7b4a4f883a73c0132ac4e2d21944c380 |
|
BLAKE2b-256 | 774057aa90d6155777c4868bd4f623195c8b6a56e56cf014942b57e8aa130ad0 |
File details
Details for the file zipline_reloaded-3.1-cp311-cp311-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp311-cp311-macosx_10_15_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.11, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6ef435791639709d89184b21e907415cd598e33b95d0b8459638c8b498dd118 |
|
MD5 | 85fb1d3ba0cfa5a65226b4d95ee40304 |
|
BLAKE2b-256 | bf7de2839aad38ac5781eb908d7cb78cff211de8229a94d76dc3bf8d8866e959 |
File details
Details for the file zipline_reloaded-3.1-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92a32612576710ed55937c923d3eb9dd3638b5c96678bedaf7e83177c22e30bd |
|
MD5 | 696e87a0b49bfa288c88c31464eae524 |
|
BLAKE2b-256 | b59f4c3248194ddae3e1ae0a12838923f6d0cb2d33465f4c8648a85b6a94a090 |
File details
Details for the file zipline_reloaded-3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 11.0 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 531ccb4934d66eec47e0f901fcfcd1fd87bc232c0d5d5638bbc828121a1c706d |
|
MD5 | 43056a7237c28d3c404ad04c73b08183 |
|
BLAKE2b-256 | 060f21690821689053b4e73f081f166f0a6214466599e37403c5d3d214408f28 |
File details
Details for the file zipline_reloaded-3.1-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a10f84b7f7bdf77976f854c39cd050d50e79b577f64c4e306bfc0ecf12aa00d1 |
|
MD5 | e3411c9ee5844422a134ecbc43374b76 |
|
BLAKE2b-256 | e7753fafa3eb23f87e9b08d8388fd8dd69f5eb6bc44873a629dac4d59124fcba |
File details
Details for the file zipline_reloaded-3.1-cp310-cp310-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp310-cp310-macosx_10_15_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.10, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7314cd525c6b7a14c93d55be6feb80a981ce7aa53ced578fc2ebfc7cf48b82a1 |
|
MD5 | f8bbc3902ce0a89c92f685abbc87002b |
|
BLAKE2b-256 | 6a0ba3db691849e896cf4bec4d947d3df596955cb2be40fcfa643331ee43a096 |
File details
Details for the file zipline_reloaded-3.1-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c779d49ba2d6f4ce137c060225c1d55865c24d9d1e718d55d82cf682b54f9574 |
|
MD5 | 6788416bc64fd3889e420bc616334a03 |
|
BLAKE2b-256 | 2122a6c6e76aa16c4c3c612e200d95491bc0eb2352f5264c2ebabc4708274b4a |
File details
Details for the file zipline_reloaded-3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 11.0 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb23f0a98822a8dcce1c2ec05acf8ba3bd6f021a709a63a60ebd0507aa825c4b |
|
MD5 | b69341930942fb6d86c12373ea34b74e |
|
BLAKE2b-256 | 1cc479dc3b527d1f382ea1ce5a6604fe717aae88f4b1baab91a0ad517be5abe5 |
File details
Details for the file zipline_reloaded-3.1-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.8 MB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ee7e59448cf2e439576d05be5d3adaba58efc930f3b0446049d94466d424f32 |
|
MD5 | 17cb2a9107888a0d455e88ef84f12582 |
|
BLAKE2b-256 | 13884804482a6bfebd2da13a028db44b2eba4c4a9ec09ee2d3e77622bf7e165d |
File details
Details for the file zipline_reloaded-3.1-cp39-cp39-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: zipline_reloaded-3.1-cp39-cp39-macosx_10_15_x86_64.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.9, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 518336e53e55340e37b2275a5a51eb2d3c0a8001c130ed115e47681fb633e4ce |
|
MD5 | 88ca3415ad2771dfa754941c1f87e310 |
|
BLAKE2b-256 | 65b1430a270468f3308860a9ad53d33de0a4c4ba6cd7d6557bca40cc2e5d27ed |