Rolling Windows: Moving Avg, Bollinger, Volatility, Drawdown | Pandas for Finance Ep11

0views
C
CelesteAI
Description
Episode 11 of Pandas for Finance. Rolling windows — the cornerstone of every technical indicator and risk metric in finance. Source code: https://github.com/GoCelesteAI/pandas-for-finance df["Close"].rolling(50).mean() is a 50-day moving average. .rolling(20).std() is the 20-day rolling standard deviation. Bollinger Bands are mean plus or minus two times that std. Annualized volatility is rolling std times the square root of 252. And .expanding().max() gives you the running peak — combine that with a wealth curve and you have the drawdown metric every backtest reports. What You'll Build: - rolling.py — load cached prices, filter to AAPL, set Date as the index, then compute 50/200-day moving averages, Bollinger Bands, 30-day annualized volatility, and the max drawdown. - The rolling window pattern: .rolling(N).mean() and .rolling(N).std() — produces NaN for the first N-1 rows, then the rolling stat from there. - Bollinger Bands in three lines: 20-day rolling mean and std, then mean plus or minus 2 sigma for the bands. - Annualized volatility: ret.rolling(30).std() times (252 0.5). - Drawdown: wealth = (1 + ret.fillna(0)).cumprod(), peak = wealth.expanding().max(), drawdown = wealth divided by peak minus 1. The 2018 tech selloff in one number. Timestamps: 0:00 - Intro — Episode 11 starts here 0:19 - Preview — moving averages, Bollinger, vol, drawdown 0:58 - Open nvim, write rolling.py 1:16 - 50 / 200 day moving averages 1:34 - Bollinger Bands (20-day, 2 sigma) 2:00 - Rolling 30-day annualized volatility 2:20 - Drawdown via expanding peak 2:47 - Save and run 2:55 - Moving averages output 3:06 - Bollinger Bands output 3:16 - Volatility output 3:22 - Max drawdown 3:50 - Recap 4:32 - End screen Key Takeaways: 1. .rolling(N) walks the last N rows and applies a function. .rolling(50).mean() is a 50-day moving average; .rolling(20).std() is rolling standard deviation. The first N-1 rows are NaN because there isn't enough history yet. This pattern is the cornerstone of every technical indicator in finance: moving averages, MACD, RSI, anything that says "look at the last N days." 2. Bollinger Bands = mean plus-or-minus 2 sigma.** Compute m = close.rolling(20).mean() and s = close.rolling(20).std(); then upper is m + 2*s and lower is m - 2*s. The 20-day window with 2-sigma bands is the textbook setting; tighten or widen the multiplier to suit. Most price action sits inside the bands; touches and breakouts are flagged as turning points (depending on who you ask). 3. Annualized volatility is rolling.std() times sqrt(252). A 30-day rolling std on daily returns gives you a one-month risk gauge in daily terms; multiplying by (252 0.5) rescales to annual. The number you read on a Bloomberg page or a risk dashboard is exactly this calculation. 4. .expanding() is "all data so far," not "the last N." Use it for cumulative max and min — wealth.expanding().max() gives the running peak. That's the missing piece for drawdown: drawdown = wealth divided by peak minus 1. Take .min() to get the worst day, .idxmin().date() for when. The 2018 tech selloff for AAPL: minus 38 percent on January 3, 2019. 5. For arbitrary window math, .rolling(N).apply(fn).** Slower than vectorized rolling but works for anything: rolling Sharpe, rolling beta, custom percentile calculations. The fast path stays in mean, std, min, max, sum, var, quantile, median, kurt, skew — pandas vectorizes those. This channel is run by Claude AI. Tutorials AI-produced; reviewed and published by Codegiz. Source code at codegiz.com. #Pandas #Python #Finance #RollingWindows #BollingerBands #Drawdown #Volatility #DataAnalytics #PythonForFinance #LearnPandas #ClaudeCode --- Generated by Claude AI · part of the Pandas for Finance series