Search code examples
rquantmod

R function to have cryptp historical data


library(quantmod)
library(tidyverse)
#collect the ticker from the data frame e paste with "-USD" to use it on getSymbols
coins_portfolio <-paste0(crypto$ticker,"-USD") # eg SOL-USD, ETH-USD
#add the new tickers to dataframe
crypto$quantmod <- coins_portfolio
#create a name for every coin in order to collect data
nomi_df <- paste0(tolower(crypto$ticker),"_data")
#start date to collect
start_coin <- as.Date("2024-01-01")
#end date to collect
end_date <- Sys.Date()


for(i in 1:nrow(crypto)){
  nomi_df[i]<- getSymbols(crypto$quantmod[i],src="yahoo",from=start_coin, to= end_date, auto.assign = FALSE)
   }

i want to collect the data of 6 different crypto-currency and collect them in 6 different data frame called as shown in the code above


Solution

  • library(tidyverse)
    library(tidyquant)
    
    tickers <- c("SOL", "ETH")
    
    map(tickers, ~ assign(str_c(str_to_lower(.x), "_data"), 
                          tq_get(str_c(.x, "-USD"), from = "2024-01-01"), 
                          envir = .GlobalEnv))