I am using tidyquant to get several stocks prices, and here are my codes.
install.packages(“tidyquant")
install.packages(“dplyr")
library(tidyquant)
library(dplyr)
tq_index(“SP500”)
stock_symbols <- c("NVDA", "TSLA", “META")
start_date <- “2018-03-24"
end_date <- “2023-03-24"
stock_prices <- stock_symbols %>%
tq_get(get = “stock.prices”,
from = start_date,
to = end_date,
collapse = “daily”)
print(stock_prices)
after I run, received warning messages as below:
Warning message:
There were 3 warnings in dplyr::mutate()
.
The first warning was:
In argument: data.. = purrr::map(...)
.
Caused by warning:
! x = 'NVDA', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "NVDA", env = , verbose = FALSE, : Unable to import “NVDA”.
$ operator is invalid for atomic vectors
Removing NVDA.
how can I fix it?
pls help me!
Try this code instead. I have executed it and it works fine.
# Load packages ----
library(dplyr)
library(tidyquant)
# Download stock prices ----
stock_symbols <- c("NVDA", "TSLA", "META")
start_date <- "2018-03-24"
end_date <- "2023-03-24"
stock_prices <- tq_get(
x = stock_symbols,
get = "stock.prices",
from = start_date,
to = end_date
)
# Print stock prices ----
stock_prices
# A tibble: 3,774 × 8
symbol date open high low close volume adjusted
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 NVDA 2018-03-26 59.5 61.1 59.0 61.1 61142400 60.5
2 NVDA 2018-03-27 61.9 62.5 55.0 56.4 140900000 55.8
3 NVDA 2018-03-28 56.0 57.3 54.2 55.3 94590400 54.8
4 NVDA 2018-03-29 56.0 58.9 55.2 57.9 91662800 57.3
5 NVDA 2018-04-02 57.2 58.7 54.4 55.3 92209200 54.7
6 NVDA 2018-04-03 57.0 57.6 55.3 56.3 66743200 55.8
7 NVDA 2018-04-04 53.8 56.7 53.5 56.6 78916800 56.0
8 NVDA 2018-04-05 57.2 57.3 54.6 55.3 70124000 54.8
9 NVDA 2018-04-06 54.3 55.4 53.3 53.6 66298800 53.0
10 NVDA 2018-04-09 54.2 55.5 53.7 53.9 49952400 53.3
# ℹ 3,764 more rows
# ℹ Use `print(n = ...)` to see more rows
system
Closed
April 27, 2023, 4:48am
3
This topic was automatically closed 21 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.