Zipline, Backtesting in Python 2: My First Data Analysis

Zipline, Backtesting in Python 2: My First Data Analysis

In this second part of the Zipline tutorial, we will perform our first data analysis using bundles and build a basic strategy. In the first part, we introduced Zipline and its integration with the Quant Stack and Pipeline.

What is a Bundle?

A bundle in Zipline is a data source that has been formatted and indexed for use in backtesting. Bundles can contain daily price data, corporate actions (splits, dividends), and other relevant information for backtesting.

Loading a Bundle

To load a bundle in Zipline, we first need to make sure it is available. The most common bundle is the quandl/eod bundle, which contains end-of-day data for US stocks.

from zipline.data import bundles

# List available bundles
bundles.bundles

# Load a bundle
bundle_data = bundles.load('quandl-eod')

First Data Analysis

Once we have loaded the bundle, we can start exploring the data:

import pandas as pd

# Get the list of available assets
assets = bundle_data.asset_finder.retrieve_all(
    bundle_data.asset_finder.sids
)

# Get data for a specific asset
from zipline.pipeline.data import USEquityPricing
from zipline.pipeline.loaders import USEquityPricingLoader

# Create a pipeline
from zipline.pipeline import Pipeline
from zipline.research import run_pipeline

pipe = Pipeline()
pipe.add(USEquityPricing.close.latest, 'close')

# Run the pipeline
data = run_pipeline(pipe, start='2020-01-01', end='2023-12-31')
print(data.head())

Building a Basic Strategy

Now that we know how to load data, we can build a basic trading strategy:

from zipline.api import order_target_percent, record, symbol, schedule_function
from zipline.utils.events import date_rules, time_rules

def initialize(context):
    context.asset = symbol('AAPL')
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())

def rebalance(context, data):
    order_target_percent(context.asset, 0.5)

def analyze(context, perf):
    import matplotlib.pyplot as plt
    perf.portfolio_value.plot()
    plt.show()

Running the Backtest

To run the backtest, we need to execute the strategy with the loaded data:

from zipline import run_algorithm
import pandas as pd

start = pd.Timestamp('2020-01-01', tz='utc')
end = pd.Timestamp('2023-12-31', tz='utc')

results = run_algorithm(
    start=start,
    end=end,
    initialize=initialize,
    analyze=analyze,
    capital_base=10000,
    bundle='quandl-eod'
)

Backtrader vs Zipline

If you are starting out, the usual question is Zipline or Backtrader. Both are event-driven Python backtesters, but with different philosophies:

  • Zipline comes from Quantopian and is built for systematic research at scale: data bundles, pipelines and an algorithm API that keeps strategy logic separate from infrastructure. The maintained branch is zipline-reloaded, the one we use in Part 1 of this series.
  • Backtrader shines for flexibility: you can feed CSVs without an ingest step, it integrates with brokers and offers a live mode that Zipline lacks. The trade-off is that its development has slowed down considerably in recent years.

Rule of thumb: systematic research with pipelines and your own data, Zipline; quick prototypes or live trading, Backtrader. If order execution is your concern, our guide on finite-state machines for execution complements either one.

FAQ

How do I ingest a data bundle?

Run zipline ingest -b bundle-name from the command line. For research with your own data, the most flexible path is building a datalake and loading it with ArcticDB, as we show in Big Data Management 4.

How do I read prices outside the pipeline?

Use data.history() inside the algorithm or a DataPortal outside it, as in the "First Data Analysis" section above: you get a pandas DataFrame indexed by time, ready for any analysis.

Zipline or Backtrader?

See the comparison above. In short: Zipline for systematic research, Backtrader for quick prototypes and live trading.

Conclusion

In this second part, we have learned how to work with bundles in Zipline, perform basic data analysis, and build a simple trading strategy. In the next part, we will explore more advanced strategies and pipeline features. In the meantime, you can apply backtesting to intraday patterns with the seasonality series or read how we use finite-state machines for order execution.

Jesús Cuesta

Odesa (Ucrania)
Inversor desde 2014. Research desde 2017. He trabajado en diferentes gestoras de capital y Hedgefunds Crypto. Apasionado del codigo, los datos y las finanzas. Actualmente localizado en Ucrania.

You might also like