Interactive web application for visualizing stock and sector rotation patterns using Relative Rotation Graphs
What is RRG โข Prerequisites โข Installation โข Usage โข Features โข Applications โข Technical Details
An interactive web application built with Streamlit that generates Relative Rotation Graphs (RRG) for smarter investing and trading decisions. Visualize sector or stock rotation patterns and identify market leaders and laggards at a glance.
In the world of investing and trading, identifying market leaders and laggards is crucial. Traditional price charts and indicators provide valuable insights, but they often fail to visualize sector or stock rotation in a clear and intuitive manner. This is where Relative Rotation Graphs (RRG) come into play.
RRG charts allow traders and investors to compare the relative strength and momentum of multiple securities against a benchmark, revealing how different stocks, sectors, or asset classes are rotating through various performance phases.

RRG is a visualization tool that plots securities based on two key indicators:
- JdK RS-Ratio (Relative Strength Ratio) โ Measures a security's relative strength against a benchmark. Higher values indicate outperformance, while lower values suggest underperformance.
- JdK RS-Momentum (Relative Strength Momentum) โ Measures the rate of change of the relative strength. Increasing momentum suggests improving strength, while decreasing momentum signals weakening performance.
By plotting these values on a two-dimensional plane, RRG divides the chart into four quadrants:
- Securities in this quadrant have weak relative strength but are gaining momentum
- These are potential turnaround candidates that may soon transition into the leading quadrant
- Ideal for investors looking for early entry points into emerging leaders
- Securities in this quadrant have both high relative strength and strong momentum
- These are the outperformers of the market
- Typically, stocks or sectors here continue to perform well, making them attractive for holding or further accumulation
- Securities in this quadrant still have high relative strength but are losing momentum
- This often indicates a mature uptrend that may be slowing down
- If a security remains in this quadrant for an extended period, it may transition into the lagging quadrant, signaling a potential exit point
- Securities in this quadrant exhibit both weak relative strength and weak momentum
- These are underperformers that should be avoided or shorted
- Investors should be cautious about bottom fishing unless there is a clear sign of reversal
RRG charts provide more than just a snapshot; they also illustrate how securities rotate through different phases over time. By analyzing the tail movement, investors can gain deeper insights into market dynamics.
- Securities tend to move from improving โ leading โ weakening โ lagging in a clockwise fashion
- This rotation aligns with market cycles and sector rotation
- A sudden move from lagging to improving suggests a strong reversal
- Conversely, a sharp drop from leading to weakening may indicate an overbought condition
- A long tail signifies high volatility and strong trends
- A short tail suggests stability but slower movement
- Stocks moving from improving to leading are prime buy candidates
- Stocks moving from weakening to lagging should be avoided
- Python 3.8 or higher
- pip (Python package installer)
- Clone the repository
git clone https://github.com/yourusername/relative-rotation-graphs.git
cd relative-rotation-graphs- Create a virtual environment (recommended)
python -m venv .venv-
Activate the virtual environment
- Windows:
.venv\Scripts\activate
- macOS/Linux:
source .venv/bin/activate
- Windows:
-
Install required packages
pip install -r requirements.txt- Run the Streamlit app
streamlit run "Relative Rotation Graphs.py"-
Configure your analysis
- Set your benchmark (e.g., ^NSEI for NIFTY 50, ^GSPC for S&P 500)
- Adjust the lookback period slider
- Modify the tail length for trend visualization
- Toggle the "Show Trail/Tail" checkbox to see historical movement
-
Add your stocks
- Use the text area to add stock symbols (one per line)
- For Indian stocks, use the .NS suffix (e.g., RELIANCE.NS)
- For US stocks, use regular ticker symbols (e.g., AAPL, MSFT)
-
Generate the RRG
- Click the "Generate RRG" button
- Wait for data to download (typically 5-15 seconds)
- Analyze the chart and summary table
- Interactive Web Interface: Built with Streamlit for easy use
- Customizable Analysis:
- Choose any benchmark (default: NIFTY 50)
- Adjust lookback period (60-500 days)
- Control tail length for trend visualization
- Toggle trail visibility
- Real-time Data: Fetches live market data using Yahoo Finance
- Visual Insights: Interactive Plotly charts with hover details
- Summary Table: Quick view of RS-Ratio and RS-Momentum values
- Pre-configured: Comes with all NIFTY 50 stocks pre-loaded
Investors can use RRG to identify which sectors are gaining strength and allocate capital accordingly. For example, if technology is moving from improving to leading, it may be a good time to increase exposure to tech stocks.
RRG helps traders focus on stocks with strong relative performance rather than just absolute price movements.
By identifying sector rotations, investors can balance their portfolios across leading and improving sectors.
Watching how securities rotate through quadrants helps investors time their entries and exits more effectively.
While RRG is a powerful tool, it should not be used in isolation. Here are a few things to keep in mind:
- Lagging Indicator โ Since it relies on historical data, RRG may not always predict sudden market reversals
- Works Best for Groups โ It is most effective when comparing multiple securities rather than analyzing a single stock
- Needs Confirmation โ Combining RRG with other technical indicators like moving averages, RSI, or MACD can improve accuracy
relative-rotation-graphs/
โ
โโโ Relative Rotation Graphs.py # Main Streamlit application
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ .venv/ # Virtual environment (not tracked)
- Relative Strength (RS): Price of security / Price of benchmark
- JdK RS-Ratio: (RS / 40-period Moving Average of RS) ร 100
- JdK RS-Momentum: (Current RS-Ratio / RS-Ratio 10 periods ago) ร 100
- Minimum 50 data points required for calculation
- Additional 80-day buffer added for rolling calculations
- Automatic alignment of dates between securities and benchmark
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
- RRG methodology developed by Julius de Kempenaer
- Market data provided by Yahoo Finance via yfinance library
- Built with Streamlit, Plotly, and Pandas
- Inspired by the article: Build and Use Relative Rotation Graphs (RRG) for Smarter Investing by FabTrader
For questions or feedback, please open an issue on GitHub.
This tool is for educational and informational purposes only. It should not be considered as financial advice. Always do your own research and consult with a qualified financial advisor before making investment decisions.
Happy Investing! ๐