Skip to main content

A Pythonic backtester for trading algorithms

Project description

Backtest your Trading Strategies

Version Info Python Anaconda-Server Badge PyPI Anaconda-Server Badge
Test Status CI Tests PyPI codecov
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.

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


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-3.1.1.tar.gz (12.3 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-3.1.1-cp313-cp313-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.13Windows x86-64

zipline_reloaded-3.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (12.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

zipline_reloaded-3.1.1-cp313-cp313-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

zipline_reloaded-3.1.1-cp313-cp313-macosx_10_15_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.13macOS 10.15+ x86-64

zipline_reloaded-3.1.1-cp312-cp312-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.12Windows x86-64

zipline_reloaded-3.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (12.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

zipline_reloaded-3.1.1-cp312-cp312-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zipline_reloaded-3.1.1-cp312-cp312-macosx_10_15_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

zipline_reloaded-3.1.1-cp311-cp311-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.11Windows x86-64

zipline_reloaded-3.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (12.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

zipline_reloaded-3.1.1-cp311-cp311-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

zipline_reloaded-3.1.1-cp311-cp311-macosx_10_15_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.11macOS 10.15+ x86-64

zipline_reloaded-3.1.1-cp310-cp310-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.10Windows x86-64

zipline_reloaded-3.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

zipline_reloaded-3.1.1-cp310-cp310-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

zipline_reloaded-3.1.1-cp310-cp310-macosx_10_15_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.10macOS 10.15+ x86-64

File details

Details for the file zipline_reloaded-3.1.1.tar.gz.

File metadata

  • Download URL: zipline_reloaded-3.1.1.tar.gz
  • Upload date:
  • Size: 12.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zipline_reloaded-3.1.1.tar.gz
Algorithm Hash digest
SHA256 4a305524616f7aad836f929e5a2ba5afc7db0e238757f47eb49487d9e2457a6f
MD5 d8e37419976df30be623f4e93e2cff4b
BLAKE2b-256 c4737f6810bfee01923519038ad361aef6e6f47ecb35283ffed98126eece48a7

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9073a21b9ad84dcd99ae7c532881c4c4ddbc785694de6336159d73a039de8182
MD5 83a67c1a5fc0b1d1d83bc4feabb47642
BLAKE2b-256 91e37d4c846280f337063f86fc063711b9d92cb22fff10b8d0b20b3b820943c7

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e71c310e17941e5913bf94bde4f11acef00bca014ddbb43ace03bee7badafefb
MD5 1c7ed324169be94b0f0e66ad479d2729
BLAKE2b-256 5e167148ced54a4b0b05fbb1bc9614841fb3919dc5295eaab9e43ca1addaeea9

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f189a554e8fe450080931427ececc4a93a7f8bbe6f68fa479f21ad066e7946f7
MD5 ce7c610721699a9c6e5781925d6cfa2c
BLAKE2b-256 82c0c0c7101d684c583fcc9bee271ad217c91e60730957a635c41fa47f910cfe

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp313-cp313-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp313-cp313-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e4a35ec6837cedba7acceb767a1b863ce7c24ca528b3961d1907f1efcc0ef349
MD5 d084a39bfab83a159d2fd15996492d60
BLAKE2b-256 95b251df1874cce661312b4ac8639a63a1e857a56295cdc39bbe0ec86256855b

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 77ebe5641def380b3d7a1fd26b101f131f1bd402a6f1a72857b6b866124b9247
MD5 38ac9b1dad93f9d76715b184eb04e4b8
BLAKE2b-256 fb82cb5c1cbad9e28c7a74c4d20f33949f150782f375f90bfca219d9d502bf84

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8fa37d5356a11e7f7ad4708dc08bc76f750f6fee900a8a9036c12f8e7d28d5e3
MD5 f981afc5430a1d95e4343de63b0a901a
BLAKE2b-256 49e03d321d6a87504f892154a7bfceffb63b90cb8a2398d8cdf4887be7f7a74c

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8bda014e3b1d8f6e1f8cd3168a35c1730dccd3233834af4d5638bbba18235469
MD5 f1bc006f5cc3c36c3059e6c4faa12f2b
BLAKE2b-256 d549e18a294e585824993b747f70206c0f357e08d73a49fa91ba67b469945cec

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp312-cp312-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6a9c7f0d9ad6a0f7604e0330640c0f6f42064b4d60d07be1bcba00002fee9e56
MD5 5ea47f38aa840881bf16a8c3ad97418c
BLAKE2b-256 763796181cca329511d0654ce1b203c07768b29cf41d3808f67d64515a6fcf06

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 70a48a27a5fdd582ef851cedcea5c49e0942ee2e968fafb2a0b49b19c3a8030b
MD5 17ac6620dc2f869d73eb81e538e8594b
BLAKE2b-256 835d62c768c27f6e360cd7fe98f20edb45c25ffcbf028c184c31ccde45356e43

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4ae380ab3b11aa667b38d25bdd315fe1bfee66f441be9c0c28c251ecf6c2895d
MD5 18c809553f5c358e2fe263efa34701d1
BLAKE2b-256 9881c0f0cca95f95f67dc207b548d9c65eaeba04218f52dfd0fa9c01a8ec344d

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70095c29457553dbc41964d171ae5f7994d962c83f82042cd7dbb63ac08f3793
MD5 3c721321fb817af94982dc3be9218db1
BLAKE2b-256 db40f7f45ce30e2588847abcb874aaed6bb6686aaa464d56fdaac6da2b7ac813

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ef36d03256a1cf58ee0bfef18ae1a94815b1812589c244a398875919e267643b
MD5 ac1739dee18cef6f820e5f5ca379c8a7
BLAKE2b-256 b8becd663f097148e2d9197a62cf76e8961aef5c524ebc73a27935f2ecb7d2fb

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d85450ef719df70c41463f5a05aeb4dd003a06386c681b2743ee3c53d475fa5d
MD5 0863c36396825178199cbce207e7f09d
BLAKE2b-256 f9bbc8e3dde941811a0adacb091ca1c93d90454ef25318bfaa59348de245d16b

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 00a03bba8402b6e3a906bcdb3b6943dece5f67ac9ef4152c2ae733830ca4957f
MD5 dcf1cfd9fa1d758dda556c48118ee74e
BLAKE2b-256 59b96287497f4a5cc30f2fcf1bfe374abfa421a351c7375fd4ee2e4c99f03fd3

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c2096acea1dafe92bd20cde5a6083b73b7aec724eb9ceea44d6701d72aed212c
MD5 2f9468cf4f1637266e6fb87adaff5f2b
BLAKE2b-256 f78c538065a23e9abf159414e1dc74ae48ffcbc11ca130522850aeddea5dbbaa

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.1-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.1-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6419b999bbd47d898b8cbbb6a88dfb95f4bc20eed47e6280d1e4c16a5ffc2fb2
MD5 162e994fbf04bac753412b797555c613
BLAKE2b-256 545dd7c8aae1a6348dc3511670dc367d4495d84423f493c3df9795f6c2bcb19b

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