MACD (Moving Average Convergence Divergence)¶
Overview¶
The Moving Average Convergence Divergence (MACD) is one of the most popular and versatile trend-following momentum indicators in technical analysis. Developed by Gerald Appel in the late 1970s, MACD reveals changes in the strength, direction, momentum, and duration of a trend by comparing two exponential moving averages. The indicator consists of three components: the MACD line (difference between fast and slow EMAs), the signal line (EMA of the MACD line), and the histogram (difference between MACD and signal line), providing multiple layers of analysis for identifying trend changes and momentum shifts.
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
prices |
Array | Required | The price data array (typically close prices) |
fast_period |
Integer | 12 | Fast EMA period for MACD line calculation |
slow_period |
Integer | 26 | Slow EMA period for MACD line calculation |
signal_period |
Integer | 9 | Signal line EMA period |
Parameter Details¶
Note: Array elements should be ordered from oldest to newest (chronological order)
prices
- Typically uses closing prices, though any price data can be used
- Requires sufficient data points: at least slow_period + signal_period values for accurate results
- More historical data provides better context for divergence and trend analysis
- Works on any timeframe: minute, hourly, daily, weekly, or monthly charts
fast_period - Standard setting is 12 periods (Appel's original recommendation) - Represents the fast-moving exponential moving average - Shorter periods (5-8) create a more reactive MACD with earlier signals but more noise - Longer periods (15-20) create a smoother MACD with fewer false signals but delayed entries - Recommended ranges: - Day trading: 5-8 for faster response to intraday moves - Swing trading: 12 (standard) for balance between speed and reliability - Position trading: 15-19 for major trend identification
slow_period - Standard setting is 26 periods (Appel's original recommendation) - Represents the slow-moving exponential moving average - Creates the baseline for measuring momentum divergence - Shorter periods (19-21) increase sensitivity to recent price changes - Longer periods (30-40) smooth out noise and focus on major trends - Typical ratio: slow_period should be roughly 2x fast_period
signal_period - Standard setting is 9 periods (Appel's original recommendation) - Smooths the MACD line to create entry/exit signals - Shorter periods (5-7) generate more frequent crossover signals - Longer periods (12-16) reduce false signals but may miss short-term moves - Directly affects histogram formation and momentum interpretation
Usage¶
Basic Usage¶
require 'sqa/tai'
prices = [44.34, 44.09, 44.15, 43.61, 44.33, 44.83,
45.10, 45.42, 45.84, 46.08, 46.03, 46.41,
46.22, 45.64, 46.21, 46.25, 46.08, 46.46,
46.57, 45.95, 46.50, 46.02, 46.55, 47.03,
47.35, 47.28, 47.61, 48.12, 48.34, 48.21]
# Calculate MACD (returns three arrays)
macd, signal, histogram = SQA::TAI.macd(prices)
puts "MACD Line: #{macd.last.round(4)}"
puts "Signal Line: #{signal.last.round(4)}"
puts "Histogram: #{histogram.last.round(4)}"
With Custom Parameters¶
# Fast MACD for short-term trading
macd_fast, signal_fast, hist_fast = SQA::TAI.macd(
prices,
fast_period: 5,
slow_period: 13,
signal_period: 5
)
# Standard MACD (default parameters)
macd_std, signal_std, hist_std = SQA::TAI.macd(prices)
# Slow MACD for long-term trend identification
macd_slow, signal_slow, hist_slow = SQA::TAI.macd(
prices,
fast_period: 19,
slow_period: 39,
signal_period: 9
)
puts "Fast MACD: #{macd_fast.last.round(4)}"
puts "Standard MACD: #{macd_std.last.round(4)}"
puts "Slow MACD: #{macd_slow.last.round(4)}"
Multiple Output Values¶
# MACD returns three arrays for comprehensive analysis
macd_line, signal_line, histogram = SQA::TAI.macd(prices, fast_period: 12, slow_period: 26, signal_period: 9)
puts "MACD Line: #{macd_line.last.round(4)}"
puts "Signal Line: #{signal_line.last.round(4)}"
puts "Histogram: #{histogram.last.round(4)}"
# Check trend direction
if macd_line.last > 0
puts "MACD above zero: Bullish trend"
else
puts "MACD below zero: Bearish trend"
end
Understanding the Indicator¶
What It Measures¶
The MACD measures momentum and trend by analyzing the relationship between two moving averages:
- Trend Direction: Whether the market is in an uptrend or downtrend based on MACD line position relative to zero
- Momentum Strength: How quickly prices are moving and whether momentum is accelerating or decelerating
- Trend Changes: When shorter-term momentum diverges from longer-term momentum, signaling potential reversals
- Market Convergence/Divergence: The convergence (moving together) or divergence (moving apart) of two moving averages reveals shifts in market dynamics
MACD answers the questions: "Is the current trend strong or weakening? Are we approaching a trend change? Is momentum accelerating or decelerating?"
The histogram provides a visual representation of the distance between the MACD line and signal line, making momentum changes immediately apparent.
Calculation Method¶
The MACD is calculated using the following process:
- Calculate Fast EMA: Compute the exponential moving average using the fast period (typically 12)
- Calculate Slow EMA: Compute the exponential moving average using the slow period (typically 26)
- Calculate MACD Line: Subtract the slow EMA from the fast EMA (Fast EMA - Slow EMA)
- Calculate Signal Line: Apply another EMA to the MACD line using the signal period (typically 9)
- Calculate Histogram: Subtract the signal line from the MACD line to show the distance between them
Formula:
Fast EMA = EMA(prices, fast_period)
Slow EMA = EMA(prices, slow_period)
MACD Line = Fast EMA - Slow EMA
Signal Line = EMA(MACD Line, signal_period)
Histogram = MACD Line - Signal Line
Where:
- EMA = Exponential Moving Average
- Default periods: fast=12, slow=26, signal=9
- Positive MACD = Fast EMA above Slow EMA (bullish)
- Negative MACD = Fast EMA below Slow EMA (bearish)
- Histogram shows momentum acceleration/deceleration
Indicator Characteristics¶
- Range: Unbounded (can be any positive or negative value)
- Type: Trend-following momentum indicator with oscillator properties
- Lag: Moderate (uses smoothed averages but responsive to trend changes)
- Best Used: Trending markets, trend confirmation, momentum shifts, divergence analysis
- Limitations: Can generate false signals in ranging/choppy markets; lagging nature may result in delayed entries/exits
Interpretation¶
Value Ranges¶
Specific guidance on what different MACD values indicate:
- MACD > 0: Fast EMA is above slow EMA, indicating bullish momentum. The market is in an uptrend, and buying pressure exceeds selling pressure.
- MACD < 0: Fast EMA is below slow EMA, indicating bearish momentum. The market is in a downtrend, and selling pressure exceeds buying pressure.
- MACD near 0: The two EMAs are converging, suggesting momentum is neutral or transitioning. Often occurs during consolidation or trend changes.
- Large positive MACD: Strong upward momentum. The larger the value, the stronger the bullish trend.
- Large negative MACD: Strong downward momentum. The more negative the value, the stronger the bearish trend.
Key Levels¶
-
Zero Line: The most critical level. MACD crossing above zero indicates the fast EMA has crossed above the slow EMA (bullish). MACD crossing below zero indicates the fast EMA has crossed below the slow EMA (bearish). The zero line represents the equilibrium point between bullish and bearish forces.
-
Signal Line Crossovers: When MACD line crosses above the signal line, it generates a bullish signal. When MACD line crosses below the signal line, it generates a bearish signal. These are the primary trading signals.
-
Histogram Zero Line: When the histogram crosses zero, it indicates a MACD/signal crossover. Histogram above zero = MACD above signal (bullish). Histogram below zero = MACD below signal (bearish).
Signal Interpretation¶
How to read the MACD's signals:
- Trend Direction
- MACD above zero line: Uptrend in progress, bullish bias
- MACD below zero line: Downtrend in progress, bearish bias
- MACD at zero line: Trend transition, neutral momentum
-
Distance from zero indicates trend strength
-
Momentum Changes
- MACD rising: Increasing bullish momentum, trend acceleration
- MACD falling: Increasing bearish momentum or decelerating uptrend
- Histogram expanding: Momentum strengthening in current direction
-
Histogram contracting: Momentum weakening, possible reversal ahead
-
Reversal Signals
- MACD/Signal crossovers: Primary entry/exit signals
- Zero line crossovers: Trend confirmation and major trend changes
- Divergences: Price and MACD moving in opposite directions signal potential reversals
- Histogram peaks/troughs: Early warning of momentum exhaustion
Trading Signals¶
Buy Signals¶
Specific conditions that generate buy signals:
- Primary Signal - Bullish MACD Crossover: MACD line crosses above the signal line
- Confirms shift from bearish to bullish momentum
- Most reliable when occurring below zero line (buying into weakness)
-
Best when histogram has been negative and turns positive
-
Confirmation Signal - Zero Line Cross: MACD crosses above the zero line
- Indicates fast EMA has crossed above slow EMA
- Confirms a new uptrend is beginning
-
Strongest when preceded by bullish MACD/signal crossover
-
Bullish Divergence: Price makes lower low, but MACD makes higher low
- Suggests downtrend is losing momentum
- Most powerful when MACD is below zero line
-
Requires confirmation from MACD/signal crossover
-
Histogram Reversal: Histogram stops decreasing and begins increasing
- Earliest signal of momentum shift
- Precedes MACD/signal crossover by 1-3 periods
- Best when transitioning from negative to positive values
Example Scenario:
When MACD line crosses above signal line while both are below zero,
consider a long position. Ideal conditions:
- Price has found support at a key technical level
- Histogram transitioning from negative to positive
- Volume increasing on recent up days
- Set stop loss below recent swing low
- Target: Previous resistance or when MACD crosses below signal
Sell Signals¶
Specific conditions that generate sell signals:
- Primary Signal - Bearish MACD Crossover: MACD line crosses below the signal line
- Confirms shift from bullish to bearish momentum
- Most reliable when occurring above zero line (selling into strength)
-
Best when histogram has been positive and turns negative
-
Confirmation Signal - Zero Line Cross: MACD crosses below the zero line
- Indicates fast EMA has crossed below slow EMA
- Confirms a new downtrend is beginning
-
Strongest when preceded by bearish MACD/signal crossover
-
Bearish Divergence: Price makes higher high, but MACD makes lower high
- Suggests uptrend is losing momentum
- Most powerful when MACD is above zero line
-
Requires confirmation from MACD/signal crossover
-
Histogram Reversal: Histogram stops increasing and begins decreasing
- Earliest signal of momentum shift
- Precedes MACD/signal crossover by 1-3 periods
- Best when transitioning from positive to negative values
Example Scenario:
When MACD line crosses below signal line while both are above zero,
consider closing longs or initiating shorts. Ideal conditions:
- Price has reached resistance at a key technical level
- Histogram transitioning from positive to negative
- Volume declining on recent up days
- Set stop loss above recent swing high
- Target: Previous support or when MACD crosses above signal
Divergence Analysis¶
Bullish Divergence: - Pattern: Price makes lower low, but MACD line makes higher low - Identification: Compare price lows with MACD lows over 20-50 bars. Look for MACD failing to confirm price weakness. - Significance: Indicates downtrend is losing momentum, buyers stepping in at lower levels. Often precedes trend reversals. - Reliability: High when MACD is significantly below zero and forms a clear higher low. Confirm with MACD/signal crossover. - Example: Stock drops from $100 to $90 (MACD at -1.5), then to $85 (MACD at -1.2). Price made new low, but MACD is higher, suggesting selling pressure is weakening.
Bearish Divergence: - Pattern: Price makes higher high, but MACD line makes lower high - Identification: Compare price highs with MACD highs over 20-50 bars. Look for MACD failing to confirm price strength. - Significance: Indicates uptrend is losing momentum, sellers stepping in at higher levels. Often precedes trend reversals. - Reliability: High when MACD is significantly above zero and forms a clear lower high. Confirm with MACD/signal crossover. - Example: Stock rises from $100 to $110 (MACD at +1.8), then to $115 (MACD at +1.5). Price made new high, but MACD is lower, suggesting buying pressure is weakening.
Best Practices¶
Optimal Use Cases¶
When MACD works best:
- Market conditions: Trending markets provide the clearest and most profitable signals. MACD excels at identifying and riding trends. Ranging or choppy markets generate numerous false crossover signals.
- Time frames: Most reliable on daily charts. Works on all timeframes but generates more noise on intraday charts (< 1 hour). Weekly/monthly charts provide very reliable long-term trend signals.
- Asset classes: Excellent for stocks, stock indices, forex pairs, and cryptocurrencies. Works well for commodities but may need parameter adjustment for seasonal patterns.
- Volatility: Best performance in moderate volatility. High volatility markets may require longer periods to filter noise.
Combining with Other Indicators¶
Recommended indicator combinations:
-
With Trend Indicators: Use 200-period SMA or EMA to identify primary trend direction. Only take MACD signals aligned with the longer-term trend (bullish MACD signals in uptrend, bearish MACD signals in downtrend). This filter dramatically improves win rates.
-
With Volume Indicators: Combine with On Balance Volume (OBV) or standard volume analysis. Strongest MACD signals occur when accompanied by volume confirmation. Look for increasing volume on MACD crossovers and divergences.
-
With RSI: Use RSI to identify overbought/oversold extremes. Best trades occur when MACD gives a signal while RSI is oversold (< 30) for longs or overbought (> 70) for shorts. This combination helps time entries at optimal risk/reward points.
-
With Support/Resistance: MACD signals at key price levels are most reliable. Bullish MACD crossovers at support or bearish crossovers at resistance have higher success rates.
Common Pitfalls¶
What to avoid:
-
Trading every crossover: MACD generates many crossover signals. Not all are tradeable. Only trade crossovers that align with the broader trend, occur at logical support/resistance levels, or confirm divergences.
-
Ignoring the trend: Taking bearish MACD signals in strong uptrends or bullish signals in strong downtrends leads to losses. The trend is your friend - trade with it, not against it.
-
Over-optimization: Backtesting to find "perfect" MACD parameters leads to curve-fitting. Standard 12/26/9 settings work well for most situations. Only adjust if you have a specific, rational reason.
-
Ignoring market context: MACD is a pure price-based indicator. It doesn't account for fundamental news, economic releases, or market sentiment. Always consider broader market context.
-
Using in ranging markets: MACD performs poorly when prices oscillate in a range. Identify trending vs ranging conditions before applying MACD strategies.
Parameter Selection Guidelines¶
How to choose optimal parameters:
- Short-term trading (day trading):
- Settings: 5/13/5 or 6/19/9 for faster signals
- Focus: Quick momentum shifts, intraday trend changes
- Time frame: 5-15 minute charts
-
Caution: More false signals, requires tight stops
-
Medium-term trading (swing trading):
- Settings: 12/26/9 (standard) for balanced signals
- Focus: Multi-day trends, position trading
- Time frame: Daily or 4-hour charts
-
Balance: Optimal balance between speed and reliability
-
Long-term trading (position trading):
- Settings: 19/39/9 or 24/52/18 for major trends
- Focus: Primary trend identification, long-term positions
- Time frame: Weekly or daily charts
-
Benefit: Fewer but more reliable signals, less noise
-
Backtesting approach: Test MACD on historical data for your specific asset and timeframe. Evaluate:
- Win rate of MACD crossover signals
- Average profit per signal vs average loss
- Performance in trending vs ranging periods
- Optimal stop loss and profit target levels
Practical Example¶
Complete trading example with context:
require 'sqa/tai'
# Real-world scenario: analyzing a stock for trend-following entry
historical_prices = [
44.34, 44.09, 44.15, 43.61, 44.33, 44.83, 45.10, 45.42,
45.84, 46.08, 46.03, 46.41, 46.22, 45.64, 46.21, 46.25,
46.08, 46.46, 46.57, 45.95, 46.50, 46.02, 45.55, 46.08,
46.34, 46.00, 45.75, 46.45, 47.02, 46.87, 47.15, 47.44,
47.80, 48.12, 47.95, 48.34, 48.56, 48.21, 48.67, 49.02
]
# Calculate MACD and supporting indicators
macd, signal, histogram = SQA::TAI.macd(
historical_prices,
fast_period: 12,
slow_period: 26,
signal_period: 9
)
# Calculate trend filter
sma_200 = SQA::TAI.sma(historical_prices, period: 30) # Using 30 as proxy for trend
# Current values
current_price = historical_prices.last
previous_price = historical_prices[-2]
current_macd = macd.last
previous_macd = macd[-2]
current_signal = signal.last
previous_signal = signal[-2]
current_histogram = histogram.last
previous_histogram = histogram[-2]
# Determine trend
long_term_trend = current_price > sma_200.last ? "UPTREND" : "DOWNTREND"
puts "=== MACD Trading Analysis ==="
puts "Symbol: EXAMPLE_STOCK"
puts "Current Price: $#{current_price.round(2)}"
puts "Trend: #{long_term_trend}"
puts
puts "MACD Values:"
puts " MACD Line: #{current_macd.round(4)}"
puts " Signal Line: #{current_signal.round(4)}"
puts " Histogram: #{current_histogram.round(4)}"
puts
# Trading Signal Analysis
def check_crossover(current, previous, current_other, previous_other)
# Returns 'bullish' if crossed above, 'bearish' if crossed below, nil otherwise
if current > current_other && previous <= previous_other
'bullish'
elsif current < current_other && previous >= previous_other
'bearish'
end
end
crossover = check_crossover(current_macd, previous_macd, current_signal, previous_signal)
zero_line_cross = check_crossover(current_macd, previous_macd, 0, 0)
# Trading logic with comprehensive analysis
if crossover == 'bullish'
puts "BULLISH SIGNAL: MACD crossed above Signal Line"
puts
if long_term_trend == "UPTREND"
if current_macd < 0
puts "STRONG BUY OPPORTUNITY"
puts "Reason: Bullish crossover in uptrend while MACD below zero"
puts "This suggests buying into temporary weakness in an uptrend"
puts
puts "Entry: $#{current_price.round(2)}"
puts "Stop Loss: $#{(current_price * 0.97).round(2)} (3% below entry)"
puts "Target 1: $#{(current_price * 1.05).round(2)} (5% gain)"
puts "Target 2: Wait for MACD to cross below signal or reach resistance"
else
puts "MODERATE BUY"
puts "Reason: Bullish crossover in uptrend, MACD above zero"
puts "Trend continuation signal - enter with caution"
end
else
puts "CAUTION: Signal against major trend"
puts "Consider waiting for zero line cross or trend reversal confirmation"
end
elsif crossover == 'bearish'
puts "BEARISH SIGNAL: MACD crossed below Signal Line"
puts
if long_term_trend == "DOWNTREND"
if current_macd > 0
puts "STRONG SELL OPPORTUNITY"
puts "Reason: Bearish crossover in downtrend while MACD above zero"
puts "This suggests selling into temporary strength in a downtrend"
puts
puts "Entry: $#{current_price.round(2)}"
puts "Stop Loss: $#{(current_price * 1.03).round(2)} (3% above entry)"
puts "Target 1: $#{(current_price * 0.95).round(2)} (5% decline)"
puts "Target 2: Wait for MACD to cross above signal or reach support"
else
puts "MODERATE SELL"
puts "Reason: Bearish crossover in downtrend, MACD below zero"
puts "Trend continuation signal - consider exiting longs"
end
else
puts "CAUTION: Signal against major trend"
puts "May be temporary pullback - consider reducing position size"
end
elsif zero_line_cross == 'bullish'
puts "TREND CHANGE: MACD crossed above Zero Line"
puts "Interpretation: Fast EMA crossed above Slow EMA"
puts "New uptrend beginning - look for continuation signals"
puts
elsif zero_line_cross == 'bearish'
puts "TREND CHANGE: MACD crossed below Zero Line"
puts "Interpretation: Fast EMA crossed below Slow EMA"
puts "New downtrend beginning - consider defensive positioning"
puts
else
# Analyze momentum
puts "NO CROSSOVER - Analyzing Momentum"
puts
if current_histogram > 0 && current_histogram > previous_histogram
puts "Histogram Analysis: Bullish momentum strengthening"
puts "MACD pulling away from signal line (upward)"
if long_term_trend == "UPTREND"
puts "Action: Hold long positions, momentum accelerating"
end
elsif current_histogram < 0 && current_histogram < previous_histogram
puts "Histogram Analysis: Bearish momentum strengthening"
puts "MACD pulling away from signal line (downward)"
if long_term_trend == "DOWNTREND"
puts "Action: Hold short positions, momentum accelerating"
end
elsif current_histogram > 0 && current_histogram < previous_histogram
puts "Histogram Analysis: Bullish momentum weakening"
puts "MACD moving toward signal line from above"
puts "Action: Consider taking partial profits, momentum slowing"
elsif current_histogram < 0 && current_histogram > previous_histogram
puts "Histogram Analysis: Bearish momentum weakening"
puts "MACD moving toward signal line from below"
puts "Action: Potential bullish reversal developing"
end
end
# Divergence detection
puts "\n=== Divergence Analysis ==="
recent_prices = historical_prices.last(15)
recent_macd = macd.compact.last(15)
# Find highs in last 15 periods
price_high_idx = recent_prices.index(recent_prices.max)
macd_high_idx = recent_macd.index(recent_macd.max)
# Find lows in last 15 periods
price_low_idx = recent_prices.index(recent_prices.min)
macd_low_idx = recent_macd.index(recent_macd.min)
# Check for divergence
if price_high_idx > macd_high_idx
puts "BEARISH DIVERGENCE WARNING"
puts "Price making new highs but MACD is not confirming"
puts "Potential trend exhaustion - consider taking profits"
puts "Wait for MACD/Signal crossover to confirm"
elsif price_low_idx > macd_low_idx
puts "BULLISH DIVERGENCE DETECTED"
puts "Price making new lows but MACD is not confirming"
puts "Potential trend reversal - watch for bullish crossover"
puts "This suggests selling pressure is diminishing"
else
puts "No significant divergence detected"
puts "Price and MACD are in agreement"
end
puts "\n=== Summary ==="
puts "Current Position: #{current_macd > current_signal ? 'BULLISH' : 'BEARISH'} (MACD vs Signal)"
puts "Trend Position: #{current_macd > 0 ? 'ABOVE' : 'BELOW'} zero line"
puts "Momentum: #{current_histogram > previous_histogram ? 'INCREASING' : 'DECREASING'}"
Example: MACD Crossover Strategy¶
prices = load_historical_prices('AAPL')
macd, signal, histogram = SQA::TAI.macd(prices)
# Check for crossovers
if macd[-2] < signal[-2] && macd[-1] > signal[-1]
puts "Bullish MACD Crossover - BUY signal"
puts "MACD: #{macd[-1].round(4)}, Signal: #{signal[-1].round(4)}"
elsif macd[-2] > signal[-2] && macd[-1] < signal[-1]
puts "Bearish MACD Crossover - SELL signal"
puts "MACD: #{macd[-1].round(4)}, Signal: #{signal[-1].round(4)}"
end
Example: MACD Histogram Analysis¶
prices = load_historical_prices('TSLA')
macd, signal, histogram = SQA::TAI.macd(prices)
# Analyze histogram momentum
recent_histogram = histogram.compact.last(5)
if recent_histogram.all? { |h| h > 0 } && recent_histogram[-1] > recent_histogram[-2]
puts "Bullish momentum strengthening"
puts "Histogram values: #{recent_histogram.map { |h| h.round(4) }}"
elsif recent_histogram.all? { |h| h < 0 } && recent_histogram[-1] < recent_histogram[-2]
puts "Bearish momentum strengthening"
puts "Histogram values: #{recent_histogram.map { |h| h.round(4) }}"
elsif recent_histogram[-1] > recent_histogram[-2]
puts "Momentum turning bullish (histogram rising)"
else
puts "Momentum weakening"
end
Example: MACD Divergence¶
prices = load_historical_prices('MSFT')
macd, signal, histogram = SQA::TAI.macd(prices)
# Find recent highs
price_high_1_idx = prices[-30..-15].index(prices[-30..-15].max) - 30
price_high_2_idx = prices[-14..-1].index(prices[-14..-1].max) - 14
price_high_1 = prices[price_high_1_idx]
price_high_2 = prices[price_high_2_idx]
macd_high_1 = macd[price_high_1_idx]
macd_high_2 = macd[price_high_2_idx]
# Bearish divergence
if price_high_2 > price_high_1 && macd_high_2 < macd_high_1
puts "Bearish Divergence detected!"
puts "Price: #{price_high_1.round(2)} -> #{price_high_2.round(2)} (higher high)"
puts "MACD: #{macd_high_1.round(4)} -> #{macd_high_2.round(4)} (lower high)"
puts "Potential trend reversal"
end
Example: MACD Zero Line Crossover¶
prices = load_historical_prices('NVDA')
macd, signal, histogram = SQA::TAI.macd(prices)
# Zero line crossovers are significant
if macd[-2] < 0 && macd[-1] > 0
puts "MACD crossed above zero line"
puts "Trend changing from bearish to bullish"
puts "Look for buying opportunities"
elsif macd[-2] > 0 && macd[-1] < 0
puts "MACD crossed below zero line"
puts "Trend changing from bullish to bearish"
puts "Consider reducing exposure"
end
# Current trend based on zero line
if macd.last > 0
puts "MACD above zero: Bullish trend"
else
puts "MACD below zero: Bearish trend"
end
Trading Strategies¶
1. MACD Crossover¶
- Buy when MACD crosses above Signal
- Sell when MACD crosses below Signal
- Most common strategy
- Best used with trend filters
2. Zero Line Crossover¶
- Buy when MACD crosses above zero
- Sell when MACD crosses below zero
- Confirms trend direction
- More reliable but fewer signals
3. Histogram Reversal¶
- Buy when histogram stops decreasing and turns up
- Sell when histogram stops increasing and turns down
- Earlier signals than crossovers
- Requires experience to read accurately
4. Divergence Trading¶
- Bullish divergence: Price lower low, MACD higher low
- Bearish divergence: Price higher high, MACD lower high
- Powerful reversal signals
- Confirm with crossovers before entering
MACD Settings¶
Standard Settings (12, 26, 9)¶
- Most widely used configuration
- Good for daily charts
- Balanced responsiveness
- Recommended for beginners
Fast Settings (5, 13, 5)¶
- More sensitive to price changes
- More signals (more false positives)
- Good for short-term trading
- Requires active management
Slow Settings (19, 39, 9)¶
- Less sensitive to noise
- Fewer but more reliable signals
- Good for long-term trends
- Reduces whipsaws in choppy markets
Combining with Other Indicators¶
prices = load_historical_prices('GOOGL')
# MACD for momentum
macd, signal, histogram = SQA::TAI.macd(prices)
# RSI for overbought/oversold
rsi = SQA::TAI.rsi(prices, period: 14)
# Trend filter
sma_200 = SQA::TAI.sma(prices, period: 200)
current_price = prices.last
# Strong buy signal
if macd.last > signal.last && # MACD bullish
rsi.last < 40 && # Not overbought
current_price > sma_200.last # Uptrend
puts "Strong BUY signal - all indicators aligned"
end
Related Indicators¶
Similar Indicators¶
- PPO (Percentage Price Oscillator): Percentage-based version of MACD. Use PPO when comparing assets with different price levels. MACD better for analyzing a single asset over time.
- APO (Absolute Price Oscillator): Simpler version using SMAs instead of EMAs. APO is less smooth than MACD but more responsive to sudden price changes.
Complementary Indicators¶
- RSI: Momentum oscillator. Use RSI to identify overbought/oversold conditions, MACD for trend direction and timing.
- Stochastic: Momentum oscillator comparing price to recent range. Stochastic provides faster signals, MACD provides more reliable trend confirmation.
- EMA: Used in MACD calculation. Understanding EMAs helps interpret MACD behavior.
- Bollinger Bands: Volatility indicator. MACD signals at Bollinger Band extremes are particularly reliable.
- Volume (OBV): Confirms momentum. MACD signals with volume confirmation have higher success rates.
Indicator Family¶
MACD belongs to the trend-following momentum family: - MACD: Moving average convergence/divergence indicator - PPO: Percentage-based MACD variant - TRIX: Triple EMA momentum oscillator - APO: Simple moving average oscillator
When to prefer MACD: For comprehensive trend and momentum analysis. MACD's combination of trend-following (zero line) and momentum (crossovers, histogram) makes it one of the most versatile indicators. Best for traders who want multiple signals from a single indicator.
Advanced Topics¶
Multi-Timeframe Analysis¶
Use MACD across multiple timeframes for comprehensive analysis:
- Higher timeframe MACD (daily/weekly) identifies major trend direction and primary trading opportunities
- Lower timeframe MACD (4H/1H) provides precise entry timing within the higher timeframe trend
- Strongest signals when all timeframes align (e.g., weekly MACD bullish + daily MACD bullish crossover)
Strategy Example: - Weekly MACD above zero and above signal = major uptrend - Wait for daily MACD bullish crossover below zero - Enter on hourly MACD bullish crossover for precise timing - Exit when daily MACD crosses below signal
This multi-timeframe approach improves win rates by 15-25% compared to single timeframe analysis.
Market Regime Adaptation¶
MACD behavior changes in different market conditions:
-
Bull Markets: MACD tends to stay above zero with frequent bullish crossovers above zero. Bearish crossovers are usually brief pullbacks, not major reversals. Best strategy: focus on zero line as support, buy bullish crossovers above zero.
-
Bear Markets: MACD tends to stay below zero with frequent bearish crossovers below zero. Bullish crossovers are usually brief rallies, not major reversals. Best strategy: focus on zero line as resistance, sell bearish crossovers below zero.
-
Ranging Markets: MACD oscillates around zero with frequent crossovers in both directions. High false signal rate. Best strategy: avoid MACD signals or use only with strong support/resistance confirmation.
-
High Volatility: MACD generates larger swings and more extreme values. Crossovers can be whipsaws. Best strategy: use longer periods (19/39/9) to filter noise, require zero line confirmation.
-
Low Volatility: MACD generates smaller swings and fewer crossovers. Signals can be delayed. Best strategy: use shorter periods (8/17/9) for faster response, or switch to different indicators.
Statistical Validation¶
MACD reliability metrics and considerations:
-
Success Rate: Studies show MACD crossover signals have 55-65% success rate when used alone. Combining with trend filters (200 SMA) improves this to 70-80%. Zero line crossovers have 65-75% success rate.
-
Risk/Reward: MACD divergences offer best risk/reward ratios (typically 1:3 or better). Simple crossovers provide 1:1.5 to 1:2 ratios. Histogram reversals offer 1:2 ratios with faster entries.
-
Optimal Settings by Market:
- Stocks: 12/26/9 (standard) - 65% win rate
- Forex: 8/17/9 or 12/26/9 - 60% win rate (more noise)
- Commodities: 19/39/9 - 70% win rate (smoother trends)
-
Cryptocurrencies: 12/26/9 or 24/52/18 - 55-65% win rate (high volatility)
-
False Signal Rate: Approximately 30-40% of MACD crossovers fail in trending markets, 50-60% fail in ranging markets. Always use stop losses and position sizing.
-
Best Performance: MACD performs best in:
- Trending markets with moderate volatility
- Daily timeframes (most reliable)
- Large-cap stocks and major forex pairs
- Clear support/resistance levels
References¶
- Appel, Gerald. "Technical Analysis: Power Tools for Active Investors" (2005) - Original MACD development and trading strategies
- Appel, Gerald. "The Moving Average Convergence-Divergence Trading Method" (1979) - Foundational MACD publication
- Murphy, John J. "Technical Analysis of the Financial Markets" (1999) - Comprehensive MACD analysis and interpretation
- Pring, Martin J. "Technical Analysis Explained" (2002) - MACD in context of broader technical analysis
- Investopedia: MACD - Comprehensive online reference
- StockCharts: MACD - Educational guide with examples
- TradingView: MACD Educational Guide