Level 5 — MasteryIndicators & Oscillators

Indicators & Oscillators

90 minutes60 marksprintable — key stays hidden on paper

Level 5 — Mastery (cross-domain: math + statistics + coding) Time limit: 90 minutes Total marks: 60

Instructions: Answer all three questions. Show derivations, code logic (pseudocode or Python is accepted), and justify all claims. Use ...... / ...... for mathematics.


Question 1 — EMA smoothing, weights & lag (20 marks)

The Exponential Moving Average is defined recursively as Et=αPt+(1α)Et1,α=2N+1E_t = \alpha P_t + (1-\alpha)E_{t-1}, \qquad \alpha = \frac{2}{N+1} where PtP_t is the closing price and NN is the period.

(a) Prove by unrolling the recursion that the EMA is a weighted sum of all past prices, and show the weights form a geometric series that sums to 11 in the infinite limit. State the weight assigned to PtkP_{t-k}. (6 marks)

(b) The center of mass (average lag, in bars) of an indicator is L=k0kwkL=\sum_{k\ge0} k\,w_k where wkw_k is the weight on PtkP_{t-k}. Derive a closed form for the EMA's center of mass in terms of α\alpha, then in terms of NN. Compare it against the SMA lag of N12\frac{N-1}{2} for N=19N=19. (7 marks)

(c) Write a vectorised or iterative code function ema(prices, N) returning the full EMA series (seed the first value with the price or an SMA — state your choice). Then explain, in terms of your part (b) result, why an EMA reacts to a price shock faster than an SMA of the same NN. (7 marks)


Question 2 — RSI construction, bounds & divergence (20 marks)

RSI uses Wilder's smoothing over NN periods: RSI=1001001+RS,RS=UDRSI = 100 - \frac{100}{1+RS}, \qquad RS = \frac{\overline{U}}{\overline{D}} where U\overline{U}, D\overline{D} are smoothed average gains and losses.

(a) Prove that RSI[0,100]RSI\in[0,100] for all valid U,D0\overline{U},\overline{D}\ge0, and find the exact RS value that produces RSI=70RSI=70. (5 marks)

(b) A stock has these 8 daily closes: 44,45,46,45,47,46,48,5044, 45, 46, 45, 47, 46, 48, 50. Using simple averaging of the up/down moves across the 7 changes (not Wilder smoothing), compute U\overline{U}, D\overline{D}, RSRS and the resulting RSIRSI. Round RSI to one decimal. (8 marks)

(c) Define bearish RSI divergence precisely in terms of price highs and RSI highs. Then design pseudocode detect_bearish_divergence(price, rsi) that flags divergence over a lookback window, and explain one reason it can produce false signals in a strong trend. (7 marks)


Question 3 — MACD, Bollinger squeeze & indicator overload (20 marks)

(a) MACD line =EMA12EMA26=EMA_{12}-EMA_{26}; signal =EMA9(MACD)=EMA_9(\text{MACD}); histogram == MACD - signal. Prove that a bullish MACD/signal crossover occurs exactly when the histogram crosses from negative to positive, and relate the histogram's sign-change to the rate of change of the MACD line. (6 marks)

(b) Bollinger Bands use middle =SMAN=SMA_N, upper/lower =SMAN±kσ=SMA_N\pm k\sigma, with typically k=2k=2. The bandwidth is BW=upperlowermiddleBW=\frac{\text{upper}-\text{lower}}{\text{middle}}. For a window of prices {20,22,21,23,24}\{20,22,21,23,24\} with N=5N=5, k=2k=2, compute the SMA, the population standard deviation, the bandwidth, and state what a falling bandwidth (a "squeeze") implies. (8 marks)

(c) Two indicators are redundant if they encode strongly correlated information. Explain mathematically why combining RSI, Stochastic %K and Williams %R on the same chart is a form of indicator overload, using the fact that all three are bounded oscillators derived from recent price extremes/changes. Propose a principled 3-indicator dashboard (one from each of: trend, momentum, volatility) and justify the low-redundancy choice. (6 marks)


Answer keyMark scheme & solutions

