Skip to main content

A backtester for trading algorithms

Project description

Backtest your Trading Strategies

Version Info Python Anaconda-Server Badge Release Anaconda-Server Badge
Test Status CI Tests PyPI Anaconda Coverage Status
Community Discourse ML4T Twitter

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.

Installation

Zipline supports Python >= 3.7 and is compatible with current versions of the relevant NumFOCUS libraries, including pandas and scikit-learn.

If your system meets the pre-requisites described in the installation instructions, you can install Zipline using pip by running:

pip install zipline-reloaded

Alternatively, if you are using the Anaconda or miniconda distributions, you can use

conda install -c ml4t -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.

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:

$ 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, 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


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.0.post1.tar.gz (8.4 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.0.post1-cp39-cp39-manylinux2010_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0.post1-cp39-cp39-macosx_10_14_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

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

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0.post1-cp38-cp38-macosx_10_14_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

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

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: zipline-reloaded-2.0.0.post1.tar.gz
  • Upload date:
  • Size: 8.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline-reloaded-2.0.0.post1.tar.gz
Algorithm Hash digest
SHA256 ac3b4164c3ae4341e9c5e8fa31801b822f7c17299e8d163c2badbb7ae0eb98ab
MD5 0d52dd537387709402abc62c4f67bafb
BLAKE2b-256 ec5101040c1d48bc4190c88667e6ffcffbfb9ca49c39d2664298e10eda4c1e9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-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/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f3343928ac0fc05ee3017511e170f28e04a56fc12a94091cc5b68eace50b18e9
MD5 b0b7a20581ddf3aa4ebba9363209e0b7
BLAKE2b-256 5d0625b2272cdc3a6844dc52a30b4d979ba39a4ed7c57debe968e7f08c762099

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-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/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7afd40e387934b67cccd3fd66f3c7e974384482838f45313c2124e589aec9ec5
MD5 0622561c4768d7431e57122392c038f6
BLAKE2b-256 2108cfb26f846b447bcf2bfc7f8a09d813d0b6cbdfda620e70914ffdbe6934c2

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0.post1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 580e4494145e3fcf0e509e1f02382d4b493df4c08936856f10f99c29a8f31d01
MD5 d9932fb1c67c85d61d13a390a2a092b8
BLAKE2b-256 d6a43c0f3dfb332de47af816aa75553814fe77b1b6b54e3601a64071b202be0c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-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/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 3a4b85dc8d7d194f3068b13c9c8c9e4c3d30cfc3e42532a62552ba9a2055b7c5
MD5 a462bd731bbfca7058b648567bf08859
BLAKE2b-256 c9362b68b7da33f4c1ee204b0697ff2b566c2d3f5e234a8ca5916a28a8cd0f4d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-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/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c744ff814a1c9216b6ab470a44009f23adc82d83a9be0bf6b27af68bd0be17cf
MD5 41e66cee8bc0f3085416fc2ac3f0f92d
BLAKE2b-256 423de5506126ebf4f7a2c4fd7f6e7ea619001eaf55227fdb7674509c35e5cb27

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0.post1-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ff7c507e7a0908215137fb8bfa8603053709fb9a63e47951a31230c9b34a196b
MD5 d7fd3bc295466840d2aca5f37e2e878d
BLAKE2b-256 989ae29998083d6e2f3ba943642c9a9446f0d4001c0f0e7e67a2885d3ceb503e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-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/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f777e90874d0ae0b6427264c55c5c5023a30647ff5b2a1cda99da9d27a13e471
MD5 1f6d241fd99fa64a7477d7fdf92a64cd
BLAKE2b-256 751a771cf133bff92621f376cd73adfd79a13ebb13ff739231d81be8f535b68b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5ee08ff329d53e16e293bdf40c0197299ae0057fea4fff02c4a70df190531d47
MD5 4293deb8670d4ce0449451bb19afd0de
BLAKE2b-256 7d5045e908c50d9de29436c03bcd82196f68751dc4fa93b45176ebe3a10c4b03

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for zipline_reloaded-2.0.0.post1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 68722038ec3a083f407fa0f665b33aa92a9e0a2db33df26fc26604e4a67dcd81
MD5 5169104ab520ab174fae74e560b14ca9
BLAKE2b-256 80273deece067cebf02338e0a54387209b752a0a24131deff27fca295d94a69d

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