@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 2 |
install.packages("quantmod")
library(quantmod)
|
1
|
nasdaq_list <- nasdaqtraded() |
1
|
symbols <- nasdaq_list$Symbol |
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
|
lowest_50_rsi <- head(rsi_data[order(rsi_data$rsi), ], 50) |
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.
@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:
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.