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 -c ranaroussi 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.2.1.dev0.tar.gz (9.5 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.2.1.dev0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_universal2.whl (4.8 MB view details)

Uploaded CPython 3.10macOS 10.14+ universal2 (ARM64, x86-64)

zipline_reloaded-2.2.1.dev0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_universal2.whl (4.8 MB view details)

Uploaded CPython 3.9macOS 10.14+ universal2 (ARM64, x86-64)

zipline_reloaded-2.2.1.dev0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_universal2.whl (4.9 MB view details)

Uploaded CPython 3.8macOS 10.14+ universal2 (ARM64, x86-64)

zipline_reloaded-2.2.1.dev0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.8 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

zipline_reloaded-2.2.1.dev0-cp37-cp37m-macosx_10_14_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

File details

Details for the file zipline-reloaded-2.2.1.dev0.tar.gz.

File metadata

  • Download URL: zipline-reloaded-2.2.1.dev0.tar.gz
  • Upload date:
  • Size: 9.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for zipline-reloaded-2.2.1.dev0.tar.gz
Algorithm Hash digest
SHA256 9267d76bb466bf0a13687fb9a81f0c7e2abe37167b77476e16b3e1af16c9706a
MD5 892a242271a4b290c85a28bcf3a3d2f8
BLAKE2b-256 50628e699eb03c501edfc0b5328ae51d16651d603bc038f71b652d9a2af5c96c

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fe0eef2b95559389869222ae03adfd24dd45bcd8b21b7e502652b56a3bae9e9
MD5 82eb59315d3803c91c6c73a61c573bb4
BLAKE2b-256 3cb15e85512d1b8499fd4f50edf51cd93571e8b2880dc1721ae817e3c52f5813

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 36f708bef947ae2d364aa69cec038ce3aefbe54ed83a1574670c27debd6e738c
MD5 4b3876335e9562175732424feecb944a
BLAKE2b-256 adbe6c070584aff30c005a3f47b2055439f5321d53ffd058de9852143fef09f2

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2dd605421f1e70bb3eb502f91d044680491457c2a3dbdd7d9317cee2bc163802
MD5 28a299eb70598a56292801024faf389a
BLAKE2b-256 f63c86cd600da8ad3e1453231f2eb9596e0dcbd4b3c0d1567faf5295c1d9fb8b

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_universal2.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp310-cp310-macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 c5d158c29e13aa271a61dabdfa60d8e1cdc3f8a818be6bb07276815716999299
MD5 9b9885ed6301dd034a268ac58314a978
BLAKE2b-256 bb3085141d9f9914e408bddcccee762bcbe582a139e7f026ed5111e7eef95d1a

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66ebcb3091e07c079aec46b2fe06c5ec30a33b392ea5abd8a7e764359e4a57e7
MD5 d03fc725ef8b6bb7e85f0958b5a3bca9
BLAKE2b-256 70f956f35e438ebcd72abcc8ddaa405f2991c4d216a4ae484a3c776e065062cf

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ea504512fc0333a95a86cc7ef6cb727c09c9a33331538bcfc8612f2449db4ad
MD5 fc486f2d656a2770de26c2a71c29c78f
BLAKE2b-256 06d1c65acac6ca8cd6971ceb8e751a968155af8ed5e4dcee5f6a3d4e86239390

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1434c6cadc3f991cb8a665fae5cdb6507c425479ab707d9d1fb8f1547f87aff5
MD5 40895286735872aacc127e2c0824f5b0
BLAKE2b-256 e2543be7820377ae46a9f6cc4da70172d18c2c0544e499319036987fcfa68391

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_universal2.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp39-cp39-macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 9dbc023e83ad14808558220edb558f5f1f7de5ad3524b60d7eda4405aad52b57
MD5 1eea7ad909ca88363ed66113b16acabc
BLAKE2b-256 881461db14c7af5ad1648995ba918d80e30326263cd1450d10621763bb0a6402

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d15fa6d7b57b8fca76ea334248a8f8274ee172bd2a4f519a3c66443ef530c611
MD5 d5491317665716b39db52bca7712860c
BLAKE2b-256 5faa244a73d93c39d4d84931f0fe35ad440e794d2c3bf75b17b435a20c26a1f2

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b965e8dedaa30d6dd9c62eb9b90ded74f0d504f3bfc674c6d5fc1e2729e48b3
MD5 1d89651010c4f08267f9ccc6189a6245
BLAKE2b-256 4e54251af95a2bad63d5412e3e4304e086f0b19e700e720bd179d21363655a2e

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 bacac9e3ce2a379fae4b70482e0dfb390dece496272aba315991592126cd442e
MD5 b741944f4f1a36001115468b2770ee09
BLAKE2b-256 1dfd10501ef800b8f22bb9045b6b67fb8991a98af5289a13f35bafc0e98681c2

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_universal2.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp38-cp38-macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 8c458e4dcbbc74143a8fe5fb63e5c44f4f01176d440bababbed24ae90c0bd5f3
MD5 a3fea4ecac38a54007d2065a36524784
BLAKE2b-256 33361d20f50b6d970b25ae82021afaf433ea9913ee2d1615c35384db555c8964

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bc2347948d9144aeb26bd9483f856149821c3daa9d188aa45d7d89be99e722f5
MD5 1f3e4b208717371df4687128d63b3d29
BLAKE2b-256 497a84db5a3f9957b6abee4cc616ff0107d4686d8a58c59de4e59b51180b2f96

See more details on using hashes here.

File details

Details for the file zipline_reloaded-2.2.1.dev0-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for zipline_reloaded-2.2.1.dev0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b0cca11522256219ec18f73199d8b10e122fe839cf458ed17e7da8ad8d619d95
MD5 8bbd7ea7d51a2f135855ef69c35550a7
BLAKE2b-256 033d9b540b8c37310cd630491b3e22328d5bb32f5d2f9916e81242124ec508ff

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