πŸ“ˆ MACD + RSI Strategy for Smarter Stock Trading

 Looking to improve your stock trading signals? Combining two powerful indicators — MACD (Moving Average Convergence Divergence) and RSI (Relative Strength Index) — can help filter false signals and improve your accuracy.

In this blog, you'll learn:

  • What MACD and RSI are

  • How to use them together in a trading strategy

  • Python code to backtest this strategy


πŸ” What is MACD?

MACD helps detect trend direction and momentum.

MACD=EMA12EMA26\text{MACD} = \text{EMA}_{12} - \text{EMA}_{26}
  • Signal line = 9-period EMA of MACD

  • Buy signal: MACD crosses above signal line

  • Sell signal: MACD crosses below signal line


πŸ’‘ What is RSI?

RSI detects overbought/oversold conditions.

RSI=100(1001+RS)\text{RSI} = 100 - \left( \frac{100}{1 + RS} \right)
  • Buy signal: RSI < 30

  • Sell signal: RSI > 70


⚙️ Strategy Rules (MACD + RSI Combo)

Buy when:

  • MACD crosses above the Signal Line

  • RSI is below 30 (oversold)

Sell when:

  • MACD crosses below the Signal Line

  • RSI is above 70 (overbought)


🐍 Python Code: Backtest MACD + RSI Strategy

✅ Step 1: Install Required Libraries

pip install yfinance pandas matplotlib

✅ Step 2: Import Libraries

import yfinance as yf
import pandas as pd import matplotlib.pyplot as plt

✅ Step 3: Download Stock Data and Compute Indicators

# Download stock data (e.g., INFY)
df = yf.download('INFY.NS', start='2022-01-01', end='2024-12-31') # Calculate MACD and Signal df['EMA12'] = df['Close'].ewm(span=12, adjust=False).mean() df['EMA26'] = df['Close'].ewm(span=26, adjust=False).mean() df['MACD'] = df['EMA12'] - df['EMA26'] df['Signal'] = df['MACD'].ewm(span=9, adjust=False).mean() # Calculate RSI delta = df['Close'].diff() gain = (delta.where(delta > 0, 0)).rolling(14).mean() loss = (-delta.where(delta < 0, 0)).rolling(14).mean() rs = gain / loss df['RSI'] = 100 - (100 / (1 + rs)) # Define Buy and Sell signals df['Buy'] = (df['MACD'] > df['Signal']) & (df['MACD'].shift(1) < df['Signal'].shift(1)) & (df['RSI'] < 30) df['Sell'] = (df['MACD'] < df['Signal']) & (df['MACD'].shift(1) > df['Signal'].shift(1)) & (df['RSI'] > 70)

✅ Step 4: Visualize Buy and Sell Signals

plt.figure(figsize=(14, 7))
plt.plot(df['Close'], label='Close Price', alpha=0.5) plt.plot(df['MACD'], label='MACD', color='blue') plt.plot(df['Signal'], label='Signal Line', color='red') plt.scatter(df[df['Buy']].index, df[df['Buy']]['Close'], marker='^', color='green', label='Buy Signal') plt.scatter(df[df['Sell']].index, df[df['Sell']]['Close'], marker='v', color='red', label='Sell Signal') plt.title('MACD + RSI Buy/Sell Signals for INFY') plt.legend() plt.grid() plt.show()

πŸ“Š Sample Output (Head of Signal Table)

DateCloseMACDSignalRSIBuySell
2023-02-2015255.213.4528.2TrueFalse
2023-05-101625-1.10-0.9573.4FalseTrue




πŸ“ˆ Strategy Benefits

Reduces false positives — RSI confirms momentum behind MACD
Flexible — Works across multiple timeframes
Easy to implement in Python


⚠️ Strategy Limitations

❌ Doesn’t consider external news or earnings
❌ May underperform in sideways markets
❌ False signals still possible in volatile markets


🧠 Final Thoughts

The MACD + RSI strategy is a powerful combo to filter signals and improve accuracy for entry and exit points.

This strategy is ideal for:

  • Swing traders looking for short-to-midterm setups

  • Coders interested in algorithmic trading

  • Beginners learning about indicator fusion

πŸ“Œ Disclaimer:

This blog is intended for educational purposes only. It does not constitute financial, investment, or professional advice. The information provided is based on personal research and for learning use only. Please consult with a certified financial advisor or conduct your own research before making any investment decisions.

Comments

Popular posts from this blog

πŸ“ˆ Dual Moving Average Strategy Using Python

πŸ—ž️πŸ’Ή News Sentiment Analysis – NLP-Based Headline Trading Using Python

πŸ“ˆ Opening Range Breakout Strategy Explained with Python Code