R函数有cryptp历史数据

问题描述 投票:0回答:1
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)
   }

我想收集6种不同加密货币的数据,并将它们收集在6个不同的数据帧中,如上面的代码所示

r quantmod
1个回答
0
投票
library(tidyquant)

tickers <- c("SOL", "ETH")

tq_get(str_c(tickers, "-USD"), from = "2024-01-01") 

# A tibble: 96 × 8
   symbol  date        open  high   low close     volume adjusted
   <chr>   <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
 1 SOL-USD 2024-01-01 102.  110.  102.  110.  2157671990    110. 
 2 SOL-USD 2024-01-02 110.  117.  106.  107.  3782057553    107. 
 3 SOL-USD 2024-01-03 107.  110.   92.9  98.6 5472216595     98.6
 4 SOL-USD 2024-01-04  98.6 108.   97.1 105.  3272723247    105. 
 5 SOL-USD 2024-01-05 105.  105.   95.9 100.  3022127826    100. 
 6 SOL-USD 2024-01-06 100.  100.   92.0  93.9 2235902092     93.9
 7 SOL-USD 2024-01-07  93.9  96.7  88.2  89.3 2288061692     89.3
 8 SOL-USD 2024-01-08  89.3  99.8  85.6  97.8 4152448360     97.8
 9 SOL-USD 2024-01-09  97.8 104.   95.9  99.4 4070131683     99.4
10 SOL-USD 2024-01-10  99.4 105.   92.6 102.  4216429514    102. 
# ℹ 86 more rows
# ℹ Use `print(n = ...)` to see more rows
© www.soinside.com 2019 - 2024. All rights reserved.