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.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.8 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:

$ 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 (since acquisition hosted by NASDAQ), 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-3.1.dev4.tar.gz (11.6 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.dev4-cp311-cp311-win_amd64.whl (4.1 MB view details)

Uploaded CPython 3.11Windows x86-64

zipline_reloaded-3.1.dev4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

zipline_reloaded-3.1.dev4-cp311-cp311-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

zipline_reloaded-3.1.dev4-cp311-cp311-macosx_10_15_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.11macOS 10.15+ x86-64

zipline_reloaded-3.1.dev4-cp310-cp310-win_amd64.whl (4.1 MB view details)

Uploaded CPython 3.10Windows x86-64

zipline_reloaded-3.1.dev4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zipline_reloaded-3.1.dev4-cp310-cp310-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

zipline_reloaded-3.1.dev4-cp310-cp310-macosx_10_15_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.10macOS 10.15+ x86-64

zipline_reloaded-3.1.dev4-cp39-cp39-win_amd64.whl (4.1 MB view details)

Uploaded CPython 3.9Windows x86-64

zipline_reloaded-3.1.dev4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

zipline_reloaded-3.1.dev4-cp39-cp39-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

zipline_reloaded-3.1.dev4-cp39-cp39-macosx_10_15_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9macOS 10.15+ x86-64

zipline_reloaded-3.1.dev4-cp38-cp38-win_amd64.whl (4.2 MB view details)

Uploaded CPython 3.8Windows x86-64

zipline_reloaded-3.1.dev4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

zipline_reloaded-3.1.dev4-cp38-cp38-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

zipline_reloaded-3.1.dev4-cp38-cp38-macosx_10_15_x86_64.whl (4.4 MB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

File details

Details for the file zipline-reloaded-3.1.dev4.tar.gz.

File metadata

  • Download URL: zipline-reloaded-3.1.dev4.tar.gz
  • Upload date:
  • Size: 11.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for zipline-reloaded-3.1.dev4.tar.gz
Algorithm Hash digest
SHA256 6ad9d3db6ffd44f617e534242d41c1f57623e3759e49da4ac3f3221b70219bfd
MD5 6bb995ae79335f3e78ed751ff20ada3c
BLAKE2b-256 5709e2977a8f1aee9f7d6b122447ccc52699cbe3ca8fe41ccd67ccf5a97a5725

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e76fc323dd4dfd78a71d5688ce78d85c1ac5919b4791d0f348e737dd2e677f58
MD5 071acef1660c94bed177ffcb172167c1
BLAKE2b-256 9a81c50f58e6c2ad943341ea243ea39bb6dfe06c8394399040efe6e4654762f5

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d2a1ba526eed9af2e94d347077d378d9479e38e37f554a9fbe11f4ee0b6e8244
MD5 8b514a890befcbc072df832c1a7159f1
BLAKE2b-256 723809cee947477539d4c3246b678677eef9d3d87fc9d1c5aa744d7eb8a41d63

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 42a493f6e38f14ff8dc85cdcdb92f8520ef3d5c42cc4974b35f2a238eb69eba5
MD5 377fa134bbe8e00976db63689e5e99f2
BLAKE2b-256 51a389fa56765cdd71d1c753377ed965b69e0ca2ea261f2754cd24f4c0fd8061

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9896c070146a886786ffbfcbc6e1f3feb70a810950cabc2d7871270a66fcc5a2
MD5 6681d7776551cb6113e9e127c5504c08
BLAKE2b-256 88222bb351093687f795644e2d74bcaa6a0b8cf864e1728774fe66299acbff45

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 58c827614acb05c7782fa7a01757cc3d8076a6b76245241af51336d03c624ea8
MD5 9d376d33d8ca3bca6004da6c4833bd62
BLAKE2b-256 1677e89d5fa897873295db575f6b28da8dab61088cfe27ea821cb10eae809f8c

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 42303679223ca4ea27bc09d67431023debf5cdba98376877b399b38d45c07a39
MD5 b760f540cc258fd5f7899cd545aca6d1
BLAKE2b-256 8480361071bc3320f51867eff4c73ea12fb1133ea0f331582c40bb7190dca6e4

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e832abe081eb09ab1fe8c62d1e26e4b1f4ebf7e67e48550dd58582f4c6604abd
MD5 40bd6d116d9d69fd4005b9693a7ef26f
BLAKE2b-256 c2c1fc3a4307c77d955fb72779b78ca9bda47a9b398929e02099349bd008e04f

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 79d9f66e77646af808ae18b510a0959f59ccdc55650e66a411f49227811662ba
MD5 b5c63a3669ec06e99af493227d1e3996
BLAKE2b-256 b023f1b4cff6687a3526b384dfe13dd4ff9930a2ee135d84c8ac3283b83386bb

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 6c69f4e68e14dae25c237c1ee99599fa3b7b95d064db016207f9f2221a167a08
MD5 9605134a43ef9366d7f88b659e5c436a
BLAKE2b-256 51a70368e7c222fb0873cd91d9aec55cf33aef6eff5e4a75994979eab4002fe2

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3aa469f278dc3e947309df2c1f1766bd3971539c87fa8d5b92f3c271f03cd539
MD5 fdcb85914036396d833d9121e5e852a5
BLAKE2b-256 6eed3a980584b6157d8351753ee822a7f9610ebc9942c62755852482812f2620

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 12e0cd6a5c633f7eaaa003c1b862efc0e618b743121139d1f8fd994ba45057bc
MD5 75fd794642136204693e82b878a2d570
BLAKE2b-256 31007f17ad8310525e8cb894dc4b95960fb0288b4687c7a4aa2183011052f670

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 cb06a75276074cd8e5976ccee6093930fa6d721807079420f51f50767a43c478
MD5 4e80c34e2667de2063b8e7ad8852868d
BLAKE2b-256 c87232ffeaf09c77ffffd42758e9a4bb4fbe8ca08182f291d080a4369492aad5

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5026304068677934b5530f46348ab3a8bdd471b9a02fa7cf82cdac05cde8e6bd
MD5 2b95c58a81ae7d84cac544cad2f234e0
BLAKE2b-256 56bc87baf8a36f708f9c2489e321f3ac39f0b5d4614617869d9f045327c8421e

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3c7bf7a74f314c28378901cb984147a69aef09aeb66e40dbeef36a57d382f795
MD5 81dc716a35b95d77adde8e2154bddf46
BLAKE2b-256 63fe9c3f24128dedfdfdb9f284a71b6aaa38e809937989e6f080167fbfb392dc

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3ef2e391cf64ddcf9afa566b9934b4ecffcf479b4cb236bd078ef1ebc96cfad
MD5 bf93519f22b3e64c0d0ba8e17c23234a
BLAKE2b-256 01cc7d9f2adc7b3c429764dd9694c0375ef24eb2f2d8bb1072b62396422502d6

See more details on using hashes here.

File details

Details for the file zipline_reloaded-3.1.dev4-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-3.1.dev4-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 21d260a72ba25fd86ef23d9dd305ccf1cc896bc1cb2353e8fe937b33e698bbfe
MD5 ab00940ca7358ef99f3ca12f928be921
BLAKE2b-256 46d59f04378e0cafb8d1b2ae30b34af3ddeb129f12b0f5f7355bc7b7ca770a18

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