Indicator TutorialPine Script v6

TradingView RSI: settings and custom signal rules

Add Relative Strength Index from Indicators. Start with length 14 and Close, then distinguish a threshold reading from a crossing event.

RSI compares smoothed positive and negative price changes on a scale from 0 to 100. A reading above 70 or below 30 describes momentum; it does not establish that price will reverse. This guide separates RSI calculation settings from its optional moving average and provides a Pine Script example with confirmed threshold alerts.

Add RSI and check which settings change its value

Open Indicators on a TradingView chart, search for Relative Strength Index, and select the built-in indicator. Open its Settings to check RSI Length and Source. TradingView documents defaults of 14 bars and Close. Fourteen bars means fourteen observations at the selected calculation timeframe, not necessarily fourteen days.

The Smoothing section adds a separate moving average of RSI. Changing that MA does not change the underlying RSI calculation. TradingView's smoothing documentation explains the MA choices and the None option. The example below explicitly uses SMA 14; check your installed indicator's inputs rather than assuming every script has the same overlay defaults.

The built-in indicator also has a Calculate Divergence option, which must be enabled for its divergence alert conditions. The script on this page implements threshold events instead. For other studies, browse TradingView indicators.

Why Traders Use This Indicator

  • Compare the balance of smoothed upward and downward price changes.
  • Define threshold crossing rules that can be inspected on individual bars.
  • Compare price swings with oscillator swings when investigating divergence.
ParameterDefaultDescription
RSI length14Number of bars used by the RSI smoothing calculation. Shorter lengths respond more sharply to recent changes.
SourceCloseSeries whose consecutive changes feed RSI. Changing to hl2 or hlc3 changes the input data, without guaranteeing less noise.
Upper / lower levels70 / 30 in this exampleReference levels and alert thresholds. Editing them does not alter the RSI line.
Internal averagingWilder RMAThe gain and loss averages in ta.rsi use RMA. This is separate from the optional Smoothing overlay.
RSI overlaySMA 14 in this exampleAn average of the calculated RSI values. It is shown for comparison and does not trigger this script's alerts.
Price trend filterOff; EMA length 200An illustrative custom rule, not a built-in RSI default. When enabled, upward events require Close above the price EMA; downward events require Close below it.

How the RSI calculation and signal rules differ

For a selected source s, define change = s - s[1], gain = max(change, 0), and loss = max(-change, 0). Compute G = RMA(gain, n) and L = RMA(loss, n). Then RS = G / L and RSI = 100 - 100 / (1 + RS).

After initialization, Wilder's recursion is RMA(t) = (RMA(t-1) × (n-1) + value(t)) / n. Its weight on the latest value is 1/n; older observations retain decreasing weight. A 14-bar RSI is therefore not just an arithmetic average of the latest fourteen changes.

For an arithmetic illustration, if G = 2 and L = 1, RSI is approximately 66.67. If G = L and both are positive, RSI is 50. A zero loss average with positive gains corresponds to 100; a zero gain average with positive losses corresponds to 0. The both-zero case cannot be resolved by dividing G by L. Use ta.rsi in the working script to retain the built-in initialization and edge-case behavior.

Signal Interpretation

A level is not a crossing

RSI < 30 remains true on every bar below 30. A crossing above 30 requires the previous value to be at or below 30 and the current value to be above 30. The example only marks that crossing after the current bar closes. Its opposite event crosses down through 70, from a previous value at or above 70.

Illustrative sequence: closed-bar readings of 28, 29 and 31 produce one upward event on 31. Readings of 72, 75 and 69 produce one downward event on 69. A move from 29 to 30 produces no upward event because the current value must exceed the level. These examples explain Boolean rules; they are not market tests or trade results.

Divergence needs a separate definition

Regular bullish divergence compares a lower price low with a higher RSI low; bearish divergence compares a higher price high with a lower RSI high. Hidden divergence reverses those pairings. Neither pattern guarantees continuation or reversal. A pivot method must specify the compared swings and the bars needed to confirm them. A marker drawn back on a pivot bar was not necessarily available on that bar. This page's threshold script does not detect divergence.

TradingView's repainting documentation explains open-bar changes and backdated pivots. Its alert documentation explains alert creation and saved script snapshots.

Combining with Other Indicators

The optional EMA filter compares Close with a price EMA, not RSI with its SMA. With the filter enabled, the 29-to-31 crossing produces an upward marker only if Close is above the EMA on that same closed bar. A close equal to the EMA qualifies for neither direction. The value 200 is an example input, not a proven optimum.

Filters remove some events and may delay or exclude useful ones. In sideways prices, repeated RSI crossings can still occur while the EMA changes direction slowly. In a persistent trend, RSI can stay outside 30/70 for an extended period. Compare filtered and unfiltered events on the same symbol, session and timeframe before drawing conclusions. See crossover alert rules for related conditions.

Writing Pine Script Manually

Challenges of Manual Coding

Confusing a persistent state, such as RSI below 30, with a one-bar crossing event.

Comparing a custom RSI with a built-in study that uses another source or calculation timeframe.

Allowing a crossing on an open bar to trigger before the final RSI value is known.

Pine Script v6
//@version=6
indicator("RSI threshold rules", shorttitle="RSI rules", overlay=false, precision=2)

rsiLength = input.int(14, "RSI length", minval=1)
rsiSource = input.source(close, "RSI source")
upperLevel = input.float(70, "Upper level", minval=50.1, maxval=100)
lowerLevel = input.float(30, "Lower level", minval=0, maxval=49.9)
maLength = input.int(14, "RSI SMA length", minval=1)
useTrendFilter = input.bool(false, "Use price EMA filter")
trendLength = input.int(200, "Price EMA length", minval=1)

