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 , 3 months ago

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

Facebook Twitter LinkedIn Whatsapp

2 answers

by rubye_denesik , 3 months 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.

Member

by annalise , 2 days 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 the steps outlined below. This code is based on the guidance provided:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Install and load required packages
if (!requireNamespace("quantmod", quietly = TRUE)) {
  install.packages("quantmod")
}
library(quantmod)

# Get the list of NASDAQ symbols
nasdaq_list <- nasdaqtraded()

# Filter NASDAQ symbols
symbols <- nasdaq_list$Symbol

# Create an empty dataframe to store RSI data
rsi_data <- data.frame()

# Retrieve RSI for each stock
for (symbol in symbols) {
  tryCatch({
    data <- getSymbols(symbol, from = "2021-01-01", to = "2021-12-31", auto.assign = FALSE)
    if (!is.null(data)) {
      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
  })
}

# Sort and select lowest 50 RSI values
lowest_50_rsi <- head(rsi_data[order(rsi_data$rsi), ], 50)

# View the resulting dataframe
print(lowest_50_rsi)


In this code snippet:

  1. We start by loading the required quantmod package and fetching the list of NASDAQ symbols.
  2. We iterate over each symbol, retrieve the RSI values for the specified period, and store them in a dataframe.
  3. Any symbols that throw errors during data retrieval are ignored.
  4. The resulting dataframe is then sorted based on RSI values in ascending order, and the lowest 50 RSI values are selected.
  5. Finally, the dataframe containing the lowest 50 RSI values for NASDAQ stocks is printed for viewing.


You can run this code in your R environment after making necessary modifications and adjustments according to your requirements. Remember to replace the date range, RSI period, or any other parameters as needed for your analysis.