Skip to main content

Programming for quant · 12 min read

Time series with pandas

Returns, rolling and exponentially weighted volatility, resampling, and the one-line shift that keeps a backtest honest.

Before you start

  • Thinking in arrays with NumPy (this track)
  • Random walks and Brownian motion

By the end you'll be able to

  • Turn prices into returns and keep data aligned by date
  • Estimate volatility with rolling and EWMA windows, and choose the window
  • Resample daily data to weekly or monthly correctly
  • Spot and fix look-ahead bias with shift

Market data is a time series: values attached to timestamps, in order, with gaps for weekends and holidays. pandas is the standard Python tool for it, and most of a quant researcher's day is spent turning prices into returns, returns into rolling statistics, and statistics into signals. This lesson covers the operations that matter most and the one mistake that turns an honest backtest into fiction.

TermMeaning
indexThe timestamps a Series or DataFrame is labelled with
returnThe proportional change rt=Pt/Pt−1−1r_t = P_t / P_{t-1} - 1
rolling windowA statistic computed over the last ww observations, recomputed each day
EWMAExponentially weighted moving average: recent data weighted more
resampleChange frequency, for example daily prices to weekly prices
look-ahead biasUsing information that wasn't available at the time of the decision

The rest of this lesson needs lifetime access

It will be part of lifetime access, which is coming soon: every lesson in all four tracks, including lessons added later, for one payment.

See pricing

Already have access? Sign in