---
title: "Zipline, Backtesting in Python 2: My First Data Analysis"
description: "Zipline data bundles in Python: load a bundle, list assets, query prices into pandas, run a first strategy and choose between Zipline and Backtrader."
url: "https://pythonparatrading.com/en/backtesting-python-zipline-bundle-2/"
lang: "en"
date: "2023-10-02"
updated: "2026-09-04"
image: "https://pythonparatrading.com/content/images/wordpress/2023/07/AdobeStock_473981217-scaled.jpeg"
tags: ["Portfolio", "Zipline"]
series: "Zipline in Python"
series_url: "https://pythonparatrading.com/en/series/zipline-in-python/"
authors: ["Jesús Cuesta"]
---

# Zipline, Backtesting in Python 2: My First Data Analysis

> Working with Zipline data bundles: load a bundle, explore it in pandas and run a first basic strategy.

- Language: en (English)
- Published: 2023-10-02
- Updated: 2026-09-04
- Topics: Portfolio, Zipline
- Series: Zipline in Python (part 2/2)
- Requires: Zipline Part 01 · zipline-reloaded · pandas · matplotlib
- Summary: In this second part of the Zipline tutorial, we will perform our first data analysis using bundles and build a basic strategy.

Key points:

- What a bundle contains and how to load one with bundles.load.
- Listing assets and querying prices through a pipeline into a pandas DataFrame.
- A basic strategy with schedule_function and order_target_percent, run with run_algorithm.
- Practical rule for choosing between Zipline and Backtrader.

---

In this second part of the Zipline tutorial, we will perform our first data analysis using bundles and build a basic strategy. This part focuses on data bundles and pandas analysis — if you have not installed Zipline yet, start with [how to test Zipline in Python (Part 1: Quant Stack & Pipeline)](https://pythonparatrading.com/en/zipline-backtesting-with-python-quant-stack/). There, 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.

```python
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:

```python
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:

```python
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:

```python
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 set up in [Part 1: the Zipline quant stack](https://pythonparatrading.com/en/zipline-backtesting-with-python-quant-stack/).
-   **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](https://pythonparatrading.com/en/finite-state-machines-trading-python/) 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](https://pythonparatrading.com/en/backtesting-with-zipline-and-arcticdb/).

### 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](https://pythonparatrading.com/en/seasonality-intraday-backtesting/) or read how we use [finite-state machines for order execution](https://pythonparatrading.com/en/finite-state-machines-trading-python/).