Question 1

(a) (6 marks) Unroll: Et=αPt+(1α)[αPt1+(1α)Et2]=E_t=\alpha P_t+(1-\alpha)[\alpha P_{t-1}+(1-\alpha)E_{t-2}]=\dots Et=αk=0(1α)kPtk.E_t=\alpha\sum_{k=0}^{\infty}(1-\alpha)^k P_{t-k}.

  • Correct unrolling to two/three levels (2)
  • General form with weight wk=α(1α)kw_k=\alpha(1-\alpha)^k on PtkP_{t-k} (2)
  • Sum of weights: k=0α(1α)k=α11(1α)=α1α=1\sum_{k=0}^\infty \alpha(1-\alpha)^k=\alpha\cdot\frac{1}{1-(1-\alpha)}=\alpha\cdot\frac1\alpha=1 ✓ (geometric series, ratio 1α<11-\alpha<1) (2)

(b) (7 marks) L=k0kα(1α)k=α(1α)(1(1α))2=α1αα2=1αα.L=\sum_{k\ge0}k\,\alpha(1-\alpha)^k=\alpha\cdot\frac{(1-\alpha)}{(1-(1-\alpha))^2}=\alpha\cdot\frac{1-\alpha}{\alpha^2}=\frac{1-\alpha}{\alpha}. Using krk=r(1r)2\sum k r^k=\frac{r}{(1-r)^2} with r=1αr=1-\alpha. (3) Substitute α=2N+1\alpha=\frac2{N+1}: L=12N+12N+1=N1N+12N+1=N12.L=\frac{1-\frac2{N+1}}{\frac2{N+1}}=\frac{\frac{N-1}{N+1}}{\frac{2}{N+1}}=\frac{N-1}{2}. (2) For N=19N=19: LEMA=182=9L_{EMA}=\frac{18}{2}=9; LSMA=N12=9L_{SMA}=\frac{N-1}{2}=9. They are equal in center-of-mass. (2) (Note/insight: EMA has same average lag but a fatter recent-weight tail, so it responds faster to new data — leads into (c).)

(c) (7 marks)

def ema(prices, N):
    alpha = 2/(N+1)
    e = [prices[0]]              # seed with first price
    for p in prices[1:]:
        e.append(alpha*p + (1-alpha)*e[-1])
    return e
  • Correct recursion & alpha (3); valid seeding stated (1)
  • Explanation (3): Although average lag matches SMA, the EMA weight w0=αw_0=\alpha on the most recent price exceeds the SMA's flat weight 1N\frac1N (since α=2N+1>1N\alpha=\frac2{N+1}>\frac1N for N>1N>1). A price shock at tt therefore moves the EMA more on the same bar, whereas the SMA weights it only 1/N1/N and must also wait for the shock to enter/exit the fixed window. Hence faster reaction to shocks.

Question 2

(a) (5 marks) As RS=U/D[0,)RS=\overline U/\overline D\in[0,\infty), 1+RS[1,)1+RS\in[1,\infty), so 1001+RS(0,100]\frac{100}{1+RS}\in(0,100], giving RSI=1001001+RS[0,100)RSI=100-\frac{100}{1+RS}\in[0,100). Limits: RS=0RSI=0RS=0\Rightarrow RSI=0; RSRSI100RS\to\infty\Rightarrow RSI\to100. Bounds proven. (3) For RSI=70RSI=70: 70=1001001+RS1001+RS=301+RS=10030RS=732.33370=100-\frac{100}{1+RS}\Rightarrow\frac{100}{1+RS}=30\Rightarrow 1+RS=\frac{100}{30}\Rightarrow RS=\frac{7}{3}\approx2.333. (2)

(b) (8 marks) Changes: +1,+1,1,+2,1,+2,+2+1,+1,-1,+2,-1,+2,+2. Gains: 1,1,2,2,21,1,2,2,2 → sum =8=8; Losses: 1,11,1 → sum =2=2. Over 7 periods: U=8/7\overline U=8/7, D=2/7\overline D=2/7. (3) RS=8/72/7=4RS=\dfrac{8/7}{2/7}=4. (2) RSI=1001001+4=10020=80.0RSI=100-\frac{100}{1+4}=100-20=80.0. (3)

