Skip to content

Commit

Permalink
fix: api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Aug 7, 2023
1 parent 3a418e9 commit 52bc37e
Show file tree
Hide file tree
Showing 42 changed files with 2,070 additions and 3,533 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: yahoofinancer
Type: Package
Title: Fetch Data from Yahoo Finance API
Version: 0.1.0.9000
Version: 0.2.0
Authors@R: person("Aravind", "Hebbali", email = "[email protected]", role = c("aut", "cre"))
Description: Obtain historical and near real time data related to stocks, index
and currencies from the Yahoo Finance API. This package is community maintained
Expand All @@ -10,6 +10,7 @@ Description: Obtain historical and near real time data related to stocks, index
Depends:
R(>= 3.4)
Imports:
curl,
httr,
jsonlite,
lubridate,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export(Index)
export(Ticker)
export(currency_converter)
export(currency_summary)
export(get_currencies)
export(get_market_summary)
export(get_trending)
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# yahoofinancer (development version)

# yahoofinancer 0.2.0

This is a minor release to fix bugs resulting from changes in Yahoo Finance API.

# yahoofinancer 0.1.0

* Added a `NEWS.md` file to track changes to the package.
- Added a `NEWS.md` file to track changes to the package.
141 changes: 73 additions & 68 deletions R/index.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' R6 Class Representing a Ticker
#'
#' @description
#'
#' @description
#' Base class for getting all data related to indices from Yahoo Finance API.
#'
#'
#' @param index Index for which data has to be retrieved.
#'
#'
#' @docType class
#' @format An R6 class object
#' @name Index-class
#'
#' @export
#'
#' @export
Index <- R6::R6Class(

"Index",
Expand All @@ -29,7 +29,8 @@ Index <- R6::R6Class(
if (validate(index)) {
self$index <- index
} else {
stop("Not a valid index.", call. = FALSE)
message("Not a valid index.")
return(invisible(NULL))
}
},

Expand All @@ -43,11 +44,12 @@ Index <- R6::R6Class(
if (validate(index)) {
self$index <- index
} else {
stop("Not a valid index.", call. = FALSE)
message("Not a valid index.")
return(invisible(NULL))
}
},

#' @description
#' @description
#' Retrieves historical data
#' @param period Length of time. Defaults to \code{'ytd'}. Valid values are:
#' \itemize{
Expand Down Expand Up @@ -79,8 +81,8 @@ Index <- R6::R6Class(
#' \item \code{'1mo'}
#' \item \code{'3mo'}
#' }
#' @param start Specific starting date. \code{String} or \code{date} object in \code{yyyy-mm-dd} format.
#' @param end Specific ending date. \code{String} or \code{date} object in \code{yyyy-mm-dd} format.
#' @param start Specific starting date. \code{String} or \code{date} object in \code{yyyy-mm-dd} format.
#' @param end Specific ending date. \code{String} or \code{date} object in \code{yyyy-mm-dd} format.
#' @return A \code{data.frame}.
#' @examples
#' \donttest{
Expand All @@ -92,11 +94,11 @@ Index <- R6::R6Class(
get_history = function(period = 'ytd', interval = '1d', start = NULL, end = NULL) {

if (!is.null(start)) {
start_date <- as.numeric(as.POSIXct(ymd(start)))
start_date <- as.numeric(as.POSIXct(ymd(start, tz = "UTC"), tz = "UTC"))
}

if (!is.null(end)) {
end_date <- as.numeric(as.POSIXct(ymd(end)))
end_date <- as.numeric(as.POSIXct(ymd(end, tz = "UTC"), tz = "UTC"))
}

path <- 'v8/finance/chart/'
Expand All @@ -106,75 +108,78 @@ Index <- R6::R6Class(
if (!is.null(start) && !is.null(end)) {
qlist <- list(period1 = start_date, period2 = end_date, interval = interval)
} else if (!is.null(start) && is.null(end)) {
qlist <- list(period1 = start_date, period2 = round(as.numeric(as.POSIXct(now()))), interval = interval)
qlist <- list(period1 = start_date, period2 = round(as.numeric(as.POSIXct(now("UTC")))), interval = interval)
} else {
qlist <- list(range = period, interval = interval)
}

if (!curl::has_internet()) {
message("No internet connection.")
return(invisible(NULL))
}

resp <- GET(url, query = qlist)
parsed <- fromJSON(content(resp, "text", encoding = "UTF-8"), simplifyVector = FALSE)

data <-
parsed %>%
use_series(chart) %>%
use_series(result) %>%
extract2(1)

indicators <-
data %>%
use_series(indicators) %>%
use_series(quote) %>%
extract2(1)

result <- data.frame(
date = as_datetime(unlist(data$timestamp)),
volume = flatten_list(indicators$volume),
high = flatten_list(indicators$high),
low = flatten_list(indicators$low),
open = flatten_list(indicators$open),
close = flatten_list(indicators$close)
)

intervals <- c('1d', '5d', '1wk', '1mo', '3mo')

if (interval %in% intervals) {
adj_close <-
data %>%
use_series(indicators) %>%
use_series(adjclose) %>%
extract2(1) %>%
use_series(adjclose) %>%
unlist()

result$adj_close <- adj_close
if (http_error(resp)) {

}
message(
cat(
"Yahoo Finance API request failed", '\n',
paste('Status:', status_code(resp)), '\n',
paste('Type:', http_status(resp)$category), '\n',
paste('Mesage:', parsed$quoteSummary$error$code), '\n',
paste('Description:', parsed$quoteSummary$error$description, '\n'),
sep = ''
)
)

result
return(invisible(NULL))
} else {

}
),
data <-
parsed %>%
use_series(chart) %>%
use_series(result) %>%
extract2(1)

indicators <-
data %>%
use_series(indicators) %>%
use_series(quote) %>%
extract2(1)

result <- data.frame(
date = as_datetime(unlist(data$timestamp)),
volume = flatten_list(indicators$volume),
high = flatten_list(indicators$high),
low = flatten_list(indicators$low),
open = flatten_list(indicators$open),
close = flatten_list(indicators$close)
)

intervals <- c('1d', '5d', '1wk', '1mo', '3mo')

if (interval %in% intervals) {
adj_close <-
data %>%
use_series(indicators) %>%
use_series(adjclose) %>%
extract2(1) %>%
use_series(adjclose) %>%
unlist()

result$adj_close <- adj_close

}

return(result)
}

active = list(

#' @field summary_detail Contains information available via the Summary tab in Yahoo Finance
summary_detail = function() {

path <- 'v7/finance/quote'
url <- modify_url(url = private$base_url, path = path)
qlist <- list(symbols = self$index)
resp <- GET(url, query = qlist)
parsed <- fromJSON(content(resp, "text", encoding = "UTF-8"), simplifyVector = FALSE)

parsed %>%
use_series(quoteResponse) %>%
use_series(result) %>%
extract2(1)

}
),

private = list(
base_url = 'https://query2.finance.yahoo.com'
)
)
)
Loading

0 comments on commit 52bc37e

Please sign in to comment.