Moving averages are the backbone of most trading systems. From the RBI monitoring economic data to Goldman Sachs' algorithms — MAs are everywhere.
Simple Moving Average (SMA)
SMA(n) = (P₁ + P₂ + ... + Pₙ) / n
The average closing price over the last n periods. Lags price — this is a feature, not a bug. It filters noise and shows the underlying trend.
Exponential Moving Average (EMA)
Gives more weight to recent prices. Reacts faster than SMA. Preferred by most active traders for entry/exit signals.
import pandas as pd
def calculate_ema(prices, period):
return prices.ewm(span=period, adjust=False).mean()
df['ema_9'] = calculate_ema(df['Close'], 9)
df['ema_21'] = calculate_ema(df['Close'], 21)
df['sma_50'] = df['Close'].rolling(50).mean()
df['sma_200'] = df['Close'].rolling(200).mean()
The 200 DMA: The Most Important Line
The 200-day SMA is the king of moving averages. When price is above 200 DMA, the stock is in a long-term uptrend. Below 200 DMA = long-term downtrend. FIIs and large institutions use this as the primary trend filter.
Market Rule of ThumbNever buy a stock below its 200 DMA unless you're an experienced contrarian. More stocks die below the 200 DMA than recover.
Golden Cross & Death Cross
Golden Cross ✓
50 SMA crosses above 200 SMA. Signals start of a bull run. Very high success rate on weekly charts.
Death Cross ✗
50 SMA crosses below 200 SMA. Signals start of a bear market. Exit long positions or hedge.
The 9/21 EMA Crossover (Swing Trading)
For swing traders: when 9 EMA crosses above 21 EMA with rising volume = buy signal. When 9 EMA crosses below 21 EMA = exit/short signal. Works best in trending markets.
Today's Setup
On any Nifty 50 stock's daily chart, add: 9 EMA, 21 EMA, 50 SMA, 200 SMA. Observe how price interacts with each level. Note how price bounces from the 200 SMA in bull markets.