Module 5 — Strategy Design
Moving Averages and Smoothing
Trend, reversion, and the diagnostic discipline that separates real edge from a curve fit.
Learning objectives
- ▸Compute SMA, EMA, and weighted MAs.
- ▸Understand lag tradeoffs.
- ▸Implement a basic MA-crossover signal.
FORMULA
Simple vs exponential
SMA_n(t) = (1/n) · Σ_{k=0..n-1} P_{t-k}
EMA: S_t = α·P_t + (1-α)·S_{t-1}, α = 2 / (n+1)
EMA reacts faster than SMA of equivalent length.TEXT
Lag is the curse of every smoother
A 50-day SMA tells you what was true ~25 days ago, not today. You can never eliminate the lag of a causal smoother — you can only trade off lag against noise. This is also why simple MA crossovers underperform in choppy markets: they generate whipsaws.
CODE
MA crossover signal
fast = prices.rolling(20).mean() slow = prices.rolling(50).mean() # +1 long, -1 short, 0 flat signal = np.sign(fast - slow).fillna(0) # shift by 1 to avoid look-ahead position = signal.shift(1) strategy_ret = position * prices.pct_change()