Step-by-Step Guide to Building a Group-Based RSI Scanner in TradingView with Pine Script


Hello! In this blog post, we will examine step by step an indicator code written in Pine Script for use on the TradingView platform. This code allows users to scan and display the RSI (Relative Strength Index) values of different cryptocurrency symbols based on the groups they select, in a table format. Whether it’s “Group A”, “Group B”, or “Group C”, with this indicator, you can easily track the RSI values of specific symbols. Now, let’s explain in detail how the code works and what it does.


Purpose and Overview of the Code

This Pine Script script enables users on TradingView to select different cryptocurrency groups and display the 14-period RSI values of the symbols in the selected group in a table format. The main features of the code are:

  • Group Selection: The user can choose from “Group A”, “Group B”, or “Group C”.
  • Symbol Definition: Specific cryptocurrency symbols (e.g., BTCUSDT, ETHUSDT) can be selected for each group by the user.
  • RSI Calculation: The RSI values of the selected symbols are calculated and dynamically placed in the table.
  • Table Visualization: The RSI values are displayed in a table with a blue background and white borders, located in the middle right of the chart.

The code is written in Pine Script version 5 (//@version=5) and is defined as an indicator placed on the chart (overlay=true).


Structure of the Code and Step-by-Step Explanation

Let’s explain how each part of the code works by examining them separately.

1. Indicator Definition and Group Selection

//@version=5
indicator("Grup Bazlı Tarama Örneği", overlay=true)

grup = input.string("Grup A", "Grup Seçimi", options = ["Grup A", "Grup B", "Grup C"])
  • indicator: The name of the indicator is set as “Grup Bazlı Tarama Örneği” (Group-Based Scanning Example), and it is specified to be placed on the chart with overlay=true.
  • grup: An input that allows the user to select a group. By default, “Grup A” is selected, but “Grup B” and “Grup C” are also available options.

2. Definition of Groups and Symbols

Three groups are defined in the code, and for each group, specific symbols can be selected by the user:

Group A

sym_1 = input.symbol("BTCUSDT", "1.", group = "Grup A")
sym_2 = input.symbol("ETHUSDT", "2.", group = "Grup A")
sym_3 = input.symbol("BNBUSDT", "3.", group = "Grup A")
sym_4 = input.symbol("XRPUSDT", "4.", group = "Grup A")
  • This group contains four symbols: Bitcoin (BTCUSDT), Ethereum (ETHUSDT), Binance Coin (BNBUSDT), and Ripple (XRPUSDT).

Group B

sym_5 = input.symbol("DOGEUSDT", "5.", group = "Grup B")
sym_6 = input.symbol("SOLUSDT", "6.", group = "Grup B")
sym_7 = input.symbol("ADAUSDT", "7.", group = "Grup B")
sym_8 = input.symbol("MATICUSDT", "8.", group = "Grup B")
  • This group includes Dogecoin (DOGEUSDT), Solana (SOLUSDT), Cardano (ADAUSDT), and Polygon (MATICUSDT).

Group C

sym_9 = input.symbol("DOTUSDT", "9.", group = "Grup C")
sym_10 = input.symbol("LTCUSDT", "10.", group = "Grup C")
sym_11 = input.symbol("SHIBUSDT", "11.", group = "Grup C")
sym_12 = input.symbol("TRXUSDT", "12.", group = "Grup C")
// From sym_13 to sym_49, empty symbols are defined
  • Group C is designed to be expandable up to 40 symbols. However, in the example, only four symbols (Polkadot, Litecoin, Shiba Inu, Tron) are defined; the rest are left empty (“”).

Note: A total of 48 symbols can be defined in the code, but only the symbols in the selected group (up to 40) are scanned each time.

3. RSI Calculation

rsi = ta.rsi(close, 14)
  • ta.rsi(close, 14) calculates the 14-period RSI based on the closing prices. This calculation is applied to each selected symbol via the request.security function.

4. Scanning Function

Tarama(_pair1, _pair2, _pair3) => 
    _pair = grup=="Grup A" ? _pair1 : grup=="Grup B" ? _pair2 : _pair3     
    _rsi_value = request.security(_pair, timeframe.period, rsi, ignore_invalid_symbol=true)  
    _rsi_value
  • Purpose: This function selects the correct symbol based on the chosen group and retrieves its RSI value.
  • How it Works:
    • _pair: Using the ternary operator, it selects _pair1 (Group A), _pair2 (Group B), or _pair3 (Group C) based on the selected group.
    • request.security: Retrieves the RSI value of the selected symbol. With ignore_invalid_symbol=true, invalid symbols are skipped without causing errors.
    • The function returns the RSI value.

5. Scanning List

rsi1 = Tarama(sym_1, sym_5, sym_9)   
rsi2 = Tarama(sym_2, sym_6, sym_10) 
rsi3 = Tarama(sym_3, sym_7, sym_11)  
rsi4 = Tarama(sym_4, sym_8, sym_12)
  • Each rsi variable holds the RSI value of the corresponding symbol based on the selected group, using the Tarama function. For example:
    • If Group A is selected: rsi1 = RSI of BTCUSDT, rsi2 = RSI of ETHUSDT, etc.
    • If Group B is selected: rsi1 = RSI of DOGEUSDT, rsi2 = RSI of SOLUSDT, etc.

6. Creating the Table

var rsi_table = table.new(position.middle_right, 1, 40, border_color = color.white, border_width = 1, bgcolor = color.blue)
  • table.new: Creates a table in the middle right of the chart (position.middle_right).
  • The table consists of 1 column and 40 rows, with white borders (border_color = color.white) and a blue background (bgcolor = color.blue).

7. Filling the Table

The table is dynamically filled based on the selected group. A separate if block is used for each group:

Group A

if grup == "Grup A"
    table.cell(rsi_table, 0, 1, text = not na(sym_1) ? str.tostring(sym_1) + ": " + str.tostring(rsi1, "#.##") : na, text_color = color.white)
    table.cell(rsi_table, 0, 2, text = not na(sym_2) ? str.tostring(sym_2) + ": " + str.tostring(rsi2, "#.##") : na, text_color = color.white)
    // Continues for sym_3 and sym_4
  • If Group A is selected, the first four rows of the table will display BTCUSDT: RSI, ETHUSDT: RSI, etc.

Similarly, for Group B and Group C, the table is filled with the corresponding symbols and their RSI values.

Note: If a symbol is not defined (empty string), the corresponding table cell will be left empty.


How the Code Works

  1. The user selects a group (e.g., “Group A”) from the indicator’s settings menu under “Group Selection”.
  2. Based on the selected group, the Tarama function retrieves the RSI values of the corresponding symbols.
  3. These values are written into the table located in the middle right of the chart, along with the symbol names.
  4. The table displays only the first four symbols of the selected group (currently, only four rows are filled in the code).

Features and Extensibility of the Code

  • Modularity: Groups can be easily expanded by adding new symbols using input.symbol.
  • Flexibility: Designed to support up to 40 symbols per group, as seen in Group C.
  • User-Friendly: The table presents RSI values in a clear and readable manner.

If you want to display more symbols, you can define additional scanning variables like rsi5, rsi6, and update the table accordingly.


Conclusion

This Pine Script script is an excellent tool for scanning and visualizing the RSI values of cryptocurrencies in groups on TradingView. Users can quickly perform technical analysis of symbols based on their selected group and easily track market movements through the table. If you want to customize the code to your needs, you can increase the number of symbols or add different indicators (e.g., MACD, Bollinger Bands). 🚀