business-science / tidyquant

Bringing financial analysis to the tidyverse

Home Page:https://business-science.github.io/tidyquant/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tq_get("SP500") not retrieving sp500 stock prices

aminekhasteh opened this issue · comments

I have been getting a warning message when I try getting SP500 index. By running the following code:

tq_index("SP500") %>% # c("SP500","XLK")
                tq_get(tq_exchange_options="NASDAQ",
                       get  = "stock.prices",
                       from = today()-180,
                       to   = today()-5) %>% 
                tq_transmute(select     = adjusted, 
                             mutate_fun = periodReturn, 
                             period     = "monthly", 
                             col_rename = "Rb")

I get this warning message:

"Problem while computing `data.. = purrr::map(...)`.
x = 'SP500', get = 'stock.prices': Error in
  getSymbols.yahoo(Symbols = "SP500", env =
  <environment>, verbose = FALSE, : Unable to import
  “SP500”. SP500 download failed after two attempts.
  Error message: HTTP error 404.  Removing SP500. "

Update:

Ran this instead and it work:

tq_index("SP500") %>% # c("SP500","XLK")
                tq_get(tq_exchange_options="NASDAQ",
                       get  = "stock.prices",
                       from = today()-180,
                       to   = today()-5) %>% 
                tq_transmute(select     = adjusted, 
                             mutate_fun = periodReturn, 
                             period     = "monthly", 
                             col_rename = "Rb")

Still shows this message:

Getting holdings for SP500
Warning messages:                                           
1: Problem while computing `data.. = purrr::map(...)`.
ℹ x = 'BRK.B', get = 'stock.prices': Error in
  getSymbols.yahoo(Symbols = "BRK.B", env = <environment>,
  verbose = FALSE, : Unable to import “BRK.B”. BRK.B download
  failed after two attempts. Error message: HTTP error 404.
  Removing BRK.B. 
2: Problem while computing `data.. = purrr::map(...)`.
ℹ x = 'BF.B', get = 'stock.prices': Error in
  getSymbols.yahoo(Symbols = "BF.B", env = <environment>,
  verbose = FALSE, : Unable to import “BF.B”. BF.B download
  failed after two attempts. Error message: HTTP error 404.
  Removing BF.B.

This should be fixed now. Yahoo Finance accepts symbols in BRK-B and BF-B format.

tq_index("SP500") %>% filter(str_detect(symbol, "BRK"))
Getting holdings for SP500
# A tibble: 1 × 8                                                                                                     
  symbol company                         identifier sedol   weight sector     shares_held local_currency
  <chr>  <chr>                           <chr>      <chr>    <dbl> <chr>            <dbl> <chr>         
1 BRK-B  Berkshire Hathaway Inc. Class B 08467070   2073390 0.0168 Financials    20439872 USD  


"BRK-B" %>% tq_get()
# A tibble: 2,736 × 8
   symbol date        open  high   low close  volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 BRK-B  2012-01-03  77.4  78.3  77.3  77.7 5343500     77.7
 2 BRK-B  2012-01-04  77.6  77.6  76.6  76.8 4264400     76.8
 3 BRK-B  2012-01-05  76.4  77.1  75.9  76.9 4235400     76.9
 4 BRK-B  2012-01-06  76.8  77.1  76.1  76.4 3751400     76.4
 5 BRK-B  2012-01-09  76.5  76.8  75.9  76.3 2978200     76.3
 6 BRK-B  2012-01-10  77    77.5  76.7  77.4 4815000     77.4
 7 BRK-B  2012-01-11  77.2  78.1  76.9  78   3819700     78  
 8 BRK-B  2012-01-12  78.3  78.5  77.6  78.5 3749300     78.5
 9 BRK-B  2012-01-13  77.8  77.9  77    77.8 4772300     77.8
10 BRK-B  2012-01-17  78.6  78.6  77.6  78.0 4344500     78.0
# … with 2,726 more rows
# ℹ Use `print(n = ...)` to see more rows