Skip to main content

A backtester for trading algorithms

Project description


Community Docs Discourse License ML4T Twitter
Version **
Info** Release Python
Test __
Status__ GitHub Workflow Status Coverage Status

Zipline is a Pythonic event-driven system for backtesting, used as the backtesting and live-trading engine by Quantopian before the company closed down in late 2020. Since then, the domain that originally hosted these docs have 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 sklearn to support development, analysis, and visualization of state-of-the-art trading systems.

Installation

Zipline supports Python 3.7, 3.8, and 3.9, and may be installed via either pip or conda.

Note: Installing Zipline is slightly more involved than the average Python package. See the full Zipline Install Documentation for detailed instructions.

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. First, you must download some sample pricing and asset data:

$ zipline ingest -b quandl
$ zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle --no-benchmark

This will download asset pricing data sourced from Quandl, and stream it through the algorithm over the specified time range. Then, the resulting performance DataFrame is saved as dma.pickle, which you can load and analyze from Python.

You can find other examples in the zipline/examples directory.

Questions?

If you find a bug, feel free to open an issue and fill out the issue template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zipline-reloaded-2.0.0rc2.tar.gz (7.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux2010_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux2010_x86_64.whl (6.9 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0rc2-cp38-cp38-macosx_10_9_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux2010_x86_64.whl (6.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0rc2-cp37-cp37m-macosx_10_9_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

Details for the file zipline-reloaded-2.0.0rc2.tar.gz.

File metadata

  • Download URL: zipline-reloaded-2.0.0rc2.tar.gz
  • Upload date:
  • Size: 7.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline-reloaded-2.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 c9c67b1913a699ff3a06cd4bd9247d73667cea69e9161267aae6729993778262
MD5 c39588cdb4794a4f5838de3b2b17e503
BLAKE2b-256 c8fde6e030e4ffd967a2916a3db3acc50c1ed3e18a924c3cf23b8fbfac3c7746

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 6.6 MB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 904ff5a17502e6bf75448cfbf74efdea923a465b16dbc25c3f1acc30c451fd9a
MD5 be6b18461f0f79817f2d17ee622eda31
BLAKE2b-256 bd8fcf2a37458da1db7debdcbe238c294336a0ebf69732d5b85eee7ca1865e29

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 6.6 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3930ccbb90de87ea82d029483940d0cbafaafa31a2144126e572e71cb4986c72
MD5 ce7aa506d5b1f5e496341e8d60e31b1b
BLAKE2b-256 ec05c4e145c305c5cc7e4f83e42452058a4f66132a67674040994508838a2def

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 6.9 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 fc9caf5aa2a15fe56526de03695c081e2d8f8b49258587d8628a49e6ad5b7c96
MD5 d6f5f32c0a5e1eff6ea741c9421c7e22
BLAKE2b-256 0414499bf43f792be5d8f79e911255cb1093bfd086710d70fd6d13ec46689799

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 6.9 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0f9f1c9125ec27bf40091c9d5761bc465cca80b5bcae608eedd770341c0cc8c2
MD5 ec61f803102fb0332fec57b03c97a1cf
BLAKE2b-256 02ffe18a23169a131d2d6b76d654ad024b0fd855801bb1be9ad570d3d0a09185

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 74412a6580411e6648f0d9d4ab1b044c0a61652e7ca9abe5b4c56939e7ee8c30
MD5 9f6965551e32b31ef776a82f7e178c68
BLAKE2b-256 c757f804f5b056d77c1b890f64f0971586025f2cd2ed7ab31c31153e7e0123e5

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 6.1 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d9801969c8e65f746c650cf63a0a7ccb129779a265bfa5320ed985d2f1d6a35a
MD5 c5b97db6b5cdd9c7e9ec5ccd3c181613
BLAKE2b-256 66ed59aa40230f8cafa5e181366d96ed6e769b4e3f8008d81c9fadc2f8f5e82d

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 6.1 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7659016918de538a1704931e718c2e07ce98387222d589ce5fc59930b4bcb090
MD5 0b32d5e736a19ded9e787230fbe6288c
BLAKE2b-256 56c570f9e7a9545b9287b29ccfeec0cefa300b77373b19ab3c7a48e10861348d

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0rc2-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0rc2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for zipline_reloaded-2.0.0rc2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e77904dd9e3910a9ee01e96d1bf8676e803428cffee4e86c0bb82b52bd6d83b7
MD5 6267336f401f4b62233cbd77c9694fb4
BLAKE2b-256 3e8c0638a593f96b940d76f4afccc52670161c477b5d367e20c58c72927d293a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page