insideout Get Pro
Learn
Quant engineering · Module 04

Python for Market Data

Working with market data in numpy and pandas, without look-ahead.

Start here · Lesson 01

Arrays, not loops

numpy: doing maths on a whole column at once.

Start lesson

LessonsAll lessons

  1. 01 Arrays, not loopsnumpy: doing maths on a whole column at once. Free
  2. 02 Tables of pricespandas DataFrames: rows of dates, columns of stocks. Free
  3. 03 Dates and alignmentTrading calendars, missing days and joining series correctly. Free
  4. 04 Returns and rolling windowsDaily returns, moving averages and rolling volatility. Pro
  5. 05 ResamplingTurning daily data into weekly or monthly without mistakes. Pro
  6. 06 Ranks across stocksComparing every stock on the same day: the cross-section. Pro
  7. 07 Point-in-time joinsUsing only what was known at the time, so no look-ahead. Pro
  8. 08 A backtest in twenty linesSignal, position, return, curve, all vectorised. Pro
  9. 09 Plotting to thinkThe few charts that catch most data problems. Pro
  10. 10 Making it fastVectorise first, then measure where the time goes. Pro

Build it yourself · on your own machine

A price pipeline with no look-ahead

A pandas pipeline that turns raw daily price files into a clean table, a monthly signal and a backtest, with a test that catches any peek at the future.
  1. Download ten years of daily prices for 20 large NYSE and Nasdaq stocks and two LSE stocks, plus FRED's daily pound-to-dollar rate (DEXUSUK).
  2. Load them into one long table, pivot to dates down and stocks across, join the two trading calendars by date, and forward-fill gaps of at most three days.
  3. Convert the LSE prices from pence to dollars, then print every daily move over 40% and explain each one as a split, a data error or real news.
  4. Compute daily returns, 20-day rolling volatility and month-end returns, and rank the stocks each month on the past 12 months' return, skipping the latest month.
  5. Write a vectorised backtest in about 20 lines that holds the top five and shorts the bottom five, with positions shifted one month.
  6. Write a truncation test: rebuild the signals from data cut at 20 random dates, and check that every signal up to each cut is unchanged.

How you'll know it worksThe truncation test passes at all 20 cut dates and fails when you delete the shift on purpose, and the pipeline runs in under a second once the files are loaded.

Python with pandas, NumPy and matplotlib. You download the price CSVs yourself from a free source such as Stooq or Yahoo Finance; FRED's data is free. Uses lessons 02 to 10.