R语言 雅虎getQuote解决方案为一个新手

epggiuax  于 2023-06-03  发布在  其他
关注(0)|答案(2)|浏览(786)

我被所有的行话吹走了。我是R的新手,用了几个月的时间来为我的Excel文件夹获取一些报价信息,作为对Bert子例程的VBA调用。我的R步骤如下:

local({r <- getOption("repos")
        r["CRAN"] <- "https://cran.microsoft.com/snapshot/2022-05-05/"
        options(repos=r)
      })
    library (quantmod)
    library(jsonlite)

    ticker <- "COKE"
    metrics <- yahooQF(
      c("Trade Time", "Last Trade (Price Only)", "Change in Percent",
        "Days High", "Days Low",
        "Dividend/Share",   "Dividend Yield",
        "Ex-Dividend Date", "Dividend Pay Date",
        "Earnings/Share", "EPS Forward",
        "Earnings Start Time", "Earnings End Time",
        "P/E Ratio (RT)", 
        "Previous Close", "Open",
        "50-day Moving Average", "200-day Moving Average"))
     quot <- getQuote(ticker, what=metrics)

从中我得到:open.connection(con,“rb”)出错:HTTP错误401.
我知道有几个问题/建议,但作为一个新手,我不知道如何解决这个问题(我的理解是由于雅虎的变化)。有没有人能用我能理解的实际代码来帮助我,并能与我的代码集成,以尝试绕过getQuote问题。

laik7k3q

laik7k3q1#

TLDR:
将quantmod更新到补丁版本

remotes::install_github("ethanbsmith/quantmod@382_add_session_getQuote")

详细说明:
我设置了以下断点:

debugonce(getQuote)
debugonce(getQuote.yahoo)

getQuote.yahoo()中,我提取了以下URL,代表您的请求:

Browse[2]> URL
[1] "https://query1.finance.yahoo.com/v7/finance/quote?symbols=COKE&fields=regularMarketPrice,regularMarketChangePercent,regularMarketDayHigh,regularMarketDayLow,trailingAnnualDividendRate,trailingAnnualDividendYield,exDividendDate,dividendDate,epsTrailingTwelveMonths,epsForward,earningsTimestampStart,earningsTimestampEnd,regularMarketPreviousClose,regularMarketOpen,fiftyDayAverage,twoHundredDayAverage"

访问此URL会得到以下JSON响应:

{"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Crumb. For Developers - [...] "}}}

我在谷歌上搜索了一下“雅虎金融无效的面包屑”,发现了下面这个Quantmod问题:https://github.com/joshuaulrich/quantmod/issues/382

> packageVersion('quantmod')
[1] ‘0.4.22’

正如问题中所建议的,我升级到包含问题补丁的指定版本的quantmod...

remotes::install_github("ethanbsmith/quantmod@382_add_session_getQuote")
packageVersion("quantmod")
[1] ‘0.4.22.1’
library (quantmod)
library(jsonlite)

ticker <- "COKE"
metrics <- yahooQF(
  c("Trade Time", "Last Trade (Price Only)", "Change in Percent",
    "Days High", "Days Low",
    "Dividend/Share",   "Dividend Yield",
    "Ex-Dividend Date", "Dividend Pay Date",
    "Earnings/Share", "EPS Forward",
    "Earnings Start Time", "Earnings End Time",
    "P/E Ratio (RT)", 
    "Previous Close", "Open",
    "50-day Moving Average", "200-day Moving Average"))
quot <- getQuote(ticker, what=metrics)
print(quot)
Trade Time   Last   % Change   High     Low Dividend/Share Dividend Yield Ex-Dividend Date Dividend Pay Date Earnings/Share EPS Forward
COKE 2023-05-31 16:00:04 661.74 -0.9104275 670.49 658.445           1.25    0.001871762       1682553600        1683849600          48.35       38.94
     Earnings Start Time Earnings End Time P. Close   Open 50-day MA 200-day MA
COKE          1690801140        1691150400   667.82 667.14   582.332   511.4602

……我们又可以继续做生意了!

dtcbnfnu

dtcbnfnu2#

这个问题的一些答案:Yahoo Finance API - GET quotes returns "Invalid Cookie"表示Yahoo!已故意禁用此API。
基于这个消息,我已经开始使用Tiingo(https://www.tiingo.com/),它可以提供大部分相同的数据。注册后,您将获得一个API密钥。免费水平是相当慷慨的,有一个付费水平(30美元/月),如果你需要更多的数据/带宽。
这里有一个脚本(R),你可以用途:

# define a function to get quotes from Tiingo
getQuotesTiingo <- function(tickers, apiKey) {
  url <- paste0("https://api.tiingo.com/iex/?token=", apiKey)
  url <- paste0(url, "&tickers=", paste(tickers, collapse = ","))
  jsonlite::fromJSON(curl::curl(url))
}

# set the ticker symbol array
ticks <- c("MSFT", "LRCX", "FERG", "AMP")

# provide your API Key
tiingoKey <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# get the quotes from Tiingo as a data.frame
getQuotesTiingo(ticks, tiingoKey)

相关问题