Relative Strength Index Pine Script — Complete TradingView Guide
The Relative Strength Index (RSI) Pine Script indicator is a momentum oscillator that measures the speed and magnitude of recent price changes to identify overbought and oversold conditions on a scale of 0 to 100. Developed by J. Welles Wilder Jr. in 1978, RSI is one of the most widely used technical indicators in TradingView. In Pine Script v6, the built-in function ta.rsi(source, length) calculates RSI with a default period of 14 bars. RSI values above 70 signal overbought conditions — potential reversal or pullback territory — while values below 30 signal oversold conditions. RSI is equally effective across stocks, crypto, forex, and futures markets on timeframes from 15-minute charts to weekly charts. This guide covers the complete Pine Script code, parameter tuning, and proven trading strategies.
What Is the Relative Strength Index (RSI)?
The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and change of price movements, used to identify overbought and oversold market conditions. Developed by J. Welles Wilder Jr. and introduced in his 1978 book New Concepts in Technical Trading Systems, RSI quickly became one of the most widely cited indicators in technical analysis. Wilder originally designed RSI for commodity futures markets, but it has since proven effective across all liquid asset classes.
RSI is calculated using the following formula:
RSI = 100 − (100 ÷ (1 + RS))
RS = Average Gain ÷ Average Loss (over N periods)
Where Average Gain is the mean of all up-closes over the lookback period, and Average Loss is the mean of all down-closes (expressed as positive numbers). For the initial calculation, Wilder used a simple average over 14 periods. Subsequent values apply Wilder Smoothing: Average Gain = (Prior Average Gain × 13 + Current Gain) ÷ 14. This smoothing method gives RSI a gradual, non-whipsaw characteristic compared to simple moving averages.
RSI oscillates between 0 and 100. The three key reference levels are:
- 70 — Overbought: price has risen rapidly; potential reversal or consolidation zone
- 50 — Midline: neutral momentum; bulls and bears are in balance
- 30 — Oversold: price has fallen rapidly; potential bounce or recovery zone
RSI applies to all major asset classes: stocks (individual equities and ETFs), cryptocurrency (BTC, ETH, altcoins), forex (major and minor currency pairs), and futures (commodities, indices, bonds). It performs best in markets with clear oscillating price behavior rather than persistent unidirectional trends.
Best timeframes for RSI: 1H–Daily charts are Wilder's original recommendation. For day trading, 15M–1H works well. For swing trading, 4H–Daily is most reliable. For position trading and long-term investing, Weekly RSI provides high-conviction oversold signals at major market bottoms. On timeframes below 15 minutes, RSI generates excessive noise and is less reliable.
RSI Pine Script Code Example
The following Pine Script v6 code calculates the Relative Strength Index using the built-in ta.rsi(close, 14) function and plots it in a separate pane with overbought (70), midline (50), and oversold (30) reference levels. To use it in TradingView, open the Pine Script Editor from the bottom toolbar, paste the code, click Save, then click Add to chart.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator(title="Relative Strength Index", overlay=false, max_labels_count=500)
// RSI — measures momentum by comparing average gains to average losses
p_ind_1 = ta.rsi(close, 14) // RSI with default period 14
// Plot the RSI line
plot(p_ind_1, "RSI", color.rgb(126, 87, 194, 0), 1)
// Reference levels: 70 = overbought, 50 = midline, 30 = oversold
p_ind_1_up = hline(70, "RSI - Upper Band", color=#787B86)
p_ind_1_middle = hline(50, "RSI - Middle Band", color=color.new(#787B86, 50))
p_ind_1_low = hline(30, "RSI - Lower Band", color=#787B86)
fill(p_ind_1_up, p_ind_1_low, color=color.rgb(126, 87, 194, 90), title="RSI - Background")
RSI Pine Script Parameters
The ta.rsi(source, length) function accepts two parameters. The table below lists each parameter, its default value, and recommended ranges for different trading styles.
| Parameter | Default Value | Description | Recommended Range |
|---|---|---|---|
| source | close | Price series used for the RSI calculation. Common alternatives: hl2, hlc3, ohlc4 | close (default) for most use cases; hl2 to reduce wick-driven extremes |
| length | 14 | Number of bars in the lookback window. Shorter values are more sensitive; longer values are smoother | 5–9 (scalping), 14 (swing, default), 21–25 (position trading) |
| overbought | 70 | Upper reference level plotted as a horizontal line (defined separately via hline()) | 60–65 (aggressive entries), 70 (standard), 80 (strong trend filter) |
| oversold | 30 | Lower reference level plotted as a horizontal line (defined separately via hline()) | 20 (strong trend filter), 30 (standard), 35–40 (aggressive entries) |
- Scalping (1M–5M charts): use
length = 7, thresholds 40/60 — faster signals with tighter range - Swing trading (4H–Daily): use the default
length = 14, thresholds 30/70 — Wilder's original settings - Position trading (Weekly): use
length = 21, thresholds 25/75 — captures multi-week momentum extremes
RSI Trading Strategies
Strategy 1: Classic Overbought / Oversold Reversal
Market environment: Ranging / Mean-reverting — works best when price oscillates within a defined range. Combine with Bollinger Bands to confirm range-bound conditions.
Entry conditions (long):
- RSI(14, close) crosses above 30 on the closing bar
- Price is within the lower Bollinger Band (20, 2.0) or near support
- Enter at the open of the next bar after the crossover confirms
Exit conditions:
- RSI reaches 70 — take full or partial profit
- Stop-loss: close below recent swing low before RSI crossed 30
Strategy 2: RSI + MACD Momentum Confirmation
Market environment: Trending — filters false signals from RSI alone by requiring MACD confirmation. Best applied on 1H–4H charts for swing trades.
Entry conditions (long):
- RSI(14) is above 50 (bullish momentum confirmed)
- MACD line crosses above signal line on the same bar or previous bar
- Price is above EMA 200 on the daily chart (bull market filter)
- Enter at next bar open after both conditions align
Exit conditions:
- RSI drops below 50 — momentum weakening, close position
- MACD line crosses below signal line — trend reversal signal
Strategy 3: RSI Divergence Reversal
Market environment: Trend exhaustion / reversal — identifies high-probability reversals before price confirms. Combine with volume to strengthen the signal.
Entry conditions (bullish divergence — long):
- Price makes a lower low compared to the previous swing low
- RSI(14) makes a higher low at the same point (hidden bullish divergence)
- RSI is in the 30–50 range at the second low
- Enter long when price closes above the swing high between the two lows
Exit conditions:
- Target: 1.5× to 2× the risk distance (measured from entry to stop-loss)
- Stop-loss: below the second swing low that formed the divergence
How to Generate RSI Pine Script in Pineify
Pineify generates production-ready Pine Script v6 code for the RSI indicator in seconds — no coding knowledge required.
- 1
Open Pineify at pineify.app
Sign up for a free account or log in to access the indicator builder.
- 2
Click "Add Indicator" and search for RSI
Select "Relative Strength Index" from the indicator library. Pineify includes 235+ built-in indicators.
- 3
Describe your strategy and configure RSI settings
Set the period, source, and overbought/oversold levels. Optionally add alert conditions, MACD confirmation, or Bollinger Band filters.
- 4
Copy the generated Pine Script v6 code
Pineify generates a complete, runnable script with your exact settings. Click "Copy" to copy it to your clipboard.
- 5
Adjust and paste into TradingView Pine Script Editor
Open the Pine Script Editor on TradingView, paste the code, click Save, and click "Add to chart" to see your RSI indicator live.
RSI Pine Script — Frequently Asked Questions
Related Pine Script Indicators
Explore other popular Pine Script indicators available in Pineify:
Ready to Generate Your RSI Indicator?
Pineify generates production-ready Pine Script v6 code for RSI and 235+ other indicators. Free to get started.
Get Started Free