blog · ~6 min read

Python for Trading: pandas, numpy, backtrader

Python is the workhorse of quant research. Learn the three libraries every trader needs — pandas, numpy, and backtrader — and what each one does.

T By tradernewbie · Curated for beginners
#algorithmic#quant-trading
Dieser Artikel ist auf Englisch. Auf deiner Sprache ansehen? Google Translate →

Interaktive Tools funktionieren in der übersetzten Ansicht möglicherweise nicht.

Python for Trading: pandas, numpy, backtrader

Three libraries carry 90% of retail quant work. Master them and you can build, test, and trade almost anything.

Python's dominance in trading comes from its library ecosystem. Three packages in particular form the backbone of nearly every retail quant workflow: numpy for the math, pandas for the data, and backtrader (or a peer) for the strategy.

numpy: the numeric engine

numpy gives you fast array operations without Python loops. Every other quantitative library is built on it.

Core ideas:

  • Vectorization: operate on whole arrays at once, not element-by-element
  • Broadcasting: combine arrays of different shapes cleanly
  • Linear algebra: matrix operations, eigenvalues, regression solvers

A 100k-bar return series computed in numpy is ~100× faster than the equivalent Python loop. Any serious backtest depends on this.

import numpy as np
returns = np.diff(np.log(prices))
sharpe = np.mean(returns) / np.std(returns) * np.sqrt(252)

pandas: the data wrangler

pandas adds labeled, time-indexed data — exactly what OHLCV bars are. It's the workhorse for cleaning, aligning, and transforming market data.

Essential patterns:

  • DataFrame: a 2D table indexed by timestamp
  • Resampling: convert 1-minute bars to 5-minute, daily to weekly
  • Rolling windows: moving averages, rolling σ
  • Groupby: per-symbol or per-day aggregations
  • Reindexing: align two assets on a common timeline
import pandas as pd
df['sma'] = df['close'].rolling(50).mean()
df['vol'] = df['close'].pct_change().rolling(20).std()

90% of quant research time is spent in pandas — getting data clean enough that the math is meaningful.

backtrader: the strategy tester

backtrader is a Python framework for backtesting event-driven strategies. It handles order simulation, broker modeling, commission, slippage, and multi-asset portfolios.

Structure of a typical strategy:

class SmaCross(bt.Strategy):
    params = (('fast', 20), ('slow', 50))
    def __init__(self):
        self.fast = bt.ind.SMA(self.data.close, self.p.fast)
        self.slow = bt.ind.SMA(self.data.close, self.p.slow)
        self.cross = bt.ind.CrossOver(self.fast, self.slow)
    def next(self):
        if self.cross > 0:
            self.buy()
        elif self.cross < 0:
            self.close()

backtrader forces you to think in event-driven terms — what happens bar-by-bar — which is closer to live trading than vectorized backtests.

Alternatives to backtrader

  • vectorbt: vectorized, extremely fast, great for parameter sweeps
  • Backtesting.py: minimal and clean for simple strategies
  • Zipline / Zipline-Reloaded: Quantopian's old engine, still used for factor research
  • QuantConnect Lean: cloud-based, multi-asset, production-grade

A realistic workflow

  1. Pull data into a pandas DataFrame, clean it (timezone, gaps, splits)
  2. Engineer features with pandas rolling/groupby
  3. Test signal logic with numpy
  4. Build the strategy in backtrader with realistic commissions
  5. Run walk-forward and out-of-sample tests
  6. Connect to a broker API (ccxt for crypto, broker SDKs for equities)

Summary

numpy does the math fast, pandas handles the data, and backtrader simulates the trading. Together they cover the full quant research loop. Master these three and you can prototype, test, and refine almost any strategy a retail trader would ever want to build.

Related market data, powered by TradingView.

Educational content · Not financial advice · Trade at your own risk