rsiValue = ta.rsi(rsiSource, rsiLength)
rsiAverage = ta.sma(rsiValue, maLength)
priceTrend = ta.ema(close, trendLength)

// Evaluate crossing functions on every bar before applying filters.
crossUp = ta.crossover(rsiValue, lowerLevel)
crossDown = ta.crossunder(rsiValue, upperLevel)
upEvent = barstate.isconfirmed and crossUp and (not useTrendFilter or close > priceTrend)
downEvent = barstate.isconfirmed and crossDown and (not useTrendFilter or close < priceTrend)

plot(rsiValue, "RSI", color=color.blue, linewidth=2)
plot(rsiAverage, "RSI SMA", color=color.orange)
hline(upperLevel, "Upper level", color=color.gray)
hline(50, "Midpoint", color=color.new(color.gray, 60))
hline(lowerLevel, "Lower level", color=color.gray)
plotshape(upEvent, title="Cross above lower level", style=shape.triangleup, location=location.bottom, color=color.teal, size=size.tiny)
plotshape(downEvent, title="Cross below upper level", style=shape.triangledown, location=location.top, color=color.red, size=size.tiny)

alertcondition(upEvent, "RSI crossed above lower level", "RSI crossed above its lower level on {{ticker}} at bar close.")
alertcondition(downEvent, "RSI crossed below upper level", "RSI crossed below its upper level on {{ticker}} at bar close.")

Maintenance Note: This is an indicator, not an order-executing strategy. The RSI and SMA lines update during an open bar; event markers and alert conditions wait for bar close. The script uses the chart timeframe, makes no higher-timeframe requests and draws no markers into past bars. Syntax validation does not establish profitability.

Build with Pineify Visual Editor

Pineify lets you configure supported indicators and export Pine Script. Available settings and plan requirements vary by indicator. Compare the generated rules with the example before using its signals.

FeatureManual Pine ScriptPineify Visual Editor
RSI settingsEdit source and length in Pine inputsConfigure the Standard RSI source and length visually
Custom conditionsWrite and inspect Boolean expressionsConfigure supported conditions or request custom code from the AI Coding Agent
VerificationCompile and compare the plotted values and eventsInspect the generated code and verify it on TradingView

Configure Standard RSI using the visual editor.

Use the AI Coding Agent to draft a specific filter or alert rule.

Review the resulting Pine Script before adding it to a chart.

Step-by-Step Tutorial

1

Create an indicator

Open the Pineify visual editor and start an indicator project.

2

Add Standard RSI

Set its source to Close and length to 14 for a baseline comparison.

3

Define the event

Specify a crossing direction and threshold. For custom code, tell the AI Coding Agent whether to wait for bar close and whether to add a price trend filter.

4

Inspect the result

Generate the code, check the inputs and conditions, then compile it in the TradingView Pine Editor.

5

Compare and create an alert

Check closed-bar RSI values against the built-in study with matching settings. Create the desired alert in TradingView after reviewing the event markers.

Trading Strategies & Pro Tips

Threshold return observations

Record crossings back above 30 and below 70 separately from touches and persistent extreme readings. The script marks events only; it specifies no positions, exits, sizing or transaction costs.

Pro Tip: Keep the same threshold pair when comparing different lengths so you can attribute changes to one input.

Trend filter comparison

Compare the optional EMA filter on and off while holding the symbol, timeframe, source and sample period constant. Record which events disappear, including events you would have preferred to keep.

Pro Tip: This comparison protocol has not been run as a performance experiment for this page.

Sideways-market review

Inspect sequences where RSI crosses repeatedly without sustained price movement. Count repeated events and examine the price path after each one before treating crossings as entries.

Pro Tip: Any trading backtest needs separate entry, exit, cost and risk rules plus data not used to select the settings.

Common Mistakes to Avoid

  • Treating 70 as a compulsory sell or 30 as a compulsory buy.
  • Changing the Smoothing MA and expecting the underlying RSI line to change.
  • Assuming a shorter RSI length or a wider threshold pair is best for every intraday market.
  • Confusing a price EMA filter with an average of RSI.
  • Reading a pivot marker plotted in the past as an alert that was available at that time.

Frequently Asked Questions

TradingView documents an RSI length of 14 bars and Close as the source. Smoothing adds a separate moving average. This tutorial uses SMA 14 and reference levels 70 and 30; check your indicator settings when comparing scripts.

There is no universal best setting established here. Shortening the length changes sensitivity, while widening thresholds changes which events qualify. Compare one change at a time and evaluate any trading rules with costs and data outside the tuning sample.

The built-in Smoothing section adds an MA of RSI. It does not replace the RMA calculation of gains and losses inside RSI. Changing the RSI source or length does change the underlying RSI line.

Its RSI and SMA values can move during an open bar. Its event markers and alert conditions require barstate.isconfirmed, so they wait for the chart bar to close. It has no pivot backdating or higher-timeframe requests. Data revisions can still alter historical values.

No. It implements crossings above the lower threshold and below the upper threshold. TradingView built-in RSI offers a separate Calculate Divergence setting, which must be enabled for its divergence alert conditions. Pivot-based signals have confirmation delays.

TradingView saves a snapshot of the script and inputs when an alert is created. Editing the chart indicator does not update that running alert. Delete it and create a new alert with the intended settings.

Create an RSI indicator with explicit rules

  • Choose the source and length in the visual editor.
  • Describe custom threshold and filter rules to the AI Coding Agent.
  • Inspect and test the generated Pine Script on TradingView.

Disclaimer: Trading involves significant risk. The indicators, code, and strategies provided are for educational purposes only and do not constitute financial advice. Past performance is not indicative of future results.