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.
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?
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.
- Read it in plain words
- Worked: monthly returns and weekly bars
- The same thing, for real
Python for Market Data overviewSpotted a mistake? Tell us.