OPEN-SOURCE SCRIPT
Sector Rotation Momentum Framework

Overview
Markets rarely move with every sector advancing or declining at the same pace. At different stages of the market cycle, some sectors may strengthen while others lose momentum.
This framework compares the performance of a selected stock with a related sector ETF to provide additional context when evaluating trend direction. Rather than analyzing a stock in isolation, the script studies whether the stock is outperforming or underperforming its sector over time.
The objective is to demonstrate one approach to incorporating relative performance into a rules-based trading framework.
Core Idea
A stock's price movement can be viewed alongside the broader sector it belongs to.
For example:
- NVIDIA compared with the Technology Select Sector SPDR Fund (XLK)
- JPMorgan Chase compared with the Financial Select Sector SPDR Fund (XLF)
- Exxon Mobil compared with the Energy Select Sector SPDR Fund (XLE)
Comparing relative performance may provide additional context when studying trends.
Strategy Logic
Relative Performance
The framework calculates a ratio between the current chart and a selected benchmark ETF.
- When the ratio increases, the stock is outperforming the benchmark.
- When the ratio decreases, the stock is underperforming the benchmark.
Trend Confirmation
A moving average is used to identify the primary trend on the current chart.
Relative Strength Filter
Trades are evaluated only when both:
- The trend direction aligns with the moving average.
- Relative performance remains above its smoothing average.
Risk Management
ATR-based stop-loss and target levels adjust according to market volatility.
Features
- Relative performance comparison
- Sector benchmark analysis
- Trend confirmation
- ATR-based exits
- Configurable benchmark symbol
Intended Use
This framework is designed as an educational example of comparing a security with a sector benchmark. It can be used to study:
- Relative performance
- Trend alignment
- Sector participation
- Cross-market comparison
Disclaimer
This script is provided for educational and research purposes only. It demonstrates one approach to comparing a security with a benchmark using Pine Script. It does not predict future market behaviour and should be independently tested before being incorporated into any trading workflow.
สคริปต์แบบโอเพนซอร์ส
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
🚀 PineGen AI — the dedicated Pine Script AI tool. Describe any strategy, get 100% bug-free, compiler-checked Pine Script v6 code in seconds.
Try it free: pinegen.ai/
Try it free: pinegen.ai/
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน
สคริปต์แบบโอเพนซอร์ส
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
🚀 PineGen AI — the dedicated Pine Script AI tool. Describe any strategy, get 100% bug-free, compiler-checked Pine Script v6 code in seconds.
Try it free: pinegen.ai/
Try it free: pinegen.ai/
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน