Ljupch0 / yfinance

Tidy Financial Statement Data in R. Via the Yahoo Finance API.
31 stars 7 forks source link

get_company_names & nested data query #6

Closed CormacH332 closed 2 years ago

CormacH332 commented 2 years ago

Hi Ljupch0,

I am new to this so if this is a simple fix apologies for wasting your time. I have been following along and I could successfully run all code bar get_company_names which returns "Error in open.connection(con, "rb") : HTTP error 404." although the tickers are spelt correctly.

I left this as it wasn't essential to continue with the tutorial but when I got to Nested Data I got the following error when mutating

biotech_balance_sheets <- tibble(

  • companies = companies
  • ) %>%
  • mutate(
  • ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol,
  • full_name = yfinance::search_stock(search_term = companies, keep_results = "top")$longname,
  • ) Warning: Problem with mutate() column ticker.
    i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. i There were ERRORS while downloading data for these items: Alexion Pharmaceuticals Inc.. Please check if you have the correct ticker. The vector of errored items and error messages can be accessed as attributes of the returned dataframe - attributes(df). Error: Problem with mutate() column ticker. i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. i ticker must be size 8 or 1, not 7. Run rlang::last_error() to see where the error occurred. biotech_balance_sheets <- tibble(
  • companies = companies
  • ) biotech_balance_sheets <- tibble(
  • companies = companies
  • ) %>%
  • mutate(
  • ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol,
  • full_name = yfinance::search_stock(search_term = companies, keep_results = "top")$longname,
  • ) Warning: Problem with mutate() column ticker.
    i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. i There were ERRORS while downloading data for these items: Alexion Pharmaceuticals Inc.. Please check if you have the correct ticker. The vector of errored items and error messages can be accessed as attributes of the returned dataframe - attributes(df). Error: Problem with mutate() column ticker. i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. i ticker must be size 8 or 1, not 7. Run rlang::last_error() to see where the error occurred. rlang::last_error() x +-<error/dplyr:::mutate_error> | Problem with mutate() column ticker. | i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. | i ticker must be size 8 or 1, not 7. -<error/dplyr:::mutate_incompatible_size> Backtrace:
    1. %>%(...)
    2. dplyr:::abort_glue(character(0), list(x_size = 7L), "dplyr:::mutate_incompatible_size")
    3. rlang::exec(abort, class = class, !!!data) Run rlang::last_trace() to see the full context. rlang::last_trace() x +-<error/dplyr:::mutate_error> | Problem with mutate() column ticker. | i ticker = yfinance::search_stock(search_term = companies, keep_results = "top")$symbol. | i ticker must be size 8 or 1, not 7. -<error/dplyr:::mutate_incompatible_size> Backtrace: x
    4. +-%>%(...)
    5. +-dplyr::mutate(...)
    6. +-dplyr:::mutate.data.frame(...)
    7. | -dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
    8. | +-base::withCallingHandlers(...)
    9. | -mask$eval_all_mutate(quo)
    10. -dplyr:::abort_glue(character(0), list(x_size = 7L), "dplyr:::mutate_incompatible_size")
    11. -rlang::exec(abort, class = class, !!!data)

Thanks

Ljupch0 commented 2 years ago

Hi, no worries at all. The API endpoint for the names seems to be down, either temporarily or permanently. You can replace that function with search_stock, which has a shortname column. yfinance::search_stock("AAPL", keep_results = "top")

I'm looking into the nested data.

Ljupch0 commented 2 years ago

The nested data example is failing because one of the companies that are being searched, got acquired since I made this example. Therefore yahoo finance cannot find its ticker, because it doesn't exist anymore. So for a request of 8 tickers, yfinance returns 7 results, and this causes problems with mutate, which is expecting either 8 results or a single result that it will replicate 8 times. I have pushed the updated notebook at the link below, but basically you just need to remove the Alexion from the companies vector. https://github.com/Ljupch0/yfinance_showcase/blob/master/yfinance%20showcase.RMd