question for my code about finance

Hello, I am trying to build a code in order to calculate the mean and variance of some stocks,
when I write this

get_stock_data3 <- function(stock_list, date_debut, date_fin){

for (stock_symbol in stock_list) {

stock_results <- tq_get(stock_symbol, from = date_debut, to = date_fin, get = "stock.prices")

stock_results <- as.numeric(unlist(stock_results[, 6]))
n <- length(stock_results)

stock_results$returns <- ((stock_results[2:n] -stock_results[1:(n-1)])/stock_results[1:(n-1)])

mean_returns <- mean(stock_results$returns, na.rm = TRUE)

sd_returns <- sd(stock_results$returns, na.rm = TRUE)

results_data <- tibble(Stock_symbol = stock_symbol, Mean_returns = mean_returns, SD_returns = sd_returns)
return(results_data)

}
}

when I write for example
stock_list <— c("AAPL", "MSFT",...)
resultstest<—get_stock_data3(stock_list, "2020-10-25","2021-10-25")
this program gives me the right answer but only for the first stock of the list.

any idea how to fix this problem ?
thank you very much

Hi,

Welcome to the RStudio community!

In order for us to help you with your question, please provide us a minimal reproducible example where you provide a minimal (dummy) dataset and code that can recreate the issue. Once we have that, we can go from there. For help on creating a Reprex, see this guide:

Good luck!
PJ

Hi,

I usually stick with the tidyverse, especially since you're using tidyquant to get the data, you're already in the tidyverse. That said, you only completed one loop since the return clause was within the internal loop, that actually escapes your function in that first loop, hence only returning the first stock.

I created a vector of type list, and then for each loop, a new list is added to the vector. At the end, I bind the lists together into one dataframe, or tibble, after the loop runs.


get_stock_data3 <- function(stock_list, date_debut, date_fin){
    
    stock_list_df <- vector(length = length(stock_list), 'list')
    i = 1

    for (stock_symbol in stock_list) {
        
        stock_results <- tq_get(stock_symbol, from = date_debut, to = date_fin, get = "stock.prices")
        
        stock_results <- as.numeric(unlist(stock_results[, 6]))
        n <- length(stock_results)
        
        stock_results$returns <- ((stock_results[2:n] -stock_results[1:(n-1)])/stock_results[1:(n-1)])
        
        mean_returns <- mean(stock_results$returns, na.rm = TRUE)
        
        sd_returns <- sd(stock_results$returns, na.rm = TRUE)
        
        results_data <- tibble(Stock_symbol = stock_symbol, Mean_returns = mean_returns, SD_returns = sd_returns )
        
        
        stock_list_df[[i]] <- results_data
        i <- i + 1
        
        #return(results_data)
    }
    
    return(do.call("rbind", stock_list_df))
}

stock_list <- c("AAPL", "MSFT")
resultstest<- get_stock_data3(stock_list, "2020-10-25","2021-10-25")

A few less lines, and in tidyverse:

get_stock_stats <-  function(stock_symbol, .date_debut, .date_fin) {
    
    tq_get(stock_symbol, from = .date_debut, to = .date_fin,  get = "stock.prices") %>% 
        select(symbol, close) %>% 
        mutate(return = close / lag(close) - 1) %>% 
        group_by(symbol) %>% 
        summarise(across(return, list(mean = ~mean(.x, na.rm = TRUE),
                                      sd = ~sd(.x, na.rm = TRUE)
        )
        )
        )
}

map_df(c("AAPL", "MSFT"), ~get_stock_stats(., .date_debut = "2020-10-25", .date_fin = "2021-10-25"))

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.