The RSI indicator, or Relative Strength Index, is J. Welles Wilder Jr.'s momentum oscillator, published in 1978, comparing smoothed upward and downward price changes on a 0–100 scale. Its formula is RSI = 100 − 100 ÷ (1 + RS), where RS is average gain divided by average loss, using Wilder's smoothing. RSI can describe a condition such as being above a level or a crossing event; neither specifies a complete trade.

RSI at a glance
Scroll horizontally to read every column.
| Feature | Meaning |
|---|---|
| Measures | Upward price changes relative to downward price changes in one price series. |
| Scale | 0–100; equal positive smoothed gains and losses give 50. |
| Smoothing | Wilder's moving average, called RMA in Pine Script. |
| Common reference levels | 70 and 30 are commonly described as overbought and oversold, not instructions to trade. |
| Platforms | TradingView's RSI and Pine Script v6's ta.rsi; MetaTrader 5's Relative Strength Index. |
TradingView's RSI help attributes the indicator to Wilder's 1978 book and gives his 70 overbought and 30 oversold levels.[11] MetaQuotes documents the same formula for MetaTrader 5.[1] The indicator rules library compares the inputs each indicator measures. RSI measures price changes; Bollinger Bands measure price location relative to a moving average and dispersion.
What is the RSI formula, including Wilder's smoothing?
Choose a price source and a positive integer period n. This page uses consecutive closing prices, with no missing observations. Let change = current close − previous close. The upward change is the larger of change and zero; the downward change is the larger of −change and zero.
Average gain = RMA(upward changes, n); average loss = RMA(downward changes, n)
RS = average gain ÷ average loss; RSI = 100 − 100 ÷ (1 + RS)
Pine Script v6's ta.rsi(source, length) uses RMA for the upward and downward changes.[2] Average loss here is a positive magnitude of downward price movement, not a losing trade's monetary result. RSI does not use account profits or compare the instrument with another instrument.
RMA today = ((n − 1) × RMA yesterday + today's input) ÷ n
The smoothing weight on today's input is 1/n. For a contiguous series, initialise the RMA with the simple average of the first n valid inputs; subsequent values use the recurrence above.[3] Consequently, an n-period RSI retains a diminishing contribution from earlier changes. It does not discard all history older than n bars.
When both averages are positive, an equivalent expression is 100 × average gain ÷ (average gain + average loss). If average loss is zero and average gain is positive, the limiting value is 100. If average gain is zero and average loss is positive, it is zero. Both being zero makes the ratio undefined; an implementation needs an explicit convention. Check flat-price and warm-up cases before treating an unavailable value as a signal.
Worked example: calculate RSI from closing prices
Illustrative numbers and period, not a recommendation. Use a four-period RSI and the five closes 100, 103, 102, 105 and 104. Five closes supply four price changes: +3, −1, +3 and −1.
- Seed the averages: upward changes total 6, so average gain = 6 ÷ 4 = 1.5. Downward changes total 2, so average loss = 2 ÷ 4 = 0.5.
- Calculate RS: 1.5 ÷ 0.5 = 3.
- Calculate RSI: 100 − 100 ÷ (1 + 3) = 75.
- Add the next close, 102: the new change is −2, so the upward input is zero and the downward input is 2.
- Update the smoothed averages: gain = (3 × 1.5 + 0) ÷ 4 = 1.125; loss = (3 × 0.5 + 2) ÷ 4 = 0.875.
- Recalculate: RSI = 100 × 1.125 ÷ (1.125 + 0.875) = 56.25.
The second value uses the previous smoothed averages. Replacing those with fresh simple averages of the latest four changes produces a different indicator. A platform with additional earlier data can also start from a different smoothing state; match the history before comparing results.
How does an RSI crossing differ from RSI being above a level?
An above-level condition describes a state. An upward crossing describes the transition from at or below a level to above it. TradingView's alerts documentation illustrates RSI crossings using ta.crossover and ta.crossunder.[4]
Cross above level L = previous RSI ≤ L and current RSI > L
Illustrative alert condition: on a confirmed chart-bar close, record an event when the previous RSI was at or below 50 and the current RSI is above 50. The number 50 defines this example, not a recommended trigger.
| Previous → current RSI | Current RSI above 50? | Cross above 50? |
|---|---|---|
| 48 → 50 | No | No: equality does not pass the current comparison. |
| 50 → 54 | Yes | Yes: equality passes the previous comparison. |
| 54 → 56 | Yes | No: RSI was already above. |

A below-level state and a downward crossing likewise differ. The downward event requires previous RSI at or above the level and current RSI strictly below it. The same distinction applies to moving-average crossover rules. Frequency settings do not supply position limits or decide what a later crossing should do.
What do overbought, oversold and divergence actually mean?
Overbought and oversold describe RSI's position relative to chosen reference levels. TradingView's RSI help reports that Wilder treated readings above 70 as overbought and below 30 as oversold, and notes that some traders alter those ranges.[11] These are conventions, not trading instructions. A value above 70 means smoothed gains dominate losses under this formula. The number does not establish that the next price change must be downward.
Regular bullish divergence compares a lower price low with a higher RSI low. Regular bearish divergence compares a higher price high with a lower RSI high.[11][1] Neither phrase alone gives a reproducible algorithm. A mechanical definition must identify:
- The pivots: what constitutes a price swing and how many bars confirm it.
- The RSI observations: RSI at the price-pivot bars, or separately detected RSI pivots.
- The matching rule: which two swings are paired and how equality is handled.
- The event time: when the later pivot becomes knowable, not merely where it is drawn.
TradingView warns that plotting confirmed pivots on earlier bars can misrepresent when the information became available.[5] A divergence marker beneath a past low may therefore describe a later discovery. The pivot confirmation guide explains that delay.
Where do RSI rules run in TradingView and MetaTrader 5?
As of 25 September 2026, Pine Script v6 exposes RSI through ta.rsi; TradingView's alerts manual includes an RSI-crossing example.[2][4] MetaTrader 5 documents Relative Strength Index among its oscillators.[1] Matching an indicator name does not establish matching price data, history or values.
For the illustrative condition above, record the source, length, feed and timeframe, then evaluate the crossing at the chart bar's close. RSI can fluctuate while a realtime bar develops.[5] Closing-bar evaluation addresses that changing input; a separate higher-timeframe input still needs its own confirmation rule.
A Pine alert condition only makes an event available. Create the running alert in TradingView and select its intended condition and frequency. TradingView stores the script, inputs, symbol and timeframe with the alert; recreate it after changing that context.[4]
The published RSI Strategy script-library example offers related implementation context. Check its language version and message parameters against the current PineConnector Syntax reference before adapting it.[6] An example's selected levels and sizing do not establish suitable settings for another rule.
How does an RSI event become a PineConnector order request?
An RSI condition needs a separate action specification: direction, broker symbol, volume, exits and repeat handling. PineConnector's setup test distinguishes message processing from the broker trade.[7] Follow the evidence through each stage:
- Condition: the defined RSI comparison becomes true.
- Alert: the configured TradingView alert triggers; inspect its log.
- Delivery: the webhook carries the selected message.
- Processing: match the message and intended account in PineConnector Portal → Bridge.
- EA request: establish whether an order request was submitted.
- Broker outcome: distinguish acceptance, the resulting deal or deals, and the account's position.
MetaQuotes defines orders, deals and positions separately; one order can produce several deals.[8] An RSI-based exit condition in TradingView is also different from a broker-side stop loss. The indicator level does not create that stop.
TradingView and MetaTrader may use different feeds, quotes and timestamps, as PineConnector's price-difference FAQ explains.[9] Compare the intended RSI event with its resulting demo-account evidence, rather than expecting a broker chart to reproduce every TradingView value.
Which RSI mistakes change the rule?
- Optimising the period after inspecting every outcome. Changing the length, levels and confirmation rules adds choices fitted to the same history. TradingView documents overfitting and testing on separate data without further tuning.[10]
- Treating an unfinished crossing as final. The RSI value may move back before the bar closes. An earlier alert cannot be undone by the later chart value.
- Replacing RMA with a rolling average. The smoothing definition and its initial state affect the result.
- Sending repeated entries from an above-level state. Remaining above the threshold is not a new crossing and says nothing about broker exposure.
- Backdating divergence. The pivot's location and its confirmation time are different observations.
No universal RSI period or threshold follows from the formula. A precise condition establishes what the rule means, not whether it has an edge.
Frequently asked questions
What is the RSI formula?
RSI equals 100 − 100 ÷ (1 + RS), where RS is Wilder-smoothed average gain divided by Wilder-smoothed average loss. Calculate upward and downward changes from the chosen price source, keeping losses as positive magnitudes. Wilder's smoothing weights each new observation by 1/n, where n is the period. Zero-denominator and warm-up cases require explicit handling.
Does RSI overbought or oversold mean a reversal?
No. RSI above 70 or below 30 is conventionally labelled overbought or oversold, but those labels do not specify the next price move. RSI measures the balance of smoothed upward and downward changes. A trading rule must separately define its event, timing, action and exits; there is no universally suitable RSI threshold.
How do you use ta.rsi in Pine Script?
Pine Script v6's ta.rsi(source, length) calculates RSI from a price source and period. A crossing condition additionally compares the current and previous RSI values with a level. The script must expose an alert event, and the user must create the running TradingView alert. Computing RSI alone neither creates an alert nor places an MT5 order.
What is RSI divergence?
Regular bullish RSI divergence pairs a lower price low with a higher RSI low; regular bearish divergence pairs a higher price high with a lower RSI high. Automating either requires precise swing selection, matching and confirmation rules. A pivot drawn on an earlier bar may only become identifiable after later bars have arrived.
Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account and no code was compiled.
Related reading
- Indicator rules library
- Moving-average crossover rules
- Bollinger Bands: formula and rule definitions
- Pivot confirmation and repainting
- RSI Strategy script-library example
Sources
- MetaQuotes – Relative Strength Index, accessed 25 September 2026.
- TradingView – Pine Script v6 reference: ta.rsi, accessed 25 September 2026.
- TradingView – Pine Script v6 reference: ta.rma, accessed 25 September 2026.
- TradingView – Alerts, accessed 25 September 2026.
- TradingView – Repainting, accessed 25 September 2026.
- PineConnector – PineConnector Syntax, accessed 25 September 2026.
- PineConnector – Test your setup, accessed 25 September 2026.
- MetaQuotes – Basic principles: orders, deals and positions, accessed 25 September 2026.
- PineConnector – Frequently asked questions: price differences, accessed 25 September 2026.
- TradingView – Strategies: overfitting, accessed 25 September 2026.
- TradingView – Relative Strength Index (RSI), accessed 25 September 2026.
PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.