How to compile a list/dataframe of lowest 50 rsi of nasdaq stocks for a period in r?

Member

by maureen , in category: Technical Analysis , a month ago

How to compile a list/dataframe of lowest 50 rsi of nasdaq stocks for a period in r?

Facebook Twitter LinkedIn Whatsapp

1 answer

by rubye_denesik , a month ago

@maureen 

To compile a list or dataframe of the lowest 50 RSI (Relative Strength Index) values of NASDAQ stocks for a specific period in R, you can follow these steps:

  1. Install and load required packages: Install the quantmod package if not already installed and then load it using the library() function.
1
2
install.packages("quantmod")
library(quantmod)


  1. Get the list of NASDAQ symbols: Use the nasdaqtraded function from the quantmod package to get a dataframe of all NASDAQ-listed stocks.
1
nasdaq_list <- nasdaqtraded()


  1. Filter NASDAQ symbols: Extract the symbols (ticker names) from the nasdaq_list dataframe.
1
symbols <- nasdaq_list$Symbol


  1. Retrieve RSI for each stock: Use a loop to iterate over the symbols and retrieve the RSI values using the RSI() function from the quantmod package. Store the RSI values in a new dataframe.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
rsi_data <- data.frame()
for (symbol in symbols) {
  tryCatch({
    data <- getSymbols(symbol, from = "2021-01-01", to = "2021-12-31", auto.assign = FALSE)
    rsi <- RSI(Cl(data), n = 14)  # Assuming a period of 14 for RSI calculation
    last_rsi <- tail(rsi, 1)
    rsi_data <- rbind(rsi_data, data.frame(symbol = symbol, rsi = last_rsi))
  }, error = function(e) {
    # Ignore symbols that throw errors
  })
}


Note: The tryCatch block is used to handle any errors that may occur while retrieving data for a particular stock. This allows the loop to continue and collect data for other stocks even if there are errors for some of them.

  1. Sort and select lowest 50 RSI values: Sort the rsi_data dataframe based on the RSI values in ascending order and select the lowest 50 RSI values using the head() function.
1
lowest_50_rsi <- head(rsi_data[order(rsi_data$rsi), ], 50)


  1. View the resulting dataframe: Print or view the lowest_50_rsi dataframe to see the symbols and corresponding RSI values.
1
print(lowest_50_rsi)


This process retrieves the RSI values of NASDAQ stocks for a specified period, selects the lowest 50 RSI values, and stores them in a dataframe for further analysis or use.


Please note that the above code is for educational purposes and may require modifications based on your specific needs or preferences.