The Chande Momentum Oscillator is one of the most direct oscillators to code — no smoothing, no seeding, just two rolling sums and one division. If you’ve built RSI, this will feel like RSI with the hard part removed. See the explainer.
What you’ll need
- Python 3.9+,
pandas/numpy - A
closeprice series.
From scratch
import numpy as np
import pandas as pd
def cmo(close, period=9):
delta = close.diff()
up = delta.clip(lower=0) # up-moves (negatives → 0)
down = -delta.clip(upper=0) # down-moves as positives
su = up.rolling(period).sum() # sum of up-moves
sd = down.rolling(period).sum() # sum of down-moves
return (100 * (su - sd) / (su + sd)).rename("cmo")
delta.clip(lower=0) keeps only the up-moves; -delta.clip(upper=0) flips the down-moves
to positive magnitudes. We sum each over the window, then (SU − SD) / (SU + SD) gives
net momentum on the ±100 scale from the output chart. That’s the entire indicator.
The key contrast with RSI
Look at what’s missing: there’s no ewm, no Wilder smoothing, no alpha. RSI would run
up and down through an exponential average before dividing. The CMO deliberately uses
plain rolling .sum(), which is why it’s faster and reaches the extremes more often. If
you find yourself reaching for .ewm() here, you’re building RSI, not the CMO.
Watch the divide-by-zero
If price is dead flat across the whole window, both SU and SD are zero and you get
0/0 = NaN. In practice this only happens on synthetic or illiquid data, but guard it if
you must:
denom = (su + sd).replace(0, np.nan)
cmo = 100 * (su - sd) / denom
A flat window has no momentum, so NaN (or 0, if you prefer to fill it) is the honest
answer.
A zero-cross and reversal signal
c = cmo(df["close"], 9)
bull_zero = (c > 0) & (c.shift() <= 0) # momentum flips positive
bear_zero = (c < 0) & (c.shift() >= 0)
oversold_turn = (c > -50) & (c.shift() <= -50) # leaving oversold
The zero-cross is the CMO’s cleanest trend cue; the ±50 turns are the overbought/oversold version — both easy to backtest in AlgoGen.
Gotchas
- No smoothing. Plain
.sum(), not.ewm(). Smoothing turns it back into RSI. - Default period 9 is Chande’s; it’s twitchy. Try 14 or 20 for calmer signals.
- ±50, not ±70/30. The CMO’s wider thresholds reflect that it reaches extremes more readily than RSI.
- Divide-by-zero on flat windows. Rare, but guard it if your data has flat spots.
The lazy one-liner
import pandas_ta as ta
df["cmo"] = ta.cmo(df["close"], length=9)
Same indicator elsewhere: MQL5, Pine Script, EasyLanguage, NinjaScript.
This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.
Sources and further reading
- Windowing operations (pandas documentation)
