Technical Indicators: Tools for Signal Generation
Master the essential technical indicators used in algorithmic trading: moving averages, RSI, Bollinger Bands, and MACD.
Essential Technical Indicators
Technical indicators are mathematical calculations based on price and volume that help identify trends, reversals, and momentum. They're the building blocks of most algorithmic trading signals.
Simple Moving Average (SMA)
Sum of last n closing prices divided by n
SMA smooths out daily price noise to show the underlying trend. A rising SMA means the market is moving higher on average. A falling SMA means lower on average.
df['SMA_20'] = df['Close'].rolling(window=20).mean() # 20-day SMA
df['SMA_50'] = df['Close'].rolling(window=50).mean() # 50-day SMA
# Golden cross: SMA_20 crosses above SMA_50 (bullish signal)
df['Signal'] = 0
df.loc[df['SMA_20'] > df['SMA_50'], 'Signal'] = 1 # Buy
df.loc[df['SMA_20'] < df['SMA_50'], 'Signal'] = -1 # Sell
Exponential Moving Average (EMA)
Like SMA but gives more weight to recent prices. Reacts faster to price changes than SMA.
Recursive formula weighted toward recent prices
RSI: Relative Strength Index
Measures momentum on 0-100 scale
RSI ranges from 0 to 100. RSI > 70 = overbought (potential sell). RSI < 30 = oversold (potential buy).
def calculate_rsi(prices, period=14):
deltas = prices.diff()
seed = deltas[:period+1]
up = seed[seed >= 0].sum() / period
down = -seed[seed < 0].sum() / period
rs = up / down
rsi = 100 - 100 / (1 + rs)
return rsi
df['RSI'] = df['Close'].rolling(window=14).apply(
lambda x: calculate_rsi(x, 14), raw=False
)
Bollinger Bands
SMA ± 2 standard deviations
Bands expand when volatility increases, contract when volatility decreases. Price touching upper band = overbought. Price touching lower band = oversold.
MACD: Moving Average Convergence Divergence
Difference between two exponential moving averages
MACD crossover: When MACD line crosses above signal line = bullish. Below signal line = bearish.
Indicators Are Lagging
All technical indicators are based on past prices. They react to moves that have already happened. Don't expect perfect entry points. Combine multiple indicators for confirmation.
- Moving averages smooth price data to identify trends
- RSI measures momentum on a 0-100 scale
- Bollinger Bands show volatility and overbought/oversold levels
- Indicators are lagging—they react to prices that already moved