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 Zipline Wheels Zipline conda distribution 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.tar.gz (690.9 kB 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-cp39-cp39-manylinux2010_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0-cp39-cp39-manylinux1_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.9

zipline_reloaded-2.0.0-cp38-cp38-win_amd64.whl (1.8 MB view details)

Uploaded CPython 3.8Windows x86-64

zipline_reloaded-2.0.0-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-cp38-cp38-manylinux1_x86_64.whl (6.9 MB view details)

Uploaded CPython 3.8

zipline_reloaded-2.0.0-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-cp37-cp37m-win_amd64.whl (1.8 MB view details)

Uploaded CPython 3.7mWindows x86-64

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

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

zipline_reloaded-2.0.0-cp37-cp37m-manylinux1_x86_64.whl (6.0 MB view details)

Uploaded CPython 3.7m

zipline_reloaded-2.0.0-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.tar.gz.

File metadata

  • Download URL: zipline-reloaded-2.0.0.tar.gz
  • Upload date:
  • Size: 690.9 kB
  • 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.tar.gz
Algorithm Hash digest
SHA256 a709181c7f817ca8644793aaa525fad9512ee6f7b08cbd2ae0c327d292e1a30d
MD5 d6967f2813813f76747a1200c184b754
BLAKE2b-256 e70ed91a00d47fae36567244818290679022d7cd49254ee00ff875593dd7e544

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 6.5 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-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c50806d8fcf8d09b421210cb487330c0dd8e65d38903296583b2f2194c339aef
MD5 572285454a117b20cc6f81493680a324
BLAKE2b-256 74ab26eb15d7d851fb8ea57166cff7100b84041f406d3c3378d061ae810ad04d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 6.5 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-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4196e3c74ad2ece359f616af24e4db756140c8436c3bdb74188408cb4d654cad
MD5 ba64e5abe445c95f983f496c5a0406c1
BLAKE2b-256 d518abb45fd03a9c19d682068ec28ed5c360ab338b4bfb9e5eef724200d7a38b

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8, Windows 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-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 4018de77e58311c2076776c0e7ea121f91efb74395224817775cc3ac772b193b
MD5 78c48b064de87ac857c54df4c566e13c
BLAKE2b-256 ea73329e186ffc18936b8444a868f5014c930f840652d4a709544f9f7e4bf61d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-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-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 22ea39cd5ad02d0f582b541c3d7325f95582a7d941103a39ca365ace5a38ecc8
MD5 f39330087b2fbeb922e55575ae22b029
BLAKE2b-256 8a18ef64b56966aac37c5ebcc7f80136269efec7a943cbc6ac102b7a85c38644

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-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-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a3578f9a7c4cfdd7dd80f9bcdffb152bedbbeeb33fac01c25dfef9801e8f65a0
MD5 babe856a21c80618a68461b7e06d6c95
BLAKE2b-256 2aba11a7403563da8b8ecf69c2bb46ca1a958c6df3df606316e2de327538308a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-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-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 996c63104e8157f7e713a6c702289bfddaa8f78e36986e1a4d15b64ba6240e37
MD5 3c7282a842245fc58c04cd9f4e7983a1
BLAKE2b-256 0ebc74b8651ef912823edef54e7869a36fcf2b46b84bf0fe2b0cc5a03ac37b43

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.0.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.7m, Windows 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-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 3285cd8c5fcb977152acbbb4a272c391dafa9abf11e2ddb1f523ab679b81e1ed
MD5 f0a0edb9261877582c06783abda01001
BLAKE2b-256 8f2489db26caaafde827f227c12d63cc47178ea24ed1fcb873257ca79e36eb99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 6.0 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-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c108eda0de8cdd8a047060b9b9dc1507f2e462ae8a99f1ed79ca4092ccdf1005
MD5 db55f5b3d268b9b1bca30b1f39acb7b9
BLAKE2b-256 98ec3126ba3c3c207b276b7c00c16aa7d0d35788183c6bc9f1abef24d442a9f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 6.0 MB
  • Tags: CPython 3.7m
  • 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-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a9d9108da649c557600668937cfc20c336d8d3f8584043dc632398592b231d10
MD5 3e85dabfb81ad3a80212885c1db92c66
BLAKE2b-256 42e247ae547a725f81c2e070648914be6ed2918cde890bf91d357ee4f67c688f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zipline_reloaded-2.0.0-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-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ffa89daac66199e1fe8e86ab048db162ce22886d835510b98d42f6a3636e593f
MD5 617b8ff481bccca669979ad25496bd30
BLAKE2b-256 9252e2b3d634a8e03f8eb7dc5a75a248543ab5c1a948fc6fd3ef6b4356d52753

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