Essentially what I am trying to do is return the names and RSI's of the companies with the lowest RSI's for that day (or period).
I have tried the follwoing code: I am wanting to either get the lowest 30 values today, or the average over the last week. Any suggestions and critisicm would be much appreciated
# Install required packages
install.packages("quantmod")
# Load required packages
library(quantmod)
# Define the start and end dates for the week
start_date <- Sys.Date() - as.numeric(format(Sys.Date(), "%u")) + 1 # Monday of the current week
end_date <- start_date + 6 # Sunday of the current week
# Get the list of S&P 500 tickers
sp500_tickers <- stockSymbols("NASDAQ")$symbols
# Define a function to calculate cumulative RSI for a given ticker
get_cumulative_rsi <- function(ticker, start_date, end_date) {
# Fetch the stock data
stock_data <- getSymbols(ticker, from = start_date, to = end_date, auto.assign = FALSE)
# Calculate RSI
rsi <- RSI(Cl(stock_data))
# Calculate cumulative RSI
cumulative_rsi <- sum(rsi, na.rm = TRUE)
return(cumulative_rsi)
}
# Initialize an empty data frame to store ticker and cumulative RSI values
cumulative_rsi_df <- data.frame(Ticker = character(), Cumulative_RSI = numeric(), stringsAsFactors = FALSE)
# Loop through each ticker in the S&P 500
for (ticker in sp500_tickers) {
tryCatch({
# Calculate cumulative RSI for the current ticker
cumulative_rsi <- get_cumulative_rsi(ticker, start_date, end_date)
# Add the ticker and cumulative RSI to the data frame
cumulative_rsi_df <- rbind(cumulative_rsi_df, data.frame(Ticker = ticker, Cumulative_RSI = cumulative_rsi, stringsAsFactors = FALSE))
}, error = function(e) {
# Print error message if any
cat("Error processing ticker", ticker, ":", conditionMessage(e), "\n")
})
}
# Sort the data frame by cumulative RSI in ascending order
sorted_rsi_df <- cumulative_rsi_df[order(cumulative_rsi_df$Cumulative_RSI), ]
# Retrieve the top 30 companies with lowest cumulative RSI
top_30_companies <- sorted_rsi_df[1:30, ]
# Print the result
print(top_30_companies)
I am only getting NA returns.
I have also tried the following, which returns "Error in stockSymbols("^GSPC", src = "yahoo", auto.assign = FALSE) : unused arguments (src = "yahoo", auto.assign = FALSE)"
'''
# Install required packages
install.packages("quantmod")
# Load required packages
library(quantmod)
# Define the date for yesterday
yesterday <- Sys.Date() - 1
# Get the list of S&P 500 tickers
sp500_tickers <- stockSymbols("^GSPC", src = "yahoo", auto.assign = FALSE)$symbols
# Define a function to calculate RSI for a given ticker on a specific date
get_rsi <- function(ticker, date) {
# Fetch the stock data
stock_data <- getSymbols(ticker, from = date, to = date, src = "yahoo", auto.assign = FALSE)
# Calculate RSI
rsi <- RSI(Cl(stock_data))
return(rsi)
}
# Initialize an empty data frame to store ticker and RSI values
rsi_df <- data.frame(Ticker = character(), RSI = numeric(), stringsAsFactors = FALSE)
# Loop through each ticker in the S&P 500
for (ticker in sp500_tickers) {
tryCatch({
# Calculate RSI for the previous day
rsi <- get_rsi(ticker, yesterday)
# Add the ticker and RSI to the data frame
rsi_df <- rbind(rsi_df, data.frame(Ticker = ticker, RSI = rsi, stringsAsFactors = FALSE))
}, error = function(e) {
# Print error message if any
cat("Error processing ticker", ticker, ":", conditionMessage(e), "\n")
})
}
# Sort the data frame by RSI in ascending order
sorted_rsi_df <- rsi_df[order(rsi_df$RSI), ]
# Retrieve the companies with the lowest 50 RSI values
lowest_50_companies <- sorted_rsi_df[1:50, ]
# Print the result
print(lowest_50_companies)
'''