Skip to main content

A Pythonic backtester for trading algorithms

Project description

Backtest your Trading Strategies

Zipline Arrow Version Info Python PyPI
Zipline Reloaded Community Discourse ML4T Twitter

Zipline Arrow (zipline-arrow) is a fork of Zipline Reloaded (zipline-reloaded) that includes modifications necessary to use zipline-polygon-bundle with custom aggregates calculated from Polygon trades (tick) data flatfiles (https://polygon.io/blog/flat-files). If your's isn't that specific use case then the upstream Zipline Reloaded is probably what you want. Even if you use Polygon minute or daily aggregate flatfiles then you can use zipline-polygon-bundle<0.2 which depends on zipline-reloaded instead of zipline-arrow.

Future plans (and the reason for the name choice) is that Zipline Arrow will migrate from the moribund bcolz-reloaded (Stefan's maintaince fork of the archived bcolz) to PyArrow.

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.2.2 (the first working release of zipline-arrow) changes the default calendar handling in order to properly support calendars that aren't in a special list of calendars ("us_futures", "CMES", "XNYS", "NYSE"). That is needed because of the bad interaction of needing to use a new calendar for extended hours and Exchange Calendars uses a default start_date of 20 years before today (whenever that is) when getting a calendar without specifying a start_date (which zipline.utils.calendar_utils.get_calendar defaults to 1990-01-01 for the hardcoded list of calendars). In order to make that work smoothly, the default handling of calendars with ingested bundles is also changed so that the calendar loaded from the bundle's metadata is always used (instead of using get_calendar by name).

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_arrow-3.2.2.tar.gz (12.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

zipline_arrow-3.2.2-cp312-cp312-win_amd64.whl (4.8 MB view details)

Uploaded CPython 3.12Windows x86-64

zipline_arrow-3.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.12macOS 11.0+ x86-64

zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_arm64.whl (4.8 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

zipline_arrow-3.2.2-cp311-cp311-win_amd64.whl (4.8 MB view details)

Uploaded CPython 3.11Windows x86-64

zipline_arrow-3.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ x86-64

zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_arm64.whl (4.8 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

zipline_arrow-3.2.2-cp310-cp310-win_amd64.whl (4.8 MB view details)

Uploaded CPython 3.10Windows x86-64

zipline_arrow-3.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_arm64.whl (4.8 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file zipline_arrow-3.2.2.tar.gz.

File metadata

  • Download URL: zipline_arrow-3.2.2.tar.gz
  • Upload date:
  • Size: 12.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zipline_arrow-3.2.2.tar.gz
Algorithm Hash digest
SHA256 469bed70d2256f086f5ab00341066daeb89c98f35fefdf8c175662ac24f10db5
MD5 7e90cdcd951133c1c18508e676248a1e
BLAKE2b-256 7adbec10a7002b6f6dd795c01bdf8aaffdc805accd9e15dc9e06662ec8e8ca97

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2.tar.gz:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 232745aaf265e0102a463a3f8f4cf35ac8ddd8c1b7c86674933b2b2e95acf26b
MD5 9b7dd28cc76a12467650d52592135d58
BLAKE2b-256 959dd9ecf86e2ee09b6ba09ba6b6e1b37b2850bbbd317ca0fbb0d9137ac1d3e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp312-cp312-win_amd64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2306325b1f0349041c9d283e3100c133dcedcf1fe2da377c210e133d7e57dc94
MD5 a7738bd0ff0bda30c4922e446092c314
BLAKE2b-256 49632701f8e311cbbbb312652d27570898dc1a609615e1549b52fa18c30b416a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 90310b33ecd187dc5e44311ed360dedaa100042d0c5e57353c9541715f77231a
MD5 f4be798e3cbdf8a56226b7f13d4a56be
BLAKE2b-256 77ebfb869523a78fb7bd6f81984d35ac289cf3e8756d7a6034feb38729b58207

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7301af3463e0d0957042c7489a31098a8c4c0127dabf429e156871ce4bacee31
MD5 e38db9c0bae296ec1384df5b9a205809
BLAKE2b-256 b542d8c07d44a3490025908b94a160401d59341d0fa6f890136ce3d248562c68

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e05a97bc86c4d712e1c3f7f0bc78b667d75a75203b76b0f0ebeea0ef0c863003
MD5 732b5c6a6bc6d9cd214fa8c185bfb53d
BLAKE2b-256 bea551f6100816984f277e4819ec8d2d4cc26ea6798023ee1d24f3acae1b8708

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp311-cp311-win_amd64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e437d561ad2e3a05203f881ac937ea5b43b37a80ceb22b68f5777c1dfb8dd838
MD5 f20b55343f906d5acf5ad12fbd08b5c8
BLAKE2b-256 d2e583fcb0e5921b9eedec1cfb6c1809cb9c1f0dc437f0632ba499e39f406390

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 ff83c705d2120b1587fec9368d4088d1ed9322ca2c7014987a563bd52d55faf9
MD5 9d85f477b2cfa4d1312847591c2f705b
BLAKE2b-256 4bb4a2bb2d6fddd78cfd65040d5187b41cd08a91cd97cac5e39ce0fe76c16c6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7e1732ba60c1ff8d135edbe87a4718c0ef7b94b43266da9e791f1cf009744a19
MD5 1766a8d1fdad93d4da8f883d1467fd56
BLAKE2b-256 ca6393cd6e9b040bd97dd0130c0acd81f6719325bbdf96aee62737963063be41

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6f21ae04e03808db5376ac67af08a9867310321b15f0f6cb92ca639ab050555f
MD5 6c07bd29d5d595d27e21ff7d649c6ff6
BLAKE2b-256 2419bda18a783d9a494a3e6631124135316fc8462f01ea858413f2a8bcc0154f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp310-cp310-win_amd64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 941dd0194027c0b12675130147a297413bef42d8a1ff5d144d098f9bc03cef5c
MD5 e405677c3e381687be6c008fc3b2e5a9
BLAKE2b-256 ba4dbfecac88ed631ee803e50c7341e0db2f2005754b7b99b28e7e2d4d0a5d5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 8158bba943c0893b24b26c9858c88e9401a3691d388fca5930daf96cb35e8619
MD5 96101174b813c5c58c5fe3f2b5466c22
BLAKE2b-256 a475463e739a5afdbe94ab72a72778f8deef5ddf0847ffb64194013c540b723b

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_x86_64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fee11755a607a21742cf724b0404592e6eed3fb37a721d2bcafa4e8256e87d25
MD5 dfd74f01489841e5d0b11fc9b5761f1c
BLAKE2b-256 b038d9d18214a6d88f8152c56a79255c091e43cc8e02c580d89c29424e75489f

See more details on using hashes here.

Provenance

The following attestation bundles were made for zipline_arrow-3.2.2-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on fovi-llc/zipline-arrow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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