(c) (7 marks) Bearish divergence: price makes a higher high while RSI makes a lower high over the same interval — momentum weakening despite rising price. (2)

def detect_bearish_divergence(price, rsi, lookback=20):
    highs = local_maxima_indices(price, lookback)   # swing highs
    for a, b in consecutive_pairs(highs):
        if price[b] > price[a] and rsi[b] < rsi[a]:
            return True, (a, b)
    return False, None
  • Correct definition (2); pseudocode identifying two swing highs & comparing price↑ vs RSI↓ (3)
  • False-signal reason (2): in a strong uptrend RSI can stay pinned high (repeated overbought) and print lower peaks purely from smoothing/scaling while price keeps advancing legitimately — divergence fires early and the trend continues.

Question 3

(a) (6 marks) Histogram H=H= MACD - signal. A bullish MACD/signal crossover is defined as MACD rising through signal, i.e. transition from MACD << signal to MACD >> signal     \iff HH transitions from <0<0 to >0>0. Thus crossover \equiv histogram zero-crossing (negative→positive). (3) Since signal =EMA9(MACD)=EMA_9(\text{MACD}) lags MACD, HH\approx MACD - (its own smoothed value) \propto the recent rate of change of MACD; H>0H>0 and rising indicates MACD accelerating upward. Sign change of HH marks where MACD's momentum turns positive relative to its average. (3)

(b) (8 marks) Mean =20+22+21+23+245=1105=22=\frac{20+22+21+23+24}{5}=\frac{110}{5}=22. (2) Deviations: 2,0,1,1,2-2,0,-1,1,2; squares 4,0,1,1,44,0,1,1,4, sum =10=10. Population variance =10/5=2=10/5=2; σ=21.4142\sigma=\sqrt2\approx1.4142. (3) Upper =22+2224.828=22+2\sqrt2\approx24.828; lower =222219.172=22-2\sqrt2\approx19.172. BW=4222=22110.2571BW=\frac{4\sqrt2}{22}=\frac{2\sqrt2}{11}\approx0.2571. (2) Falling bandwidth = squeeze: contracting volatility, bands tighten; often precedes a volatility expansion / breakout. (1)

(c) (6 marks) RSI, Stochastic %K and Williams %R are all bounded momentum oscillators built from recent price range/change; %K and Williams %R are algebraically near-mirror images (%R=%K100\%R=\%K-100 over the same NN), and both correlate strongly with RSI. Stacking them adds near-zero independent information but multiplies visual noise and gives a false sense of "confirmation" (correlated signals agreeing is not independent evidence) — classic indicator overload. (3) Low-redundancy dashboard: Trend = ADX (or MA crossover) — measures directional strength; Momentum = RSI or MACD — one oscillator only; Volatility = Bollinger Bandwidth or ATR — measures dispersion. Each captures an orthogonal dimension (direction / speed / spread), minimizing correlation and maximizing information per indicator. (3)

[
  {"claim":"EMA center of mass = (N-1)/2 for N=19 equals 9","code":"N=19; alpha=Rational(2,N+1); L=(1-alpha)/alpha; result=(L==9)"},
  {"claim":"RS for RSI=70 is 7/3","code":"RS=symbols('RS'); sol=solve(Eq(100-100/(1+RS),70),RS)[0]; result=(sol==Rational(7,3))"},
  {"claim":"Q2b RSI equals 80","code":"U=Rational(8,7); D=Rational(2,7); RS=U/D; RSI=100-100/(1+RS); result=(RSI==80)"},
  {"claim":"Bollinger population sigma sqrt2 and bandwidth 2*sqrt2/11","code":"vals=[20,22,21,23,24]; m=Rational(sum(vals),5); var=sum((v-m)**2 for v in vals)/5; sd=sqrt(var); bw=(4*sd)/m; result=(simplify(sd-sqrt(2))==0 and simplify(bw-2*sqrt(2)/11)==0)"}
]