insideout Get Pro

05 · Resampling

Pro

Python for Market Data, lesson 5

Resampling

Turning daily data into weekly or monthly without mistakes.

You can compute returns and rolling windows on a table of daily prices.

Part 1. Squeeze a week into one number

Three months of daily closes for a simulated stock. Turn them into weekly prices, and choose how each week becomes one number.

Daily to weeklyLive

last() kept each Friday's close, a price you could have traded at, so returns between those dots are real weekly returns. mean() drew a smoother line, but each dot was a blend of five days with a Friday label. Over 40 years of simulated prices, its weekly changes were correlated with each other, about 0.25, while the prices underneath had no pattern at all.

Resample

resample("W-FRI") groups rows into weeks ending on Friday and labels each week with that Friday. "ME" groups by calendar month and labels each with the month's last day; pandas before version 2.2 spells it "M". Then you say how to squash each group: last(), first(), mean(), max() or sum().

Your turn

You average each week's daily prices, then take returns between the weekly averages. What do those returns look like?

Neighbouring weekly averages share the stretch of prices near their boundary, so one week's change leaks into the next. A random walk averaged like this shows weekly changes with a correlation near 0.25, a pattern that isn't in the prices. A signal built on it trades on nothing.

Part 2. Read it in plain words

Keep going with Pro

You've done the first part. Pro opens the rest of this lesson, and every other one.

Still to come
  1. Read it in plain words
  2. Worked: monthly returns and weekly bars
  3. The same thing, for real

Python for Market Data overviewSpotted a mistake? Tell us.