To create a TradingView indicator comparing a stock's momentum with Nifty using Pine Script, follow this implementation:
Momentum Comparison Indicator
//@version=5
indicator("Stock vs Nifty Momentum", overlay=false)
// Input parameters
stockSymbol = input.string("Stock Symbol", "RELIANCE")
niftySymbol = input.string("Nifty Symbol", "NIFTY")
momentumLength = input.int(14, "Momentum Length")
// Fetch data using request.security
stockClose = request.security(stockSymbol, timeframe.period, close)
niftyClose = request.security(niftySymbol, timeframe.period, close)
// Calculate momentum for both symbols
stockMomentum = ta.mom(stockClose, momentumLength)
niftyMomentum = ta.mom(niftyClose, momentumLength)
// Plot results
plot(stockMomentum, color=color.blue, title="Stock Momentum")
plot(niftyMomentum, color=color.red, title="Nifty Momentum")
Key features:
-
Symbol Flexibility
- Input fields allow users to specify any stock and Nifty index (e.g.,
NIFTY, NIFTY50)[11][14]
- Default symbols:
RELIANCE (stock) and NIFTY (index)
-
Momentum Calculation
- Uses
ta.mom() for rate-of-change calculation[3][6]
- Adjustable lookback period (default: 14 bars)
-
Visual Comparison
- Blue line: Stock momentum
- Red line: Nifty momentum
- Separate pane display (set
overlay=true for chart overlay)
Usage Tips:
- Relative Strength Analysis: Look for divergences where the stock's momentum line crosses above/below Nifty's line[2][11]
- Timeframe Consistency: Ensure both symbols use the same timeframe (automatically handled by
timeframe.period)[11]
- Customization: Modify colors or add alerts for crossover events using
ta.crossover()[4][7]
For advanced implementations, consider adding:
- Z-Score Normalization: Compare momentum relative to historical performance[2]
- Multi-Timeframe Support: Use
request.security() with different timeframes[3][8]
- Alert Conditions: Trigger notifications when momentum diverges significantly[7][15]
This script provides a foundational framework for momentum comparison, which can be extended with additional technical indicators or statistical measures as needed[1][4].